Merge "Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment"
[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 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Maintenance script that rebuilds recent changes from scratch.
31 *
32 * @ingroup Maintenance
33 */
34 class RebuildRecentchanges extends Maintenance {
35 /** @var int UNIX timestamp */
36 private $cutoffFrom;
37 /** @var int UNIX timestamp */
38 private $cutoffTo;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Rebuild recent changes' );
43
44 $this->addOption(
45 'from',
46 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
47 false,
48 true
49 );
50 $this->addOption(
51 'to',
52 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
53 false,
54 true
55 );
56 $this->setBatchSize( 200 );
57 }
58
59 public function execute() {
60 if (
61 ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
62 ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
63 ) {
64 $this->fatalError( "Both 'from' and 'to' must be given, or neither" );
65 }
66
67 $this->rebuildRecentChangesTablePass1();
68 $this->rebuildRecentChangesTablePass2();
69 $this->rebuildRecentChangesTablePass3();
70 $this->rebuildRecentChangesTablePass4();
71 $this->rebuildRecentChangesTablePass5();
72 if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
73 $this->purgeFeeds();
74 }
75 $this->output( "Done.\n" );
76 }
77
78 /**
79 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
80 */
81 private function rebuildRecentChangesTablePass1() {
82 $dbw = $this->getDB( DB_MASTER );
83 $commentStore = CommentStore::getStore();
84
85 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
86 $this->cutoffFrom = wfTimestamp( TS_UNIX, $this->getOption( 'from' ) );
87 $this->cutoffTo = wfTimestamp( TS_UNIX, $this->getOption( 'to' ) );
88
89 $sec = $this->cutoffTo - $this->cutoffFrom;
90 $days = $sec / 24 / 3600;
91 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
92 } else {
93 global $wgRCMaxAge;
94
95 $days = $wgRCMaxAge / 24 / 3600;
96 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
97
98 $this->cutoffFrom = time() - $wgRCMaxAge;
99 $this->cutoffTo = time();
100 }
101
102 $this->output( "Clearing recentchanges table for time range...\n" );
103 $rcids = $dbw->selectFieldValues(
104 'recentchanges',
105 'rc_id',
106 [
107 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
108 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
109 ]
110 );
111 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
112 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__ );
113 wfGetLBFactory()->waitForReplication();
114 }
115
116 $this->output( "Loading from page and revision tables...\n" );
117
118 $commentQuery = $commentStore->getJoin( 'rev_comment' );
119 $res = $dbw->select(
120 [ 'revision', 'page' ] + $commentQuery['tables'],
121 [
122 'rev_timestamp',
123 'rev_user',
124 'rev_user_text',
125 'rev_minor_edit',
126 'rev_id',
127 'rev_deleted',
128 'page_namespace',
129 'page_title',
130 'page_is_new',
131 'page_id'
132 ] + $commentQuery['fields'],
133 [
134 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
135 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
136 ],
137 __METHOD__,
138 [ 'ORDER BY' => 'rev_timestamp DESC' ],
139 [
140 'page' => [ 'JOIN', 'rev_page=page_id' ],
141 ] + $commentQuery['joins']
142 );
143
144 $this->output( "Inserting from page and revision tables...\n" );
145 $inserted = 0;
146 foreach ( $res as $row ) {
147 $comment = $commentStore->getComment( 'rev_comment', $row );
148 $dbw->insert(
149 'recentchanges',
150 [
151 'rc_timestamp' => $row->rev_timestamp,
152 'rc_user' => $row->rev_user,
153 'rc_user_text' => $row->rev_user_text,
154 'rc_namespace' => $row->page_namespace,
155 'rc_title' => $row->page_title,
156 'rc_minor' => $row->rev_minor_edit,
157 'rc_bot' => 0,
158 'rc_new' => $row->page_is_new,
159 'rc_cur_id' => $row->page_id,
160 'rc_this_oldid' => $row->rev_id,
161 'rc_last_oldid' => 0, // is this ok?
162 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
163 'rc_source' => $row->page_is_new ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
164 'rc_deleted' => $row->rev_deleted
165 ] + $commentStore->insert( $dbw, 'rc_comment', $comment ),
166 __METHOD__
167 );
168 if ( ( ++$inserted % $this->getBatchSize() ) == 0 ) {
169 wfGetLBFactory()->waitForReplication();
170 }
171 }
172 }
173
174 /**
175 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
176 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
177 */
178 private function rebuildRecentChangesTablePass2() {
179 $dbw = $this->getDB( DB_MASTER );
180
181 $this->output( "Updating links and size differences...\n" );
182
183 # Fill in the rc_last_oldid field, which points to the previous edit
184 $res = $dbw->select(
185 'recentchanges',
186 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
187 [
188 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
189 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
190 ],
191 __METHOD__,
192 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
193 );
194
195 $lastCurId = 0;
196 $lastOldId = 0;
197 $lastSize = null;
198 $updated = 0;
199 foreach ( $res as $obj ) {
200 $new = 0;
201
202 if ( $obj->rc_cur_id != $lastCurId ) {
203 # Switch! Look up the previous last edit, if any
204 $lastCurId = intval( $obj->rc_cur_id );
205 $emit = $obj->rc_timestamp;
206
207 $row = $dbw->selectRow(
208 'revision',
209 [ 'rev_id', 'rev_len' ],
210 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
211 __METHOD__,
212 [ 'ORDER BY' => 'rev_timestamp DESC' ]
213 );
214 if ( $row ) {
215 $lastOldId = intval( $row->rev_id );
216 # Grab the last text size if available
217 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
218 } else {
219 # No previous edit
220 $lastOldId = 0;
221 $lastSize = null;
222 $new = 1; // probably true
223 }
224 }
225
226 if ( $lastCurId == 0 ) {
227 $this->output( "Uhhh, something wrong? No curid\n" );
228 } else {
229 # Grab the entry's text size
230 $size = (int)$dbw->selectField(
231 'revision',
232 'rev_len',
233 [ 'rev_id' => $obj->rc_this_oldid ],
234 __METHOD__
235 );
236
237 $dbw->update(
238 'recentchanges',
239 [
240 'rc_last_oldid' => $lastOldId,
241 'rc_new' => $new,
242 'rc_type' => $new ? RC_NEW : RC_EDIT,
243 'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
244 'rc_old_len' => $lastSize,
245 'rc_new_len' => $size,
246 ],
247 [
248 'rc_cur_id' => $lastCurId,
249 'rc_this_oldid' => $obj->rc_this_oldid,
250 'rc_timestamp' => $obj->rc_timestamp // index usage
251 ],
252 __METHOD__
253 );
254
255 $lastOldId = intval( $obj->rc_this_oldid );
256 $lastSize = $size;
257
258 if ( ( ++$updated % $this->getBatchSize() ) == 0 ) {
259 wfGetLBFactory()->waitForReplication();
260 }
261 }
262 }
263 }
264
265 /**
266 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
267 */
268 private function rebuildRecentChangesTablePass3() {
269 global $wgLogTypes, $wgLogRestrictions;
270
271 $dbw = $this->getDB( DB_MASTER );
272 $commentStore = CommentStore::getStore();
273
274 $this->output( "Loading from user, page, and logging tables...\n" );
275
276 $commentQuery = $commentStore->getJoin( 'log_comment' );
277 $res = $dbw->select(
278 [ 'user', 'logging', 'page' ] + $commentQuery['tables'],
279 [
280 'log_timestamp',
281 'log_user',
282 'user_name',
283 'log_namespace',
284 'log_title',
285 'page_id',
286 'log_type',
287 'log_action',
288 'log_id',
289 'log_params',
290 'log_deleted'
291 ] + $commentQuery['fields'],
292 [
293 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
294 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
295 'log_user=user_id',
296 // Some logs don't go in RC since they are private.
297 // @FIXME: core/extensions also have spammy logs that don't go in RC.
298 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
299 ],
300 __METHOD__,
301 [ 'ORDER BY' => 'log_timestamp DESC' ],
302 [
303 'page' =>
304 [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
305 ] + $commentQuery['joins']
306 );
307
308 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
309
310 $inserted = 0;
311 foreach ( $res as $row ) {
312 $comment = $commentStore->getComment( 'log_comment', $row );
313 $dbw->insert(
314 'recentchanges',
315 [
316 'rc_timestamp' => $row->log_timestamp,
317 'rc_user' => $row->log_user,
318 'rc_user_text' => $row->user_name,
319 'rc_namespace' => $row->log_namespace,
320 'rc_title' => $row->log_title,
321 'rc_minor' => 0,
322 'rc_bot' => 0,
323 'rc_patrolled' => 1,
324 'rc_new' => 0,
325 'rc_this_oldid' => 0,
326 'rc_last_oldid' => 0,
327 'rc_type' => RC_LOG,
328 'rc_source' => RecentChange::SRC_LOG,
329 'rc_cur_id' => $field->isNullable()
330 ? $row->page_id
331 : (int)$row->page_id, // NULL => 0,
332 'rc_log_type' => $row->log_type,
333 'rc_log_action' => $row->log_action,
334 'rc_logid' => $row->log_id,
335 'rc_params' => $row->log_params,
336 'rc_deleted' => $row->log_deleted
337 ] + $commentStore->insert( $dbw, 'rc_comment', $comment ),
338 __METHOD__
339 );
340
341 if ( ( ++$inserted % $this->getBatchSize() ) == 0 ) {
342 wfGetLBFactory()->waitForReplication();
343 }
344 }
345 }
346
347 /**
348 * Rebuild pass 4: Mark bot and autopatrolled entries.
349 */
350 private function rebuildRecentChangesTablePass4() {
351 global $wgUseRCPatrol, $wgMiserMode;
352
353 $dbw = $this->getDB( DB_MASTER );
354
355 list( $recentchanges, $usergroups, $user ) =
356 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
357
358 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
359 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
360 # may be lost at this point (aside from joining on the patrol log table entries).
361 $botgroups = [ 'bot' ];
362 $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : [];
363
364 # Flag our recent bot edits
365 if ( $botgroups ) {
366 $botwhere = $dbw->makeList( $botgroups );
367
368 $this->output( "Flagging bot account edits...\n" );
369
370 # Find all users that are bots
371 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
372 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
373 $res = $dbw->query( $sql, __METHOD__ );
374
375 $botusers = [];
376 foreach ( $res as $obj ) {
377 $botusers[] = $obj->user_name;
378 }
379
380 # Fill in the rc_bot field
381 if ( $botusers ) {
382 $rcids = $dbw->selectFieldValues(
383 'recentchanges',
384 'rc_id',
385 [
386 'rc_user_text' => $botusers,
387 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
388 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
389 ],
390 __METHOD__
391 );
392
393 foreach ( array_chunk( $rcids, $this->getBatchSize() ) as $rcidBatch ) {
394 $dbw->update(
395 'recentchanges',
396 [ 'rc_bot' => 1 ],
397 [ 'rc_id' => $rcidBatch ],
398 __METHOD__
399 );
400 wfGetLBFactory()->waitForReplication();
401 }
402 }
403 }
404
405 # Flag our recent autopatrolled edits
406 if ( !$wgMiserMode && $autopatrolgroups ) {
407 $patrolwhere = $dbw->makeList( $autopatrolgroups );
408 $patrolusers = [];
409
410 $this->output( "Flagging auto-patrolled edits...\n" );
411
412 # Find all users in RC with autopatrol rights
413 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
414 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
415 $res = $dbw->query( $sql, __METHOD__ );
416
417 foreach ( $res as $obj ) {
418 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
419 }
420
421 # Fill in the rc_patrolled field
422 if ( $patrolusers ) {
423 $patrolwhere = implode( ',', $patrolusers );
424 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
425 "WHERE rc_user_text IN($patrolwhere) " .
426 "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ) . ' ' .
427 "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) );
428 $dbw->query( $sql2 );
429 }
430 }
431 }
432
433 /**
434 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
435 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
436 */
437 private function rebuildRecentChangesTablePass5() {
438 $dbw = wfGetDB( DB_MASTER );
439
440 $this->output( "Removing duplicate revision and logging entries...\n" );
441
442 $res = $dbw->select(
443 [ 'logging', 'log_search' ],
444 [ 'ls_value', 'ls_log_id' ],
445 [
446 'ls_log_id = log_id',
447 'ls_field' => 'associated_rev_id',
448 'log_type' => 'upload',
449 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
450 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
451 ],
452 __METHOD__
453 );
454
455 $updates = 0;
456 foreach ( $res as $obj ) {
457 $rev_id = $obj->ls_value;
458 $log_id = $obj->ls_log_id;
459
460 // Mark the logging row as having an associated rev id
461 $dbw->update(
462 'recentchanges',
463 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
464 /*WHERE*/ [ 'rc_logid' => $log_id ],
465 __METHOD__
466 );
467
468 // Delete the revision row
469 $dbw->delete(
470 'recentchanges',
471 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
472 __METHOD__
473 );
474
475 if ( ( ++$updates % $this->getBatchSize() ) == 0 ) {
476 wfGetLBFactory()->waitForReplication();
477 }
478 }
479 }
480
481 /**
482 * Purge cached feeds in $wanCache
483 */
484 private function purgeFeeds() {
485 global $wgFeedClasses;
486
487 $this->output( "Deleting feed timestamps.\n" );
488
489 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
490 foreach ( $wgFeedClasses as $feed => $className ) {
491 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
492 }
493 }
494 }
495
496 $maintClass = RebuildRecentchanges::class;
497 require_once RUN_MAINTENANCE_IF_MAIN;