MediaWiki  master
RecentChangesUpdateJob.php
Go to the documentation of this file.
1 <?php
23 
30 class RecentChangesUpdateJob extends Job {
31  public function __construct( Title $title, array $params ) {
32  parent::__construct( 'recentChangesUpdate', $title, $params );
33 
34  if ( !isset( $params['type'] ) ) {
35  throw new Exception( "Missing 'type' parameter." );
36  }
37 
38  $this->executionFlags |= self::JOB_NO_EXPLICIT_TRX_ROUND;
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  $rcMaxAge = MediaWikiServices::getInstance()->getMainConfig()->get( 'RCMaxAge' );
76  $updateRowsPerQuery = MediaWikiServices::getInstance()->getMainConfig()->get( 'UpdateRowsPerQuery' );
77  $dbw = wfGetDB( DB_PRIMARY );
78  $lockKey = $dbw->getDomainID() . ':recentchanges-prune';
79  if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
80  // already in progress
81  return;
82  }
83 
84  $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
85  $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
86  $cutoff = $dbw->timestamp( time() - $rcMaxAge );
87  $rcQuery = RecentChange::getQueryInfo();
88  do {
89  $rcIds = [];
90  $rows = [];
91  $res = $dbw->select(
92  $rcQuery['tables'],
93  $rcQuery['fields'],
94  [ 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ],
95  __METHOD__,
96  [ 'LIMIT' => $updateRowsPerQuery ],
97  $rcQuery['joins']
98  );
99  foreach ( $res as $row ) {
100  $rcIds[] = $row->rc_id;
101  $rows[] = $row;
102  }
103  if ( $rcIds ) {
104  $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIds ], __METHOD__ );
105  Hooks::runner()->onRecentChangesPurgeRows( $rows );
106  // There might be more, so try waiting for replica DBs
107  if ( !$factory->commitAndWaitForReplication(
108  __METHOD__, $ticket, [ 'timeout' => 3 ]
109  ) ) {
110  // Another job will continue anyway
111  break;
112  }
113  }
114  } while ( $rcIds );
115 
116  $dbw->unlock( $lockKey, __METHOD__ );
117  }
118 
119  protected function updateActiveUsers() {
120  $activeUserDays = MediaWikiServices::getInstance()->getMainConfig()->get( 'ActiveUserDays' );
121 
122  // Users that made edits at least this many days ago are "active"
123  $days = $activeUserDays;
124  // Pull in the full window of active users in this update
125  $window = $activeUserDays * 86400;
126 
127  $dbw = wfGetDB( DB_PRIMARY );
128  $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
129  $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
130 
131  $lockKey = $dbw->getDomainID() . '-activeusers';
132  if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
133  // Exclusive update (avoids duplicate entries)… it's usually fine to just
134  // drop out here, if the Job is already running.
135  return;
136  }
137 
138  // Long-running queries expected
139  $dbw->setSessionOptions( [ 'connTimeout' => 900 ] );
140 
141  $nowUnix = time();
142  // Get the last-updated timestamp for the cache
143  $cTime = $dbw->selectField( 'querycache_info',
144  'qci_timestamp',
145  [ 'qci_type' => 'activeusers' ],
146  __METHOD__
147  );
148  $cTimeUnix = $cTime ? (int)wfTimestamp( TS_UNIX, $cTime ) : 1;
149 
150  // Pick the date range to fetch from. This is normally from the last
151  // update to till the present time, but has a limited window.
152  // If the window is limited, multiple runs are need to fully populate it.
153  $sTimestamp = max( $cTimeUnix, $nowUnix - $days * 86400 );
154  $eTimestamp = min( $sTimestamp + $window, $nowUnix );
155 
156  // Get all the users active since the last update
157  $res = $dbw->select(
158  [ 'recentchanges', 'actor' ],
159  [
160  'actor_name',
161  'lastedittime' => 'MAX(rc_timestamp)'
162  ],
163  [
164  'actor_user IS NOT NULL', // actual accounts
165  'rc_type != ' . $dbw->addQuotes( RC_EXTERNAL ), // no wikidata
166  'rc_log_type IS NULL OR rc_log_type != ' . $dbw->addQuotes( 'newusers' ),
167  'rc_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $sTimestamp ) ),
168  'rc_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $eTimestamp ) )
169  ],
170  __METHOD__,
171  [
172  'GROUP BY' => 'actor_name',
173  'ORDER BY' => 'NULL' // avoid filesort
174  ],
175  [
176  'actor' => [ 'JOIN', 'actor_id=rc_actor' ]
177  ]
178  );
179  $names = [];
180  foreach ( $res as $row ) {
181  $names[$row->actor_name] = $row->lastedittime;
182  }
183 
184  // Find which of the recently active users are already accounted for
185  if ( count( $names ) ) {
186  $res = $dbw->select( 'querycachetwo',
187  [ 'user_name' => 'qcc_title' ],
188  [
189  'qcc_type' => 'activeusers',
190  'qcc_namespace' => NS_USER,
191  'qcc_title' => array_map( 'strval', array_keys( $names ) ),
192  'qcc_value >= ' . $dbw->addQuotes( $nowUnix - $days * 86400 ), // TS_UNIX
193  ],
194  __METHOD__
195  );
196  // Note: In order for this to be actually consistent, we would need
197  // to update these rows with the new lastedittime.
198  foreach ( $res as $row ) {
199  unset( $names[$row->user_name] );
200  }
201  }
202 
203  // Insert the users that need to be added to the list
204  if ( count( $names ) ) {
205  $newRows = [];
206  foreach ( $names as $name => $lastEditTime ) {
207  $newRows[] = [
208  'qcc_type' => 'activeusers',
209  'qcc_namespace' => NS_USER,
210  'qcc_title' => $name,
211  'qcc_value' => (int)wfTimestamp( TS_UNIX, $lastEditTime ),
212  'qcc_namespacetwo' => 0, // unused
213  'qcc_titletwo' => '' // unused
214  ];
215  }
216  foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
217  $dbw->insert( 'querycachetwo', $rowBatch, __METHOD__ );
218  $factory->commitAndWaitForReplication( __METHOD__, $ticket );
219  }
220  }
221 
222  // If a transaction was already started, it might have an old
223  // snapshot, so kludge the timestamp range back as needed.
224  $asOfTimestamp = min( $eTimestamp, (int)$dbw->trxTimestamp() );
225 
226  // Touch the data freshness timestamp
227  $dbw->replace(
228  'querycache_info',
229  'qci_type',
230  [ 'qci_type' => 'activeusers',
231  'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ) ], // not always $now
232  __METHOD__
233  );
234 
235  // Rotate out users that have not edited in too long (according to old data set)
236  $dbw->delete( 'querycachetwo',
237  [
238  'qcc_type' => 'activeusers',
239  'qcc_value < ' . $dbw->addQuotes( $nowUnix - $days * 86400 ) // TS_UNIX
240  ],
241  __METHOD__
242  );
243 
244  $dbw->unlock( $lockKey, __METHOD__ );
245  }
246 }
RecentChange\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object.
Definition: RecentChange.php:258
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
RecentChangesUpdateJob
Job for pruning recent changes.
Definition: RecentChangesUpdateJob.php:30
RecentChangesUpdateJob\updateActiveUsers
updateActiveUsers()
Definition: RecentChangesUpdateJob.php:119
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1649
RecentChangesUpdateJob\run
run()
Run the job.
Definition: RecentChangesUpdateJob.php:61
Job\$title
Title $title
Definition: Job.php:49
RecentChangesUpdateJob\__construct
__construct(Title $title, array $params)
Definition: RecentChangesUpdateJob.php:31
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:131
RecentChangesUpdateJob\purgeExpiredRows
purgeExpiredRows()
Definition: RecentChangesUpdateJob.php:74
$res
$res
Definition: testCompression.php:57
Job\$params
array $params
Array of job parameters.
Definition: Job.php:43
RecentChangesUpdateJob\newCacheUpdateJob
static newCacheUpdateJob()
Definition: RecentChangesUpdateJob.php:55
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:38
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2186
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:118
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
NS_USER
const NS_USER
Definition: Defines.php:66
Title
Represents a title within MediaWiki.
Definition: Title.php:47
RecentChangesUpdateJob\newPurgeJob
static newPurgeJob()
Definition: RecentChangesUpdateJob.php:45