3 * Rebuild recent changes from scratch. This takes several hours,
4 * depending on the database size and server configuration.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that rebuilds recent changes from scratch.
31 * @ingroup Maintenance
33 class RebuildRecentchanges
extends Maintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->mDescription
= "Rebuild recent changes";
39 public function execute() {
40 $this->rebuildRecentChangesTablePass1();
41 $this->rebuildRecentChangesTablePass2();
42 $this->rebuildRecentChangesTablePass3();
43 $this->rebuildRecentChangesTablePass4();
45 $this->output( "Done.\n" );
49 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
51 private function rebuildRecentChangesTablePass1() {
52 $dbw = $this->getDB( DB_MASTER
);
54 $dbw->delete( 'recentchanges', '*' );
56 $this->output( "Loading from page and revision tables...\n" );
60 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
61 $days = $wgRCMaxAge / 24 / 3600;
62 if ( intval( $days ) == $days ) {
63 $this->output( " (" . $days . " days)\n" );
65 $this->output( " (approx. " . intval( $days ) . " days)\n" );
68 $cutoff = time() - $wgRCMaxAge;
69 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
71 'rc_timestamp' => 'rev_timestamp',
72 'rc_user' => 'rev_user',
73 'rc_user_text' => 'rev_user_text',
74 'rc_namespace' => 'page_namespace',
75 'rc_title' => 'page_title',
76 'rc_comment' => 'rev_comment',
77 'rc_minor' => 'rev_minor_edit',
79 'rc_new' => 'page_is_new',
80 'rc_cur_id' => 'page_id',
81 'rc_this_oldid' => 'rev_id',
82 'rc_last_oldid' => 0, // is this ok?
83 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW
, RC_EDIT
),
84 'rc_source' => $dbw->conditional(
86 $dbw->addQuotes( RecentChange
::SRC_NEW
),
87 $dbw->addQuotes( RecentChange
::SRC_EDIT
)
89 'rc_deleted' => 'rev_deleted'
92 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
96 array(), // INSERT options
97 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
102 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
103 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
105 private function rebuildRecentChangesTablePass2() {
106 $dbw = $this->getDB( DB_MASTER
);
107 list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
109 $this->output( "Updating links and size differences...\n" );
111 # Fill in the rc_last_oldid field, which points to the previous edit
112 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
113 "ORDER BY rc_cur_id,rc_timestamp";
114 $res = $dbw->query( $sql, DB_MASTER
);
118 foreach ( $res as $obj ) {
120 if ( $obj->rc_cur_id
!= $lastCurId ) {
121 # Switch! Look up the previous last edit, if any
122 $lastCurId = intval( $obj->rc_cur_id
);
123 $emit = $obj->rc_timestamp
;
124 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
125 "WHERE rev_page={$lastCurId} " .
126 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
127 $sql2 = $dbw->limitResult( $sql2, 1, false );
128 $res2 = $dbw->query( $sql2 );
129 $row = $dbw->fetchObject( $res2 );
131 $lastOldId = intval( $row->rev_id
);
132 # Grab the last text size if available
133 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : null;
138 $new = 1; // probably true
141 if ( $lastCurId == 0 ) {
142 $this->output( "Uhhh, something wrong? No curid\n" );
144 # Grab the entry's text size
145 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid
) );
147 $dbw->update( 'recentchanges',
149 'rc_last_oldid' => $lastOldId,
152 'rc_source' => $new === 1 ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
153 'rc_old_len' => $lastSize,
154 'rc_new_len' => $size,
156 'rc_cur_id' => $lastCurId,
157 'rc_this_oldid' => $obj->rc_this_oldid
,
162 $lastOldId = intval( $obj->rc_this_oldid
);
169 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
171 private function rebuildRecentChangesTablePass3() {
172 $dbw = $this->getDB( DB_MASTER
);
174 $this->output( "Loading from user, page, and logging tables...\n" );
176 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
177 // Some logs don't go in RC. This should check for that
178 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
180 $cutoff = time() - $wgRCMaxAge;
181 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
186 "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)"
189 'rc_timestamp' => 'log_timestamp',
190 'rc_user' => 'log_user',
191 'rc_user_text' => 'user_name',
192 'rc_namespace' => 'log_namespace',
193 'rc_title' => 'log_title',
194 'rc_comment' => 'log_comment',
199 'rc_this_oldid' => 0,
200 'rc_last_oldid' => 0,
202 'rc_source' => $dbw->addQuotes( RecentChange
::SRC_LOG
),
203 'rc_cur_id' => $dbw->cascadingDeletes() ?
'page_id' : 'COALESCE(page_id, 0)',
204 'rc_log_type' => 'log_type',
205 'rc_log_action' => 'log_action',
206 'rc_logid' => 'log_id',
207 'rc_params' => 'log_params',
208 'rc_deleted' => 'log_deleted'
211 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
213 'log_type' => $basicRCLogs,
216 array(), // INSERT options
217 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
222 * Rebuild pass 4: Mark bot and autopatrolled entries.
224 private function rebuildRecentChangesTablePass4() {
225 global $wgUseRCPatrol;
227 $dbw = $this->getDB( DB_MASTER
);
229 list( $recentchanges, $usergroups, $user ) =
230 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
232 $botgroups = User
::getGroupsWithPermission( 'bot' );
233 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : array();
234 # Flag our recent bot edits
235 if ( !empty( $botgroups ) ) {
236 $botwhere = $dbw->makeList( $botgroups );
239 $this->output( "Flagging bot account edits...\n" );
241 # Find all users that are bots
242 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
243 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
244 $res = $dbw->query( $sql, DB_MASTER
);
246 foreach ( $res as $obj ) {
247 $botusers[] = $dbw->addQuotes( $obj->user_name
);
249 # Fill in the rc_bot field
250 if ( !empty( $botusers ) ) {
251 $botwhere = implode( ',', $botusers );
252 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
253 "WHERE rc_user_text IN($botwhere)";
254 $dbw->query( $sql2 );
258 # Flag our recent autopatrolled edits
259 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
260 $patrolwhere = $dbw->makeList( $autopatrolgroups );
261 $patrolusers = array();
263 $this->output( "Flagging auto-patrolled edits...\n" );
265 # Find all users in RC with autopatrol rights
266 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
267 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
268 $res = $dbw->query( $sql, DB_MASTER
);
270 foreach ( $res as $obj ) {
271 $patrolusers[] = $dbw->addQuotes( $obj->user_name
);
274 # Fill in the rc_patrolled field
275 if ( !empty( $patrolusers ) ) {
276 $patrolwhere = implode( ',', $patrolusers );
277 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
278 "WHERE rc_user_text IN($patrolwhere)";
279 $dbw->query( $sql2 );
285 * Purge cached feeds in $messageMemc
287 private function purgeFeeds() {
288 global $wgFeedClasses, $messageMemc;
290 $this->output( "Deleting feed timestamps.\n" );
292 foreach ( $wgFeedClasses as $feed => $className ) {
293 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
298 $maintClass = "RebuildRecentchanges";
299 require_once RUN_MAINTENANCE_IF_MAIN
;