MediaWiki  REL1_22
rebuildrecentchanges.php
Go to the documentation of this file.
00001 <?php
00026 require_once __DIR__ . '/Maintenance.php';
00027 
00033 class RebuildRecentchanges extends Maintenance {
00034     public function __construct() {
00035         parent::__construct();
00036         $this->mDescription = "Rebuild recent changes";
00037     }
00038 
00039     public function execute() {
00040         $this->rebuildRecentChangesTablePass1();
00041         $this->rebuildRecentChangesTablePass2();
00042         $this->rebuildRecentChangesTablePass3();
00043         $this->rebuildRecentChangesTablePass4();
00044         $this->purgeFeeds();
00045         $this->output( "Done.\n" );
00046     }
00047 
00052     private function rebuildRecentChangesTablePass1() {
00053         $dbw = wfGetDB( DB_MASTER );
00054 
00055         $dbw->delete( 'recentchanges', '*' );
00056 
00057         $this->output( "Loading from page and revision tables...\n" );
00058 
00059         global $wgRCMaxAge;
00060 
00061         $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
00062         $days = $wgRCMaxAge / 24 / 3600;
00063         if ( intval( $days ) == $days ) {
00064             $this->output( " (" . $days . " days)\n" );
00065         } else {
00066             $this->output( " (approx. " . intval( $days ) . " days)\n" );
00067         }
00068 
00069         $cutoff = time() - $wgRCMaxAge;
00070         $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
00071             array(
00072                 'rc_timestamp'  => 'rev_timestamp',
00073                 'rc_cur_time'   => 'rev_timestamp',
00074                 'rc_user'       => 'rev_user',
00075                 'rc_user_text'  => 'rev_user_text',
00076                 'rc_namespace'  => 'page_namespace',
00077                 'rc_title'      => 'page_title',
00078                 'rc_comment'    => 'rev_comment',
00079                 'rc_minor'      => 'rev_minor_edit',
00080                 'rc_bot'        => 0,
00081                 'rc_new'        => 'page_is_new',
00082                 'rc_cur_id'     => 'page_id',
00083                 'rc_this_oldid' => 'rev_id',
00084                 'rc_last_oldid' => 0, // is this ok?
00085                 'rc_type'       => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
00086                 'rc_deleted'    => 'rev_deleted'
00087             ), array(
00088                 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
00089                 'rev_page=page_id'
00090             ), __METHOD__,
00091             array(), // INSERT options
00092             array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
00093         );
00094     }
00095 
00100     private function rebuildRecentChangesTablePass2() {
00101         $dbw = wfGetDB( DB_MASTER );
00102         list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
00103 
00104         $this->output( "Updating links and size differences...\n" );
00105 
00106         # Fill in the rc_last_oldid field, which points to the previous edit
00107         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
00108             "ORDER BY rc_cur_id,rc_timestamp";
00109         $res = $dbw->query( $sql, DB_MASTER );
00110 
00111         $lastCurId = 0;
00112         $lastOldId = 0;
00113         foreach ( $res as $obj ) {
00114             $new = 0;
00115             if ( $obj->rc_cur_id != $lastCurId ) {
00116                 # Switch! Look up the previous last edit, if any
00117                 $lastCurId = intval( $obj->rc_cur_id );
00118                 $emit = $obj->rc_timestamp;
00119                 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
00120                     "WHERE rev_page={$lastCurId} " .
00121                     "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
00122                 $sql2 = $dbw->limitResult( $sql2, 1, false );
00123                 $res2 = $dbw->query( $sql2 );
00124                 $row = $dbw->fetchObject( $res2 );
00125                 if ( $row ) {
00126                     $lastOldId = intval( $row->rev_id );
00127                     # Grab the last text size if available
00128                     $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
00129                 } else {
00130                     # No previous edit
00131                     $lastOldId = 0;
00132                     $lastSize = null;
00133                     $new = 1; // probably true
00134                 }
00135             }
00136             if ( $lastCurId == 0 ) {
00137                 $this->output( "Uhhh, something wrong? No curid\n" );
00138             } else {
00139                 # Grab the entry's text size
00140                 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
00141 
00142                 $dbw->update( 'recentchanges',
00143                     array(
00144                         'rc_last_oldid' => $lastOldId,
00145                         'rc_new' => $new,
00146                         'rc_type' => $new,
00147                         'rc_old_len' => $lastSize,
00148                         'rc_new_len' => $size,
00149                     ), array(
00150                         'rc_cur_id' => $lastCurId,
00151                         'rc_this_oldid' => $obj->rc_this_oldid,
00152                     ),
00153                     __METHOD__
00154                 );
00155 
00156                 $lastOldId = intval( $obj->rc_this_oldid );
00157                 $lastSize = $size;
00158             }
00159         }
00160     }
00161 
00166     private function rebuildRecentChangesTablePass3() {
00167         $dbw = wfGetDB( DB_MASTER );
00168 
00169         $this->output( "Loading from user, page, and logging tables...\n" );
00170 
00171         global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
00172         // Some logs don't go in RC. This should check for that
00173         $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
00174 
00175         // Escape...blah blah
00176         $selectLogs = array();
00177         foreach ( $basicRCLogs as $logtype ) {
00178             $safetype = $dbw->strencode( $logtype );
00179             $selectLogs[] = "'$safetype'";
00180         }
00181 
00182         $cutoff = time() - $wgRCMaxAge;
00183         list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
00184         $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
00185             array(
00186                 'rc_timestamp'  => 'log_timestamp',
00187                 'rc_cur_time'   => 'log_timestamp',
00188                 'rc_user'       => 'log_user',
00189                 'rc_user_text'  => 'user_name',
00190                 'rc_namespace'  => 'log_namespace',
00191                 'rc_title'      => 'log_title',
00192                 'rc_comment'    => 'log_comment',
00193                 'rc_minor'      => 0,
00194                 'rc_bot'        => 0,
00195                 'rc_patrolled'  => 1,
00196                 'rc_new'        => 0,
00197                 'rc_this_oldid' => 0,
00198                 'rc_last_oldid' => 0,
00199                 'rc_type'       => RC_LOG,
00200                 'rc_cur_id'     => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
00201                 'rc_log_type'   => 'log_type',
00202                 'rc_log_action' => 'log_action',
00203                 'rc_logid'      => 'log_id',
00204                 'rc_params'     => 'log_params',
00205                 'rc_deleted'    => 'log_deleted'
00206             ), array(
00207                 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
00208                 'log_user=user_id',
00209                 'log_type IN(' . implode( ',', $selectLogs ) . ')'
00210             ), __METHOD__,
00211             array(), // INSERT options
00212             array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
00213         );
00214     }
00215 
00220     private function rebuildRecentChangesTablePass4() {
00221         global $wgUseRCPatrol;
00222 
00223         $dbw = wfGetDB( DB_MASTER );
00224 
00225         list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
00226 
00227         $botgroups = User::getGroupsWithPermission( 'bot' );
00228         $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
00229         # Flag our recent bot edits
00230         if ( !empty( $botgroups ) ) {
00231             $botwhere = $dbw->makeList( $botgroups );
00232             $botusers = array();
00233 
00234             $this->output( "Flagging bot account edits...\n" );
00235 
00236             # Find all users that are bots
00237             $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
00238                 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
00239             $res = $dbw->query( $sql, DB_MASTER );
00240 
00241             foreach ( $res as $obj ) {
00242                 $botusers[] = $dbw->addQuotes( $obj->user_name );
00243             }
00244             # Fill in the rc_bot field
00245             if ( !empty( $botusers ) ) {
00246                 $botwhere = implode( ',', $botusers );
00247                 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
00248                     "WHERE rc_user_text IN($botwhere)";
00249                 $dbw->query( $sql2 );
00250             }
00251         }
00252         global $wgMiserMode;
00253         # Flag our recent autopatrolled edits
00254         if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
00255             $patrolwhere = $dbw->makeList( $autopatrolgroups );
00256             $patrolusers = array();
00257 
00258             $this->output( "Flagging auto-patrolled edits...\n" );
00259 
00260             # Find all users in RC with autopatrol rights
00261             $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
00262                 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
00263             $res = $dbw->query( $sql, DB_MASTER );
00264 
00265             foreach ( $res as $obj ) {
00266                 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
00267             }
00268 
00269             # Fill in the rc_patrolled field
00270             if ( !empty( $patrolusers ) ) {
00271                 $patrolwhere = implode( ',', $patrolusers );
00272                 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
00273                     "WHERE rc_user_text IN($patrolwhere)";
00274                 $dbw->query( $sql2 );
00275             }
00276         }
00277     }
00278 
00282     private function purgeFeeds() {
00283         global $wgFeedClasses, $messageMemc;
00284 
00285         $this->output( "Deleting feed timestamps.\n" );
00286 
00287         foreach ( $wgFeedClasses as $feed => $className ) {
00288             $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
00289         }
00290     }
00291 
00292 }
00293 
00294 $maintClass = "RebuildRecentchanges";
00295 require_once RUN_MAINTENANCE_IF_MAIN;