SiteStatsUpdate: Avoid deprecated wfMemcKey()
[lhc/web/wiklou.git] / includes / deferred / SiteStatsUpdate.php
index d8bc35b..8c38d8b 100644 (file)
  *
  * @file
  */
+use MediaWiki\MediaWikiServices;
+use Wikimedia\Assert\Assert;
+use Wikimedia\Rdbms\IDatabase;
 
 /**
  * Class for handling updates to the site_stats table
  */
-class SiteStatsUpdate implements DeferrableUpdate {
+class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate {
        /** @var int */
        protected $edits = 0;
-
        /** @var int */
        protected $pages = 0;
-
        /** @var int */
        protected $articles = 0;
-
        /** @var int */
        protected $users = 0;
-
        /** @var int */
        protected $images = 0;
 
+       private static $counters = [ 'edits', 'pages', 'articles', 'users', 'images' ];
+
        // @todo deprecate this constructor
        function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
                $this->edits = $edits;
@@ -45,6 +46,15 @@ class SiteStatsUpdate implements DeferrableUpdate {
                $this->users = $users;
        }
 
+       public function merge( MergeableUpdate $update ) {
+               /** @var SiteStatsUpdate $update */
+               Assert::parameterType( __CLASS__, $update, '$update' );
+
+               foreach ( self::$counters as $field ) {
+                       $this->$field += $update->$field;
+               }
+       }
+
        /**
         * @param array $deltas
         * @return SiteStatsUpdate
@@ -52,8 +62,7 @@ class SiteStatsUpdate implements DeferrableUpdate {
        public static function factory( array $deltas ) {
                $update = new self( 0, 0, 0 );
 
-               $fields = [ 'views', 'edits', 'pages', 'articles', 'users', 'images' ];
-               foreach ( $fields as $field ) {
+               foreach ( self::$counters as $field ) {
                        if ( isset( $deltas[$field] ) && $deltas[$field] ) {
                                $update->$field = $deltas[$field];
                        }
@@ -85,7 +94,7 @@ class SiteStatsUpdate implements DeferrableUpdate {
                global $wgSiteStatsAsyncFactor;
 
                $dbw = wfGetDB( DB_MASTER );
-               $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
+               $lockKey = wfWikiID() . ':site_stats'; // prepend wiki ID
                $pd = [];
                if ( $wgSiteStatsAsyncFactor ) {
                        // Lock the table so we don't have double DB/memcached updates
@@ -162,7 +171,7 @@ class SiteStatsUpdate implements DeferrableUpdate {
        }
 
        protected function doUpdateContextStats() {
-               $stats = RequestContext::getMain()->getStats();
+               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
                foreach ( [ 'edits', 'articles', 'pages', 'users', 'images' ] as $type ) {
                        $delta = $this->$type;
                        if ( $delta !== 0 ) {
@@ -198,12 +207,13 @@ class SiteStatsUpdate implements DeferrableUpdate {
        }
 
        /**
+        * @param BagOStuff $cache
         * @param string $type
         * @param string $sign ('+' or '-')
         * @return string
         */
-       private function getTypeCacheKey( $type, $sign ) {
-               return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
+       private function getTypeCacheKey( BagOStuff $cache, $type, $sign ) {
+               return $cache->makeKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
        }
 
        /**
@@ -213,11 +223,11 @@ class SiteStatsUpdate implements DeferrableUpdate {
         * @param int $delta Delta (positive or negative)
         */
        protected function adjustPending( $type, $delta ) {
-               $cache = ObjectCache::getMainStashInstance();
+               $cache = MediaWikiServices::getInstance()->getMainObjectStash();
                if ( $delta < 0 ) { // decrement
-                       $key = $this->getTypeCacheKey( $type, '-' );
+                       $key = $this->getTypeCacheKey( $cache, $type, '-' );
                } else { // increment
-                       $key = $this->getTypeCacheKey( $type, '+' );
+                       $key = $this->getTypeCacheKey( $cache, $type, '+' );
                }
 
                $magnitude = abs( $delta );
@@ -229,7 +239,7 @@ class SiteStatsUpdate implements DeferrableUpdate {
         * @return array Positive and negative deltas for each type
         */
        protected function getPendingDeltas() {
-               $cache = ObjectCache::getMainStashInstance();
+               $cache = MediaWikiServices::getInstance()->getMainObjectStash();
 
                $pending = [];
                foreach ( [ 'ss_total_edits',
@@ -237,8 +247,8 @@ class SiteStatsUpdate implements DeferrableUpdate {
                ) {
                        // Get pending increments and pending decrements
                        $flg = BagOStuff::READ_LATEST;
-                       $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $type, '+' ), $flg );
-                       $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $type, '-' ), $flg );
+                       $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '+' ), $flg );
+                       $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '-' ), $flg );
                }
 
                return $pending;
@@ -249,12 +259,12 @@ class SiteStatsUpdate implements DeferrableUpdate {
         * @param array $pd Result of getPendingDeltas(), used for DB update
         */
        protected function removePendingDeltas( array $pd ) {
-               $cache = ObjectCache::getMainStashInstance();
+               $cache = MediaWikiServices::getInstance()->getMainObjectStash();
 
                foreach ( $pd as $type => $deltas ) {
                        foreach ( $deltas as $sign => $magnitude ) {
                                // Lower the pending counter now that we applied these changes
-                               $cache->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );
+                               $cache->decr( $this->getTypeCacheKey( $cache, $type, $sign ), $magnitude );
                        }
                }
        }