Merge "Have Linker::normaliseSpecialPage() accept LinkTarget objects"
[lhc/web/wiklou.git] / includes / WatchedItemStore.php
index c4340ad..fcf6d3b 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
+use MediaWiki\Linker\LinkTarget;
 use Wikimedia\Assert\Assert;
 
 /**
@@ -10,7 +12,7 @@ use Wikimedia\Assert\Assert;
  *
  * @since 1.27
  */
-class WatchedItemStore {
+class WatchedItemStore implements StatsdAwareInterface {
 
        const SORT_DESC = 'DESC';
        const SORT_ASC = 'ASC';
@@ -43,6 +45,11 @@ class WatchedItemStore {
         */
        private $revisionGetTimestampFromIdCallback;
 
+       /**
+        * @var StatsdDataFactoryInterface
+        */
+       private $stats;
+
        /**
         * @var self|null
         */
@@ -58,10 +65,15 @@ class WatchedItemStore {
        ) {
                $this->loadBalancer = $loadBalancer;
                $this->cache = $cache;
+               $this->stats = new NullStatsdDataFactory();
                $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ];
                $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ];
        }
 
+       public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) {
+               $this->stats = $stats;
+       }
+
        /**
         * Overrides the DeferredUpdates::addCallableUpdate callback
         * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
@@ -148,6 +160,7 @@ class WatchedItemStore {
                                wfGetLB(),
                                new HashBagOStuff( [ 'maxKeys' => 100 ] )
                        );
+                       self::$instance->setStatsdDataFactory( RequestContext::getMain()->getStats() );
                }
                return self::$instance;
        }
@@ -166,18 +179,22 @@ class WatchedItemStore {
                $key = $this->getCacheKey( $user, $target );
                $this->cache->set( $key, $item );
                $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
+               $this->stats->increment( 'WatchedItemStore.cache' );
        }
 
        private function uncache( User $user, LinkTarget $target ) {
                $this->cache->delete( $this->getCacheKey( $user, $target ) );
                unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
+               $this->stats->increment( 'WatchedItemStore.uncache' );
        }
 
        private function uncacheLinkTarget( LinkTarget $target ) {
                if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) {
                        return;
                }
+               $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget' );
                foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) {
+                       $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
                        $this->cache->delete( $key );
                }
        }
@@ -451,8 +468,10 @@ class WatchedItemStore {
 
                $cached = $this->getCached( $user, $target );
                if ( $cached ) {
+                       $this->stats->increment( 'WatchedItemStore.getWatchedItem.cached' );
                        return $cached;
                }
+               $this->stats->increment( 'WatchedItemStore.getWatchedItem.load' );
                return $this->loadWatchedItem( $user, $target );
        }
 
@@ -602,7 +621,7 @@ class WatchedItemStore {
                $this->reuseConnection( $dbr );
 
                foreach ( $res as $row ) {
-                       $timestamps[(int)$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
+                       $timestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
                }
 
                return $timestamps;
@@ -887,12 +906,8 @@ class WatchedItemStore {
         * @param LinkTarget $newTarget
         */
        public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
-               if ( !$oldTarget instanceof Title ) {
-                       $oldTarget = Title::newFromLinkTarget( $oldTarget );
-               }
-               if ( !$newTarget instanceof Title ) {
-                       $newTarget = Title::newFromLinkTarget( $newTarget );
-               }
+               $oldTarget = Title::newFromLinkTarget( $oldTarget );
+               $newTarget = Title::newFromLinkTarget( $newTarget );
 
                $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
                $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );