3 use MediaWiki\MediaWikiServices
;
6 * Job to clear a users watchlist in batches.
13 class ClearUserWatchlistJob
extends Job
implements GenericParameterJob
{
15 * @param array $params
16 * - userId, The ID for the user whose watchlist is being cleared.
17 * - maxWatchlistId, The maximum wl_id at the time the job was first created,
19 public function __construct( array $params ) {
20 parent
::__construct( 'clearUserWatchlist', $params );
22 $this->removeDuplicates
= true;
26 * @param User $user User to clear the watchlist for.
27 * @param int $maxWatchlistId The maximum wl_id at the time the job was first created.
29 * @return ClearUserWatchlistJob
31 public static function newForUser( User
$user, $maxWatchlistId ) {
32 return new self( [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ] );
35 public function run() {
36 global $wgUpdateRowsPerQuery;
37 $userId = $this->params
['userId'];
38 $maxWatchlistId = $this->params
['maxWatchlistId'];
39 $batchSize = $wgUpdateRowsPerQuery;
41 $loadBalancer = MediaWikiServices
::getInstance()->getDBLoadBalancer();
42 $dbw = $loadBalancer->getConnection( DB_MASTER
);
43 $dbr = $loadBalancer->getConnection( DB_REPLICA
, [ 'watchlist' ] );
45 // Wait before lock to try to reduce time waiting in the lock.
46 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
47 $this->setLastError( 'Timed out waiting for replica to catch up before lock' );
51 // Use a named lock so that jobs for this user see each others' changes
52 $lockKey = "ClearUserWatchlistJob:$userId";
53 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__
, 10 );
55 $this->setLastError( "Could not acquire lock '$lockKey'" );
59 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
60 $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
64 // Clear any stale REPEATABLE-READ snapshot
65 $dbr->flushSnapshot( __METHOD__
);
67 $watchlistIds = $dbr->selectFieldValues(
72 'wl_id <= ' . $maxWatchlistId
76 'ORDER BY' => 'wl_id ASC',
77 'LIMIT' => $batchSize,
81 if ( count( $watchlistIds ) == 0 ) {
85 $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__
);
87 // Commit changes and remove lock before inserting next job.
88 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
89 $lbf->commitMasterChanges( __METHOD__
);
92 if ( count( $watchlistIds ) === (int)$batchSize ) {
93 // Until we get less results than the limit, recursively push
94 // the same job again.
95 JobQueueGroup
::singleton()->push( new self( $this->getParams() ) );
101 public function getDeduplicationInfo() {
102 $info = parent
::getDeduplicationInfo();
103 // This job never has a namespace or title so we can't use it for deduplication
104 unset( $info['namespace'] );
105 unset( $info['title'] );