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 * - batchSize, Number of watchlist entries to remove at once.
32 * - userId, The ID for the user whose watchlist is being cleared.
33 * - maxWatchlistId, The maximum wl_id at the time the job was first created,
35 public function __construct( Title
$title = null, array $params ) {
36 if ( !array_key_exists( 'batchSize', $params ) ) {
37 $params['batchSize'] = 1000;
42 SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' ),
46 $this->removeDuplicates
= true;
49 public function run() {
50 $userId = $this->params
['userId'];
51 $maxWatchlistId = $this->params
['maxWatchlistId'];
53 $loadBalancer = MediaWikiServices
::getInstance()->getDBLoadBalancer();
54 $dbw = $loadBalancer->getConnection( DB_MASTER
);
55 $dbr = $loadBalancer->getConnection( DB_REPLICA
, [ 'watchlist' ] );
57 // Wait before lock to try to reduce time waiting in the lock.
58 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
59 $this->setLastError( 'Timed out while waiting for slave to catch up before lock' );
63 // Use a named lock so that jobs for this user see each others' changes
64 $lockKey = "ClearUserWatchlistJob:$userId";
65 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__
, 10 );
67 $this->setLastError( "Could not acquire lock '$lockKey'" );
71 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
72 $this->setLastError( 'Timed out while waiting for slave to catch up within lock' );
76 // Clear any stale REPEATABLE-READ snapshot
77 $dbr->flushSnapshot( __METHOD__
);
79 $watchlistIds = $dbr->selectFieldValues(
84 'wl_id <= ' . $maxWatchlistId
88 'ORDER BY' => 'wl_id ASC',
89 'LIMIT' => $this->params
['batchSize'],
93 if ( count( $watchlistIds ) == 0 ) {
97 $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__
);
99 // Commit changes and remove lock before inserting next job.
100 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
101 $lbf->commitMasterChanges( __METHOD__
);
102 unset( $scopedLock );
104 if ( count( $watchlistIds ) == $this->params
['batchSize'] ) {
105 JobQueueGroup
::singleton()->push( new self( $this->getTitle(), $this->getParams() ) );
111 public function getDeduplicationInfo() {
112 $info = parent
::getDeduplicationInfo();
113 // This job never has a namespace or title so we can't use it for deduplication
114 unset( $info['namespace'] );
115 unset( $info['title'] );