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