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