Merge "resourceloader: Throw exception when config serialization fails"
[lhc/web/wiklou.git] / maintenance / migrateActors.php
1 <?php
2 /**
3 * Migrate actors from pre-1.31 columns to the 'actor' table
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 use Wikimedia\Rdbms\IDatabase;
25
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script that migrates actors from pre-1.31 columns to the
30 * 'actor' table
31 *
32 * @ingroup Maintenance
33 */
34 class MigrateActors extends LoggedUpdateMaintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Migrates actors from pre-1.31 columns to the \'actor\' table' );
38 $this->setBatchSize( 100 );
39 }
40
41 protected function getUpdateKey() {
42 return __CLASS__;
43 }
44
45 protected function doDBUpdates() {
46 global $wgActorTableSchemaMigrationStage;
47
48 if ( !( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) ) {
49 $this->output(
50 "...cannot update while \$wgActorTableSchemaMigrationStage lacks SCHEMA_COMPAT_WRITE_NEW\n"
51 );
52 return false;
53 }
54
55 $this->output( "Creating actor entries for all registered users\n" );
56 $end = 0;
57 $dbw = $this->getDB( DB_MASTER );
58 $max = $dbw->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
59 $count = 0;
60 while ( $end < $max ) {
61 $start = $end + 1;
62 $end = min( $start + $this->mBatchSize, $max );
63 $this->output( "... $start - $end\n" );
64 $dbw->insertSelect(
65 'actor',
66 'user',
67 [ 'actor_user' => 'user_id', 'actor_name' => 'user_name' ],
68 [ "user_id >= $start", "user_id <= $end" ],
69 __METHOD__,
70 [ 'IGNORE' ],
71 [ 'ORDER BY' => [ 'user_id' ] ]
72 );
73 $count += $dbw->affectedRows();
74 wfWaitForSlaves();
75 }
76 $this->output( "Completed actor creation, added $count new actor(s)\n" );
77
78 $errors = 0;
79 $errors += $this->migrateToTemp(
80 'revision', 'rev_id', [ 'revactor_timestamp' => 'rev_timestamp', 'revactor_page' => 'rev_page' ],
81 'rev_user', 'rev_user_text', 'revactor_rev', 'revactor_actor'
82 );
83 $errors += $this->migrate( 'archive', 'ar_id', 'ar_user', 'ar_user_text', 'ar_actor' );
84 $errors += $this->migrate( 'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text', 'ipb_by_actor' );
85 $errors += $this->migrate( 'image', 'img_name', 'img_user', 'img_user_text', 'img_actor' );
86 $errors += $this->migrate(
87 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text', 'oi_actor'
88 );
89 $errors += $this->migrate( 'filearchive', 'fa_id', 'fa_user', 'fa_user_text', 'fa_actor' );
90 $errors += $this->migrate( 'recentchanges', 'rc_id', 'rc_user', 'rc_user_text', 'rc_actor' );
91 $errors += $this->migrate( 'logging', 'log_id', 'log_user', 'log_user_text', 'log_actor' );
92
93 $errors += $this->migrateLogSearch();
94
95 return $errors === 0;
96 }
97
98 /**
99 * Calculate a "next" condition and a display string
100 * @param IDatabase $dbw
101 * @param string[] $primaryKey Primary key of the table.
102 * @param object $row Database row
103 * @return array [ string $next, string $display ]
104 */
105 private function makeNextCond( $dbw, $primaryKey, $row ) {
106 $next = '';
107 $display = [];
108 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
109 $field = $primaryKey[$i];
110 $display[] = $field . '=' . $row->$field;
111 $value = $dbw->addQuotes( $row->$field );
112 if ( $next === '' ) {
113 $next = "$field > $value";
114 } else {
115 $next = "$field > $value OR $field = $value AND ($next)";
116 }
117 }
118 $display = implode( ' ', array_reverse( $display ) );
119 return [ $next, $display ];
120 }
121
122 /**
123 * Add actors for anons in a set of rows
124 * @param IDatabase $dbw
125 * @param string $nameField
126 * @param object[] &$rows
127 * @param array &$complainedAboutUsers
128 * @param int &$countErrors
129 * @return int Count of actors inserted
130 */
131 private function addActorsForRows(
132 IDatabase $dbw, $nameField, array &$rows, array &$complainedAboutUsers, &$countErrors
133 ) {
134 $needActors = [];
135 $countActors = 0;
136
137 $keep = [];
138 foreach ( $rows as $index => $row ) {
139 $keep[$index] = true;
140 if ( $row->actor_id === null ) {
141 // All registered users should have an actor_id already. So
142 // if we have a usable name here, it means they didn't run
143 // maintenance/cleanupUsersWithNoId.php
144 $name = $row->$nameField;
145 if ( User::isUsableName( $name ) ) {
146 if ( !isset( $complainedAboutUsers[$name] ) ) {
147 $complainedAboutUsers[$name] = true;
148 $this->error(
149 "User name \"$name\" is usable, cannot create an anonymous actor for it."
150 . " Run maintenance/cleanupUsersWithNoId.php to fix this situation.\n"
151 );
152 }
153 unset( $keep[$index] );
154 $countErrors++;
155 } else {
156 $needActors[$name] = 0;
157 }
158 }
159 }
160 $rows = array_intersect_key( $rows, $keep );
161
162 if ( $needActors ) {
163 $dbw->insert(
164 'actor',
165 array_map( function ( $v ) {
166 return [
167 'actor_name' => $v,
168 ];
169 }, array_keys( $needActors ) ),
170 __METHOD__
171 );
172 $countActors += $dbw->affectedRows();
173
174 $res = $dbw->select(
175 'actor',
176 [ 'actor_id', 'actor_name' ],
177 [ 'actor_name' => array_keys( $needActors ) ],
178 __METHOD__
179 );
180 foreach ( $res as $row ) {
181 $needActors[$row->actor_name] = $row->actor_id;
182 }
183 foreach ( $rows as $row ) {
184 if ( $row->actor_id === null ) {
185 $row->actor_id = $needActors[$row->$nameField];
186 }
187 }
188 }
189
190 return $countActors;
191 }
192
193 /**
194 * Migrate actors in a table.
195 *
196 * Assumes any row with the actor field non-zero have already been migrated.
197 * Blanks the name field when migrating.
198 *
199 * @param string $table Table to migrate
200 * @param string|string[] $primaryKey Primary key of the table.
201 * @param string $userField User ID field name
202 * @param string $nameField User name field name
203 * @param string $actorField Actor field name
204 * @return int Number of errors
205 */
206 protected function migrate( $table, $primaryKey, $userField, $nameField, $actorField ) {
207 $complainedAboutUsers = [];
208
209 $primaryKey = (array)$primaryKey;
210 $pkFilter = array_flip( $primaryKey );
211 $this->output(
212 "Beginning migration of $table.$userField and $table.$nameField to $table.$actorField\n"
213 );
214 wfWaitForSlaves();
215
216 $dbw = $this->getDB( DB_MASTER );
217 $next = '1=1';
218 $countUpdated = 0;
219 $countActors = 0;
220 $countErrors = 0;
221 while ( true ) {
222 // Fetch the rows needing update
223 $res = $dbw->select(
224 [ $table, 'actor' ],
225 array_merge( $primaryKey, [ $userField, $nameField, 'actor_id' ] ),
226 [
227 $actorField => 0,
228 $next,
229 ],
230 __METHOD__,
231 [
232 'ORDER BY' => $primaryKey,
233 'LIMIT' => $this->mBatchSize,
234 ],
235 [
236 'actor' => [
237 'LEFT JOIN',
238 "$userField != 0 AND actor_user = $userField OR "
239 . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
240 ]
241 ]
242 );
243 if ( !$res->numRows() ) {
244 break;
245 }
246
247 // Insert new actors for rows that need one
248 $rows = iterator_to_array( $res );
249 $lastRow = end( $rows );
250 $countActors += $this->addActorsForRows(
251 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
252 );
253
254 // Update the existing rows
255 foreach ( $rows as $row ) {
256 if ( !$row->actor_id ) {
257 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
258 $this->error(
259 "Could not make actor for row with $display "
260 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
261 );
262 $countErrors++;
263 continue;
264 }
265 $dbw->update(
266 $table,
267 [
268 $actorField => $row->actor_id,
269 ],
270 array_intersect_key( (array)$row, $pkFilter ) + [
271 $actorField => 0
272 ],
273 __METHOD__
274 );
275 $countUpdated += $dbw->affectedRows();
276 }
277
278 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
279 $this->output( "... $display\n" );
280 wfWaitForSlaves();
281 }
282
283 $this->output(
284 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
285 . "$countErrors error(s)\n"
286 );
287 return $countErrors;
288 }
289
290 /**
291 * Migrate actors in a table to a temporary table.
292 *
293 * Assumes the new table is named "{$table}_actor_temp", and it has two
294 * columns, in order, being the primary key of the original table and the
295 * actor ID field.
296 * Blanks the name field when migrating.
297 *
298 * @param string $table Table to migrate
299 * @param string $primaryKey Primary key of the table.
300 * @param array $extra Extra fields to copy
301 * @param string $userField User ID field name
302 * @param string $nameField User name field name
303 * @param string $newPrimaryKey Primary key of the new table.
304 * @param string $actorField Actor field name
305 */
306 protected function migrateToTemp(
307 $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
308 ) {
309 $complainedAboutUsers = [];
310
311 $newTable = $table . '_actor_temp';
312 $this->output(
313 "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
314 );
315 wfWaitForSlaves();
316
317 $dbw = $this->getDB( DB_MASTER );
318 $next = [];
319 $countUpdated = 0;
320 $countActors = 0;
321 $countErrors = 0;
322 while ( true ) {
323 // Fetch the rows needing update
324 $res = $dbw->select(
325 [ $table, $newTable, 'actor' ],
326 [ $primaryKey, $userField, $nameField, 'actor_id' ] + $extra,
327 [ $newPrimaryKey => null ] + $next,
328 __METHOD__,
329 [
330 'ORDER BY' => $primaryKey,
331 'LIMIT' => $this->mBatchSize,
332 ],
333 [
334 $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ],
335 'actor' => [
336 'LEFT JOIN',
337 "$userField != 0 AND actor_user = $userField OR "
338 . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
339 ]
340 ]
341 );
342 if ( !$res->numRows() ) {
343 break;
344 }
345
346 // Insert new actors for rows that need one
347 $rows = iterator_to_array( $res );
348 $lastRow = end( $rows );
349 $countActors += $this->addActorsForRows(
350 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
351 );
352
353 // Update rows
354 if ( $rows ) {
355 $inserts = [];
356 $updates = [];
357 foreach ( $rows as $row ) {
358 if ( !$row->actor_id ) {
359 list( , $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $row );
360 $this->error(
361 "Could not make actor for row with $display "
362 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
363 );
364 $countErrors++;
365 continue;
366 }
367 $ins = [
368 $newPrimaryKey => $row->$primaryKey,
369 $actorField => $row->actor_id,
370 ];
371 foreach ( $extra as $to => $from ) {
372 $ins[$to] = $row->$to; // It's aliased
373 }
374 $inserts[] = $ins;
375 $updates[] = $row->$primaryKey;
376 }
377 $this->beginTransaction( $dbw, __METHOD__ );
378 $dbw->insert( $newTable, $inserts, __METHOD__ );
379 $countUpdated += $dbw->affectedRows();
380 $this->commitTransaction( $dbw, __METHOD__ );
381 }
382
383 // Calculate the "next" condition
384 list( $n, $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $lastRow );
385 $next = [ $n ];
386 $this->output( "... $display\n" );
387 }
388
389 $this->output(
390 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
391 . "$countErrors error(s)\n"
392 );
393 return $countErrors;
394 }
395
396 /**
397 * Migrate actors in the log_search table.
398 * @return int Number of errors
399 */
400 protected function migrateLogSearch() {
401 $complainedAboutUsers = [];
402
403 $primaryKey = [ 'ls_field', 'ls_value' ];
404 $pkFilter = array_flip( $primaryKey );
405 $this->output( "Beginning migration of log_search\n" );
406 wfWaitForSlaves();
407
408 $dbw = $this->getDB( DB_MASTER );
409 $countUpdated = 0;
410 $countActors = 0;
411 $countErrors = 0;
412
413 $next = '1=1';
414 while ( true ) {
415 // Fetch the rows needing update
416 $res = $dbw->select(
417 [ 'log_search', 'actor' ],
418 [ 'ls_field', 'ls_value', 'actor_id' ],
419 [
420 'ls_field' => 'target_author_id',
421 $next,
422 ],
423 __METHOD__,
424 [
425 'DISTINCT',
426 'ORDER BY' => [ 'ls_value' ],
427 'LIMIT' => $this->mBatchSize,
428 ],
429 [ 'actor' => [ 'LEFT JOIN', 'ls_value = ' . $dbw->buildStringCast( 'actor_user' ) ] ]
430 );
431 if ( !$res->numRows() ) {
432 break;
433 }
434
435 // Update the rows
436 $del = [];
437 foreach ( $res as $row ) {
438 $lastRow = $row;
439 if ( !$row->actor_id ) {
440 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
441 $this->error( "No actor for row with $display\n" );
442 $countErrors++;
443 continue;
444 }
445 $dbw->update(
446 'log_search',
447 [
448 'ls_field' => 'target_author_actor',
449 'ls_value' => $row->actor_id,
450 ],
451 [
452 'ls_field' => $row->ls_field,
453 'ls_value' => $row->ls_value,
454 ],
455 __METHOD__,
456 [ 'IGNORE' ]
457 );
458 $countUpdated += $dbw->affectedRows();
459 $del[] = $row->ls_value;
460 }
461 if ( $del ) {
462 $dbw->delete(
463 'log_search', [ 'ls_field' => 'target_author_id', 'ls_value' => $del ], __METHOD__
464 );
465 $countUpdated += $dbw->affectedRows();
466 }
467
468 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
469 $this->output( "... $display\n" );
470 wfWaitForSlaves();
471 }
472
473 $next = '1=1';
474 while ( true ) {
475 // Fetch the rows needing update
476 $res = $dbw->select(
477 [ 'log_search', 'actor' ],
478 [ 'ls_field', 'ls_value', 'actor_id' ],
479 [
480 'ls_field' => 'target_author_ip',
481 $next,
482 ],
483 __METHOD__,
484 [
485 'DISTINCT',
486 'ORDER BY' => [ 'ls_value' ],
487 'LIMIT' => $this->mBatchSize,
488 ],
489 [ 'actor' => [ 'LEFT JOIN', 'ls_value = actor_name' ] ]
490 );
491 if ( !$res->numRows() ) {
492 break;
493 }
494
495 // Insert new actors for rows that need one
496 $rows = iterator_to_array( $res );
497 $lastRow = end( $rows );
498 $countActors += $this->addActorsForRows(
499 $dbw, 'ls_value', $rows, $complainedAboutUsers, $countErrors
500 );
501
502 // Update the rows
503 $del = [];
504 foreach ( $rows as $row ) {
505 if ( !$row->actor_id ) {
506 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
507 $this->error( "Could not make actor for row with $display\n" );
508 $countErrors++;
509 continue;
510 }
511 $dbw->update(
512 'log_search',
513 [
514 'ls_field' => 'target_author_actor',
515 'ls_value' => $row->actor_id,
516 ],
517 [
518 'ls_field' => $row->ls_field,
519 'ls_value' => $row->ls_value,
520 ],
521 __METHOD__,
522 [ 'IGNORE' ]
523 );
524 $countUpdated += $dbw->affectedRows();
525 $del[] = $row->ls_value;
526 }
527 if ( $del ) {
528 $dbw->delete(
529 'log_search', [ 'ls_field' => 'target_author_ip', 'ls_value' => $del ], __METHOD__
530 );
531 $countUpdated += $dbw->affectedRows();
532 }
533
534 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
535 $this->output( "... $display\n" );
536 wfWaitForSlaves();
537 }
538
539 $this->output(
540 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
541 . "$countErrors error(s)\n"
542 );
543 return $countErrors;
544 }
545 }
546
547 $maintClass = "MigrateActors";
548 require_once RUN_MAINTENANCE_IF_MAIN;