MediaWiki master
RecentChangesUpdateJob.php
Go to the documentation of this file.
1<?php
27use Wikimedia\Timestamp\ConvertibleTimestamp;
28
37 public function __construct( Title $title, array $params ) {
38 parent::__construct( 'recentChangesUpdate', $title, $params );
39
40 if ( !isset( $params['type'] ) ) {
41 throw new InvalidArgumentException( "Missing 'type' parameter." );
42 }
43
44 $this->executionFlags |= self::JOB_NO_EXPLICIT_TRX_ROUND;
45 $this->removeDuplicates = true;
46 }
47
51 final public static function newPurgeJob() {
52 return new self(
53 SpecialPage::getTitleFor( 'Recentchanges' ), [ 'type' => 'purge' ]
54 );
55 }
56
61 final public static function newCacheUpdateJob() {
62 return new self(
63 SpecialPage::getTitleFor( 'Recentchanges' ), [ 'type' => 'cacheUpdate' ]
64 );
65 }
66
67 public function run() {
68 if ( $this->params['type'] === 'purge' ) {
69 $this->purgeExpiredRows();
70 } elseif ( $this->params['type'] === 'cacheUpdate' ) {
71 $this->updateActiveUsers();
72 } else {
73 throw new InvalidArgumentException(
74 "Invalid 'type' parameter '{$this->params['type']}'." );
75 }
76
77 return true;
78 }
79
80 protected function purgeExpiredRows() {
81 $services = MediaWikiServices::getInstance();
82 $rcMaxAge = $services->getMainConfig()->get(
83 MainConfigNames::RCMaxAge );
84 $updateRowsPerQuery = $services->getMainConfig()->get(
85 MainConfigNames::UpdateRowsPerQuery );
86 $dbProvider = $services->getConnectionProvider();
87 $dbw = $dbProvider->getPrimaryDatabase();
88 $lockKey = $dbw->getDomainID() . ':recentchanges-prune';
89 if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
90 // already in progress
91 return;
92 }
93 $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
94 $hookRunner = new HookRunner( $services->getHookContainer() );
95 $cutoff = $dbw->timestamp( ConvertibleTimestamp::time() - $rcMaxAge );
96 $rcQuery = RecentChange::getQueryInfo();
97 do {
98 $rcIds = [];
99 $rows = [];
100 $res = $dbw->newSelectQueryBuilder()
101 ->queryInfo( $rcQuery )
102 ->where( $dbw->expr( 'rc_timestamp', '<', $cutoff ) )
103 ->limit( $updateRowsPerQuery )
104 ->caller( __METHOD__ )
105 ->fetchResultSet();
106 foreach ( $res as $row ) {
107 $rcIds[] = $row->rc_id;
108 $rows[] = $row;
109 }
110 if ( $rcIds ) {
111 $dbw->newDeleteQueryBuilder()
112 ->deleteFrom( 'recentchanges' )
113 ->where( [ 'rc_id' => $rcIds ] )
114 ->caller( __METHOD__ )->execute();
115 $hookRunner->onRecentChangesPurgeRows( $rows );
116 // There might be more, so try waiting for replica DBs
117 if ( !$dbProvider->commitAndWaitForReplication(
118 __METHOD__, $ticket, [ 'timeout' => 3 ]
119 ) ) {
120 // Another job will continue anyway
121 break;
122 }
123 }
124 } while ( $rcIds );
125
126 $dbw->unlock( $lockKey, __METHOD__ );
127 }
128
129 protected function updateActiveUsers() {
130 $activeUserDays = MediaWikiServices::getInstance()->getMainConfig()->get(
131 MainConfigNames::ActiveUserDays );
132
133 // Users that made edits at least this many days ago are "active"
134 $days = $activeUserDays;
135 // Pull in the full window of active users in this update
136 $window = $activeUserDays * 86400;
137
138 $dbProvider = MediaWikiServices::getInstance()->getConnectionProvider();
139 $dbw = $dbProvider->getPrimaryDatabase();
140 $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
141
142 $lockKey = $dbw->getDomainID() . '-activeusers';
143 if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
144 // Exclusive update (avoids duplicate entries)… it's usually fine to just
145 // drop out here, if the Job is already running.
146 return;
147 }
148
149 // Long-running queries expected
150 $dbw->setSessionOptions( [ 'connTimeout' => 900 ] );
151
152 $nowUnix = time();
153 // Get the last-updated timestamp for the cache
154 $cTime = $dbw->newSelectQueryBuilder()
155 ->select( 'qci_timestamp' )
156 ->from( 'querycache_info' )
157 ->where( [ 'qci_type' => 'activeusers' ] )
158 ->caller( __METHOD__ )->fetchField();
159 $cTimeUnix = $cTime ? (int)wfTimestamp( TS_UNIX, $cTime ) : 1;
160
161 // Pick the date range to fetch from. This is normally from the last
162 // update to till the present time, but has a limited window.
163 // If the window is limited, multiple runs are need to fully populate it.
164 $sTimestamp = max( $cTimeUnix, $nowUnix - $days * 86400 );
165 $eTimestamp = min( $sTimestamp + $window, $nowUnix );
166
167 // Get all the users active since the last update
168 $res = $dbw->newSelectQueryBuilder()
169 ->select( [ 'actor_name', 'lastedittime' => 'MAX(rc_timestamp)' ] )
170 ->from( 'recentchanges' )
171 ->join( 'actor', null, 'actor_id=rc_actor' )
172 ->where( [
173 $dbw->expr( 'actor_user', '!=', null ), // actual accounts
174 $dbw->expr( 'rc_type', '!=', RC_EXTERNAL ), // no wikidata
175 $dbw->expr( 'rc_log_type', '=', null )->or( 'rc_log_type', '!=', 'newusers' ),
176 $dbw->expr( 'rc_timestamp', '>=', $dbw->timestamp( $sTimestamp ) ),
177 $dbw->expr( 'rc_timestamp', '<=', $dbw->timestamp( $eTimestamp ) ),
178 ] )
179 ->groupBy( 'actor_name' )
180 ->orderBy( 'NULL' ) // avoid filesort
181 ->caller( __METHOD__ )->fetchResultSet();
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->newSelectQueryBuilder()
191 ->select( [ 'user_name' => 'qcc_title' ] )
192 ->from( 'querycachetwo' )
193 ->where( [
194 'qcc_type' => 'activeusers',
195 'qcc_namespace' => NS_USER,
196 'qcc_title' => array_map( 'strval', array_keys( $names ) ),
197 $dbw->expr( 'qcc_value', '>=', $nowUnix - $days * 86400 ),
198 ] )
199 ->caller( __METHOD__ )->fetchResultSet();
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->newInsertQueryBuilder()
222 ->insertInto( 'querycachetwo' )
223 ->rows( $rowBatch )
224 ->caller( __METHOD__ )->execute();
225 $dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
226 }
227 }
228
229 // If a transaction was already started, it might have an old
230 // snapshot, so kludge the timestamp range back as needed.
231 $asOfTimestamp = min( $eTimestamp, (int)$dbw->trxTimestamp() );
232
233 // Touch the data freshness timestamp
234 $dbw->newReplaceQueryBuilder()
235 ->replaceInto( 'querycache_info' )
236 ->row( [
237 'qci_type' => 'activeusers',
238 'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ), // not always $now
239 ] )
240 ->uniqueIndexFields( [ 'qci_type' ] )
241 ->caller( __METHOD__ )->execute();
242
243 // Rotate out users that have not edited in too long (according to old data set)
244 $dbw->newDeleteQueryBuilder()
245 ->deleteFrom( 'querycachetwo' )
246 ->where( [
247 'qcc_type' => 'activeusers',
248 $dbw->expr( 'qcc_value', '<', $nowUnix - $days * 86400 ) // TS_UNIX
249 ] )
250 ->caller( __METHOD__ )->execute();
251
252 if ( !MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MiserMode ) ) {
253 SiteStatsUpdate::cacheUpdate( $dbw );
254 }
255
256 $dbw->unlock( $lockKey, __METHOD__ );
257 }
258}
const NS_USER
Definition Defines.php:67
const RC_EXTERNAL
Definition Defines.php:120
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
Describe and execute a background job.
Definition Job.php:41
Class for handling updates to the site_stats table.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Parent class for all special pages.
Represents a title within MediaWiki.
Definition Title.php:78
Purge expired rows from the recentchanges table.
__construct(Title $title, array $params)