<?php
/**
- * Rebuild link tracking tables from scratch. This takes several
- * hours, depending on the database size and server configuration.
+ * Rebuild recent changes from scratch. This takes several hours,
+ * depending on the database size and server configuration.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
+ * @file
* @ingroup Maintenance
* @todo Document
*/
-require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+require_once( __DIR__ . '/Maintenance.php' );
+/**
+ * Maintenance script that rebuilds recent changes from scratch.
+ *
+ * @ingroup Maintenance
+ */
class RebuildRecentchanges extends Maintenance {
public function __construct() {
parent::__construct();
}
public function execute() {
- global $wgTitle;
- $wgTitle = Title::newFromText( "Rebuild recent changes script" );
$this->rebuildRecentChangesTablePass1();
$this->rebuildRecentChangesTablePass2();
$this->rebuildRecentChangesTablePass3();
* Rebuild pass 1
* DOCUMENT ME!
*/
- function rebuildRecentChangesTablePass1()
- {
+ private function rebuildRecentChangesTablePass1() {
$dbw = wfGetDB( DB_MASTER );
-
+
$dbw->delete( 'recentchanges', '*' );
-
+
$this->output( "Loading from page and revision tables...\n" );
-
+
global $wgRCMaxAge;
-
+
$this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
$days = $wgRCMaxAge / 24 / 3600;
if ( intval( $days ) == $days ) {
} else {
$this->output( " (approx. " . intval( $days ) . " days)\n" );
}
-
+
$cutoff = time() - $wgRCMaxAge;
$dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
array(
- 'rc_id' => ( $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' ) > 0 ) ? $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' ) : 0,
'rc_timestamp' => 'rev_timestamp',
'rc_cur_time' => 'rev_timestamp',
'rc_user' => 'rev_user',
private function rebuildRecentChangesTablePass2() {
$dbw = wfGetDB( DB_MASTER );
list ( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
-
+
$this->output( "Updating links and size differences...\n" );
-
+
# Fill in the rc_last_oldid field, which points to the previous edit
$sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
"ORDER BY rc_cur_id,rc_timestamp";
$res = $dbw->query( $sql, DB_MASTER );
-
+
$lastCurId = 0;
$lastOldId = 0;
foreach ( $res as $obj ) {
if ( $row ) {
$lastOldId = intval( $row->rev_id );
# Grab the last text size if available
- $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : 'NULL';
+ $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
} else {
# No previous edit
$lastOldId = 0;
- $lastSize = 'NULL';
+ $lastSize = null;
$new = 1; // probably true
}
}
} else {
# Grab the entry's text size
$size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
- $size = !is_null( $size ) ? intval( $size ) : 'NULL';
-
- $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
- "rc_old_len=$lastSize,rc_new_len=$size " .
- "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
- $dbw->query( $sql3 );
-
+
+ $dbw->update( 'recentchanges',
+ array(
+ 'rc_last_oldid' => $lastOldId,
+ 'rc_new' => $new,
+ 'rc_type' => $new,
+ 'rc_old_len' => $lastSize,
+ 'rc_new_len' => $size,
+ ), array(
+ 'rc_cur_id' => $lastCurId,
+ 'rc_this_oldid' => $obj->rc_this_oldid,
+ ),
+ __METHOD__
+ );
+
$lastOldId = intval( $obj->rc_this_oldid );
$lastSize = $size;
}
*/
private function rebuildRecentChangesTablePass3() {
$dbw = wfGetDB( DB_MASTER );
-
+
$this->output( "Loading from user, page, and logging tables...\n" );
-
+
global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
// Some logs don't go in RC. This should check for that
$basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
-
+
// Escape...blah blah
$selectLogs = array();
foreach ( $basicRCLogs as $logtype ) {
$safetype = $dbw->strencode( $logtype );
$selectLogs[] = "'$safetype'";
}
-
+
$cutoff = time() - $wgRCMaxAge;
list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
$dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
* DOCUMENT ME!
*/
private function rebuildRecentChangesTablePass4() {
- global $wgGroupPermissions, $wgUseRCPatrol;
-
+ global $wgUseRCPatrol;
+
$dbw = wfGetDB( DB_MASTER );
-
+
list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
-
- $botgroups = $autopatrolgroups = array();
- foreach ( $wgGroupPermissions as $group => $rights ) {
- if ( isset( $rights['bot'] ) && $rights['bot'] ) {
- $botgroups[] = $dbw->addQuotes( $group );
- }
- if ( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] ) {
- $autopatrolgroups[] = $dbw->addQuotes( $group );
- }
- }
+
+ $botgroups = User::getGroupsWithPermission( 'bot' );
+ $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
# Flag our recent bot edits
if ( !empty( $botgroups ) ) {
- $botwhere = implode( ',', $botgroups );
+ $botwhere = $dbw->makeList( $botgroups );
$botusers = array();
-
+
$this->output( "Flagging bot account edits...\n" );
-
+
# Find all users that are bots
$sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
"WHERE ug_group IN($botwhere) AND user_id = ug_user";
$res = $dbw->query( $sql, DB_MASTER );
-
+
foreach ( $res as $obj ) {
$botusers[] = $dbw->addQuotes( $obj->user_name );
}
global $wgMiserMode;
# Flag our recent autopatrolled edits
if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
- $patrolwhere = implode( ',', $autopatrolgroups );
+ $patrolwhere = $dbw->makeList( $autopatrolgroups );
$patrolusers = array();
-
+
$this->output( "Flagging auto-patrolled edits...\n" );
-
+
# Find all users in RC with autopatrol rights
$sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
"WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
$res = $dbw->query( $sql, DB_MASTER );
-
+
foreach ( $res as $obj ) {
$patrolusers[] = $dbw->addQuotes( $obj->user_name );
}
-
+
# Fill in the rc_patrolled field
if ( !empty( $patrolusers ) ) {
$patrolwhere = implode( ',', $patrolusers );
}
$maintClass = "RebuildRecentchanges";
-require_once( DO_MAINTENANCE );
+require_once( RUN_MAINTENANCE_IF_MAIN );