MediaWiki
REL1_23
|
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( 'page_is_new != 0', $dbw->addQuotes( RecentChange::SRC_NEW ), $dbw->addQuotes( RecentChange::SRC_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_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT, 00148 'rc_old_len' => $lastSize, 00149 'rc_new_len' => $size, 00150 ), array( 00151 'rc_cur_id' => $lastCurId, 00152 'rc_this_oldid' => $obj->rc_this_oldid, 00153 ), 00154 __METHOD__ 00155 ); 00156 00157 $lastOldId = intval( $obj->rc_this_oldid ); 00158 $lastSize = $size; 00159 } 00160 } 00161 } 00162 00167 private function rebuildRecentChangesTablePass3() { 00168 $dbw = wfGetDB( DB_MASTER ); 00169 00170 $this->output( "Loading from user, page, and logging tables...\n" ); 00171 00172 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions; 00173 // Some logs don't go in RC. This should check for that 00174 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ); 00175 00176 // Escape...blah blah 00177 $selectLogs = array(); 00178 foreach ( $basicRCLogs as $logtype ) { 00179 $safetype = $dbw->strencode( $logtype ); 00180 $selectLogs[] = "'$safetype'"; 00181 } 00182 00183 $cutoff = time() - $wgRCMaxAge; 00184 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' ); 00185 $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ), 00186 array( 00187 'rc_timestamp' => '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_source' => $dbw->addQuotes( RecentChange::SRC_LOG ), 00201 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)', 00202 'rc_log_type' => 'log_type', 00203 'rc_log_action' => 'log_action', 00204 'rc_logid' => 'log_id', 00205 'rc_params' => 'log_params', 00206 'rc_deleted' => 'log_deleted' 00207 ), array( 00208 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ), 00209 'log_user=user_id', 00210 'log_type IN(' . implode( ',', $selectLogs ) . ')' 00211 ), __METHOD__, 00212 array(), // INSERT options 00213 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options 00214 ); 00215 } 00216 00221 private function rebuildRecentChangesTablePass4() { 00222 global $wgUseRCPatrol; 00223 00224 $dbw = wfGetDB( DB_MASTER ); 00225 00226 list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' ); 00227 00228 $botgroups = User::getGroupsWithPermission( 'bot' ); 00229 $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array(); 00230 # Flag our recent bot edits 00231 if ( !empty( $botgroups ) ) { 00232 $botwhere = $dbw->makeList( $botgroups ); 00233 $botusers = array(); 00234 00235 $this->output( "Flagging bot account edits...\n" ); 00236 00237 # Find all users that are bots 00238 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " . 00239 "WHERE ug_group IN($botwhere) AND user_id = ug_user"; 00240 $res = $dbw->query( $sql, DB_MASTER ); 00241 00242 foreach ( $res as $obj ) { 00243 $botusers[] = $dbw->addQuotes( $obj->user_name ); 00244 } 00245 # Fill in the rc_bot field 00246 if ( !empty( $botusers ) ) { 00247 $botwhere = implode( ',', $botusers ); 00248 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " . 00249 "WHERE rc_user_text IN($botwhere)"; 00250 $dbw->query( $sql2 ); 00251 } 00252 } 00253 global $wgMiserMode; 00254 # Flag our recent autopatrolled edits 00255 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) { 00256 $patrolwhere = $dbw->makeList( $autopatrolgroups ); 00257 $patrolusers = array(); 00258 00259 $this->output( "Flagging auto-patrolled edits...\n" ); 00260 00261 # Find all users in RC with autopatrol rights 00262 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " . 00263 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user"; 00264 $res = $dbw->query( $sql, DB_MASTER ); 00265 00266 foreach ( $res as $obj ) { 00267 $patrolusers[] = $dbw->addQuotes( $obj->user_name ); 00268 } 00269 00270 # Fill in the rc_patrolled field 00271 if ( !empty( $patrolusers ) ) { 00272 $patrolwhere = implode( ',', $patrolusers ); 00273 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " . 00274 "WHERE rc_user_text IN($patrolwhere)"; 00275 $dbw->query( $sql2 ); 00276 } 00277 } 00278 } 00279 00283 private function purgeFeeds() { 00284 global $wgFeedClasses, $messageMemc; 00285 00286 $this->output( "Deleting feed timestamps.\n" ); 00287 00288 foreach ( $wgFeedClasses as $feed => $className ) { 00289 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now. 00290 } 00291 } 00292 00293 } 00294 00295 $maintClass = "RebuildRecentchanges"; 00296 require_once RUN_MAINTENANCE_IF_MAIN;