Introduce StatsdAwareInterface
[lhc/web/wiklou.git] / includes / WatchedItemStore.php
index c59beec..8ae7932 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
 use Wikimedia\Assert\Assert;
 
 /**
@@ -10,7 +11,10 @@ use Wikimedia\Assert\Assert;
  *
  * @since 1.27
  */
-class WatchedItemStore {
+class WatchedItemStore implements StatsdAwareInterface {
+
+       const SORT_DESC = 'DESC';
+       const SORT_ASC = 'ASC';
 
        /**
         * @var LoadBalancer
@@ -40,6 +44,11 @@ class WatchedItemStore {
         */
        private $revisionGetTimestampFromIdCallback;
 
+       /**
+        * @var StatsdDataFactoryInterface
+        */
+       private $stats;
+
        /**
         * @var self|null
         */
@@ -55,17 +64,24 @@ 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.
         *
         * @param callable $callback
+        *
         * @see DeferredUpdates::addCallableUpdate for callback signiture
         *
+        * @return ScopedCallback to reset the overridden value
         * @throws MWException
         */
        public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) {
@@ -75,7 +91,12 @@ class WatchedItemStore {
                        );
                }
                Assert::parameterType( 'callable', $callback, '$callback' );
+
+               $previousValue = $this->deferredUpdatesAddCallableUpdateCallback;
                $this->deferredUpdatesAddCallableUpdateCallback = $callback;
+               return new ScopedCallback( function() use ( $previousValue ) {
+                       $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
+               } );
        }
 
        /**
@@ -85,6 +106,7 @@ class WatchedItemStore {
         * @param callable $callback
         * @see Revision::getTimestampFromId for callback signiture
         *
+        * @return ScopedCallback to reset the overridden value
         * @throws MWException
         */
        public function overrideRevisionGetTimestampFromIdCallback( $callback ) {
@@ -94,24 +116,38 @@ class WatchedItemStore {
                        );
                }
                Assert::parameterType( 'callable', $callback, '$callback' );
+
+               $previousValue = $this->revisionGetTimestampFromIdCallback;
                $this->revisionGetTimestampFromIdCallback = $callback;
+               return new ScopedCallback( function() use ( $previousValue ) {
+                       $this->revisionGetTimestampFromIdCallback = $previousValue;
+               } );
        }
 
        /**
         * Overrides the default instance of this class
         * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
         *
-        * @param WatchedItemStore $store
+        * If this method is used it MUST also be called with null after a test to ensure a new
+        * default instance is created next time getDefaultInstance is called.
+        *
+        * @param WatchedItemStore|null $store
         *
+        * @return ScopedCallback to reset the overridden value
         * @throws MWException
         */
-       public static function overrideDefaultInstance( WatchedItemStore $store ) {
+       public static function overrideDefaultInstance( WatchedItemStore $store = null ) {
                if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
                        throw new MWException(
                                'Cannot override ' . __CLASS__ . 'default instance in operation.'
                        );
                }
+
+               $previousValue = self::$instance;
                self::$instance = $store;
+               return new ScopedCallback( function() use ( $previousValue ) {
+                       self::$instance = $previousValue;
+               } );
        }
 
        /**
@@ -123,6 +159,7 @@ class WatchedItemStore {
                                wfGetLB(),
                                new HashBagOStuff( [ 'maxKeys' => 100 ] )
                        );
+                       self::$instance->setStatsdDataFactory( RequestContext::getMain()->getStats() );
                }
                return self::$instance;
        }
@@ -141,18 +178,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 );
                }
        }
@@ -184,13 +225,55 @@ class WatchedItemStore {
                ];
        }
 
+       /**
+        * @param int $slaveOrMaster DB_MASTER or DB_SLAVE
+        *
+        * @return DatabaseBase
+        * @throws MWException
+        */
+       private function getConnection( $slaveOrMaster ) {
+               return $this->loadBalancer->getConnection( $slaveOrMaster, [ 'watchlist' ] );
+       }
+
+       /**
+        * @param DatabaseBase $connection
+        *
+        * @throws MWException
+        */
+       private function reuseConnection( $connection ) {
+               $this->loadBalancer->reuseConnection( $connection );
+       }
+
+       /**
+        * Count the number of individual items that are watched by the user.
+        * If a subject and corresponding talk page are watched this will return 2.
+        *
+        * @param User $user
+        *
+        * @return int
+        */
+       public function countWatchedItems( User $user ) {
+               $dbr = $this->getConnection( DB_SLAVE );
+               $return = (int)$dbr->selectField(
+                       'watchlist',
+                       'COUNT(*)',
+                       [
+                               'wl_user' => $user->getId()
+                       ],
+                       __METHOD__
+               );
+               $this->reuseConnection( $dbr );
+
+               return $return;
+       }
+
        /**
         * @param LinkTarget $target
         *
         * @return int
         */
        public function countWatchers( LinkTarget $target ) {
-               $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
+               $dbr = $this->getConnection( DB_SLAVE );
                $return = (int)$dbr->selectField(
                        'watchlist',
                        'COUNT(*)',
@@ -200,7 +283,7 @@ class WatchedItemStore {
                        ],
                        __METHOD__
                );
-               $this->loadBalancer->reuseConnection( $dbr );
+               $this->reuseConnection( $dbr );
 
                return $return;
        }
