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->newSelectQueryBuilder()
70  ->select( 'wl_id' )
71  ->from( 'watchlist' )
72  ->where( [ 'wl_user' => $userId ] )
73  ->andWhere( $dbr->buildComparison( '<=', [ 'wl_id' => $maxWatchlistId ] ) )
74  ->limit( $batchSize )
75  ->caller( __METHOD__ )->fetchFieldValues();
76  if ( count( $watchlistIds ) == 0 ) {
77  return true;
78  }
79 
80  $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__ );
81  if ( MediaWikiServices::getInstance()->getMainConfig()->get(
82  MainConfigNames::WatchlistExpiry ) ) {
83  $dbw->delete( 'watchlist_expiry', [ 'we_item' => $watchlistIds ], __METHOD__ );
84  }
85 
86  // Commit changes and remove lock before inserting next job.
87  $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
88  $lbf->commitPrimaryChanges( __METHOD__ );
89  unset( $scopedLock );
90 
91  if ( count( $watchlistIds ) === (int)$batchSize ) {
92  // Until we get less results than the limit, recursively push
93  // the same job again.
94  MediaWikiServices::getInstance()->getJobQueueGroup()->push( new self( $this->getParams() ) );
95  }
96 
97  return true;
98  }
99 
100  public function getDeduplicationInfo() {
101  $info = parent::getDeduplicationInfo();
102  // This job never has a namespace or title so we can't use it for deduplication
103  unset( $info['namespace'] );
104  unset( $info['title'] );
105  return $info;
106  }
107 
108 }
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:153
setLastError( $error)
Definition: Job.php:431
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