MediaWiki  1.29.1
RecentChangesUpdateJob.php
Go to the documentation of this file.
1 <?php
24 
31 class RecentChangesUpdateJob extends Job {
33  parent::__construct( 'recentChangesUpdate', $title, $params );
34 
35  if ( !isset( $params['type'] ) ) {
36  throw new Exception( "Missing 'type' parameter." );
37  }
38 
39  $this->removeDuplicates = true;
40  }
41 
45  final public static function newPurgeJob() {
46  return new self(
47  SpecialPage::getTitleFor( 'Recentchanges' ), [ 'type' => 'purge' ]
48  );
49  }
50 
55  final public static function newCacheUpdateJob() {
56  return new self(
57  SpecialPage::getTitleFor( 'Recentchanges' ), [ 'type' => 'cacheUpdate' ]
58  );
59  }
60 
61  public function run() {
62  if ( $this->params['type'] === 'purge' ) {
63  $this->purgeExpiredRows();
64  } elseif ( $this->params['type'] === 'cacheUpdate' ) {
65  $this->updateActiveUsers();
66  } else {
67  throw new InvalidArgumentException(
68  "Invalid 'type' parameter '{$this->params['type']}'." );
69  }
70 
71  return true;
72  }
73 
74  protected function purgeExpiredRows() {
75  global $wgRCMaxAge, $wgUpdateRowsPerQuery;
76 
77  $lockKey = wfWikiID() . ':recentchanges-prune';
78 
79  $dbw = wfGetDB( DB_MASTER );
80  if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
81  || !$dbw->lock( $lockKey, __METHOD__, 1 )
82  ) {
83  return; // already in progress
84  }
85 
86  $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
87  $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
88  $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
89  do {
90  $rcIds = $dbw->selectFieldValues( 'recentchanges',
91  'rc_id',
92  [ 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ],
93  __METHOD__,
94  [ 'LIMIT' => $wgUpdateRowsPerQuery ]
95  );
96  if ( $rcIds ) {
97  $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIds ], __METHOD__ );
98  // There might be more, so try waiting for replica DBs
99  try {
100  $factory->commitAndWaitForReplication(
101  __METHOD__, $ticket, [ 'timeout' => 3 ]
102  );
103  } catch ( DBReplicationWaitError $e ) {
104  // Another job will continue anyway
105  break;
106  }
107  }
108  } while ( $rcIds );
109 
110  $dbw->unlock( $lockKey, __METHOD__ );
111  }
112 
113  protected function updateActiveUsers() {
114  global $wgActiveUserDays;
115 
116  // Users that made edits at least this many days ago are "active"
117  $days = $wgActiveUserDays;
118  // Pull in the full window of active users in this update
119  $window = $wgActiveUserDays * 86400;
120 
121  $dbw = wfGetDB( DB_MASTER );
122  // JobRunner uses DBO_TRX, but doesn't call begin/commit itself;
123  // onTransactionIdle() will run immediately since there is no trx.
124  $dbw->onTransactionIdle(
125  function () use ( $dbw, $days, $window ) {
126  $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
127  $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
128  // Avoid disconnect/ping() cycle that makes locks fall off
129  $dbw->setSessionOptions( [ 'connTimeout' => 900 ] );
130 
131  $lockKey = wfWikiID() . '-activeusers';
132  if ( !$dbw->lockIsFree( $lockKey, __METHOD__ ) || !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
133  // Exclusive update (avoids duplicate entries)… it's usually fine to just drop out here,
134  // if the Job is already running.
135  return;
136  }
137 
138  $nowUnix = time();
139  // Get the last-updated timestamp for the cache
140  $cTime = $dbw->selectField( 'querycache_info',
141  'qci_timestamp',
142  [ 'qci_type' => 'activeusers' ]
143  );
144  $cTimeUnix = $cTime ? wfTimestamp( TS_UNIX, $cTime ) : 1;
145 
146  // Pick the date range to fetch from. This is normally from the last
147  // update to till the present time, but has a limited window for sanity.
148  // If the window is limited, multiple runs are need to fully populate it.
149  $sTimestamp = max( $cTimeUnix, $nowUnix - $days * 86400 );
150  $eTimestamp = min( $sTimestamp + $window, $nowUnix );
151 
152  // Get all the users active since the last update
153  $res = $dbw->select(
154  [ 'recentchanges' ],
155  [ 'rc_user_text', 'lastedittime' => 'MAX(rc_timestamp)' ],
156  [
157  'rc_user > 0', // actual accounts
158  'rc_type != ' . $dbw->addQuotes( RC_EXTERNAL ), // no wikidata
159  'rc_log_type IS NULL OR rc_log_type != ' . $dbw->addQuotes( 'newusers' ),
160  'rc_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $sTimestamp ) ),
161  'rc_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $eTimestamp ) )
162  ],
163  __METHOD__,
164  [
165  'GROUP BY' => [ 'rc_user_text' ],
166  'ORDER BY' => 'NULL' // avoid filesort
167  ]
168  );
169  $names = [];
170  foreach ( $res as $row ) {
171  $names[$row->rc_user_text] = $row->lastedittime;
172  }
173 
174  // Find which of the recently active users are already accounted for
175  if ( count( $names ) ) {
176  $res = $dbw->select( 'querycachetwo',
177  [ 'user_name' => 'qcc_title' ],
178  [
179  'qcc_type' => 'activeusers',
180  'qcc_namespace' => NS_USER,
181  'qcc_title' => array_keys( $names ),
182  'qcc_value >= ' . $dbw->addQuotes( $nowUnix - $days * 86400 ), // TS_UNIX
183  ],
184  __METHOD__
185  );
186  // Note: In order for this to be actually consistent, we would need
187  // to update these rows with the new lastedittime.
188  foreach ( $res as $row ) {
189  unset( $names[$row->user_name] );
190  }
191  }
192 
193  // Insert the users that need to be added to the list
194  if ( count( $names ) ) {
195  $newRows = [];
196  foreach ( $names as $name => $lastEditTime ) {
197  $newRows[] = [
198  'qcc_type' => 'activeusers',
199  'qcc_namespace' => NS_USER,
200  'qcc_title' => $name,
201  'qcc_value' => wfTimestamp( TS_UNIX, $lastEditTime ),
202  'qcc_namespacetwo' => 0, // unused
203  'qcc_titletwo' => '' // unused
204  ];
205  }
206  foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
207  $dbw->insert( 'querycachetwo', $rowBatch, __METHOD__ );
208  $factory->commitAndWaitForReplication( __METHOD__, $ticket );
209  }
210  }
211 
212  // If a transaction was already started, it might have an old
213  // snapshot, so kludge the timestamp range back as needed.
214  $asOfTimestamp = min( $eTimestamp, (int)$dbw->trxTimestamp() );
215 
216  // Touch the data freshness timestamp
217  $dbw->replace( 'querycache_info',
218  [ 'qci_type' ],
219  [ 'qci_type' => 'activeusers',
220  'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ) ], // not always $now
221  __METHOD__
222  );
223 
224  $dbw->unlock( $lockKey, __METHOD__ );
225 
226  // Rotate out users that have not edited in too long (according to old data set)
227  $dbw->delete( 'querycachetwo',
228  [
229  'qcc_type' => 'activeusers',
230  'qcc_value < ' . $dbw->addQuotes( $nowUnix - $days * 86400 ) // TS_UNIX
231  ],
232  __METHOD__
233  );
234 
235  },
236  __METHOD__
237  );
238  }
239 }
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:143
RecentChangesUpdateJob
Job for pruning recent changes.
Definition: RecentChangesUpdateJob.php:31
captcha-old.count
count
Definition: captcha-old.py:225
RecentChangesUpdateJob\updateActiveUsers
updateActiveUsers()
Definition: RecentChangesUpdateJob.php:113
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1994
RecentChangesUpdateJob\run
run()
Run the job.
Definition: RecentChangesUpdateJob.php:61
Wikimedia\Rdbms\DBReplicationWaitError
Exception class for replica DB wait timeouts.
Definition: DBReplicationWaitError.php:28
Job\$title
Title $title
Definition: Job.php:42
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
RecentChangesUpdateJob\__construct
__construct(Title $title, array $params)
Definition: RecentChangesUpdateJob.php:32
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
RecentChangesUpdateJob\purgeExpiredRows
purgeExpiredRows()
Definition: RecentChangesUpdateJob.php:74
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
Job\$params
array $params
Array of job parameters.
Definition: Job.php:36
RecentChangesUpdateJob\newCacheUpdateJob
static newCacheUpdateJob()
Definition: RecentChangesUpdateJob.php:55
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:35
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:31
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3060
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_MASTER
const DB_MASTER
Definition: defines.php:26
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3011
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
Title
Represents a title within MediaWiki.
Definition: Title.php:39
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
RecentChangesUpdateJob\newPurgeJob
static newPurgeJob()
Definition: RecentChangesUpdateJob.php:45
NS_USER
const NS_USER
Definition: Defines.php:64
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:23
array
the array() calling protocol came about after MediaWiki 1.4rc1.