-<?
-
-# Rebuild recent changes table.
-#
+<?php
+/**
+ * Rebuild recent changes table.
+ *
+ * @todo document
+ * @addtogroup Maintenance
+ */
+
+/** Public entry; more passes might come in! :) */
+function rebuildRecentChangesTable() {
+ rebuildRecentChangesTablePass1();
+ rebuildRecentChangesTablePass2();
+ rebuildRecentChangesTablePass3();
+}
+/** */
function rebuildRecentChangesTablePass1()
{
- $sql = "DROP TABLE IF EXISTS recentchanges";
- wfQuery( $sql );
-
- $sql = "CREATE TABLE recentchanges (
- rc_timestamp varchar(14) binary NOT NULL default '',
- rc_cur_time varchar(14) binary NOT NULL default '',
- rc_user int(10) unsigned NOT NULL default '0',
- rc_user_text varchar(255) binary NOT NULL default '',
- rc_namespace tinyint(3) unsigned NOT NULL default '0',
- rc_title varchar(255) binary NOT NULL default '',
- rc_comment varchar(255) binary NOT NULL default '',
- rc_minor tinyint(3) unsigned NOT NULL default '0',
- rc_bot tinyint(3) unsigned NOT NULL default '0',
- rc_new tinyint(3) unsigned NOT NULL default '0',
- rc_cur_id int(10) unsigned NOT NULL default '0',
- rc_this_oldid int(10) unsigned NOT NULL default '0',
- rc_last_oldid int(10) unsigned NOT NULL default '0',
- INDEX rc_cur_id (rc_cur_id),
- INDEX rc_cur_time (rc_cur_time),
- INDEX rc_timestamp (rc_timestamp),
- INDEX rc_namespace (rc_namespace),
- INDEX rc_title (rc_title)
-) TYPE=MyISAM PACK_KEYS=1;";
- wfQuery( $sql );
-
- print( "Loading from CUR table...\n" );
-
- $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time,rc_user," .
- "rc_user_text,rc_namespace,rc_title,rc_comment,rc_minor,rc_bot,rc_new," .
- "rc_cur_id,rc_this_oldid,rc_last_oldid) SELECT cur_timestamp," .
- "cur_timestamp,cur_user,cur_user_text,cur_namespace,cur_title," .
- "cur_comment,cur_minor_edit,0,cur_is_new,cur_id,0,0 FROM cur " .
- "ORDER BY cur_timestamp DESC LIMIT 5000";
- wfQuery( $sql );
-
- print( "Loading from OLD table...\n" );
-
- $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time,rc_user," .
- "rc_user_text,rc_namespace,rc_title,rc_comment,rc_minor,rc_bot,rc_new," .
- "rc_cur_id,rc_this_oldid,rc_last_oldid) SELECT old_timestamp,''," .
- "old_user,old_user_text,old_namespace,old_title,old_comment," .
- "old_minor_edit,0,0,0,old_id,0 FROM old ORDER BY old_timestamp " .
- "DESC LIMIT 5000";
- wfQuery( $sql );
-
- $sql = "SELECT rc_timestamp FROM recentchanges " .
- "ORDER BY rc_timestamp DESC LIMIT 5000,1";
- $res = wfQuery( $sql );
- $obj = wfFetchObject( $res );
- $ts = $obj->rc_timestamp;
-
- $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$ts}'";
- wfQuery( $sql );
+ $fname = 'rebuildRecentChangesTablePass1';
+ $dbw = wfGetDB( DB_MASTER );
+
+ $dbw->delete( 'recentchanges', '*' );
+
+ print( "Loading from page and revision tables...\n" );
+
+ global $wgRCMaxAge;
+ $cutoff = time() - $wgRCMaxAge;
+ $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
+ array(
+ 'rc_timestamp' => 'rev_timestamp',
+ 'rc_cur_time' => 'rev_timestamp',
+ 'rc_user' => 'rev_user',
+ 'rc_user_text' => 'rev_user_text',
+ 'rc_namespace' => 'page_namespace',
+ 'rc_title' => 'page_title',
+ 'rc_comment' => 'rev_comment',
+ 'rc_minor' => 'rev_minor_edit',
+ 'rc_bot' => 0,
+ 'rc_new' => 'page_is_new',
+ 'rc_cur_id' => 'page_id',
+ 'rc_this_oldid' => 'rev_id',
+ 'rc_last_oldid' => 0, // is this ok?
+ 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
+ ), array(
+ 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
+ 'rev_page=page_id'
+ ), $fname,
+ array(), // INSERT options
+ array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
+ );
}
function rebuildRecentChangesTablePass2()
{
- $ns = $id = $count = 0;
- $title = $ct = "";
-
- print( "Updating links...\n" );
-
- $sql = "SELECT rc_namespace,rc_title,rc_timestamp FROM recentchanges " .
- "ORDER BY rc_namespace,rc_title,rc_timestamp DESC";
- $res = wfQuery( $sql );
+ $dbw = wfGetDB( DB_MASTER );
+ list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
+
+ print( "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;
+ while ( $obj = $dbw->fetchObject( $res ) ) {
+ $new = 0;
+ if( $obj->rc_cur_id != $lastCurId ) {
+ # Switch! Look up the previous last edit, if any
+ $lastCurId = intval( $obj->rc_cur_id );
+ $emit = $obj->rc_timestamp;
+ $sql2 = "SELECT rev_id, rev_len FROM $revision " .
+ "WHERE rev_page={$lastCurId} ".
+ "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
+ $res2 = $dbw->query( $sql2 );
+ if( $row = $dbw->fetchObject( $res2 ) ) {
+ $lastOldId = intval( $row->rev_id );
+ $lastSize = $row->rev_len; # Grab the last text size
+ } else {
+ # No previous edit
+ $lastOldId = 0;
+ $lastSize = 'NULL';
+ $new = 1;
+ }
+ $dbw->freeResult( $res2 );
+ }
+ if( $lastCurId == 0 ) {
+ print "Uhhh, something wrong? No curid\n";
+ } else {
+ # Grab the entry's text size
+ $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
+ $size = $size ? $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 );
+
+ $lastOldId = intval( $obj->rc_this_oldid );
+ }
+ }
+ $dbw->freeResult( $res );
+}
- while ( $obj = wfFetchObject( $res ) ) {
- if ( ! ( $ns == $obj->rc_namespace &&
- 0 == strcmp( $title, wfStrencode( $obj->rc_title ) ) ) ) {
+function rebuildRecentChangesTablePass3()
+{
+ global $wgGroupPermissions, $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'] == true ) {
+ $botgroups[] = $dbw->addQuotes( $group );
+ }
+ if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
+ $autopatrolgroups[] = $dbw->addQuotes( $group );
+ }
+ }
+ # Flag our recent bot edits
+ if( !empty($botgroups) ) {
+ $botwhere = implode(',',$botgroups);
+ $botusers = array();
- $ns = $obj->rc_namespace;
- $title = wfStrencode( $obj->rc_title );
+ print( "Flagging bot account edits...\n" );
- $sql = "SELECT cur_id,cur_timestamp FROM cur WHERE " .
- "cur_namespace={$ns} AND cur_title='{$title}'";
- $res2 = wfQuery( $sql );
- $obj2 = wfFetchObject( $res2 );
+ # 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 );
- $id = $obj2->cur_id;
- $ct = $obj2->cur_timestamp;
+ while( $obj = $dbw->fetchObject( $res ) ) {
+ $botusers[] = $dbw->addQuotes( $obj->user_name );
}
- $sql = "SELECT old_id FROM old WHERE old_namespace={$ns} " .
- "AND old_title='{$title}' AND old_timestamp < '" .
- "{$obj->rc_timestamp}' ORDER BY old_timestamp DESC LIMIT 1";
- $res2 = wfQuery( $sql );
-
- if ( 0 != wfNumRows( $res2 ) ) {
- $obj2 = wfFetchObject( $res2 );
-
- $sql = "UPDATE recentchanges SET rc_cur_id={$id},rc_cur_time=" .
- "'{$ct}',rc_last_oldid={$obj2->old_id} WHERE " .
- "rc_namespace={$ns} AND rc_title='{$title}' AND " .
- "rc_timestamp='{$obj->rc_timestamp}'";
- wfQuery( $sql );
- } else {
- $sql = "UPDATE recentchanges SET rc_cur_id={$id},rc_cur_time=" .
- "'{$ct}' WHERE rc_namespace={$ns} AND rc_title='{$title}' " .
- "AND rc_timestamp='{$obj->rc_timestamp}'";
- wfQuery( $sql );
+ # Fill in the rc_bot field
+ if( !empty($botusers) ) {
+ $botwhere = implode(',',$botusers);
+ $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
+ "WHERE rc_user_text IN($botwhere)";
+ $dbw->query( $sql2 );
}
+ }
+ # Flag our recent autopatrolled edits
+ if( !empty($autopatrolgroups) ) {
+ $patrolwhere = implode(',',$autopatrolgroups);
+ $patrolusers = array();
+
+ print( "Flagging auto-patrolled edits...\n" );
- if ( 0 == ( ++$count % 500 ) ) {
- printf( "%d records processed.\n", $count );
+ # 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 );
+
+ while( $obj = $dbw->fetchObject( $res ) ) {
+ $patrolusers[] = $dbw->addQuotes( $obj->user_name );
+ }
+
+ # Fill in the rc_patrolled field
+ if( !empty($patrolusers) ) {
+ $patrolwhere = implode(',',$patrolusers);
+ $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
+ "WHERE rc_user_text IN($patrolwhere)";
+ $dbw->query( $sql2 );
}
}
+
+ $dbw->freeResult( $res );
}
?>