MediaWiki REL1_28
RecentChangesUpdateJob.php
Go to the documentation of this file.
1<?php
23
32 parent::__construct( 'recentChangesUpdate', $title, $params );
33
34 if ( !isset( $params['type'] ) ) {
35 throw new Exception( "Missing 'type' parameter." );
36 }
37
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 $lockKey = wfWikiID() . ':recentchanges-prune';
77
78 $dbw = wfGetDB( DB_MASTER );
79 if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
80 || !$dbw->lock( $lockKey, __METHOD__, 1 )
81 ) {
82 return; // already in progress
83 }
84
85 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
86 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
87 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
88 do {
89 $rcIds = $dbw->selectFieldValues( 'recentchanges',
90 'rc_id',
91 [ 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ],
92 __METHOD__,
93 [ 'LIMIT' => $wgUpdateRowsPerQuery ]
94 );
95 if ( $rcIds ) {
96 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIds ], __METHOD__ );
97 // There might be more, so try waiting for replica DBs
98 try {
99 $factory->commitAndWaitForReplication(
100 __METHOD__, $ticket, [ 'timeout' => 3 ]
101 );
102 } catch ( DBReplicationWaitError $e ) {
103 // Another job will continue anyway
104 break;
105 }
106 }
107 } while ( $rcIds );
108
109 $dbw->unlock( $lockKey, __METHOD__ );
110 }
111
112 protected function updateActiveUsers() {
114
115 // Users that made edits at least this many days ago are "active"
116 $days = $wgActiveUserDays;
117 // Pull in the full window of active users in this update
118 $window = $wgActiveUserDays * 86400;
119
120 $dbw = wfGetDB( DB_MASTER );
121 // JobRunner uses DBO_TRX, but doesn't call begin/commit itself;
122 // onTransactionIdle() will run immediately since there is no trx.
123 $dbw->onTransactionIdle(
124 function () use ( $dbw, $days, $window ) {
125 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
126 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
127 // Avoid disconnect/ping() cycle that makes locks fall off
128 $dbw->setSessionOptions( [ 'connTimeout' => 900 ] );
129
130 $lockKey = wfWikiID() . '-activeusers';
131 if ( !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
132 return; // exclusive update (avoids duplicate entries)
133 }
134
135 $nowUnix = time();
136 // Get the last-updated timestamp for the cache
137 $cTime = $dbw->selectField( 'querycache_info',
138 'qci_timestamp',
139 [ 'qci_type' => 'activeusers' ]
140 );
141 $cTimeUnix = $cTime ? wfTimestamp( TS_UNIX, $cTime ) : 1;
142
143 // Pick the date range to fetch from. This is normally from the last
144 // update to till the present time, but has a limited window for sanity.
145 // If the window is limited, multiple runs are need to fully populate it.
146 $sTimestamp = max( $cTimeUnix, $nowUnix - $days * 86400 );
147 $eTimestamp = min( $sTimestamp + $window, $nowUnix );
148
149 // Get all the users active since the last update
150 $res = $dbw->select(
151 [ 'recentchanges' ],
152 [ 'rc_user_text', 'lastedittime' => 'MAX(rc_timestamp)' ],
153 [
154 'rc_user > 0', // actual accounts
155 'rc_type != ' . $dbw->addQuotes( RC_EXTERNAL ), // no wikidata
156 'rc_log_type IS NULL OR rc_log_type != ' . $dbw->addQuotes( 'newusers' ),
157 'rc_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $sTimestamp ) ),
158 'rc_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $eTimestamp ) )
159 ],
160 __METHOD__,
161 [
162 'GROUP BY' => [ 'rc_user_text' ],
163 'ORDER BY' => 'NULL' // avoid filesort
164 ]
165 );
166 $names = [];
167 foreach ( $res as $row ) {
168 $names[$row->rc_user_text] = $row->lastedittime;
169 }
170
171 // Rotate out users that have not edited in too long (according to old data set)
172 $dbw->delete( 'querycachetwo',
173 [
174 'qcc_type' => 'activeusers',
175 'qcc_value < ' . $dbw->addQuotes( $nowUnix - $days * 86400 ) // TS_UNIX
176 ],
177 __METHOD__
178 );
179
180 // Find which of the recently active users are already accounted for
181 if ( count( $names ) ) {
182 $res = $dbw->select( 'querycachetwo',
183 [ 'user_name' => 'qcc_title' ],
184 [
185 'qcc_type' => 'activeusers',
186 'qcc_namespace' => NS_USER,
187 'qcc_title' => array_keys( $names ) ],
188 __METHOD__
189 );
190 foreach ( $res as $row ) {
191 unset( $names[$row->user_name] );
192 }
193 }
194
195 // Insert the users that need to be added to the list
196 if ( count( $names ) ) {
197 $newRows = [];
198 foreach ( $names as $name => $lastEditTime ) {
199 $newRows[] = [
200 'qcc_type' => 'activeusers',
201 'qcc_namespace' => NS_USER,
202 'qcc_title' => $name,
203 'qcc_value' => wfTimestamp( TS_UNIX, $lastEditTime ),
204 'qcc_namespacetwo' => 0, // unused
205 'qcc_titletwo' => '' // unused
206 ];
207 }
208 foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
209 $dbw->insert( 'querycachetwo', $rowBatch, __METHOD__ );
210 $factory->commitAndWaitForReplication( __METHOD__, $ticket );
211 }
212 }
213
214 // If a transaction was already started, it might have an old
215 // snapshot, so kludge the timestamp range back as needed.
216 $asOfTimestamp = min( $eTimestamp, (int)$dbw->trxTimestamp() );
217
218 // Touch the data freshness timestamp
219 $dbw->replace( 'querycache_info',
220 [ 'qci_type' ],
221 [ 'qci_type' => 'activeusers',
222 'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ) ], // not always $now
223 __METHOD__
224 );
225
226 $dbw->unlock( $lockKey, __METHOD__ );
227 },
228 __METHOD__
229 );
230 }
231}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$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.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Exception class for replica DB wait timeouts.
Class to both describe a background job and handle jobs.
Definition Job.php:31
Title $title
Definition Job.php:42
array $params
Array of job parameters.
Definition Job.php:36
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:36
$res
Definition database.txt:21
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_USER
Definition Defines.php:58
const RC_EXTERNAL
Definition Defines.php:139
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
returning false will NOT prevent logging $e
Definition hooks.txt:2110
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
const DB_MASTER
Definition defines.php:23
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition defines.php:6