MediaWiki  master
ClearUserWatchlistJob.php
Go to the documentation of this file.
1 <?php
2 
6 
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  $updateRowsPerQuery = MediaWikiServices::getInstance()->getMainConfig()->get(
38  MainConfigNames::UpdateRowsPerQuery );
39  $userId = $this->params['userId'];
40  $maxWatchlistId = $this->params['maxWatchlistId'];
41  $batchSize = $updateRowsPerQuery;
42 
43  $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
44  $dbw = $loadBalancer->getConnectionRef( DB_PRIMARY );
45  $dbr = $loadBalancer->getConnectionRef( DB_REPLICA );
46 
47  // Wait before lock to try to reduce time waiting in the lock.
48  if ( !$loadBalancer->waitForPrimaryPos( $dbr ) ) {
49  $this->setLastError( 'Timed out waiting for replica to catch up before lock' );
50  return false;
51  }
52 
53  // Use a named lock so that jobs for this user see each others' changes
54  $lockKey = "{{$dbw->getDomainID()}}:ClearUserWatchlist:$userId"; // per-wiki
55  $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 10 );
56  if ( !$scopedLock ) {
57  $this->setLastError( "Could not acquire lock '$lockKey'" );
58  return false;
59  }
60 
61  if ( !$loadBalancer->waitForPrimaryPos( $dbr ) ) {
62  $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
63  return false;
64  }
65 
66  // Clear any stale REPEATABLE-READ snapshot
67  $dbr->flushSnapshot( __METHOD__ );
68 
69  $watchlistIds = $dbr->selectFieldValues(
70  'watchlist',
71  'wl_id',
72  [
73  'wl_user' => $userId,
74  'wl_id <= ' . $maxWatchlistId
75  ],
76  __METHOD__,
77  [
78  'LIMIT' => $batchSize,
79  ]
80  );
81 
82  if ( count( $watchlistIds ) == 0 ) {
83  return true;
84  }
85 
86  $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__ );
87  if ( MediaWikiServices::getInstance()->getMainConfig()->get(
88  MainConfigNames::WatchlistExpiry ) ) {
89  $dbw->delete( 'watchlist_expiry', [ 'we_item' => $watchlistIds ], __METHOD__ );
90  }
91 
92  // Commit changes and remove lock before inserting next job.
93  $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
94  $lbf->commitPrimaryChanges( __METHOD__ );
95  unset( $scopedLock );
96 
97  if ( count( $watchlistIds ) === (int)$batchSize ) {
98  // Until we get less results than the limit, recursively push
99  // the same job again.
100  MediaWikiServices::getInstance()->getJobQueueGroup()->push( new self( $this->getParams() ) );
101  }
102 
103  return true;
104  }
105 
106  public function getDeduplicationInfo() {
107  $info = parent::getDeduplicationInfo();
108  // This job never has a namespace or title so we can't use it for deduplication
109  unset( $info['namespace'] );
110  unset( $info['title'] );
111  return $info;
112  }
113 
114 }
Job to clear a users watchlist in batches.
static newForUser(UserIdentity $user, $maxWatchlistId)
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Class to both describe a background job and handle jobs.
Definition: Job.php:39
getParams()
array Parameters that specify sources, targets, and options for execution
Definition: Job.php:193
setLastError( $error)
Definition: Job.php:469
array $params
Array of job parameters.
Definition: Job.php:44
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Interface for generic jobs only uses the parameters field and are JSON serializable.
Interface for objects representing user identity.
getId( $wikiId=self::LOCAL)
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28