Merge "Fix table loading bug for CRH transliteration"
[lhc/web/wiklou.git] / includes / watcheditem / WatchedItemStore.php
index 094297c..35e824e 100644 (file)
@@ -13,10 +13,6 @@ use Wikimedia\Rdbms\LoadBalancer;
  * Database interaction & caching
  * TODO caching should be factored out into a CachingWatchedItemStore class
  *
- * Uses database because this uses User::isAnon
- *
- * @group Database
- *
  * @author Addshore
  * @since 1.27
  */
@@ -55,6 +51,11 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
         */
        private $revisionGetTimestampFromIdCallback;
 
+       /**
+        * @var int
+        */
+       private $updateRowsPerQuery;
+
        /**
         * @var StatsdDataFactoryInterface
         */
@@ -64,20 +65,28 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
         * @param LoadBalancer $loadBalancer
         * @param HashBagOStuff $cache
         * @param ReadOnlyMode $readOnlyMode
+        * @param int $updateRowsPerQuery
         */
        public function __construct(
                LoadBalancer $loadBalancer,
                HashBagOStuff $cache,
-               ReadOnlyMode $readOnlyMode
+               ReadOnlyMode $readOnlyMode,
+               $updateRowsPerQuery
        ) {
                $this->loadBalancer = $loadBalancer;
                $this->cache = $cache;
                $this->readOnlyMode = $readOnlyMode;
                $this->stats = new NullStatsdDataFactory();
-               $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ];
-               $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ];
+               $this->deferredUpdatesAddCallableUpdateCallback =
+                       [ DeferredUpdates::class, 'addCallableUpdate' ];
+               $this->revisionGetTimestampFromIdCallback =
+                       [ Revision::class, 'getTimestampFromId' ];
+               $this->updateRowsPerQuery = $updateRowsPerQuery;
        }
 
+       /**
+        * @param StatsdDataFactoryInterface $stats
+        */
        public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) {
                $this->stats = $stats;
        }
@@ -212,8 +221,87 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
                return $this->loadBalancer->getConnectionRef( $dbIndex, [ 'watchlist' ] );
        }
 
