3 * Rebuild link tracking tables from scratch. This takes several
4 * hours, 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
21 * @ingroup Maintenance
25 require_once( dirname( __FILE__
) . '/Maintenance.php' );
27 class RebuildRecentchanges
extends Maintenance
{
28 public function __construct() {
29 parent
::__construct();
30 $this->mDescription
= "Rebuild recent changes";
33 public function execute() {
34 $this->rebuildRecentChangesTablePass1();
35 $this->rebuildRecentChangesTablePass2();
36 $this->rebuildRecentChangesTablePass3();
37 $this->rebuildRecentChangesTablePass4();
39 $this->output( "Done.\n" );
46 function rebuildRecentChangesTablePass1()
48 $dbw = wfGetDB( DB_MASTER
);
50 $dbw->delete( 'recentchanges', '*' );
52 $this->output( "Loading from page and revision tables...\n" );
56 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
57 $days = $wgRCMaxAge / 24 / 3600;
58 if ( intval( $days ) == $days ) {
59 $this->output( " (" . $days . " days)\n" );
61 $this->output( " (approx. " . intval( $days ) . " days)\n" );
64 $cutoff = time() - $wgRCMaxAge;
65 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
67 'rc_timestamp' => 'rev_timestamp',
68 'rc_cur_time' => 'rev_timestamp',
69 'rc_user' => 'rev_user',
70 'rc_user_text' => 'rev_user_text',
71 'rc_namespace' => 'page_namespace',
72 'rc_title' => 'page_title',
73 'rc_comment' => 'rev_comment',
74 'rc_minor' => 'rev_minor_edit',
76 'rc_new' => 'page_is_new',
77 'rc_cur_id' => 'page_id',
78 'rc_this_oldid' => 'rev_id',
79 'rc_last_oldid' => 0, // is this ok?
80 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW
, RC_EDIT
),
81 'rc_deleted' => 'rev_deleted'
83 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
86 array(), // INSERT options
87 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
95 private function rebuildRecentChangesTablePass2() {
96 $dbw = wfGetDB( DB_MASTER
);
97 list ( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
99 $this->output( "Updating links and size differences...\n" );
101 # Fill in the rc_last_oldid field, which points to the previous edit
102 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
103 "ORDER BY rc_cur_id,rc_timestamp";
104 $res = $dbw->query( $sql, DB_MASTER
);
108 foreach ( $res as $obj ) {
110 if ( $obj->rc_cur_id
!= $lastCurId ) {
111 # Switch! Look up the previous last edit, if any
112 $lastCurId = intval( $obj->rc_cur_id
);
113 $emit = $obj->rc_timestamp
;
114 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
115 "WHERE rev_page={$lastCurId} " .
116 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
117 $sql2 = $dbw->limitResult( $sql2, 1, false );
118 $res2 = $dbw->query( $sql2 );
119 $row = $dbw->fetchObject( $res2 );
121 $lastOldId = intval( $row->rev_id
);
122 # Grab the last text size if available
123 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : 'NULL';
128 $new = 1; // probably true
131 if ( $lastCurId == 0 ) {
132 $this->output( "Uhhh, something wrong? No curid\n" );
134 # Grab the entry's text size
135 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid
) );
136 $size = !is_null( $size ) ?
intval( $size ) : 'NULL';
138 $dbw->update( 'recentchanges',
140 'rc_old_len' => $lastSize,
141 'rc_new_len' => $size,
143 'rc_cur_id' => $lastCurId,
144 'rc_this_oldid' => $obj->rc_this_oldid
,
149 $lastOldId = intval( $obj->rc_this_oldid
);
159 private function rebuildRecentChangesTablePass3() {
160 $dbw = wfGetDB( DB_MASTER
);
162 $this->output( "Loading from user, page, and logging tables...\n" );
164 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
165 // Some logs don't go in RC. This should check for that
166 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
168 // Escape...blah blah
169 $selectLogs = array();
170 foreach ( $basicRCLogs as $logtype ) {
171 $safetype = $dbw->strencode( $logtype );
172 $selectLogs[] = "'$safetype'";
175 $cutoff = time() - $wgRCMaxAge;
176 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
177 $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
179 'rc_timestamp' => 'log_timestamp',
180 'rc_cur_time' => 'log_timestamp',
181 'rc_user' => 'log_user',
182 'rc_user_text' => 'user_name',
183 'rc_namespace' => 'log_namespace',
184 'rc_title' => 'log_title',
185 'rc_comment' => 'log_comment',
190 'rc_this_oldid' => 0,
191 'rc_last_oldid' => 0,
193 'rc_cur_id' => $dbw->cascadingDeletes() ?
'page_id' : 'COALESCE(page_id, 0)',
194 'rc_log_type' => 'log_type',
195 'rc_log_action' => 'log_action',
196 'rc_logid' => 'log_id',
197 'rc_params' => 'log_params',
198 'rc_deleted' => 'log_deleted'
200 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
202 'log_type IN(' . implode( ',', $selectLogs ) . ')'
204 array(), // INSERT options
205 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
213 private function rebuildRecentChangesTablePass4() {
214 global $wgGroupPermissions, $wgUseRCPatrol;
216 $dbw = wfGetDB( DB_MASTER
);
218 list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
220 $botgroups = $autopatrolgroups = array();
221 foreach ( $wgGroupPermissions as $group => $rights ) {
222 if ( isset( $rights['bot'] ) && $rights['bot'] ) {
223 $botgroups[] = $dbw->addQuotes( $group );
225 if ( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] ) {
226 $autopatrolgroups[] = $dbw->addQuotes( $group );
229 # Flag our recent bot edits
230 if ( !empty( $botgroups ) ) {
231 $botwhere = implode( ',', $botgroups );
234 $this->output( "Flagging bot account edits...\n" );
236 # Find all users that are bots
237 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
238 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
239 $res = $dbw->query( $sql, DB_MASTER
);
241 foreach ( $res as $obj ) {
242 $botusers[] = $dbw->addQuotes( $obj->user_name
);
244 # Fill in the rc_bot field
245 if ( !empty( $botusers ) ) {
246 $botwhere = implode( ',', $botusers );
247 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
248 "WHERE rc_user_text IN($botwhere)";
249 $dbw->query( $sql2 );
253 # Flag our recent autopatrolled edits
254 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
255 $patrolwhere = implode( ',', $autopatrolgroups );
256 $patrolusers = array();
258 $this->output( "Flagging auto-patrolled edits...\n" );
260 # Find all users in RC with autopatrol rights
261 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
262 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
263 $res = $dbw->query( $sql, DB_MASTER
);
265 foreach ( $res as $obj ) {
266 $patrolusers[] = $dbw->addQuotes( $obj->user_name
);
269 # Fill in the rc_patrolled field
270 if ( !empty( $patrolusers ) ) {
271 $patrolwhere = implode( ',', $patrolusers );
272 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
273 "WHERE rc_user_text IN($patrolwhere)";
274 $dbw->query( $sql2 );
280 * Purge cached feeds in $messageMemc
282 private function purgeFeeds() {
283 global $wgFeedClasses, $messageMemc;
285 $this->output( "Deleting feed timestamps.\n" );
287 foreach ( $wgFeedClasses as $feed => $className ) {
288 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
294 $maintClass = "RebuildRecentchanges";
295 require_once( DO_MAINTENANCE
);