@@ -216,7 +299,7 @@ class WatchedItemStore {
         * @throws MWException
         */
        public function countVisitingWatchers( LinkTarget $target, $threshold ) {
-               $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
+               $dbr = $this->getConnection( DB_SLAVE );
                $visitingWatchers = (int)$dbr->selectField(
                        'watchlist',
                        'COUNT(*)',
@@ -229,7 +312,7 @@ class WatchedItemStore {
                        ],
                        __METHOD__
                );
-               $this->loadBalancer->reuseConnection( $dbr );
+               $this->reuseConnection( $dbr );
 
                return $visitingWatchers;
        }
@@ -246,7 +329,7 @@ class WatchedItemStore {
        public function countWatchersMultiple( array $targets, array $options = [] ) {
                $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
 
-               $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
+               $dbr = $this->getConnection( DB_SLAVE );
 
                if ( array_key_exists( 'minimumWatchers', $options ) ) {
                        $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
@@ -261,7 +344,7 @@ class WatchedItemStore {
                        $dbOptions
                );
 
-               $this->loadBalancer->reuseConnection( $dbr );
+               $this->reuseConnection( $dbr );
 
                $watchCounts = [];
                foreach ( $targets as $linkTarget ) {
@@ -275,6 +358,100 @@ class WatchedItemStore {
                return $watchCounts;
        }
 
+       /**
+        * Number of watchers of each page who have visited recent edits to that page
+        *
+        * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold),
+        *        $threshold is:
+        *        - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp)
+        *        - null if $target doesn't exist
+        * @param int|null $minimumWatchers
+        * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers,
+        *         where $watchers is an int:
+        *         - if the page exists, number of users watching who have visited the page recently
+        *         - if the page doesn't exist, number of users that have the page on their watchlist
+        *         - 0 means there are no visiting watchers or their number is below the minimumWatchers
+        *         option (if passed).
+        */
+       public function countVisitingWatchersMultiple(
+               array $targetsWithVisitThresholds,
+               $minimumWatchers = null
+       ) {
+               $dbr = $this->getConnection( DB_SLAVE );
+
+               $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
+
+               $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
+               if ( $minimumWatchers !== null ) {
+                       $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
+               }
+               $res = $dbr->select(
+                       'watchlist',
+                       [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
+                       $conds,
+                       __METHOD__,
+                       $dbOptions
+               );
+
+               $this->reuseConnection( $dbr );
+
+               $watcherCounts = [];
+               foreach ( $targetsWithVisitThresholds as list( $target ) ) {
+                       /* @var LinkTarget $target */
+                       $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
+               }
+
+               foreach ( $res as $row ) {
+                       $watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
+               }
+
+               return $watcherCounts;
+       }
+
+       /**
+        * Generates condition for the query used in a batch count visiting watchers.
+        *
+        * @param IDatabase $db
+        * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
+        * @return string
+        */
+       private function getVisitingWatchersCondition(
+               IDatabase $db,
+               array $targetsWithVisitThresholds
+       ) {
+               $missingTargets = [];
+               $namespaceConds = [];
+               foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
+                       if ( $threshold === null ) {
+                               $missingTargets[] = $target;
+                               continue;
+                       }
+                       /* @var LinkTarget $target */
+                       $namespaceConds[$target->getNamespace()][] = $db->makeList( [
+                               'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
+                               $db->makeList( [
+                                       'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
+                                       'wl_notificationtimestamp IS NULL'
+                               ], LIST_OR )
+                       ], LIST_AND );
+               }
+
+               $conds = [];
+               foreach ( $namespaceConds as $namespace => $pageConds ) {
+                       $conds[] = $db->makeList( [
+                               'wl_namespace = ' . $namespace,
+                               '(' . $db->makeList( $pageConds, LIST_OR ) . ')'
+                       ], LIST_AND );
+               }
+
+               if ( $missingTargets ) {
+                       $lb = new LinkBatch( $missingTargets );
+                       $conds[] = $lb->constructSet( 'wl', $db );
+               }
+
+               return $db->makeList( $conds, LIST_OR );
+       }
+
        /**
         * Get an item (may be cached)
         *
@@ -290,8 +467,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 );
        }
 
@@ -309,14 +488,14 @@ class WatchedItemStore {
                        return false;
                }
 
-               $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
+               $dbr = $this->getConnection( DB_SLAVE );
                $row = $dbr->selectRow(
                        'watchlist',
                        'wl_notificationtimestamp',
                        $this->dbCond( $user, $target ),
                        __METHOD__
                );
-               $this->loadBalancer->reuseConnection( $dbr );
+               $this->reuseConnection( $dbr );
 
                if ( !$row ) {
                        return false;
@@ -332,6 +511,54 @@ class WatchedItemStore {
                return $item;
        }
 
+       /**
+        * @param User $user
+        * @param array $options Allowed keys:
+        *        'forWrite' => bool defaults to false
+        *        'sort' => string optional sorting by namespace ID and title
+        *                     one of the self::SORT_* constants
+        *
+        * @return WatchedItem[]
+        */
+       public function getWatchedItemsForUser( User $user, array $options = [] ) {
+               $options += [ 'forWrite' => false ];
+
+               $dbOptions = [];
+               if ( array_key_exists( 'sort', $options ) ) {
+                       Assert::parameter(
+                               ( in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ),
+                               '$options[\'sort\']',
+                               'must be SORT_ASC or SORT_DESC'
+                       );
+                       $dbOptions['ORDER BY'] = [
+                               "wl_namespace {$options['sort']}",
+                               "wl_title {$options['sort']}"
+                       ];
+               }
+               $db = $this->getConnection( $options['forWrite'] ? DB_MASTER : DB_SLAVE );
+
+               $res = $db->select(
+                       'watchlist',
+                       [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
+                       [ 'wl_user' => $user->getId() ],
+                       __METHOD__,
+                       $dbOptions
+               );
+               $this->reuseConnection( $db );
+
+               $watchedItems = [];
+               foreach ( $res as $row ) {
+                       // todo these could all be cached at some point?
+                       $watchedItems[] = new WatchedItem(
+                               $user,
+                               new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
+                               $row->wl_notificationtimestamp
+                       );
+               }
+
+               return $watchedItems;
+       }
+
        /**
         * Must be called separately for Subject & Talk namespaces
         *
@@ -344,6 +571,61 @@ class WatchedItemStore {
                return (bool)$this->getWatchedItem( $user, $target );
        }
 
+       /**
+        * @param User $user
+        * @param LinkTarget[] $targets
+        *
+        * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp,
+        *         where $timestamp is:
+        *         - string|null value of wl_notificationtimestamp,
+        *         - false if $target is not watched by $user.
+        */
+       public function getNotificationTimestampsBatch( User $user, array $targets ) {
+               $timestamps = [];
+               foreach ( $targets as $target ) {
+                       $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
+               }
+
+               if ( $user->isAnon() ) {
+                       return $timestamps;
+               }
+
+               $targetsToLoad = [];
+               foreach ( $targets as $target ) {
+                       $cachedItem = $this->getCached( $user, $target );
+                       if ( $cachedItem ) {
+                               $timestamps[$target->getNamespace()][$target->getDBkey()] =
+                                       $cachedItem->getNotificationTimestamp();
+                       } else {
+                               $targetsToLoad[] = $target;
+                       }
+               }
+
+               if ( !$targetsToLoad ) {
+                       return $timestamps;
+               }
+
+               $dbr = $this->getConnection( DB_SLAVE );
+
+               $lb = new LinkBatch( $targetsToLoad );
+               $res = $dbr->select(
+                       'watchlist',
+                       [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
+                       [
+                               $lb->constructSet( 'wl', $dbr ),
+                               'wl_user' => $user->getId(),
+                       ],
+                       __METHOD__
+               );
+               $this->reuseConnection( $dbr );
+
+               foreach ( $res as $row ) {
+                       $timestamps[(int)$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
+               }
+
+               return $timestamps;
+       }
+
        /**
         * Must be called separately for Subject & Talk namespaces
         *
@@ -351,30 +633,30 @@ class WatchedItemStore {
         * @param LinkTarget $target
         */
        public function addWatch( User $user, LinkTarget $target ) {
-               $this->addWatchBatch( [ [ $user, $target ] ] );
+               $this->addWatchBatchForUser( $user, [ $target ] );
        }
 
        /**
-        * @param array[] $userTargetCombinations array of arrays containing [0] => User [1] => LinkTarget
+        * @param User $user
+        * @param LinkTarget[] $targets
         *
         * @return bool success
         */
-       public function addWatchBatch( array $userTargetCombinations ) {
+       public function addWatchBatchForUser( User $user, array $targets ) {
                if ( $this->loadBalancer->getReadOnlyReason() !== false ) {
                        return false;
                }
+               // Only loggedin user can have a watchlist
+               if ( $user->isAnon() ) {
+                       return false;
+               }
+
+               if ( !$targets ) {
+                       return true;
+               }
 
                $rows = [];
-               foreach ( $userTargetCombinations as list( $user, $target ) ) {
-                       /**
-                        * @var User $user
-                        * @var LinkTarget $target
-                        */
-
-                       // Only loggedin user can have a watchlist
-                       if ( $user->isAnon() ) {
-                               continue;
-                       }
+               foreach ( $targets as $target ) {
                        $rows[] = [
                                'wl_user' => $user->getId(),
                                'wl_namespace' => $target->getNamespace(),
@@ -384,17 +666,13 @@ class WatchedItemStore {
                        $this->uncache( $user, $target );
                }
 
-               if ( !$rows ) {
-                       return false;
-               }
-
-               $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
+               $dbw = $this->getConnection( DB_MASTER );
                foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
                        // Use INSERT IGNORE to avoid overwriting the notification timestamp
                        // if there's already an entry for this page
                        $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
                }
-               $this->loadBalancer->reuseConnection( $dbw );
+               $this->reuseConnection( $dbw );
 
                return true;
        }
@@ -418,7 +696,7 @@ class WatchedItemStore {
 
                $this->uncache( $user, $target );
 
-               $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
+               $dbw = $this->getConnection( DB_MASTER );
                $dbw->delete( 'watchlist',
                        [
                                'wl_user' => $user->getId(),
@@ -427,7 +705,7 @@ class WatchedItemStore {
                        ], __METHOD__
                );
                $success = (bool)$dbw->affectedRows();
-               $this->loadBalancer->reuseConnection( $dbw );
+               $this->reuseConnection( $dbw );
 
                return $success;
        }
@@ -441,7 +719,7 @@ class WatchedItemStore {
         * @return int[] Array of user IDs the timestamp has been updated for
         */
        public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
-               $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
+               $dbw = $this->getConnection( DB_MASTER );
                $res = $dbw->select( [ 'watchlist' ],
                        [ 'wl_user' ],
                        [
@@ -476,7 +754,7 @@ class WatchedItemStore {
                        );
                }
 
-               $this->loadBalancer->reuseConnection( $dbw );
+               $this->reuseConnection( $dbw );
 
                return $watchers;
        }
@@ -579,6 +857,44 @@ class WatchedItemStore {
                return $notificationTimestamp;
        }
 
+       /**
+        * @param User $user
+        * @param int $unreadLimit
+        *
+        * @return int|bool The number of unread notifications
+        *                  true if greater than or equal to $unreadLimit
+        */
+       public function countUnreadNotifications( User $user, $unreadLimit = null ) {
+               $queryOptions = [];
+               if ( $unreadLimit !== null ) {
+                       $unreadLimit = (int)$unreadLimit;
+                       $queryOptions['LIMIT'] = $unreadLimit;
+               }
+
+               $dbr = $this->getConnection( DB_SLAVE );
+               $rowCount = $dbr->selectRowCount(
+                       'watchlist',
+                       '1',
+                       [
+                               'wl_user' => $user->getId(),
+                               'wl_notificationtimestamp IS NOT NULL',
+                       ],
+                       __METHOD__,
+                       $queryOptions
+               );
+               $this->reuseConnection( $dbr );
+
+               if ( !isset( $unreadLimit ) ) {
+                       return $rowCount;
+               }
+
+               if ( $rowCount >= $unreadLimit ) {
+                       return true;
+               }
+
+               return $rowCount;
+       }
+
        /**
         * Check if the given title already is watched by the user, and if so
         * add a watch for the new title.
@@ -611,7 +927,7 @@ class WatchedItemStore {
         * @param LinkTarget $newTarget
         */
        public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
-               $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
+               $dbw = $this->getConnection( DB_MASTER );
 
                $result = $dbw->select(
                        'watchlist',
@@ -650,7 +966,7 @@ class WatchedItemStore {
                        );
                }
 
-               $this->loadBalancer->reuseConnection( $dbw );
+               $this->reuseConnection( $dbw );
        }
 
 }