Make SiteStats (re)initializing more sane
authorMarius Hoch <hoo@online.de>
Sun, 23 Feb 2014 20:47:48 +0000 (21:47 +0100)
committerTim Starling <tstarling@wikimedia.org>
Mon, 24 Feb 2014 00:40:51 +0000 (00:40 +0000)
Don't set ss_active_users back to its default in SiteStatsInit::refresh.
Also remove SiteStatsInit::update and make refresh() protected.
Also don't consider ss_active_users in SiteStats::isSane as that value
isn't going to be fixed by the following SiteStatsInit::refresh call.

Change-Id: I268599be96106e1175fdf9750a2adc9468ebc93c

includes/SiteStats.php

index 0df6d90..6e2b5fa 100644 (file)
@@ -240,7 +240,6 @@ class SiteStats {
                        'ss_good_articles',
                        'ss_total_pages',
                        'ss_users',
-                       'ss_active_users',
                        'ss_images',
                ) as $member ) {
                        if ( $row->$member > 2000000000 || $row->$member < 0 ) {
@@ -360,7 +359,6 @@ class SiteStatsInit {
         * - Boolean: whether to use the master DB
         * - DatabaseBase: database connection to use
         * @param array $options 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)
         */
@@ -381,12 +379,7 @@ class SiteStatsInit {
                        $counter->views();
                }
 
-               // Update/refresh
-               if ( $options['update'] ) {
-                       $counter->update();
-               } else {
-                       $counter->refresh();
-               }
+               $counter->refresh();
 
                // Count active users if need be
                if ( $options['activeUsers'] ) {
@@ -395,39 +388,21 @@ class SiteStatsInit {
        }
 
        /**
-        * Update the current row with the selected values
-        */
-       public function update() {
-               list( $values, $conds ) = $this->getDbParams();
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->update( 'site_stats', $values, $conds, __METHOD__ );
-       }
-
-       /**
-        * Refresh site_stats. Erase the current record and save all
-        * the new values.
+        * Refresh site_stats.
         */
-       public function refresh() {
-               list( $values, $conds, $views ) = $this->getDbParams();
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->delete( 'site_stats', $conds, __METHOD__ );
-               $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__ );
-       }
-
-       /**
-        * Return three arrays of params for the db queries
-        * @return Array
-        */
-       private function getDbParams() {
+       protected function refresh() {
                $values = array(
+                       'ss_row_id' => 1,
                        'ss_total_edits' => $this->mEdits,
                        'ss_good_articles' => $this->mArticles,
                        'ss_total_pages' => $this->mPages,
                        'ss_users' => $this->mUsers,
-                       'ss_images' => $this->mFiles
+                       'ss_images' => $this->mFiles,
+               ) + (
+                       $this->mViews ? array( 'ss_total_views' => $this->mViews ) : array()
                );
-               $conds = array( 'ss_row_id' => 1 );
-               $views = array( 'ss_total_views' => $this->mViews );
-               return array( $values, $conds, $views );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->upsert( 'site_stats', $values, array( 'ss_row_id' ), $values, __METHOD__ );
        }
 }