3 use MediaWiki\MediaWikiServices
;
6 * Job to clear a users watchlist in batches.
13 class ClearUserWatchlistJob
extends Job
{
16 * @param User $user User to clear the watchlist for.
17 * @param int $maxWatchlistId The maximum wl_id at the time the job was first created.
19 * @return ClearUserWatchlistJob
21 public static function newForUser( User
$user, $maxWatchlistId ) {
24 [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ]
29 * @param Title|null $title Not used by this job.
30 * @param array $params
31 * - userId, The ID for the user whose watchlist is being cleared.
32 * - maxWatchlistId, The maximum wl_id at the time the job was first created,
34 public function __construct( Title
$title = null, array $params ) {
37 SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' ),
41 $this->removeDuplicates
= true;
44 public function run() {
45 global $wgUpdateRowsPerQuery;
46 $userId = $this->params
['userId'];
47 $maxWatchlistId = $this->params
['maxWatchlistId'];
48 $batchSize = $wgUpdateRowsPerQuery;
50 $loadBalancer = MediaWikiServices
::getInstance()->getDBLoadBalancer();
51 $dbw = $loadBalancer->getConnection( DB_MASTER
);
52 $dbr = $loadBalancer->getConnection( DB_REPLICA
, [ 'watchlist' ] );
54 // Wait before lock to try to reduce time waiting in the lock.
55 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
56 $this->setLastError( 'Timed out waiting for replica to catch up before lock' );
60 // Use a named lock so that jobs for this user see each others' changes
61 $lockKey = "ClearUserWatchlistJob:$userId";
62 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__
, 10 );
64 $this->setLastError( "Could not acquire lock '$lockKey'" );
68 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
69 $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
73 // Clear any stale REPEATABLE-READ snapshot
74 $dbr->flushSnapshot( __METHOD__
);
76 $watchlistIds = $dbr->selectFieldValues(
81 'wl_id <= ' . $maxWatchlistId
85 'ORDER BY' => 'wl_id ASC',
86 'LIMIT' => $batchSize,
90 if ( count( $watchlistIds ) == 0 ) {
94 $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__
);
96 // Commit changes and remove lock before inserting next job.
97 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
98 $lbf->commitMasterChanges( __METHOD__
);
101 if ( count( $watchlistIds ) === (int)$batchSize ) {
102 // Until we get less results than the limit, recursively push
103 // the same job again.
104 JobQueueGroup
::singleton()->push( new self( $this->getTitle(), $this->getParams() ) );
110 public function getDeduplicationInfo() {
111 $info = parent
::getDeduplicationInfo();
112 // This job never has a namespace or title so we can't use it for deduplication
113 unset( $info['namespace'] );
114 unset( $info['title'] );