Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / rebuildrecentchanges.php
blob18348258b04a8fd79e6743d1bcca972d85ba7bd6
1 <?php
2 /**
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
21 * @file
22 * @ingroup Maintenance
23 * @todo Document
26 require_once __DIR__ . '/Maintenance.php';
28 /**
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();
44 $this->purgeFeeds();
45 $this->output( "Done.\n" );
48 /**
49 * Rebuild pass 1
50 * DOCUMENT ME!
52 private function rebuildRecentChangesTablePass1() {
53 $dbw = wfGetDB( DB_MASTER );
55 $dbw->delete( 'recentchanges', '*' );
57 $this->output( "Loading from page and revision tables...\n" );
59 global $wgRCMaxAge;
61 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
62 $days = $wgRCMaxAge / 24 / 3600;
63 if ( intval( $days ) == $days ) {
64 $this->output( " (" . $days . " days)\n" );
65 } else {
66 $this->output( " (approx. " . intval( $days ) . " days)\n" );
69 $cutoff = time() - $wgRCMaxAge;
70 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
71 array(
72 'rc_timestamp' => 'rev_timestamp',
73 'rc_cur_time' => 'rev_timestamp',
74 'rc_user' => 'rev_user',
75 'rc_user_text' => 'rev_user_text',
76 'rc_namespace' => 'page_namespace',
77 'rc_title' => 'page_title',
78 'rc_comment' => 'rev_comment',
79 'rc_minor' => 'rev_minor_edit',
80 'rc_bot' => 0,
81 'rc_new' => 'page_is_new',
82 'rc_cur_id' => 'page_id',
83 'rc_this_oldid' => 'rev_id',
84 'rc_last_oldid' => 0, // is this ok?
85 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
86 'rc_deleted' => 'rev_deleted'
87 ), array(
88 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
89 'rev_page=page_id'
90 ), __METHOD__,
91 array(), // INSERT options
92 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
96 /**
97 * Rebuild pass 2
98 * DOCUMENT ME!
100 private function rebuildRecentChangesTablePass2() {
101 $dbw = wfGetDB( DB_MASTER );
102 list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
104 $this->output( "Updating links and size differences...\n" );
106 # Fill in the rc_last_oldid field, which points to the previous edit
107 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
108 "ORDER BY rc_cur_id,rc_timestamp";
109 $res = $dbw->query( $sql, DB_MASTER );
111 $lastCurId = 0;
112 $lastOldId = 0;
113 foreach ( $res as $obj ) {
114 $new = 0;
115 if ( $obj->rc_cur_id != $lastCurId ) {
116 # Switch! Look up the previous last edit, if any
117 $lastCurId = intval( $obj->rc_cur_id );
118 $emit = $obj->rc_timestamp;
119 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
120 "WHERE rev_page={$lastCurId} " .
121 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
122 $sql2 = $dbw->limitResult( $sql2, 1, false );
123 $res2 = $dbw->query( $sql2 );
124 $row = $dbw->fetchObject( $res2 );
125 if ( $row ) {
126 $lastOldId = intval( $row->rev_id );
127 # Grab the last text size if available
128 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
129 } else {
130 # No previous edit
131 $lastOldId = 0;
132 $lastSize = null;
133 $new = 1; // probably true
136 if ( $lastCurId == 0 ) {
137 $this->output( "Uhhh, something wrong? No curid\n" );
138 } else {
139 # Grab the entry's text size
140 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
142 $dbw->update( 'recentchanges',
143 array(
144 'rc_last_oldid' => $lastOldId,
145 'rc_new' => $new,
146 'rc_type' => $new,
147 'rc_old_len' => $lastSize,
148 'rc_new_len' => $size,
149 ), array(
150 'rc_cur_id' => $lastCurId,
151 'rc_this_oldid' => $obj->rc_this_oldid,
153 __METHOD__
156 $lastOldId = intval( $obj->rc_this_oldid );
157 $lastSize = $size;
163 * Rebuild pass 3
164 * DOCUMENT ME!
166 private function rebuildRecentChangesTablePass3() {
167 $dbw = wfGetDB( DB_MASTER );
169 $this->output( "Loading from user, page, and logging tables...\n" );
171 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
172 // Some logs don't go in RC. This should check for that
173 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
175 // Escape...blah blah
176 $selectLogs = array();
177 foreach ( $basicRCLogs as $logtype ) {
178 $safetype = $dbw->strencode( $logtype );
179 $selectLogs[] = "'$safetype'";
182 $cutoff = time() - $wgRCMaxAge;
183 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
184 $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
185 array(
186 'rc_timestamp' => 'log_timestamp',
187 'rc_cur_time' => 'log_timestamp',
188 'rc_user' => 'log_user',
189 'rc_user_text' => 'user_name',
190 'rc_namespace' => 'log_namespace',
191 'rc_title' => 'log_title',
192 'rc_comment' => 'log_comment',
193 'rc_minor' => 0,
194 'rc_bot' => 0,
195 'rc_patrolled' => 1,
196 'rc_new' => 0,
197 'rc_this_oldid' => 0,
198 'rc_last_oldid' => 0,
199 'rc_type' => RC_LOG,
200 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
201 'rc_log_type' => 'log_type',
202 'rc_log_action' => 'log_action',
203 'rc_logid' => 'log_id',
204 'rc_params' => 'log_params',
205 'rc_deleted' => 'log_deleted'
206 ), array(
207 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
208 'log_user=user_id',
209 'log_type IN(' . implode( ',', $selectLogs ) . ')'
210 ), __METHOD__,
211 array(), // INSERT options
212 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
217 * Rebuild pass 4
218 * DOCUMENT ME!
220 private function rebuildRecentChangesTablePass4() {
221 global $wgUseRCPatrol;
223 $dbw = wfGetDB( DB_MASTER );
225 list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
227 $botgroups = User::getGroupsWithPermission( 'bot' );
228 $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
229 # Flag our recent bot edits
230 if ( !empty( $botgroups ) ) {
231 $botwhere = $dbw->makeList( $botgroups );
232 $botusers = array();
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 );
252 global $wgMiserMode;
253 # Flag our recent autopatrolled edits
254 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
255 $patrolwhere = $dbw->makeList( $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 RUN_MAINTENANCE_IF_MAIN;