26 require_once __DIR__ .
'/Maintenance.php';
40 parent::__construct();
45 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
51 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
63 $this->
error(
"Both 'from' and 'to' must be given, or neither", 1 );
74 $this->
output(
"Done.\n" );
88 $days = $sec / 24 / 3600;
89 $this->
output(
"Rebuilding range of $sec seconds ($days days)\n" );
93 $days = $wgRCMaxAge / 24 / 3600;
94 $this->
output(
"Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
96 $this->cutoffFrom = time() - $wgRCMaxAge;
97 $this->cutoffTo = time();
100 $this->
output(
"Clearing recentchanges table for time range...\n" );
101 $rcids = $dbw->selectFieldValues(
105 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
106 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
109 foreach ( array_chunk( $rcids, $this->mBatchSize )
as $rcidBatch ) {
110 $dbw->delete(
'recentchanges', [
'rc_id' => $rcidBatch ], __METHOD__ );
114 $this->
output(
"Loading from page and revision tables...\n" );
116 [
'page',
'revision' ],
131 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
132 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
136 [
'ORDER BY' =>
'rev_timestamp DESC' ]
139 $this->
output(
"Inserting from page and revision tables...\n" );
141 foreach (
$res as $row ) {
145 'rc_timestamp' => $row->rev_timestamp,
146 'rc_user' => $row->rev_user,
147 'rc_user_text' => $row->rev_user_text,
148 'rc_namespace' => $row->page_namespace,
149 'rc_title' => $row->page_title,
150 'rc_comment' => $row->rev_comment,
151 'rc_minor' => $row->rev_minor_edit,
153 'rc_new' => $row->page_is_new,
154 'rc_cur_id' => $row->page_id,
155 'rc_this_oldid' => $row->rev_id,
156 'rc_last_oldid' => 0,
158 'rc_source' => $row->page_is_new
162 'rc_deleted' => $row->rev_deleted
166 if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
179 $this->
output(
"Updating links and size differences...\n" );
181 # Fill in the rc_last_oldid field, which points to the previous edit
184 [
'rc_cur_id',
'rc_this_oldid',
'rc_timestamp' ],
186 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
187 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
190 [
'ORDER BY' =>
'rc_cur_id,rc_timestamp' ]
197 foreach (
$res as $obj ) {
200 if ( $obj->rc_cur_id != $lastCurId ) {
201 # Switch! Look up the previous last edit, if any
202 $lastCurId = intval( $obj->rc_cur_id );
203 $emit = $obj->rc_timestamp;
205 $row = $dbw->selectRow(
207 [
'rev_id',
'rev_len' ],
208 [
'rev_page' => $lastCurId,
"rev_timestamp < " . $dbw->addQuotes( $emit ) ],
210 [
'ORDER BY' =>
'rev_timestamp DESC' ]
213 $lastOldId = intval( $row->rev_id );
214 # Grab the last text size if available
215 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) :
null;
224 if ( $lastCurId == 0 ) {
225 $this->
output(
"Uhhh, something wrong? No curid\n" );
227 # Grab the entry's text size
228 $size = (int)$dbw->selectField(
231 [
'rev_id' => $obj->rc_this_oldid ],
238 'rc_last_oldid' => $lastOldId,
241 'rc_source' => $new === 1
244 'rc_old_len' => $lastSize,
245 'rc_new_len' => $size,
248 'rc_cur_id' => $lastCurId,
249 'rc_this_oldid' => $obj->rc_this_oldid,
250 'rc_timestamp' => $obj->rc_timestamp
255 $lastOldId = intval( $obj->rc_this_oldid );
258 if ( ( ++$updated % $this->mBatchSize ) == 0 ) {
269 global $wgLogTypes, $wgLogRestrictions;
273 $this->
output(
"Loading from user, page, and logging tables...\n" );
276 [
'user',
'logging',
'page' ],
292 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
293 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
297 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
300 [
'ORDER BY' =>
'log_timestamp DESC' ],
303 [
'LEFT JOIN', [
'log_namespace=page_namespace',
'log_title=page_title' ] ]
307 $field = $dbw->fieldInfo(
'recentchanges',
'rc_cur_id' );
310 foreach (
$res as $row ) {
314 'rc_timestamp' => $row->log_timestamp,
315 'rc_user' => $row->log_user,
316 'rc_user_text' => $row->user_name,
317 'rc_namespace' => $row->log_namespace,
318 'rc_title' => $row->log_title,
319 'rc_comment' => $row->log_comment,
324 'rc_this_oldid' => 0,
325 'rc_last_oldid' => 0,
328 'rc_cur_id' => $field->isNullable()
330 : (int)$row->page_id,
331 'rc_log_type' => $row->log_type,
332 'rc_log_action' => $row->log_action,
333 'rc_logid' => $row->log_id,
334 'rc_params' => $row->log_params,
335 'rc_deleted' => $row->log_deleted
340 if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
354 list( $recentchanges, $usergroups,
$user ) =
355 $dbw->tableNamesN(
'recentchanges',
'user_groups',
'user' );
357 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
358 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
359 # may be lost at this point (aside from joining on the patrol log table entries).
360 $botgroups = [
'bot' ];
363 # Flag our recent bot edits
365 $botwhere = $dbw->makeList( $botgroups );
367 $this->
output(
"Flagging bot account edits...\n" );
369 # Find all users that are bots
370 $sql =
"SELECT DISTINCT user_name FROM $usergroups, $user " .
371 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
372 $res = $dbw->query( $sql, __METHOD__ );
375 foreach (
$res as $obj ) {
376 $botusers[] = $obj->user_name;
379 # Fill in the rc_bot field
381 $rcids = $dbw->selectFieldValues(
385 'rc_user_text' => $botusers,
386 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
387 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
392 foreach ( array_chunk( $rcids, $this->mBatchSize )
as $rcidBatch ) {
396 [
'rc_id' => $rcidBatch ],
404 # Flag our recent autopatrolled edits
406 $patrolwhere = $dbw->makeList( $autopatrolgroups );
409 $this->
output(
"Flagging auto-patrolled edits...\n" );
411 # Find all users in RC with autopatrol rights
412 $sql =
"SELECT DISTINCT user_name FROM $usergroups, $user " .
413 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
414 $res = $dbw->query( $sql, __METHOD__ );
416 foreach (
$res as $obj ) {
417 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
420 # Fill in the rc_patrolled field
421 if ( $patrolusers ) {
422 $patrolwhere = implode(
',', $patrolusers );
423 $sql2 =
"UPDATE $recentchanges SET rc_patrolled=1 " .
424 "WHERE rc_user_text IN($patrolwhere) " .
425 "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ) .
' ' .
426 "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) );
427 $dbw->query( $sql2 );
439 $this->
output(
"Removing duplicate revision and logging entries...\n" );
442 [
'logging',
'log_search' ],
443 [
'ls_value',
'ls_log_id' ],
445 'ls_log_id = log_id',
446 'ls_field' =>
'associated_rev_id',
447 'log_type' =>
'upload',
448 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
449 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
455 foreach (
$res as $obj ) {
456 $rev_id = $obj->ls_value;
457 $log_id = $obj->ls_log_id;
462 [
'rc_this_oldid' => $rev_id ],
463 [
'rc_logid' => $log_id ],
470 [
'rc_this_oldid' => $rev_id,
'rc_logid' => 0 ],
474 if ( ( ++$updates % $this->mBatchSize ) == 0 ) {
486 $this->
output(
"Deleting feed timestamps.\n" );
488 foreach ( $wgFeedClasses
as $feed => $className ) {