Merge "User::pingLimiter() profiles per action as well"
[lhc/web/wiklou.git] / includes / SiteStats.php
index 0df6d90..4e737d1 100644 (file)
  * Static accessor class for site_stats and related things
  */
 class SiteStats {
-       static $row, $loaded = false;
-       static $jobs;
-       static $pageCount = array();
-       static $groupMemberCounts = array();
+       /** @var bool|ResultWrapper */
+       private static $row;
+
+       /** @var bool */
+       private static $loaded = false;
+
+       /** @var int */
+       private static $jobs;
+
+       /** @var int[] */
+       private static $pageCount = array();
+
+       /** @var int[] */
+       private static $groupMemberCounts = array();
 
        static function recache() {
                self::load( true );
        }
 
        /**
-        * @param $recache bool
+        * @param bool $recache
         */
        static function load( $recache = false ) {
                if ( self::$loaded && !$recache ) {
@@ -55,7 +65,7 @@ class SiteStats {
        }
 
        /**
-        * @return Bool|ResultWrapper
+        * @return bool|ResultWrapper
         */
        static function loadAndLazyInit() {
                wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
@@ -86,8 +96,8 @@ class SiteStats {
        }
 
        /**
-        * @param $db DatabaseBase
-        * @return Bool|ResultWrapper
+        * @param DatabaseBase $db
+        * @return bool|ResultWrapper
         */
        static function doLoad( $db ) {
                return $db->selectRow( 'site_stats', array(
@@ -161,7 +171,7 @@ class SiteStats {
        /**
         * Find the number of users in a given user group.
         * @param string $group name of group
-        * @return Integer
+        * @return int
         */
        static function numberingroup( $group ) {
                if ( !isset( self::$groupMemberCounts[$group] ) ) {
@@ -190,7 +200,10 @@ class SiteStats {
                if ( !isset( self::$jobs ) ) {
                        $dbr = wfGetDB( DB_SLAVE );
                        self::$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
-                       /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
+                       /**
+                        * Zero rows still do single row read for row that doesn't exist,
+                        * but people are annoyed by that
+                        */
                        if ( self::$jobs == 1 ) {
                                self::$jobs = 0;
                        }
@@ -199,7 +212,7 @@ class SiteStats {
        }
 
        /**
-        * @param $ns int
+        * @param int $ns
         *
         * @return int
         */
@@ -221,7 +234,9 @@ class SiteStats {
        /**
         * Is the provided row of site stats sane, or should it be regenerated?
         *
-        * @param $row
+        * Checks only fields which are filled by SiteStatsInit::refresh.
+        *
+        * @param bool|object $row
         *
         * @return bool
         */
@@ -229,7 +244,6 @@ class SiteStats {
                if ( $row === false
                        || $row->ss_total_pages < $row->ss_good_articles
                        || $row->ss_total_edits < $row->ss_total_pages
-                       || $row->ss_users < $row->ss_active_users
                ) {
                        return false;
                }
@@ -240,7 +254,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 ) {
@@ -264,7 +277,7 @@ class SiteStatsInit {
 
        /**
         * Constructor
-        * @param $database Boolean or DatabaseBase:
+        * @param bool|DatabaseBase $database
         * - Boolean: whether to use the master DB
         * - DatabaseBase: database connection to use
         */
@@ -278,7 +291,7 @@ class SiteStatsInit {
 
        /**
         * Count the total number of edits
-        * @return Integer
+        * @return int
         */
        public function edits() {
                $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
@@ -288,7 +301,7 @@ class SiteStatsInit {
 
        /**
         * Count pages in article space(s)
-        * @return Integer
+        * @return int
         */
        public function articles() {
                global $wgArticleCountMethod;
@@ -318,7 +331,7 @@ class SiteStatsInit {
 
        /**
         * Count total pages
-        * @return Integer
+        * @return int
         */
        public function pages() {
                $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
@@ -327,7 +340,7 @@ class SiteStatsInit {
 
        /**
         * Count total users
-        * @return Integer
+        * @return int
         */
        public function users() {
                $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
@@ -336,7 +349,7 @@ class SiteStatsInit {
 
        /**
         * Count views
-        * @return Integer
+        * @return int
         */
        public function views() {
                $this->mViews = $this->db->selectField( 'page', 'SUM(page_counter)', '', __METHOD__ );
@@ -345,7 +358,7 @@ class SiteStatsInit {
 
        /**
         * Count total files
-        * @return Integer
+        * @return int
         */
        public function files() {
                $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
@@ -356,11 +369,10 @@ class SiteStatsInit {
         * Do all updates and commit them. More or less a replacement
         * for the original initStats, but without output.
         *
-        * @param $database DatabaseBase|bool
+        * @param DatabaseBase|bool $database
         * - 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 +393,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 +402,21 @@ class SiteStatsInit {
        }
 
        /**
-        * Update the current row with the selected values
+        * Refresh site_stats.
         */
-       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.
-        */
-       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__ );
        }
 }