Cleanup some comments related to DB replication
[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 {
14
15 /**
16 * @param User $user User to clear the watchlist for.
17 * @param int $maxWatchlistId The maximum wl_id at the time the job was first created.
18 *
19 * @return ClearUserWatchlistJob
20 */
21 public static function newForUser( User $user, $maxWatchlistId ) {
22 return new self(
23 null,
24 [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ]
25 );
26 }
27
28 /**
29 * @param Title|null $title Not used by this job.
30 * @param array $params
31 * - userId, The ID for the user whose watchlist is being cleared.
32 * - maxWatchlistId, The maximum wl_id at the time the job was first created,
33 */
34 public function __construct( Title $title = null, array $params ) {
35 parent::__construct(
36 'clearUserWatchlist',
37 SpecialPage::getTitleFor( 'EditWatchlist', 'clear' ),
38 $params
39 );
40
41 $this->removeDuplicates = true;
42 }
43
44 public function run() {
45 global $wgUpdateRowsPerQuery;
46 $userId = $this->params['userId'];
47 $maxWatchlistId = $this->params['maxWatchlistId'];
48 $batchSize = $wgUpdateRowsPerQuery;
49
50 $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
51 $dbw = $loadBalancer->getConnection( DB_MASTER );
52 $dbr = $loadBalancer->getConnection( DB_REPLICA, [ 'watchlist' ] );
53
54 // Wait before lock to try to reduce time waiting in the lock.
55 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
56 $this->setLastError( 'Timed out waiting for replica to catch up before lock' );
57 return false;
58 }
59
60 // Use a named lock so that jobs for this user see each others' changes
61 $lockKey = "ClearUserWatchlistJob:$userId";
62 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 10 );
63 if ( !$scopedLock ) {
64 $this->setLastError( "Could not acquire lock '$lockKey'" );
65 return false;
66 }
67
68 if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
69 $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
70 return false;
71 }
72
73 // Clear any stale REPEATABLE-READ snapshot
74 $dbr->flushSnapshot( __METHOD__ );
75
76 $watchlistIds = $dbr->selectFieldValues(
77 'watchlist',
78 'wl_id',
79 [
80 'wl_user' => $userId,
81 'wl_id <= ' . $maxWatchlistId
82 ],
83 __METHOD__,
84 [
85 'ORDER BY' => 'wl_id ASC',
86 'LIMIT' => $batchSize,
87 ]
88 );
89
90 if ( count( $watchlistIds ) == 0 ) {
91 return true;
92 }
93
94 $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__ );
95
96 // Commit changes and remove lock before inserting next job.
97 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
98 $lbf->commitMasterChanges( __METHOD__ );
99 unset( $scopedLock );
100
101 if ( count( $watchlistIds ) === (int)$batchSize ) {
102 // Until we get less results than the limit, recursively push
103 // the same job again.
104 JobQueueGroup::singleton()->push( new self( $this->getTitle(), $this->getParams() ) );
105 }
106
107 return true;
108 }
109
110 public function getDeduplicationInfo() {
111 $info = parent::getDeduplicationInfo();
112 // This job never has a namespace or title so we can't use it for deduplication
113 unset( $info['namespace'] );
114 unset( $info['title'] );
115 return $info;
116 }
117
118 }