Merge "Update Parser to use NamespaceInfo"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / ClearUserWatchlistJob.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Job to clear a users watchlist in batches.
7 *
8 * @author Addshore
9 *
10 * @ingroup JobQueue
11 * @since 1.31
12 */
13 class ClearUserWatchlistJob extends Job implements GenericParameterJob {
14 /**
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,
18 */
19 public function __construct( array $params ) {
20 parent::__construct( 'clearUserWatchlist', $params );
21
22 $this->removeDuplicates = true;
23 }
24
25 /**
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.
28 *
29 * @return ClearUserWatchlistJob
30 */
31 public static function newForUser( User $user, $maxWatchlistId ) {
32 return new self( [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ] );
33 }
34
35 public function run() {
36 global $wgUpdateRowsPerQuery;
37 $userId = $this->params['userId'];
38 $maxWatchlistId = $this->params['maxWatchlistId'];
39 $batchSize = $wgUpdateRowsPerQuery;
40
41 $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
42 $dbw = $loadBalancer->getConnection( DB_MASTER );
43 $dbr = $loadBalancer->getConnection( DB_REPLICA, [ 'watchlist' ] );
44
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' );
48 return false;
49 }
50
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 );
54 if ( !$scopedLock ) {
55 $this->setLastError( "Could not acquire lock '$lockKey'" );
56 return false;
57 }
58
59 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
60 $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
61 return false;
62 }
63
64 // Clear any stale REPEATABLE-READ snapshot
65 $dbr->flushSnapshot( __METHOD__ );
66
67 $watchlistIds = $dbr->selectFieldValues(
68 'watchlist',
69 'wl_id',
70 [
71 'wl_user' => $userId,
72 'wl_id <= ' . $maxWatchlistId
73 ],
74 __METHOD__,
75 [
76 'ORDER BY' => 'wl_id ASC',
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
87 // Commit changes and remove lock before inserting next job.
88 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
89 $lbf->commitMasterChanges( __METHOD__ );
90 unset( $scopedLock );
91
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() ) );
96 }
97
98 return true;
99 }
100
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'] );
106 return $info;
107 }
108
109 }