MediaWiki  master
ClearUserWatchlistJob.php
Go to the documentation of this file.
1 <?php
2 
5 
14 class ClearUserWatchlistJob extends Job implements GenericParameterJob {
20  public function __construct( array $params ) {
21  parent::__construct( 'clearUserWatchlist', $params );
22 
23  $this->removeDuplicates = true;
24  }
25 
32  public static function newForUser( UserIdentity $user, $maxWatchlistId ) {
33  return new self( [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ] );
34  }
35 
36  public function run() {
37  global $wgUpdateRowsPerQuery;
38  $userId = $this->params['userId'];
39  $maxWatchlistId = $this->params['maxWatchlistId'];
40  $batchSize = $wgUpdateRowsPerQuery;
41 
42  $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
43  $dbw = $loadBalancer->getConnectionRef( DB_MASTER );
44  $dbr = $loadBalancer->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
45 
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' );
49  return false;
50  }
51 
52  // Use a named lock so that jobs for this user see each others' changes
53  $lockKey = "{{$dbw->getDomainID()}}:ClearUserWatchlist:$userId"; // per-wiki
54  $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 10 );
55  if ( !$scopedLock ) {
56  $this->setLastError( "Could not acquire lock '$lockKey'" );
57  return false;
58  }
59 
60  if ( !$loadBalancer->waitForMasterPos( $dbr ) ) {
61  $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
62  return false;
63  }
64 
65  // Clear any stale REPEATABLE-READ snapshot
66  $dbr->flushSnapshot( __METHOD__ );
67 
68  $watchlistIds = $dbr->selectFieldValues(
69  'watchlist',
70  'wl_id',
71  [
72  'wl_user' => $userId,
73  'wl_id <= ' . $maxWatchlistId
74  ],
75  __METHOD__,
76  [
77  'LIMIT' => $batchSize,
78  ]
79  );
80 
81  if ( count( $watchlistIds ) == 0 ) {
82  return true;
83  }
84 
85  $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__ );
86  if ( MediaWikiServices::getInstance()->getMainConfig()->get( 'WatchlistExpiry' ) ) {
87  $dbw->delete( 'watchlist_expiry', [ 'we_item' => $watchlistIds ], __METHOD__ );
88  }
89 
90  // Commit changes and remove lock before inserting next job.
91  $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
92  $lbf->commitMasterChanges( __METHOD__ );
93  unset( $scopedLock );
94 
95  if ( count( $watchlistIds ) === (int)$batchSize ) {
96  // Until we get less results than the limit, recursively push
97  // the same job again.
98  JobQueueGroup::singleton()->push( new self( $this->getParams() ) );
99  }
100 
101  return true;
102  }
103 
104  public function getDeduplicationInfo() {
105  $info = parent::getDeduplicationInfo();
106  // This job never has a namespace or title so we can't use it for deduplication
107  unset( $info['namespace'] );
108  unset( $info['title'] );
109  return $info;
110  }
111 
112 }
ClearUserWatchlistJob\getDeduplicationInfo
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition: ClearUserWatchlistJob.php:104
Job\getParams
getParams()
array Parameters that specify sources, targets, and options for execution
Definition: Job.php:188
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:173
ClearUserWatchlistJob\newForUser
static newForUser(UserIdentity $user, $maxWatchlistId)
Definition: ClearUserWatchlistJob.php:32
MediaWiki\User\UserIdentity\getId
getId( $wikiId=self::LOCAL)
Job\$params
array $params
Array of job parameters.
Definition: Job.php:40
ClearUserWatchlistJob\run
run()
Run the job.
Definition: ClearUserWatchlistJob.php:36
Job\setLastError
setLastError( $error)
Definition: Job.php:464
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
ClearUserWatchlistJob\__construct
__construct(array $params)
Definition: ClearUserWatchlistJob.php:20
$dbr
$dbr
Definition: testCompression.php:54
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:35
$wgUpdateRowsPerQuery
$wgUpdateRowsPerQuery
Number of rows to update per query.
Definition: DefaultSettings.php:9067
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
ClearUserWatchlistJob
Job to clear a users watchlist in batches.
Definition: ClearUserWatchlistJob.php:14
GenericParameterJob
Interface for generic jobs only uses the parameters field and are JSON serializable.
Definition: GenericParameterJob.php:31