+       /**
+        * Deletes ALL watched items for the given user when under
+        * $updateRowsPerQuery entries exist.
+        *
+        * @since 1.30
+        *
+        * @param User $user
+        *
+        * @return bool true on success, false when too many items are watched
+        */
+       public function clearUserWatchedItems( User $user ) {
+               if ( $this->countWatchedItems( $user ) > $this->updateRowsPerQuery ) {
+                       return false;
+               }
+
+               $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
+               $dbw->delete(
+                       'watchlist',
+                       [ 'wl_user' => $user->getId() ],
+                       __METHOD__
+               );
+               $this->uncacheAllItemsForUser( $user );
+
+               return true;
+       }
+
+       private function uncacheAllItemsForUser( User $user ) {
+               $userId = $user->getId();
+               foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
+                       foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
+                               if ( array_key_exists( $userId, $userIndex ) ) {
+                                       $this->cache->delete( $userIndex[$userId] );
+                                       unset( $this->cacheIndex[$ns][$dbKey][$userId] );
+                               }
+                       }
+               }
+
+               // Cleanup empty cache keys
+               foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
+                       foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
+                               if ( empty( $this->cacheIndex[$ns][$dbKey] ) ) {
+                                       unset( $this->cacheIndex[$ns][$dbKey] );
+                               }
+                       }
+                       if ( empty( $this->cacheIndex[$ns] ) ) {
+                               unset( $this->cacheIndex[$ns] );
+                       }
+               }
+       }
+
+       /**
+        * Queues a job that will clear the users watchlist using the Job Queue.
+        *
+        * @since 1.31
+        *
+        * @param User $user
+        */
+       public function clearUserWatchedItemsUsingJobQueue( User $user ) {
+               $job = ClearUserWatchlistJob::newForUser( $user, $this->getMaxId() );
+               // TODO inject me.
+               JobQueueGroup::singleton()->push( $job );
+       }
+
        /**
         * @since 1.31
+        * @return int The maximum current wl_id
+        */
+       public function getMaxId() {
+               $dbr = $this->getConnectionRef( DB_REPLICA );
+               return (int)$dbr->selectField(
+                       'watchlist',
+                       'MAX(wl_id)',
+                       '',
+                       __METHOD__
+               );
+       }
+
+       /**
+        * @since 1.31
+        * @param User $user
+        * @return int
         */
        public function countWatchedItems( User $user ) {
                $dbr = $this->getConnectionRef( DB_REPLICA );
@@ -231,6 +319,8 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param LinkTarget $target
+        * @return int
         */
        public function countWatchers( LinkTarget $target ) {
                $dbr = $this->getConnectionRef( DB_REPLICA );
@@ -249,6 +339,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param LinkTarget $target
+        * @param string|int $threshold
+        * @return int
         */
        public function countVisitingWatchers( LinkTarget $target, $threshold ) {
                $dbr = $this->getConnectionRef( DB_REPLICA );
@@ -270,6 +363,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param LinkTarget[] $targets
+        * @param array $options
+        * @return array
         */
        public function countWatchersMultiple( array $targets, array $options = [] ) {
                $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
@@ -303,6 +399,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param array $targetsWithVisitThresholds
+        * @param int|null $minimumWatchers
+        * @return array
         */
        public function countVisitingWatchersMultiple(
                array $targetsWithVisitThresholds,
@@ -383,6 +482,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param LinkTarget $target
+        * @return bool
         */
        public function getWatchedItem( User $user, LinkTarget $target ) {
                if ( $user->isAnon() ) {
@@ -400,6 +502,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param LinkTarget $target
+        * @return bool
         */
        public function loadWatchedItem( User $user, LinkTarget $target ) {
                // Only loggedin user can have a watchlist
@@ -431,6 +536,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param array $options
+        * @return WatchedItem[]
         */
        public function getWatchedItemsForUser( User $user, array $options = [] ) {
                $options += [ 'forWrite' => false ];
@@ -472,6 +580,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param LinkTarget $target
+        * @return bool
         */
        public function isWatched( User $user, LinkTarget $target ) {
                return (bool)$this->getWatchedItem( $user, $target );
@@ -479,6 +590,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param LinkTarget[] $targets
+        * @return array
         */
        public function getNotificationTimestampsBatch( User $user, array $targets ) {
                $timestamps = [];
@@ -528,6 +642,8 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param LinkTarget $target
         */
        public function addWatch( User $user, LinkTarget $target ) {
                $this->addWatchBatchForUser( $user, [ $target ] );
@@ -535,6 +651,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param LinkTarget[] $targets
+        * @return bool
         */
        public function addWatchBatchForUser( User $user, array $targets ) {
                if ( $this->readOnlyMode->isReadOnly() ) {
@@ -584,6 +703,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param LinkTarget $target
+        * @return bool
         */
        public function removeWatch( User $user, LinkTarget $target ) {
                // Only logged in user can have a watchlist
@@ -608,6 +730,10 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param string|int $timestamp
+        * @param LinkTarget[] $targets
+        * @return bool
         */
        public function setNotificationTimestampsForUser( User $user, $timestamp, array $targets = [] ) {
                // Only loggedin user can have a watchlist
@@ -641,6 +767,10 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $editor
+        * @param LinkTarget $target
+        * @param string|int $timestamp
+        * @return int
         */
        public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
                $dbw = $this->getConnectionRef( DB_MASTER );
@@ -697,6 +827,11 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param Title $title
+        * @param string $force
+        * @param int $oldid
+        * @return bool
         */
        public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
                // Only loggedin user can have a watchlist
@@ -786,6 +921,9 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param User $user
+        * @param int|null $unreadLimit
+        * @return int|bool
         */
        public function countUnreadNotifications( User $user, $unreadLimit = null ) {
                $queryOptions = [];
@@ -819,6 +957,8 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param LinkTarget $oldTarget
+        * @param LinkTarget $newTarget
         */
        public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
                $oldTarget = Title::newFromLinkTarget( $oldTarget );
@@ -830,6 +970,8 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac
 
        /**
         * @since 1.27
+        * @param LinkTarget $oldTarget
+        * @param LinkTarget $newTarget
         */
        public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
                $dbw = $this->getConnectionRef( DB_MASTER );