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 ) ) {
91 $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
92 $hookRunner =
new HookRunner( $services->getHookContainer() );
93 $cutoff = $dbw->timestamp( time() - $rcMaxAge );
94 $rcQuery = RecentChange::getQueryInfo();
101 [ $dbw->expr(
'rc_timestamp',
'<', $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 ( !$dbProvider->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 $dbProvider = MediaWikiServices::getInstance()->getConnectionProvider();
139 $dbw = $dbProvider->getPrimaryDatabase();
140 $ticket = $dbProvider->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 $dbw->expr(
'actor_user',
'!=',
null ),
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 ) ),
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->expr(
'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->newInsertQueryBuilder()
222 ->insertInto(
'querycachetwo' )
224 ->caller( __METHOD__ )->execute();
225 $dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
231 $asOfTimestamp = min( $eTimestamp, (
int)$dbw->trxTimestamp() );
234 $dbw->newReplaceQueryBuilder()
235 ->replaceInto(
'querycache_info' )
237 'qci_type' =>
'activeusers',
238 'qci_timestamp' => $dbw->timestamp( $asOfTimestamp ),
240 ->uniqueIndexFields( [
'qci_type' ] )
241 ->caller( __METHOD__ )->execute();
244 $dbw->newDeleteQueryBuilder()
245 ->deleteFrom(
'querycachetwo' )
247 'qcc_type' =>
'activeusers',
248 $dbw->expr(
'qcc_value',
'<', $nowUnix - $days * 86400 )
250 ->caller( __METHOD__ )->execute();
252 if ( !MediaWikiServices::getInstance()->getMainConfig()->
get( MainConfigNames::MiserMode ) ) {
253 SiteStatsUpdate::cacheUpdate( $dbw );
256 $dbw->unlock( $lockKey, __METHOD__ );