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