3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\User\UserIdentity
;
7 * Job to clear a users watchlist in batches.
14 class ClearUserWatchlistJob
extends Job
implements GenericParameterJob
{
16 * @param array $params
17 * - userId, The ID for the user whose watchlist is being cleared.
18 * - maxWatchlistId, The maximum wl_id at the time the job was first created,
20 public function __construct( array $params ) {
21 parent
::__construct( 'clearUserWatchlist', $params );
23 $this->removeDuplicates
= true;
27 * @param UserIdentity $user User to clear the watchlist for.
28 * @param int $maxWatchlistId The maximum wl_id at the time the job was first created.
30 * @return ClearUserWatchlistJob
32 public static function newForUser( UserIdentity
$user, $maxWatchlistId ) {
33 return new self( [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ] );
36 public function run() {
37 global $wgUpdateRowsPerQuery;
38 $userId = $this->params
['userId'];
39 $maxWatchlistId = $this->params
['maxWatchlistId'];
40 $batchSize = $wgUpdateRowsPerQuery;
42 $loadBalancer = MediaWikiServices
::getInstance()->getDBLoadBalancer();
43 $dbw = $loadBalancer->getConnection( DB_MASTER
);
44 $dbr = $loadBalancer->getConnection( DB_REPLICA
, [ 'watchlist' ] );
46 // Wait before lock to try to reduce time waiting in the lock.
47 if ( !$loadBalancer->waitForMasterPos( $dbr ) ) {
48 $this->setLastError( 'Timed out waiting for replica to catch up before lock' );
52 // Use a named lock so that jobs for this user see each others' changes
53 $lockKey = "ClearUserWatchlistJob:$userId";
54 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__
, 10 );
56 $this->setLastError( "Could not acquire lock '$lockKey'" );
60 if ( !$loadBalancer->waitForMasterPos( $dbr ) ) {
61 $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
65 // Clear any stale REPEATABLE-READ snapshot
66 $dbr->flushSnapshot( __METHOD__
);
68 $watchlistIds = $dbr->selectFieldValues(
73 'wl_id <= ' . $maxWatchlistId
77 'ORDER BY' => 'wl_id ASC',
78 'LIMIT' => $batchSize,
82 if ( count( $watchlistIds ) == 0 ) {
86 $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__
);
88 // Commit changes and remove lock before inserting next job.
89 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
90 $lbf->commitMasterChanges( __METHOD__
);
93 if ( count( $watchlistIds ) === (int)$batchSize ) {
94 // Until we get less results than the limit, recursively push
95 // the same job again.
96 JobQueueGroup
::singleton()->push( new self( $this->getParams() ) );
102 public function getDeduplicationInfo() {
103 $info = parent
::getDeduplicationInfo();
104 // This job never has a namespace or title so we can't use it for deduplication
105 unset( $info['namespace'] );
106 unset( $info['title'] );