Output more MW version info in update.php
[lhc/web/wiklou.git] / includes / SiteStats.php
index 7b2b8d3..e3cb617 100644 (file)
@@ -56,13 +56,13 @@ class SiteStats {
                wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" );
                $row = self::doLoadFromDB( $dbr );
 
-               if ( !self::isSane( $row ) && $lb->hasOrMadeRecentMasterChanges() ) {
+               if ( !self::isRowSane( $row ) && $lb->hasOrMadeRecentMasterChanges() ) {
                        // Might have just been initialized during this request? Underflow?
                        wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
                        $row = self::doLoadFromDB( $lb->getConnection( DB_MASTER ) );
                }
 
-               if ( !self::isSane( $row ) ) {
+               if ( !self::isRowSane( $row ) ) {
                        if ( $config->get( 'MiserMode' ) ) {
                                // Start off with all zeroes, assuming that this is a new wiki or any
                                // repopulations where done manually via script.
@@ -79,35 +79,22 @@ class SiteStats {
                        $row = self::doLoadFromDB( $lb->getConnection( DB_MASTER ) );
                }
 
-               if ( !self::isSane( $row ) ) {
+               if ( !self::isRowSane( $row ) ) {
                        wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
                        // Always return a row-like object
-                       $row = (object)array_fill_keys( self::selectFields(), 0 );
+                       $row = self::salvageInsaneRow( $row );
                }
 
                return $row;
        }
 
-       /**
-        * @param IDatabase $db
-        * @return stdClass|bool
-        */
-       private static function doLoadFromDB( IDatabase $db ) {
-               return $db->selectRow(
-                       'site_stats',
-                       self::selectFields(),
-                       [ 'ss_row_id' => 1 ],
-                       __METHOD__
-               );
-       }
-
        /**
         * @return int
         */
        public static function edits() {
                self::load();
 
-               return self::$row->ss_total_edits;
+               return (int)self::$row->ss_total_edits;
        }
 
        /**
@@ -116,7 +103,7 @@ class SiteStats {
        public static function articles() {
                self::load();
 
-               return self::$row->ss_good_articles;
+               return (int)self::$row->ss_good_articles;
        }
 
        /**
@@ -125,7 +112,7 @@ class SiteStats {
        public static function pages() {
                self::load();
 
-               return self::$row->ss_total_pages;
+               return (int)self::$row->ss_total_pages;
        }
 
        /**
@@ -134,7 +121,7 @@ class SiteStats {
        public static function users() {
                self::load();
 
-               return self::$row->ss_users;
+               return (int)self::$row->ss_users;
        }
 
        /**
@@ -143,7 +130,7 @@ class SiteStats {
        public static function activeUsers() {
                self::load();
 
-               return self::$row->ss_active_users;
+               return (int)self::$row->ss_active_users;
        }
 
        /**
@@ -152,7 +139,7 @@ class SiteStats {
        public static function images() {
                self::load();
 
-               return self::$row->ss_images;
+               return (int)self::$row->ss_images;
        }
 
        /**
@@ -162,11 +149,12 @@ class SiteStats {
         */
        public static function numberingroup( $group ) {
                $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               $fname = __METHOD__;
 
                return $cache->getWithSetCallback(
                        $cache->makeKey( 'SiteStats', 'groupcounts', $group ),
                        $cache::TTL_HOUR,
-                       function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
+                       function ( $oldValue, &$ttl, array &$setOpts ) use ( $group, $fname ) {
                                $dbr = self::getLB()->getConnection( DB_REPLICA );
                                $setOpts += Database::getCacheSetOptions( $dbr );
 
@@ -177,7 +165,7 @@ class SiteStats {
                                                'ug_group' => $group,
                                                'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
                                        ],
-                                       __METHOD__
+                                       $fname
                                );
                        },
                        [ 'pcTTL' => $cache::TTL_PROC_LONG ]
@@ -212,11 +200,12 @@ class SiteStats {
         */
        public static function pagesInNs( $ns ) {
                $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               $fname = __METHOD__;
 
                return $cache->getWithSetCallback(
                        $cache->makeKey( 'SiteStats', 'page-in-namespace', $ns ),
                        $cache::TTL_HOUR,
-                       function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns ) {
+                       function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns, $fname ) {
                                $dbr = self::getLB()->getConnection( DB_REPLICA );
                                $setOpts += Database::getCacheSetOptions( $dbr );
 
@@ -224,7 +213,7 @@ class SiteStats {
                                        'page',
                                        'COUNT(*)',
                                        [ 'page_namespace' => $ns ],
-                                       __METHOD__
+                                       $fname
                                );
                        },
                        [ 'pcTTL' => $cache::TTL_PROC_LONG ]
@@ -245,6 +234,19 @@ class SiteStats {
                ];
        }
 
+       /**
+        * @param IDatabase $db
+        * @return stdClass|bool
+        */
+       private static function doLoadFromDB( IDatabase $db ) {
+               return $db->selectRow(
+                       'site_stats',
+                       self::selectFields(),
+                       [ 'ss_row_id' => 1 ],
+                       __METHOD__
+               );
+       }
+
        /**
         * Is the provided row of site stats sane, or should it be regenerated?
         *
@@ -253,7 +255,7 @@ class SiteStats {
         * @param bool|object $row
         * @return bool
         */
-       private static function isSane( $row ) {
+       private static function isRowSane( $row ) {
                if ( $row === false
                        || $row->ss_total_pages < $row->ss_good_articles
                        || $row->ss_total_edits < $row->ss_total_pages
@@ -268,7 +270,7 @@ class SiteStats {
                        'ss_users',
                        'ss_images',
                ] as $member ) {
-                       if ( $row->$member > 2000000000 || $row->$member < 0 ) {
+                       if ( $row->$member < 0 ) {
                                return false;
                        }
                }
@@ -276,6 +278,22 @@ class SiteStats {
                return true;
        }
 
+       /**
+        * @param stdClass|bool $row
+        * @return stdClass
+        */
+       private static function salvageInsaneRow( $row ) {
+               $map = $row ? (array)$row : [];
+               // Fill in any missing values with zero
+               $map += array_fill_keys( self::selectFields(), 0 );
+               // Convert negative values to zero
+               foreach ( $map as $field => $value ) {
+                       $map[$field] = max( 0, $value );
+               }
+
+               return (object)$row;
+       }
+
        /**
         * @return LoadBalancer
         */