MediaWiki  REL1_31
ClearUserWatchlistJob.php
Go to the documentation of this file.
1 <?php
2 
4 
13 class ClearUserWatchlistJob extends Job {
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' ),
38  $params
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 }
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
ClearUserWatchlistJob\getDeduplicationInfo
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition: ClearUserWatchlistJob.php:110
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
Job\getParams
getParams()
Definition: Job.php:160
array
the array() calling protocol came about after MediaWiki 1.4rc1.
Job\$title
Title $title
Definition: Job.php:42
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:82
Job\$params
array $params
Array of job parameters.
Definition: Job.php:36
ClearUserWatchlistJob\run
run()
Run the job.
Definition: ClearUserWatchlistJob.php:44
Job\setLastError
setLastError( $error)
Definition: Job.php:419
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Job\getTitle
getTitle()
Definition: Job.php:153
$dbr
$dbr
Definition: testCompression.php:50
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:31
ClearUserWatchlistJob\__construct
__construct(Title $title=null, array $params)
Definition: ClearUserWatchlistJob.php:34
$wgUpdateRowsPerQuery
$wgUpdateRowsPerQuery
Number of rows to update per query.
Definition: DefaultSettings.php:8437
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Title
Represents a title within MediaWiki.
Definition: Title.php:39
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:72
ClearUserWatchlistJob
Job to clear a users watchlist in batches.
Definition: ClearUserWatchlistJob.php:13
ClearUserWatchlistJob\newForUser
static newForUser(User $user, $maxWatchlistId)
Definition: ClearUserWatchlistJob.php:21
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:25
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53