+ * @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