Merge "Add 3D filetype for STL files"
[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 integer UNIX timestamp */
36 private $cutoffFrom;
37 /** @var integer 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->error( "Both 'from' and 'to' must be given, or neither", 1 );
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
84 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
85 $this->cutoffFrom = wfTimestamp( TS_UNIX, $this->getOption( 'from' ) );
86 $this->cutoffTo = wfTimestamp( TS_UNIX, $this->getOption( 'to' ) );
87
88 $sec = $this->cutoffTo - $this->cutoffFrom;
89 $days = $sec / 24 / 3600;
90 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
91 } else {
92 global $wgRCMaxAge;
93
94 $days = $wgRCMaxAge / 24 / 3600;
95 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
96
97 $this->cutoffFrom = time() - $wgRCMaxAge;
98 $this->cutoffTo = time();
99 }
100
101 $this->output( "Clearing recentchanges table for time range...\n" );
102 $rcids = $dbw->selectFieldValues(
103 'recentchanges',
104 'rc_id',
105 [
106 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
107 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
108 ]
109 );
110 foreach ( array_chunk( $rcids, $this->mBatchSize ) as $rcidBatch ) {
111 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__ );
112 wfGetLBFactory()->waitForReplication();
113 }
114
115 $this->output( "Loading from page and revision tables...\n" );
116 $res = $dbw->select(
117 [ 'page', 'revision' ],
118 [
119 'rev_timestamp',
120 'rev_user',
121 'rev_user_text',
122 'rev_comment',
123 'rev_minor_edit',
124 'rev_id',
125 'rev_deleted',
126 'page_namespace',
127 'page_title',
128 'page_is_new',
129 'page_id'
130 ],
131 [
132 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
133 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
134 'rev_page=page_id'
135 ],
136 __METHOD__,
137 [ 'ORDER BY' => 'rev_timestamp DESC' ]
138 );
139
140 $this->output( "Inserting from page and revision tables...\n" );
141 $inserted = 0;
142 foreach ( $res as $row ) {
143 $dbw->insert(
144 'recentchanges',
145 [
146 'rc_timestamp' => $row->rev_timestamp,
147 'rc_user' => $row->rev_user,
148 'rc_user_text' => $row->rev_user_text,
149 'rc_namespace' => $row->page_namespace,
150 'rc_title' => $row->page_title,
151 'rc_comment' => $row->rev_comment,
152 'rc_minor' => $row->rev_minor_edit,
153 'rc_bot' => 0,
154 'rc_new' => $row->page_is_new,
155 'rc_cur_id' => $row->page_id,
156 'rc_this_oldid' => $row->rev_id,
157 'rc_last_oldid' => 0, // is this ok?
158 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
159 'rc_source' => $row->page_is_new
160 ? $dbw->addQuotes( RecentChange::SRC_NEW )
161 : $dbw->addQuotes( RecentChange::SRC_EDIT )
162 ,
163 'rc_deleted' => $row->rev_deleted
164 ],
165 __METHOD__
166 );
167 if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
168 wfGetLBFactory()->waitForReplication();
169 }
170 }
171 }
172
173 /**
174 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
175 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
176 */
177 private function rebuildRecentChangesTablePass2() {
178 $dbw = $this->getDB( DB_MASTER );
179
180 $this->output( "Updating links and size differences...\n" );
181
182 # Fill in the rc_last_oldid field, which points to the previous edit
183 $res = $dbw->select(
184 'recentchanges',
185 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
186 [
187 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
188 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
189 ],
190 __METHOD__,
191 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
192 );
193
194 $lastCurId = 0;
195 $lastOldId = 0;
196 $lastSize = null;
197 $updated = 0;
198 foreach ( $res as $obj ) {
199 $new = 0;
200
201 if ( $obj->rc_cur_id != $lastCurId ) {
202 # Switch! Look up the previous last edit, if any
203 $lastCurId = intval( $obj->rc_cur_id );
204 $emit = $obj->rc_timestamp;
205
206 $row = $dbw->selectRow(
207 'revision',
208 [ 'rev_id', 'rev_len' ],
209 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
210 __METHOD__,
211 [ 'ORDER BY' => 'rev_timestamp DESC' ]
212 );
213 if ( $row ) {
214 $lastOldId = intval( $row->rev_id );
215 # Grab the last text size if available
216 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
217 } else {
218 # No previous edit
219 $lastOldId = 0;
220 $lastSize = null;
221 $new = 1; // probably true
222 }
223 }
224
225 if ( $lastCurId == 0 ) {
226 $this->output( "Uhhh, something wrong? No curid\n" );
227 } else {
228 # Grab the entry's text size
229 $size = (int)$dbw->selectField(
230 'revision',
231 'rev_len',
232 [ 'rev_id' => $obj->rc_this_oldid ],
233 __METHOD__
234 );
235
236 $dbw->update(
237 'recentchanges',
238 [
239 'rc_last_oldid' => $lastOldId,
240 'rc_new' => $new,
241 'rc_type' => $new ? RC_NEW : RC_EDIT,
242 'rc_source' => $new === 1
243 ? $dbw->addQuotes( RecentChange::SRC_NEW )
244 : $dbw->addQuotes( RecentChange::SRC_EDIT ),
245 'rc_old_len' => $lastSize,
246 'rc_new_len' => $size,
247 ],
248 [
249 'rc_cur_id' => $lastCurId,
250 'rc_this_oldid' => $obj->rc_this_oldid,
251 'rc_timestamp' => $obj->rc_timestamp // index usage
252 ],
253 __METHOD__
254 );
255
256 $lastOldId = intval( $obj->rc_this_oldid );
257 $lastSize = $size;
258
259 if ( ( ++$updated % $this->mBatchSize ) == 0 ) {
260 wfGetLBFactory()->waitForReplication();
261 }
262 }
263 }
264 }
265
266 /**
267 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
268 */
269 private function rebuildRecentChangesTablePass3() {
270 global $wgLogTypes, $wgLogRestrictions;
271
272 $dbw = $this->getDB( DB_MASTER );
273
274 $this->output( "Loading from user, page, and logging tables...\n" );
275
276 $res = $dbw->select(
277 [ 'user', 'logging', 'page' ],
278 [
279 'log_timestamp',
280 'log_user',
281 'user_name',
282 'log_namespace',
283 'log_title',
284 'log_comment',
285 'page_id',
286 'log_type',
287 'log_action',
288 'log_id',
289 'log_params',
290 'log_deleted'
291 ],
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 ]
306 );
307
308 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
309
310 $inserted = 0;
311 foreach ( $res as $row ) {
312 $dbw->insert(
313 'recentchanges',
314 [
315 'rc_timestamp' => $row->log_timestamp,
316 'rc_user' => $row->log_user,
317 'rc_user_text' => $row->user_name,
318 'rc_namespace' => $row->log_namespace,
319 'rc_title' => $row->log_title,
320 'rc_comment' => $row->log_comment,
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' => $dbw->addQuotes( 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 ],
338 __METHOD__
339 );
340
341 if ( ( ++$inserted % $this->mBatchSize ) == 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->mBatchSize ) 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->mBatchSize ) == 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";
497 require_once RUN_MAINTENANCE_IF_MAIN;