X-Git-Url: https://git.cyclocoop.org/admin/?a=blobdiff_plain;f=includes%2Fwatcheditem%2FWatchedItemStore.php;h=6e907deef825dc89571ecf712ba073f4ab14a783;hb=96f3dab658cdbaf50acefee0a33866177e854bb1;hp=d6d9ff0eb932e4ab2c21c1b35adef31427c3d75e;hpb=faf7cc4a09848c538320bd2b9067b1a77c0a0183;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/watcheditem/WatchedItemStore.php b/includes/watcheditem/WatchedItemStore.php index d6d9ff0eb9..6e907deef8 100644 --- a/includes/watcheditem/WatchedItemStore.php +++ b/includes/watcheditem/WatchedItemStore.php @@ -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,18 +65,23 @@ 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::class, 'addCallableUpdate' ]; - $this->revisionGetTimestampFromIdCallback = [ Revision::class, 'getTimestampFromId' ]; + $this->deferredUpdatesAddCallableUpdateCallback = + [ DeferredUpdates::class, 'addCallableUpdate' ]; + $this->revisionGetTimestampFromIdCallback = + [ Revision::class, 'getTimestampFromId' ]; + $this->updateRowsPerQuery = $updateRowsPerQuery; } /** @@ -215,6 +221,56 @@ 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. * @@ -448,7 +504,7 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac * @since 1.27 * @param User $user * @param LinkTarget $target - * @return bool + * @return WatchedItem|bool */ public function loadWatchedItem( User $user, LinkTarget $target ) { // Only loggedin user can have a watchlist @@ -637,7 +693,7 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac } // Update process cache to ensure skin doesn't claim that the current // page is unwatched in the response of action=watch itself (T28292). - // This would otherwise be re-queried from a slave by isWatched(). + // This would otherwise be re-queried from a replica by isWatched(). foreach ( $items as $item ) { $this->cache( $item ); } @@ -709,12 +765,34 @@ class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterfac return $success; } + public function resetAllNotificationTimestampsForUser( User $user ) { + // Only loggedin user can have a watchlist + if ( $user->isAnon() ) { + return; + } + + // If the page is watched by the user (or may be watched), update the timestamp + $job = new ClearWatchlistNotificationsJob( + $user->getUserPage(), + [ 'userId' => $user->getId(), 'casTime' => time() ] + ); + + // Try to run this post-send + // Calls DeferredUpdates::addCallableUpdate in normal operation + call_user_func( + $this->deferredUpdatesAddCallableUpdateCallback, + function () use ( $job ) { + $job->run(); + } + ); + } + /** * @since 1.27 * @param User $editor * @param LinkTarget $target * @param string|int $timestamp - * @return int + * @return int[] */ public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) { $dbw = $this->getConnectionRef( DB_MASTER );