35 parent::__construct(
'recentChangesUpdate',
$title,
$params );
37 if ( !isset(
$params[
'type'] ) ) {
38 throw new Exception(
"Missing 'type' parameter." );
41 $this->executionFlags |= self::JOB_NO_EXPLICIT_TRX_ROUND;
42 $this->removeDuplicates =
true;
50 SpecialPage::getTitleFor(
'Recentchanges' ), [
'type' =>
'purge' ]
60 SpecialPage::getTitleFor(
'Recentchanges' ), [
'type' =>
'cacheUpdate' ]
64 public function run() {
65 if ( $this->params[
'type'] ===
'purge' ) {
67 } elseif ( $this->params[
'type'] ===
'cacheUpdate' ) {
70 throw new InvalidArgumentException(
71 "Invalid 'type' parameter '{$this->params['type']}'." );
78 $services = MediaWikiServices::getInstance();
79 $rcMaxAge = $services->getMainConfig()->get(
80 MainConfigNames::RCMaxAge );
81 $updateRowsPerQuery = $services->getMainConfig()->get(
82 MainConfigNames::UpdateRowsPerQuery );
84 $lockKey = $dbw->getDomainID() .
':recentchanges-prune';
85 if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
90 $factory = $services->getDBLoadBalancerFactory();
91 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
92 $hookRunner =
new HookRunner( $services->getHookContainer() );
93 $cutoff = $dbw->timestamp( time() - $rcMaxAge );
101 [
'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ],
103 [
'LIMIT' => $updateRowsPerQuery ],
106 foreach ( $res as $row ) {
107 $rcIds[] = $row->rc_id;
111 $dbw->newDeleteQueryBuilder()
112 ->deleteFrom(
'recentchanges' )
113 ->where( [
'rc_id' => $rcIds ] )
114 ->caller( __METHOD__ )->execute();
115 $hookRunner->onRecentChangesPurgeRows( $rows );
117 if ( !$factory->commitAndWaitForReplication(
118 __METHOD__, $ticket, [
'timeout' => 3 ]
126 $dbw->unlock( $lockKey, __METHOD__ );
130 $activeUserDays = MediaWikiServices::getInstance()->getMainConfig()->get(
131 MainConfigNames::ActiveUserDays );
134 $days = $activeUserDays;
136 $window = $activeUserDays * 86400;
138 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
139 $dbw = $factory->getPrimaryDatabase();
140 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
142 $lockKey = $dbw->getDomainID() .
'-activeusers';
143 if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
150 $dbw->setSessionOptions( [
'connTimeout' => 900 ] );
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;
164 $sTimestamp = max( $cTimeUnix, $nowUnix - $days * 86400 );
165 $eTimestamp = min( $sTimestamp + $window, $nowUnix );
168 $res = $dbw->newSelectQueryBuilder()
169 ->select( [
'actor_name',
'lastedittime' =>
'MAX(rc_timestamp)' ] )
170 ->from(
'recentchanges' )
171 ->join(
'actor',
null,
'actor_id=rc_actor' )
173 'actor_user IS NOT NULL',
175 'rc_log_type IS NULL OR rc_log_type != ' . $dbw->addQuotes(
'newusers' ),
176 $dbw->buildComparison(
'>=', [
'rc_timestamp' => $dbw->timestamp( $sTimestamp ) ] ),
177 $dbw->buildComparison(
'<=', [
'rc_timestamp' => $dbw->timestamp( $eTimestamp ) ] ),
179 ->groupBy(
'actor_name' )
181 ->caller( __METHOD__ )->fetchResultSet();
184 foreach ( $res as $row ) {
185 $names[$row->actor_name] = $row->lastedittime;
189 if ( count( $names ) ) {
190 $res = $dbw->newSelectQueryBuilder()
191 ->select( [
'user_name' =>
'qcc_title' ] )
192 ->from(
'querycachetwo' )
194 'qcc_type' =>
'activeusers',
196 'qcc_title' => array_map(
'strval', array_keys( $names ) ),
197 $dbw->buildComparison(
'>=', [
'qcc_value' => $nowUnix - $days * 86400 ] ),
199 ->caller( __METHOD__ )->fetchResultSet();
202 foreach ( $res as $row ) {
203 unset( $names[$row->user_name] );
208 if ( count( $names ) ) {
210 foreach ( $names as $name => $lastEditTime ) {
212 'qcc_type' =>
'activeusers',
214 'qcc_title' => $name,
215 'qcc_value' => (int)
wfTimestamp( TS_UNIX, $lastEditTime ),
216 'qcc_namespacetwo' => 0,
220 foreach ( array_chunk( $newRows, 500 ) as $rowBatch ) {
221 $dbw->insert(
'querycachetwo', $rowBatch, __METHOD__ );
222 $factory->commitAndWaitForReplication( __METHOD__, $ticket );
228 $asOfTimestamp = min( $eTimestamp, (
int)$dbw->trxTimestamp() );
231 $dbw->newReplaceQueryBuilder()
232 ->replaceInto(
'querycache_info' )
234 'qci_type' =>
'activeusers',
235 'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ) ,
237 ->uniqueIndexFields( [
'qci_type' ] )
238 ->caller( __METHOD__ )->execute();
241 $dbw->newDeleteQueryBuilder()
242 ->deleteFrom(
'querycachetwo' )
244 'qcc_type' =>
'activeusers',
245 $dbw->buildComparison(
'<', [
'qcc_value' => $nowUnix - $days * 86400 ] )
247 ->caller( __METHOD__ )->execute();
249 if ( !MediaWikiServices::getInstance()->getMainConfig()->
get( MainConfigNames::MiserMode ) ) {
253 $dbw->unlock( $lockKey, __METHOD__ );
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Class to both describe a background job and handle jobs.
array $params
Array of job parameters.
A class containing constants representing the names of configuration variables.
Parent class for all special pages.
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object.
Puurge expired rows from the recentchanges table.
static newCacheUpdateJob()
__construct(Title $title, array $params)
static cacheUpdate(IDatabase $dbw)