Merge "Revert "[MCR] Turn Revision into a proxy to new code.""
[lhc/web/wiklou.git] / includes / SiteStats.php
index 1c7730c..ce87596 100644 (file)
@@ -22,6 +22,7 @@
 
 use Wikimedia\Rdbms\Database;
 use Wikimedia\Rdbms\IDatabase;
+use MediaWiki\MediaWikiServices;
 
 /**
  * Static accessor class for site_stats and related things
@@ -33,9 +34,6 @@ class SiteStats {
        /** @var bool */
        private static $loaded = false;
 
-       /** @var int */
-       private static $jobs;
-
        /** @var int[] */
        private static $pageCount = [];
 
@@ -78,9 +76,12 @@ class SiteStats {
                $row = self::doLoad( wfGetDB( DB_REPLICA ) );
 
                if ( !self::isSane( $row ) ) {
-                       // Might have just been initialized during this request? Underflow?
-                       wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
-                       $row = self::doLoad( wfGetDB( DB_MASTER ) );
+                       $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+                       if ( $lb->hasOrMadeRecentMasterChanges() ) {
+                               // Might have just been initialized during this request? Underflow?
+                               wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
+                               $row = self::doLoad( wfGetDB( DB_MASTER ) );
+                       }
                }
 
                if ( !$wgMiserMode && !self::isSane( $row ) ) {
@@ -98,6 +99,7 @@ class SiteStats {
                if ( !self::isSane( $row ) ) {
                        wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
                }
+
                return $row;
        }
 
@@ -184,12 +186,11 @@ class SiteStats {
         * @return int
         */
        static function numberingroup( $group ) {
-               $cache = ObjectCache::getMainWANInstance();
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
                return $cache->getWithSetCallback(
-                       wfMemcKey( 'SiteStats', 'groupcounts', $group ),
+                       $cache->makeKey( 'SiteStats', 'groupcounts', $group ),
                        $cache::TTL_HOUR,
                        function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
-                               global $wgDisableUserGroupExpiry;
                                $dbr = wfGetDB( DB_REPLICA );
 
                                $setOpts += Database::getCacheSetOptions( $dbr );
@@ -199,9 +200,7 @@ class SiteStats {
                                        'COUNT(*)',
                                        [
                                                'ug_group' => $group,
-                                               $wgDisableUserGroupExpiry ?
-                                                       '1' :
-                                                       'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
+                                               'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
                                        ],
                                        __METHOD__
                                );
@@ -211,24 +210,24 @@ class SiteStats {
        }
 
        /**
+        * Total number of jobs in the job queue.
         * @return int
         */
        static function jobs() {
-               if ( !isset( self::$jobs ) ) {
-                       try{
-                               self::$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
-                       } catch ( JobQueueError $e ) {
-                               self::$jobs = 0;
-                       }
-                       /**
-                        * 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;
-                       }
-               }
-               return self::$jobs;
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'SiteStats', 'jobscount' ),
+                       $cache::TTL_MINUTE,
+                       function ( $oldValue, &$ttl, array &$setOpts ) {
+                               try{
+                                       $jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
+                               } catch ( JobQueueError $e ) {
+                                       $jobs = 0;
+                               }
+                               return $jobs;
+                       },
+                       [ 'pcTTL' => $cache::TTL_PROC_LONG ]
+               );
        }
 
        /**
@@ -294,9 +293,8 @@ class SiteStatsInit {
        private $mUsers = null, $mFiles = null;
 
        /**
-        * Constructor
         * @param bool|IDatabase $database
-        * - boolean: Whether to use the master DB
+        * - bool: Whether to use the master DB
         * - IDatabase: Database connection to use
         */
        public function __construct( $database = false ) {
@@ -381,10 +379,10 @@ class SiteStatsInit {
         * for the original initStats, but without output.
         *
         * @param IDatabase|bool $database
-        * - boolean: Whether to use the master DB
+        * - bool: Whether to use the master DB
         * - IDatabase: Database connection to use
         * @param array $options Array of options, may contain the following values
-        * - activeUsers boolean: Whether to update the number of active users (default: false)
+        * - activeUsers bool: Whether to update the number of active users (default: false)
         */
        public static function doAllAndCommit( $database, array $options = [] ) {
                $options += [ 'update' => false, 'activeUsers' => false ];