MediaWiki REL1_32
ClearUserWatchlistJob.php
Go to the documentation of this file.
1<?php
2
4
14
21 public static function newForUser( User $user, $maxWatchlistId ) {
22 return new self(
23 null,
24 [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ]
25 );
26 }
27
34 public function __construct( Title $title = null, array $params ) {
35 parent::__construct(
36 'clearUserWatchlist',
37 SpecialPage::getTitleFor( 'EditWatchlist', 'clear' ),
39 );
40
41 $this->removeDuplicates = true;
42 }
43
44 public function run() {
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}
$wgUpdateRowsPerQuery
Number of rows to update per query.
Job to clear a users watchlist in batches.
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
static newForUser(User $user, $maxWatchlistId)
__construct(Title $title=null, array $params)
Class to both describe a background job and handle jobs.
Definition Job.php:30
getParams()
Definition Job.php:142
setLastError( $error)
Definition Job.php:384
array $params
Array of job parameters.
Definition Job.php:35
getTitle()
Definition Job.php:135
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26