Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / SiteStats.php
index d66407b..e9137fe 100644 (file)
@@ -55,7 +55,7 @@ class SiteStats {
                        // clean schema with mwdumper.
                        wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
 
-                       SiteStatsInit::doAllAndCommit( false );
+                       SiteStatsInit::doAllAndCommit( wfGetDB( DB_SLAVE ) );
 
                        $row = self::doLoad( wfGetDB( DB_MASTER ) );
                }
@@ -179,7 +179,7 @@ class SiteStats {
                wfProfileIn( __METHOD__ );
                if( !isset( self::$pageCount[$ns] ) ) {
                        $dbr = wfGetDB( DB_SLAVE );
-                       $pageCount[$ns] = (int)$dbr->selectField(
+                       self::$pageCount[$ns] = (int)$dbr->selectField(
                                'page',
                                'COUNT(*)',
                                array( 'page_namespace' => $ns ),
@@ -187,7 +187,7 @@ class SiteStats {
                        );
                }
                wfProfileOut( __METHOD__ );
-               return $pageCount[$ns];
+               return self::$pageCount[$ns];
        }
 
        /**
@@ -220,9 +220,9 @@ class SiteStats {
 }
 
 /**
- *
+ * Class for handling updates to the site_stats table
  */
-class SiteStatsUpdate {
+class SiteStatsUpdate implements DeferrableUpdate {
 
        var $mViews, $mEdits, $mGood, $mPages, $mUsers;
 
@@ -317,10 +317,16 @@ class SiteStatsInit {
 
        /**
         * Constructor
-        * @param $useMaster Boolean: whether to use the master DB
+        * @param $database Boolean or DatabaseBase:
+        * - Boolean: whether to use the master DB
+        * - DatabaseBase: database connection to use
         */
-       public function __construct( $useMaster = false ) {
-               $this->db = wfGetDB( $useMaster ? DB_MASTER : DB_SLAVE );
+       public function __construct( $database = false ) {
+               if ( $database instanceof DatabaseBase ) {
+                       $this->db = $database;
+               } else {
+                       $this->db = wfGetDB( $database ? DB_MASTER : DB_SLAVE );
+               }
        }
 
        /**
@@ -371,7 +377,7 @@ class SiteStatsInit {
                $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
                return $this->mPages;
        }
-       
+
        /**
         * Count total users
         * @return Integer
@@ -380,7 +386,7 @@ class SiteStatsInit {
                $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
                return $this->mUsers;
        }
-       
+
        /**
         * Count views
         * @return Integer
@@ -401,14 +407,22 @@ class SiteStatsInit {
 
        /**
         * Do all updates and commit them. More or less a replacement
-        * for the original initStats, but without the calls to wfOut()
-        * @param $update Boolean: whether to update the current stats or write fresh
-        * @param $noViews Boolean: when true, do not update the number of page views
-        * @param $activeUsers Boolean: whether to update the number of active users
+        * for the original initStats, but without output.
+        *
+        * @param $database Boolean or DatabaseBase:
+        * - Boolean: whether to use the master DB
+        * - DatabaseBase: database connection to use
+        * @param $options Array of options, may contain the following values
+        * - update Boolean: whether to update the current stats (true) or write fresh (false) (default: false)
+        * - views Boolean: when true, do not update the number of page views (default: true)
+        * - activeUsers Boolean: whether to update the number of active users (default: false)
         */
-       public static function doAllAndCommit( $update, $noViews = false, $activeUsers = false ) {
+       public static function doAllAndCommit( $database, array $options = array() ) {
+               $options += array( 'update' => false, 'views' => true, 'activeUsers' => false );
+
                // Grab the object and count everything
-               $counter = new SiteStatsInit( false );
+               $counter = new SiteStatsInit( $database );
+
                $counter->edits();
                $counter->articles();
                $counter->pages();
@@ -416,19 +430,19 @@ class SiteStatsInit {
                $counter->files();
 
                // Only do views if we don't want to not count them
-               if( !$noViews ) {
+               if( $options['views'] ) {
                        $counter->views();
                }
 
                // Update/refresh
-               if( $update ) {
+               if( $options['update'] ) {
                        $counter->update();
                } else {
                        $counter->refresh();
                }
 
                // Count active users if need be
-               if( $activeUsers ) {
+               if( $options['activeUsers'] ) {
                        SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
                }
        }