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" );
52 private function rebuildRecentChangesTablePass1() {
53 $dbw = wfGetDB( DB_MASTER
);
55 $dbw->delete( 'recentchanges', '*' );
57 $this->output( "Loading from page and revision tables...\n" );
61 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
62 $days = $wgRCMaxAge / 24 / 3600;
63 if ( intval( $days ) == $days ) {
64 $this->output( " (" . $days . " days)\n" );
66 $this->output( " (approx. " . intval( $days ) . " days)\n" );
69 $cutoff = time() - $wgRCMaxAge;
70 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
72 'rc_timestamp' => 'rev_timestamp',
73 'rc_user' => 'rev_user',
74 'rc_user_text' => 'rev_user_text',
75 'rc_namespace' => 'page_namespace',
76 'rc_title' => 'page_title',
77 'rc_comment' => 'rev_comment',
78 'rc_minor' => 'rev_minor_edit',
80 'rc_new' => 'page_is_new',
81 'rc_cur_id' => 'page_id',
82 'rc_this_oldid' => 'rev_id',
83 'rc_last_oldid' => 0, // is this ok?
84 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW
, RC_EDIT
),
85 'rc_source' => $dbw->conditional(
87 $dbw->addQuotes( RecentChange
::SRC_NEW
),
88 $dbw->addQuotes( RecentChange
::SRC_EDIT
)
90 'rc_deleted' => 'rev_deleted'
93 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
97 array(), // INSERT options
98 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
106 private function rebuildRecentChangesTablePass2() {
107 $dbw = wfGetDB( DB_MASTER
);
108 list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
110 $this->output( "Updating links and size differences...\n" );
112 # Fill in the rc_last_oldid field, which points to the previous edit
113 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
114 "ORDER BY rc_cur_id,rc_timestamp";
115 $res = $dbw->query( $sql, DB_MASTER
);
119 foreach ( $res as $obj ) {
121 if ( $obj->rc_cur_id
!= $lastCurId ) {
122 # Switch! Look up the previous last edit, if any
123 $lastCurId = intval( $obj->rc_cur_id
);
124 $emit = $obj->rc_timestamp
;
125 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
126 "WHERE rev_page={$lastCurId} " .
127 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
128 $sql2 = $dbw->limitResult( $sql2, 1, false );
129 $res2 = $dbw->query( $sql2 );
130 $row = $dbw->fetchObject( $res2 );
132 $lastOldId = intval( $row->rev_id
);
133 # Grab the last text size if available
134 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : null;
139 $new = 1; // probably true
142 if ( $lastCurId == 0 ) {
143 $this->output( "Uhhh, something wrong? No curid\n" );
145 # Grab the entry's text size
146 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid
) );
148 $dbw->update( 'recentchanges',
150 'rc_last_oldid' => $lastOldId,
153 'rc_source' => $new === 1 ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
154 'rc_old_len' => $lastSize,
155 'rc_new_len' => $size,
157 'rc_cur_id' => $lastCurId,
158 'rc_this_oldid' => $obj->rc_this_oldid
,
163 $lastOldId = intval( $obj->rc_this_oldid
);
173 private function rebuildRecentChangesTablePass3() {
174 $dbw = wfGetDB( DB_MASTER
);
176 $this->output( "Loading from user, page, and logging tables...\n" );
178 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
179 // Some logs don't go in RC. This should check for that
180 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
182 // Escape...blah blah
183 $selectLogs = array();
184 foreach ( $basicRCLogs as $logtype ) {
185 $safetype = $dbw->strencode( $logtype );
186 $selectLogs[] = "'$safetype'";
189 $cutoff = time() - $wgRCMaxAge;
190 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
195 "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)"
198 'rc_timestamp' => 'log_timestamp',
199 'rc_user' => 'log_user',
200 'rc_user_text' => 'user_name',
201 'rc_namespace' => 'log_namespace',
202 'rc_title' => 'log_title',
203 'rc_comment' => 'log_comment',
208 'rc_this_oldid' => 0,
209 'rc_last_oldid' => 0,
211 'rc_source' => $dbw->addQuotes( RecentChange
::SRC_LOG
),
212 'rc_cur_id' => $dbw->cascadingDeletes() ?
'page_id' : 'COALESCE(page_id, 0)',
213 'rc_log_type' => 'log_type',
214 'rc_log_action' => 'log_action',
215 'rc_logid' => 'log_id',
216 'rc_params' => 'log_params',
217 'rc_deleted' => 'log_deleted'
220 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
222 'log_type IN(' . implode( ',', $selectLogs ) . ')'
225 array(), // INSERT options
226 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
234 private function rebuildRecentChangesTablePass4() {
235 global $wgUseRCPatrol;
237 $dbw = wfGetDB( DB_MASTER
);
239 list( $recentchanges, $usergroups, $user ) =
240 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
242 $botgroups = User
::getGroupsWithPermission( 'bot' );
243 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : array();
244 # Flag our recent bot edits
245 if ( !empty( $botgroups ) ) {
246 $botwhere = $dbw->makeList( $botgroups );
249 $this->output( "Flagging bot account edits...\n" );
251 # Find all users that are bots
252 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
253 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
254 $res = $dbw->query( $sql, DB_MASTER
);
256 foreach ( $res as $obj ) {
257 $botusers[] = $dbw->addQuotes( $obj->user_name
);
259 # Fill in the rc_bot field
260 if ( !empty( $botusers ) ) {
261 $botwhere = implode( ',', $botusers );
262 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
263 "WHERE rc_user_text IN($botwhere)";
264 $dbw->query( $sql2 );
268 # Flag our recent autopatrolled edits
269 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
270 $patrolwhere = $dbw->makeList( $autopatrolgroups );
271 $patrolusers = array();
273 $this->output( "Flagging auto-patrolled edits...\n" );
275 # Find all users in RC with autopatrol rights
276 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
277 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
278 $res = $dbw->query( $sql, DB_MASTER
);
280 foreach ( $res as $obj ) {
281 $patrolusers[] = $dbw->addQuotes( $obj->user_name
);
284 # Fill in the rc_patrolled field
285 if ( !empty( $patrolusers ) ) {
286 $patrolwhere = implode( ',', $patrolusers );
287 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
288 "WHERE rc_user_text IN($patrolwhere)";
289 $dbw->query( $sql2 );
295 * Purge cached feeds in $messageMemc
297 private function purgeFeeds() {
298 global $wgFeedClasses, $messageMemc;
300 $this->output( "Deleting feed timestamps.\n" );
302 foreach ( $wgFeedClasses as $feed => $className ) {
303 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
308 $maintClass = "RebuildRecentchanges";
309 require_once RUN_MAINTENANCE_IF_MAIN
;