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
;
24 * Job for pruning recent changes
29 class RecentChangesUpdateJob
extends Job
{
30 function __construct( Title
$title, array $params ) {
31 parent
::__construct( 'recentChangesUpdate', $title, $params );
33 if ( !isset( $params['type'] ) ) {
34 throw new Exception( "Missing 'type' parameter." );
37 $this->executionFlags |
= self
::JOB_NO_EXPLICIT_TRX_ROUND
;
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 $dbw = wfGetDB( DB_MASTER
);
77 $lockKey = $dbw->getDomainID() . ':recentchanges-prune';
78 if ( !$dbw->lock( $lockKey, __METHOD__
, 0 ) ) {
79 // already in progress
83 $factory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
84 $ticket = $factory->getEmptyTransactionTicket( __METHOD__
);
85 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
86 $rcQuery = RecentChange
::getQueryInfo();
93 [ 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ],
95 [ 'LIMIT' => $wgUpdateRowsPerQuery ],
98 foreach ( $res as $row ) {
99 $rcIds[] = $row->rc_id
;
103 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIds ], __METHOD__
);
104 Hooks
::run( 'RecentChangesPurgeRows', [ $rows ] );
105 // There might be more, so try waiting for replica DBs
106 if ( !$factory->commitAndWaitForReplication(
107 __METHOD__
, $ticket, [ 'timeout' => 3 ]
109 // Another job will continue anyway
115 $dbw->unlock( $lockKey, __METHOD__
);
118 protected function updateActiveUsers() {
119 global $wgActiveUserDays;
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;
126 $dbw = wfGetDB( DB_MASTER
);
127 $factory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
128 $ticket = $factory->getEmptyTransactionTicket( __METHOD__
);
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.
137 // Long-running queries expected
138 $dbw->setSessionOptions( [ 'connTimeout' => 900 ] );
141 // Get the last-updated timestamp for the cache
142 $cTime = $dbw->selectField( 'querycache_info',
144 [ 'qci_type' => 'activeusers' ]
146 $cTimeUnix = $cTime ?
wfTimestamp( TS_UNIX
, $cTime ) : 1;
148 // Pick the date range to fetch from. This is normally from the last
149 // update to till the present time, but has a limited window for sanity.
150 // If the window is limited, multiple runs are need to fully populate it.
151 $sTimestamp = max( $cTimeUnix, $nowUnix - $days * 86400 );
152 $eTimestamp = min( $sTimestamp +
$window, $nowUnix );
154 // Get all the users active since the last update
155 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rc_user' );
157 [ 'recentchanges' ] +
$actorQuery['tables'],
159 'rc_user_text' => $actorQuery['fields']['rc_user_text'],
160 'lastedittime' => 'MAX(rc_timestamp)'
163 $actorQuery['fields']['rc_user'] . ' > 0', // 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 ) )
171 'GROUP BY' => [ 'rc_user_text' ],
172 'ORDER BY' => 'NULL' // avoid filesort
177 foreach ( $res as $row ) {
178 $names[$row->rc_user_text
] = $row->lastedittime
;
181 // Find which of the recently active users are already accounted for
182 if ( count( $names ) ) {
183 $res = $dbw->select( 'querycachetwo',
184 [ 'user_name' => 'qcc_title' ],
186 'qcc_type' => 'activeusers',
187 'qcc_namespace' => NS_USER
,
188 'qcc_title' => array_keys( $names ),
189 'qcc_value >= ' . $dbw->addQuotes( $nowUnix - $days * 86400 ), // TS_UNIX
193 // Note: In order for this to be actually consistent, we would need
194 // to update these rows with the new lastedittime.
195 foreach ( $res as $row ) {
196 unset( $names[$row->user_name
] );
200 // Insert the users that need to be added to the list
201 if ( count( $names ) ) {
203 foreach ( $names as $name => $lastEditTime ) {
205 'qcc_type' => 'activeusers',
206 'qcc_namespace' => NS_USER
,
207 'qcc_title' => $name,
208 'qcc_value' => wfTimestamp( TS_UNIX
, $lastEditTime ),
209 'qcc_namespacetwo' => 0, // unused
210 'qcc_titletwo' => '' // unused
213 foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
214 $dbw->insert( 'querycachetwo', $rowBatch, __METHOD__
);
215 $factory->commitAndWaitForReplication( __METHOD__
, $ticket );
219 // If a transaction was already started, it might have an old
220 // snapshot, so kludge the timestamp range back as needed.
221 $asOfTimestamp = min( $eTimestamp, (int)$dbw->trxTimestamp() );
223 // Touch the data freshness timestamp
224 $dbw->replace( 'querycache_info',
226 [ 'qci_type' => 'activeusers',
227 'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ) ], // not always $now
231 $dbw->unlock( $lockKey, __METHOD__
);
233 // Rotate out users that have not edited in too long (according to old data set)
234 $dbw->delete( 'querycachetwo',
236 'qcc_type' => 'activeusers',
237 'qcc_value < ' . $dbw->addQuotes( $nowUnix - $days * 86400 ) // TS_UNIX