* Lazy-initialize site_stats row on load when empty. Somewhat kinder to
authorBrion Vibber <brion@users.mediawiki.org>
Wed, 21 Feb 2007 00:04:21 +0000 (00:04 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Wed, 21 Feb 2007 00:04:21 +0000 (00:04 +0000)
  dump-based installations, avoiding PHP warnings when NUMBEROFARTICLES
  and such are used.

RELEASE-NOTES
includes/SiteStats.php

index 634a4aa..02af192 100644 (file)
@@ -204,6 +204,9 @@ lighter making things easier to read.
 * Partial support for Flash cross-domain-policy filtering. 
 * Hide irrelevant block options in Special:Blockip based on whether an
   IP address/range or username is listed. (Dynamic using JS.)
+* Lazy-initialize site_stats row on load when empty. Somewhat kinder to
+  dump-based installations, avoiding PHP warnings when NUMBEROFARTICLES
+  and such are used.
 
 
 == Languages updated ==
index 46700cc..bf2c2a5 100644 (file)
@@ -17,8 +17,7 @@ class SiteStats {
                        return;
                }
 
-               $dbr = wfGetDB( DB_SLAVE );
-               self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
+               self::$row = self::loadAndLazyInit();
 
                # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
                if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
@@ -28,6 +27,43 @@ class SiteStats {
                        self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
                }
        }
+       
+       static function loadAndLazyInit() {
+               wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
+               $row = self::doLoad( wfGetDB( DB_SLAVE ) );
+               
+               if( $row === false ) {
+                       // Might have just been initialzed during this request?
+                       wfDebug( __METHOD__ . ": site_stats missing on slave\n" );
+                       $row = self::doLoad( wfGetDB( DB_MASTER ) );
+               }
+               
+               if( $row === false ) {
+                       // Normally the site_stats table is initialized at install time.
+                       // Some manual construction scenarios may leave the table empty,
+                       // however, for instance when importing from a dump into a clean
+                       // schema with mwdumper.
+                       wfDebug( __METHOD__ . ": initializing empty site_stats\n" );
+                       
+                       global $IP;
+                       require_once "$IP/maintenance/initStats.inc";
+                       
+                       ob_start();
+                       wfInitStats();
+                       ob_end_clean();
+                       
+                       $row = self::doLoad( wfGetDB( DB_MASTER ) );
+               }
+               
+               if( $row === false ) {
+                       wfDebug( __METHOD__ . ": init of site_stats failed o_O\n" );
+               }
+               return $row;
+       }
+
+       static function doLoad( $db ) {
+               return $db->selectRow( 'site_stats', '*', false, __METHOD__ );
+       }
 
        static function views() {
                self::load();