Merge "mediawiki.userSuggest: Use formatversion=2 for API request"
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.php
1 <?php
2 /**
3 * Rebuild recent changes from scratch. This takes several hours,
4 * depending on the database size and server configuration.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 * @todo Document
24 */
25
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script that rebuilds recent changes from scratch.
30 *
31 * @ingroup Maintenance
32 */
33 class RebuildRecentchanges extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Rebuild recent changes' );
37 }
38
39 public function execute() {
40 $this->rebuildRecentChangesTablePass1();
41 $this->rebuildRecentChangesTablePass2();
42 $this->rebuildRecentChangesTablePass3();
43 $this->rebuildRecentChangesTablePass4();
44 $this->rebuildRecentChangesTablePass5();
45 $this->purgeFeeds();
46 $this->output( "Done.\n" );
47 }
48
49 /**
50 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
51 */
52 private function rebuildRecentChangesTablePass1() {
53 $dbw = $this->getDB( DB_MASTER );
54
55 $dbw->delete( 'recentchanges', '*' );
56
57 $this->output( "Loading from page and revision tables...\n" );
58
59 global $wgRCMaxAge;
60
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" );
67 }
68
69 $cutoff = time() - $wgRCMaxAge;
70 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
71 array(
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',
79 'rc_bot' => 0,
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(
86 'page_is_new != 0',
87 $dbw->addQuotes( RecentChange::SRC_NEW ),
88 $dbw->addQuotes( RecentChange::SRC_EDIT )
89 ),
90 'rc_deleted' => 'rev_deleted'
91 ),
92 array(
93 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
94 'rev_page=page_id'
95 ),
96 __METHOD__,
97 array(), // INSERT options
98 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
99 );
100 }
101
102 /**
103 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
104 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
105 */
106 private function rebuildRecentChangesTablePass2() {
107 $dbw = $this->getDB( DB_MASTER );
108 list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
109
110 $this->output( "Updating links and size differences...\n" );
111
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 );
116
117 $lastCurId = 0;
118 $lastOldId = 0;
119 foreach ( $res as $obj ) {
120 $new = 0;
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 );
131 if ( $row ) {
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;
135 } else {
136 # No previous edit
137 $lastOldId = 0;
138 $lastSize = null;
139 $new = 1; // probably true
140 }
141 }
142 if ( $lastCurId == 0 ) {
143 $this->output( "Uhhh, something wrong? No curid\n" );
144 } else {
145 # Grab the entry's text size
146 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
147
148 $dbw->update( 'recentchanges',
149 array(
150 'rc_last_oldid' => $lastOldId,
151 'rc_new' => $new,
152 'rc_type' => $new,
153 'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
154 'rc_old_len' => $lastSize,
155 'rc_new_len' => $size,
156 ), array(
157 'rc_cur_id' => $lastCurId,
158 'rc_this_oldid' => $obj->rc_this_oldid,
159 ),
160 __METHOD__
161 );
162
163 $lastOldId = intval( $obj->rc_this_oldid );
164 $lastSize = $size;
165 }
166 }
167 }
168
169 /**
170 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
171 */
172 private function rebuildRecentChangesTablePass3() {
173 $dbw = $this->getDB( DB_MASTER );
174
175 $this->output( "Loading from user, page, and logging tables...\n" );
176
177 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
178 // Some logs don't go in RC. This should check for that
179 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
180
181 $cutoff = time() - $wgRCMaxAge;
182 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
183 $dbw->insertSelect(
184 'recentchanges',
185 array(
186 'user',
187 "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)"
188 ),
189 array(
190 'rc_timestamp' => 'log_timestamp',
191 'rc_user' => 'log_user',
192 'rc_user_text' => 'user_name',
193 'rc_namespace' => 'log_namespace',
194 'rc_title' => 'log_title',
195 'rc_comment' => 'log_comment',
196 'rc_minor' => 0,
197 'rc_bot' => 0,
198 'rc_patrolled' => 1,
199 'rc_new' => 0,
200 'rc_this_oldid' => 0,
201 'rc_last_oldid' => 0,
202 'rc_type' => RC_LOG,
203 'rc_source' => $dbw->addQuotes( RecentChange::SRC_LOG ),
204 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
205 'rc_log_type' => 'log_type',
206 'rc_log_action' => 'log_action',
207 'rc_logid' => 'log_id',
208 'rc_params' => 'log_params',
209 'rc_deleted' => 'log_deleted'
210 ),
211 array(
212 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
213 'log_user=user_id',
214 'log_type' => $basicRCLogs,
215 ),
216 __METHOD__,
217 array(), // INSERT options
218 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
219 );
220 }
221
222 /**
223 * Rebuild pass 4: Mark bot and autopatrolled entries.
224 */
225 private function rebuildRecentChangesTablePass4() {
226 global $wgUseRCPatrol;
227
228 $dbw = $this->getDB( DB_MASTER );
229
230 list( $recentchanges, $usergroups, $user ) =
231 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
232
233 $botgroups = User::getGroupsWithPermission( 'bot' );
234 $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
235 # Flag our recent bot edits
236 if ( !empty( $botgroups ) ) {
237 $botwhere = $dbw->makeList( $botgroups );
238 $botusers = array();
239
240 $this->output( "Flagging bot account edits...\n" );
241
242 # Find all users that are bots
243 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
244 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
245 $res = $dbw->query( $sql, DB_MASTER );
246
247 foreach ( $res as $obj ) {
248 $botusers[] = $dbw->addQuotes( $obj->user_name );
249 }
250 # Fill in the rc_bot field
251 if ( !empty( $botusers ) ) {
252 $botwhere = implode( ',', $botusers );
253 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
254 "WHERE rc_user_text IN($botwhere)";
255 $dbw->query( $sql2 );
256 }
257 }
258 global $wgMiserMode;
259 # Flag our recent autopatrolled edits
260 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
261 $patrolwhere = $dbw->makeList( $autopatrolgroups );
262 $patrolusers = array();
263
264 $this->output( "Flagging auto-patrolled edits...\n" );
265
266 # Find all users in RC with autopatrol rights
267 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
268 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
269 $res = $dbw->query( $sql, DB_MASTER );
270
271 foreach ( $res as $obj ) {
272 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
273 }
274
275 # Fill in the rc_patrolled field
276 if ( !empty( $patrolusers ) ) {
277 $patrolwhere = implode( ',', $patrolusers );
278 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
279 "WHERE rc_user_text IN($patrolwhere)";
280 $dbw->query( $sql2 );
281 }
282 }
283 }
284
285 /**
286 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
287 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
288 */
289 private function rebuildRecentChangesTablePass5() {
290 $dbw = wfGetDB( DB_MASTER );
291
292 $this->output( "Removing duplicate revision and logging entries...\n" );
293
294 $res = $dbw->select(
295 array( 'logging', 'log_search' ),
296 array( 'ls_value', 'ls_log_id' ),
297 array(
298 'ls_log_id = log_id',
299 'ls_field' => 'associated_rev_id',
300 'log_type' => 'upload',
301 ),
302 __METHOD__
303 );
304 foreach ( $res as $obj ) {
305 $rev_id = $obj->ls_value;
306 $log_id = $obj->ls_log_id;
307
308 // Mark the logging row as having an associated rev id
309 $dbw->update(
310 'recentchanges',
311 /*SET*/ array( 'rc_this_oldid' => $rev_id ),
312 /*WHERE*/ array( 'rc_logid' => $log_id ),
313 __METHOD__
314 );
315
316 // Delete the revision row
317 $dbw->delete(
318 'recentchanges',
319 /*WHERE*/ array( 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ),
320 __METHOD__
321 );
322 }
323 }
324
325 /**
326 * Purge cached feeds in $messageMemc
327 */
328 private function purgeFeeds() {
329 global $wgFeedClasses, $messageMemc;
330
331 $this->output( "Deleting feed timestamps.\n" );
332
333 foreach ( $wgFeedClasses as $feed => $className ) {
334 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
335 }
336 }
337 }
338
339 $maintClass = "RebuildRecentchanges";
340 require_once RUN_MAINTENANCE_IF_MAIN;