MediaWiki 1.42.0
RecentChangesUpdateJob.php
Go to the documentation of this file.
1<?php
27
35 public function __construct( Title $title, array $params ) {
36 parent::__construct( 'recentChangesUpdate', $title, $params );
37
38 if ( !isset( $params['type'] ) ) {
39 throw new InvalidArgumentException( "Missing 'type' parameter." );
40 }
41
42 $this->executionFlags |= self::JOB_NO_EXPLICIT_TRX_ROUND;
43 $this->removeDuplicates = true;
44 }
45
49 final public static function newPurgeJob() {
50 return new self(
51 SpecialPage::getTitleFor( 'Recentchanges' ), [ 'type' => 'purge' ]
52 );
53 }
54
59 final public static function newCacheUpdateJob() {
60 return new self(
61 SpecialPage::getTitleFor( 'Recentchanges' ), [ 'type' => 'cacheUpdate' ]
62 );
63 }
64
65 public function run() {
66 if ( $this->params['type'] === 'purge' ) {
67 $this->purgeExpiredRows();
68 } elseif ( $this->params['type'] === 'cacheUpdate' ) {
69 $this->updateActiveUsers();
70 } else {
71 throw new InvalidArgumentException(
72 "Invalid 'type' parameter '{$this->params['type']}'." );
73 }
74
75 return true;
76 }
77
78 protected function purgeExpiredRows() {
79 $services = MediaWikiServices::getInstance();
80 $rcMaxAge = $services->getMainConfig()->get(
81 MainConfigNames::RCMaxAge );
82 $updateRowsPerQuery = $services->getMainConfig()->get(
83 MainConfigNames::UpdateRowsPerQuery );
84 $dbProvider = $services->getConnectionProvider();
85 $dbw = $dbProvider->getPrimaryDatabase();
86 $lockKey = $dbw->getDomainID() . ':recentchanges-prune';
87 if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
88 // already in progress
89 return;
90 }
91 $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
92 $hookRunner = new HookRunner( $services->getHookContainer() );
93 $cutoff = $dbw->timestamp( time() - $rcMaxAge );
94 $rcQuery = RecentChange::getQueryInfo();
95 do {
96 $rcIds = [];
97 $rows = [];
98 $res = $dbw->select(
99 $rcQuery['tables'],
100 $rcQuery['fields'],
101 [ $dbw->expr( 'rc_timestamp', '<', $cutoff ) ],
102 __METHOD__,
103 [ 'LIMIT' => $updateRowsPerQuery ],
104 $rcQuery['joins']
105 );
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:66
const RC_EXTERNAL
Definition Defines.php:119
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:40
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
Puurge expired rows from the recentchanges table.
__construct(Title $title, array $params)