MediaWiki  REL1_24
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_user' => 'rev_user',
00074                 'rc_user_text' => 'rev_user_text',
00075                 'rc_namespace' => 'page_namespace',
00076                 'rc_title' => 'page_title',
00077                 'rc_comment' => 'rev_comment',
00078                 'rc_minor' => 'rev_minor_edit',
00079                 'rc_bot' => 0,
00080                 'rc_new' => 'page_is_new',
00081                 'rc_cur_id' => 'page_id',
00082                 'rc_this_oldid' => 'rev_id',
00083                 'rc_last_oldid' => 0, // is this ok?
00084                 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
00085                 'rc_source' => $dbw->conditional(
00086                         'page_is_new != 0',
00087                         $dbw->addQuotes( RecentChange::SRC_NEW ),
00088                         $dbw->addQuotes( RecentChange::SRC_EDIT )
00089                 ),
00090                 'rc_deleted' => 'rev_deleted'
00091             ),
00092             array(
00093                 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
00094                 'rev_page=page_id'
00095             ),
00096             __METHOD__,
00097             array(), // INSERT options
00098             array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
00099         );
00100     }
00101 
00106     private function rebuildRecentChangesTablePass2() {
00107         $dbw = wfGetDB( DB_MASTER );
00108         list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
00109 
00110         $this->output( "Updating links and size differences...\n" );
00111 
00112         # Fill in the rc_last_oldid field, which points to the previous edit
00113         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
00114             "ORDER BY rc_cur_id,rc_timestamp";
00115         $res = $dbw->query( $sql, DB_MASTER );
00116 
00117         $lastCurId = 0;
00118         $lastOldId = 0;
00119         foreach ( $res as $obj ) {
00120             $new = 0;
00121             if ( $obj->rc_cur_id != $lastCurId ) {
00122                 # Switch! Look up the previous last edit, if any
00123                 $lastCurId = intval( $obj->rc_cur_id );
00124                 $emit = $obj->rc_timestamp;
00125                 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
00126                     "WHERE rev_page={$lastCurId} " .
00127                     "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
00128                 $sql2 = $dbw->limitResult( $sql2, 1, false );
00129                 $res2 = $dbw->query( $sql2 );
00130                 $row = $dbw->fetchObject( $res2 );
00131                 if ( $row ) {
00132                     $lastOldId = intval( $row->rev_id );
00133                     # Grab the last text size if available
00134                     $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
00135                 } else {
00136                     # No previous edit
00137                     $lastOldId = 0;
00138                     $lastSize = null;
00139                     $new = 1; // probably true
00140                 }
00141             }
00142             if ( $lastCurId == 0 ) {
00143                 $this->output( "Uhhh, something wrong? No curid\n" );
00144             } else {
00145                 # Grab the entry's text size
00146                 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
00147 
00148                 $dbw->update( 'recentchanges',
00149                     array(
00150                         'rc_last_oldid' => $lastOldId,
00151                         'rc_new' => $new,
00152                         'rc_type' => $new,
00153                         'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
00154                         'rc_old_len' => $lastSize,
00155                         'rc_new_len' => $size,
00156                     ), array(
00157                         'rc_cur_id' => $lastCurId,
00158                         'rc_this_oldid' => $obj->rc_this_oldid,
00159                     ),
00160                     __METHOD__
00161                 );
00162 
00163                 $lastOldId = intval( $obj->rc_this_oldid );
00164                 $lastSize = $size;
00165             }
00166         }
00167     }
00168 
00173     private function rebuildRecentChangesTablePass3() {
00174         $dbw = wfGetDB( DB_MASTER );
00175 
00176         $this->output( "Loading from user, page, and logging tables...\n" );
00177 
00178         global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
00179         // Some logs don't go in RC. This should check for that
00180         $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
00181 
00182         $cutoff = time() - $wgRCMaxAge;
00183         list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
00184         $dbw->insertSelect(
00185             'recentchanges',
00186             array(
00187                 'user',
00188                 "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)"
00189             ),
00190             array(
00191                 'rc_timestamp' => 'log_timestamp',
00192                 'rc_user' => 'log_user',
00193                 'rc_user_text' => 'user_name',
00194                 'rc_namespace' => 'log_namespace',
00195                 'rc_title' => 'log_title',
00196                 'rc_comment' => 'log_comment',
00197                 'rc_minor' => 0,
00198                 'rc_bot' => 0,
00199                 'rc_patrolled' => 1,
00200                 'rc_new' => 0,
00201                 'rc_this_oldid' => 0,
00202                 'rc_last_oldid' => 0,
00203                 'rc_type' => RC_LOG,
00204                 'rc_source' => $dbw->addQuotes( RecentChange::SRC_LOG ),
00205                 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
00206                 'rc_log_type' => 'log_type',
00207                 'rc_log_action' => 'log_action',
00208                 'rc_logid' => 'log_id',
00209                 'rc_params' => 'log_params',
00210                 'rc_deleted' => 'log_deleted'
00211             ),
00212             array(
00213                 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
00214                 'log_user=user_id',
00215                 'log_type' => $basicRCLogs,
00216             ),
00217             __METHOD__,
00218             array(), // INSERT options
00219             array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
00220         );
00221     }
00222 
00227     private function rebuildRecentChangesTablePass4() {
00228         global $wgUseRCPatrol;
00229 
00230         $dbw = wfGetDB( DB_MASTER );
00231 
00232         list( $recentchanges, $usergroups, $user ) =
00233             $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
00234 
00235         $botgroups = User::getGroupsWithPermission( 'bot' );
00236         $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
00237         # Flag our recent bot edits
00238         if ( !empty( $botgroups ) ) {
00239             $botwhere = $dbw->makeList( $botgroups );
00240             $botusers = array();
00241 
00242             $this->output( "Flagging bot account edits...\n" );
00243 
00244             # Find all users that are bots
00245             $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
00246                 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
00247             $res = $dbw->query( $sql, DB_MASTER );
00248 
00249             foreach ( $res as $obj ) {
00250                 $botusers[] = $dbw->addQuotes( $obj->user_name );
00251             }
00252             # Fill in the rc_bot field
00253             if ( !empty( $botusers ) ) {
00254                 $botwhere = implode( ',', $botusers );
00255                 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
00256                     "WHERE rc_user_text IN($botwhere)";
00257                 $dbw->query( $sql2 );
00258             }
00259         }
00260         global $wgMiserMode;
00261         # Flag our recent autopatrolled edits
00262         if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
00263             $patrolwhere = $dbw->makeList( $autopatrolgroups );
00264             $patrolusers = array();
00265 
00266             $this->output( "Flagging auto-patrolled edits...\n" );
00267 
00268             # Find all users in RC with autopatrol rights
00269             $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
00270                 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
00271             $res = $dbw->query( $sql, DB_MASTER );
00272 
00273             foreach ( $res as $obj ) {
00274                 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
00275             }
00276 
00277             # Fill in the rc_patrolled field
00278             if ( !empty( $patrolusers ) ) {
00279                 $patrolwhere = implode( ',', $patrolusers );
00280                 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
00281                     "WHERE rc_user_text IN($patrolwhere)";
00282                 $dbw->query( $sql2 );
00283             }
00284         }
00285     }
00286 
00290     private function purgeFeeds() {
00291         global $wgFeedClasses, $messageMemc;
00292 
00293         $this->output( "Deleting feed timestamps.\n" );
00294 
00295         foreach ( $wgFeedClasses as $feed => $className ) {
00296             $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
00297         }
00298     }
00299 }
00300 
00301 $maintClass = "RebuildRecentchanges";
00302 require_once RUN_MAINTENANCE_IF_MAIN;