3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\MediaWikiServices
;
22 use Wikimedia\Rdbms\DBReplicationWaitError
;
25 * Job for pruning recent changes
30 class RecentChangesUpdateJob
extends Job
{
31 function __construct( Title
$title, array $params ) {
32 parent
::__construct( 'recentChangesUpdate', $title, $params );
34 if ( !isset( $params['type'] ) ) {
35 throw new Exception( "Missing 'type' parameter." );
38 $this->removeDuplicates
= true;
42 * @return RecentChangesUpdateJob
44 final public static function newPurgeJob() {
46 SpecialPage
::getTitleFor( 'Recentchanges' ), [ 'type' => 'purge' ]
51 * @return RecentChangesUpdateJob
54 final public static function newCacheUpdateJob() {
56 SpecialPage
::getTitleFor( 'Recentchanges' ), [ 'type' => 'cacheUpdate' ]
60 public function run() {
61 if ( $this->params
['type'] === 'purge' ) {
62 $this->purgeExpiredRows();
63 } elseif ( $this->params
['type'] === 'cacheUpdate' ) {
64 $this->updateActiveUsers();
66 throw new InvalidArgumentException(
67 "Invalid 'type' parameter '{$this->params['type']}'." );
73 protected function purgeExpiredRows() {
74 global $wgRCMaxAge, $wgUpdateRowsPerQuery;
76 $lockKey = wfWikiID() . ':recentchanges-prune';
78 $dbw = wfGetDB( DB_MASTER
);
79 if ( !$dbw->lock( $lockKey, __METHOD__
, 0 ) ) {
80 // already in progress
84 $factory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
85 $ticket = $factory->getEmptyTransactionTicket( __METHOD__
);
86 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
87 $rcQuery = RecentChange
::getQueryInfo();
94 [ 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ],
96 [ 'LIMIT' => $wgUpdateRowsPerQuery ],
99 foreach ( $res as $row ) {
100 $rcIds[] = $row->rc_id
;
104 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIds ], __METHOD__
);
105 Hooks
::run( 'RecentChangesPurgeRows', [ $rows ] );
106 // There might be more, so try waiting for replica DBs
108 $factory->commitAndWaitForReplication(
109 __METHOD__
, $ticket, [ 'timeout' => 3 ]
111 } catch ( DBReplicationWaitError
$e ) {
112 // Another job will continue anyway
118 $dbw->unlock( $lockKey, __METHOD__
);
121 protected function updateActiveUsers() {
122 global $wgActiveUserDays;
124 // Users that made edits at least this many days ago are "active"
125 $days = $wgActiveUserDays;
126 // Pull in the full window of active users in this update
127 $window = $wgActiveUserDays * 86400;
129 $dbw = wfGetDB( DB_MASTER
);
130 // JobRunner uses DBO_TRX, but doesn't call begin/commit itself;
131 // onTransactionIdle() will run immediately since there is no trx.
132 $dbw->onTransactionIdle(
133 function () use ( $dbw, $days, $window ) {
134 $factory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
135 $ticket = $factory->getEmptyTransactionTicket( __METHOD__
);
136 // Avoid disconnect/ping() cycle that makes locks fall off
137 $dbw->setSessionOptions( [ 'connTimeout' => 900 ] );
139 $lockKey = wfWikiID() . '-activeusers';
140 if ( !$dbw->lock( $lockKey, __METHOD__
, 0 ) ) {
141 // Exclusive update (avoids duplicate entries)… it's usually fine to just drop out here,
142 // if the Job is already running.
147 // Get the last-updated timestamp for the cache
148 $cTime = $dbw->selectField( 'querycache_info',
150 [ 'qci_type' => 'activeusers' ]
152 $cTimeUnix = $cTime ?
wfTimestamp( TS_UNIX
, $cTime ) : 1;
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 for sanity.
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 );
160 // Get all the users active since the last update
161 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rc_user' );
163 [ 'recentchanges' ] +
$actorQuery['tables'],
165 'rc_user_text' => $actorQuery['fields']['rc_user_text'],
166 'lastedittime' => 'MAX(rc_timestamp)'
169 $actorQuery['fields']['rc_user'] . ' > 0', // actual accounts
170 'rc_type != ' . $dbw->addQuotes( RC_EXTERNAL
), // no wikidata
171 'rc_log_type IS NULL OR rc_log_type != ' . $dbw->addQuotes( 'newusers' ),
172 'rc_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $sTimestamp ) ),
173 'rc_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $eTimestamp ) )
177 'GROUP BY' => [ 'rc_user_text' ],
178 'ORDER BY' => 'NULL' // avoid filesort
183 foreach ( $res as $row ) {
184 $names[$row->rc_user_text
] = $row->lastedittime
;
187 // Find which of the recently active users are already accounted for
188 if ( count( $names ) ) {
189 $res = $dbw->select( 'querycachetwo',
190 [ 'user_name' => 'qcc_title' ],
192 'qcc_type' => 'activeusers',
193 'qcc_namespace' => NS_USER
,
194 'qcc_title' => array_keys( $names ),
195 'qcc_value >= ' . $dbw->addQuotes( $nowUnix - $days * 86400 ), // TS_UNIX
199 // Note: In order for this to be actually consistent, we would need
200 // to update these rows with the new lastedittime.
201 foreach ( $res as $row ) {
202 unset( $names[$row->user_name
] );
206 // Insert the users that need to be added to the list
207 if ( count( $names ) ) {
209 foreach ( $names as $name => $lastEditTime ) {
211 'qcc_type' => 'activeusers',
212 'qcc_namespace' => NS_USER
,
213 'qcc_title' => $name,
214 'qcc_value' => wfTimestamp( TS_UNIX
, $lastEditTime ),
215 'qcc_namespacetwo' => 0, // unused
216 'qcc_titletwo' => '' // unused
219 foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
220 $dbw->insert( 'querycachetwo', $rowBatch, __METHOD__
);
221 $factory->commitAndWaitForReplication( __METHOD__
, $ticket );
225 // If a transaction was already started, it might have an old
226 // snapshot, so kludge the timestamp range back as needed.
227 $asOfTimestamp = min( $eTimestamp, (int)$dbw->trxTimestamp() );
229 // Touch the data freshness timestamp
230 $dbw->replace( 'querycache_info',
232 [ 'qci_type' => 'activeusers',
233 'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ) ], // not always $now
237 $dbw->unlock( $lockKey, __METHOD__
);
239 // Rotate out users that have not edited in too long (according to old data set)
240 $dbw->delete( 'querycachetwo',
242 'qcc_type' => 'activeusers',
243 'qcc_value < ' . $dbw->addQuotes( $nowUnix - $days * 86400 ) // TS_UNIX