Merge "Add support for 'hu-formal'"
[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 < MIGRATION_WRITE_NEW ) {
49 $this->output(
50 "...cannot update while \$wgActorTableSchemaMigrationStage < MIGRATION_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 $nameField => '',
270 ],
271 array_intersect_key( (array)$row, $pkFilter ) + [
272 $actorField => 0
273 ],
274 __METHOD__
275 );
276 $countUpdated += $dbw->affectedRows();
277 }
278
279 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
280 $this->output( "... $display\n" );
281 wfWaitForSlaves();
282 }
283
284 $this->output(
285 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
286 . "$countErrors error(s)\n"
287 );
288 return $countErrors;
289 }
290
291 /**
292 * Migrate actors in a table to a temporary table.
293 *
294 * Assumes the new table is named "{$table}_actor_temp", and it has two
295 * columns, in order, being the primary key of the original table and the
296 * actor ID field.
297 * Blanks the name field when migrating.
298 *
299 * @param string $table Table to migrate
300 * @param string $primaryKey Primary key of the table.
301 * @param array $extra Extra fields to copy
302 * @param string $userField User ID field name
303 * @param string $nameField User name field name
304 * @param string $newPrimaryKey Primary key of the new table.
305 * @param string $actorField Actor field name
306 */
307 protected function migrateToTemp(
308 $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
309 ) {
310 $complainedAboutUsers = [];
311
312 $newTable = $table . '_actor_temp';
313 $this->output(
314 "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
315 );
316 wfWaitForSlaves();
317
318 $dbw = $this->getDB( DB_MASTER );
319 $next = [];
320 $countUpdated = 0;
321 $countActors = 0;
322 $countErrors = 0;
323 while ( true ) {
324 // Fetch the rows needing update
325 $res = $dbw->select(
326 [ $table, $newTable, 'actor' ],
327 [ $primaryKey, $userField, $nameField, 'actor_id' ] + $extra,
328 [ $newPrimaryKey => null ] + $next,
329 __METHOD__,
330 [
331 'ORDER BY' => $primaryKey,
332 'LIMIT' => $this->mBatchSize,
333 ],
334 [
335 $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ],
336 'actor' => [
337 'LEFT JOIN',
338 "$userField != 0 AND actor_user = $userField OR "
339 . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
340 ]
341 ]
342 );
343 if ( !$res->numRows() ) {
344 break;
345 }
346
347 // Insert new actors for rows that need one
348 $rows = iterator_to_array( $res );
349 $lastRow = end( $rows );
350 $countActors += $this->addActorsForRows(
351 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
352 );
353
354 // Update rows
355 if ( $rows ) {
356 $inserts = [];
357 $updates = [];
358 foreach ( $rows as $row ) {
359 if ( !$row->actor_id ) {
360 list( , $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $row );
361 $this->error(
362 "Could not make actor for row with $display "
363 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
364 );
365 $countErrors++;
366 continue;
367 }
368 $ins = [
369 $newPrimaryKey => $row->$primaryKey,
370 $actorField => $row->actor_id,
371 ];
372 foreach ( $extra as $to => $from ) {
373 $ins[$to] = $row->$to; // It's aliased
374 }
375 $inserts[] = $ins;
376 $updates[] = $row->$primaryKey;
377 }
378 $this->beginTransaction( $dbw, __METHOD__ );
379 $dbw->insert( $newTable, $inserts, __METHOD__ );
380 $dbw->update( $table, [ $nameField => '' ], [ $primaryKey => $updates ], __METHOD__ );
381 $countUpdated += $dbw->affectedRows();
382 $this->commitTransaction( $dbw, __METHOD__ );
383 }
384
385 // Calculate the "next" condition
386 list( $n, $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $lastRow );
387 $next = [ $n ];
388 $this->output( "... $display\n" );
389 }
390
391 $this->output(
392 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
393 . "$countErrors error(s)\n"
394 );
395 return $countErrors;
396 }
397
398 /**
399 * Migrate actors in the log_search table.
400 * @return int Number of errors
401 */
402 protected function migrateLogSearch() {
403 $complainedAboutUsers = [];
404
405 $primaryKey = [ 'ls_field', 'ls_value' ];
406 $pkFilter = array_flip( $primaryKey );
407 $this->output( "Beginning migration of log_search\n" );
408 wfWaitForSlaves();
409
410 $dbw = $this->getDB( DB_MASTER );
411 $countUpdated = 0;
412 $countActors = 0;
413 $countErrors = 0;
414
415 $next = '1=1';
416 while ( true ) {
417 // Fetch the rows needing update
418 $res = $dbw->select(
419 [ 'log_search', 'actor' ],
420 [ 'ls_field', 'ls_value', 'actor_id' ],
421 [
422 'ls_field' => 'target_author_id',
423 $next,
424 ],
425 __METHOD__,
426 [
427 'DISTINCT',
428 'ORDER BY' => [ 'ls_value' ],
429 'LIMIT' => $this->mBatchSize,
430 ],
431 [ 'actor' => [ 'LEFT JOIN', 'ls_value = ' . $dbw->buildStringCast( 'actor_user' ) ] ]
432 );
433 if ( !$res->numRows() ) {
434 break;
435 }
436
437 // Update the rows
438 $del = [];
439 foreach ( $res as $row ) {
440 $lastRow = $row;
441 if ( !$row->actor_id ) {
442 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
443 $this->error( "No actor for row with $display\n" );
444 $countErrors++;
445 continue;
446 }
447 $dbw->update(
448 'log_search',
449 [
450 'ls_field' => 'target_author_actor',
451 'ls_value' => $row->actor_id,
452 ],
453 [
454 'ls_field' => $row->ls_field,
455 'ls_value' => $row->ls_value,
456 ],
457 __METHOD__,
458 [ 'IGNORE' ]
459 );
460 $countUpdated += $dbw->affectedRows();
461 $del[] = $row->ls_value;
462 }
463 if ( $del ) {
464 $dbw->delete(
465 'log_search', [ 'ls_field' => 'target_author_id', 'ls_value' => $del ], __METHOD__
466 );
467 $countUpdated += $dbw->affectedRows();
468 }
469
470 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
471 $this->output( "... $display\n" );
472 wfWaitForSlaves();
473 }
474
475 $next = '1=1';
476 while ( true ) {
477 // Fetch the rows needing update
478 $res = $dbw->select(
479 [ 'log_search', 'actor' ],
480 [ 'ls_field', 'ls_value', 'actor_id' ],
481 [
482 'ls_field' => 'target_author_ip',
483 $next,
484 ],
485 __METHOD__,
486 [
487 'DISTINCT',
488 'ORDER BY' => [ 'ls_value' ],
489 'LIMIT' => $this->mBatchSize,
490 ],
491 [ 'actor' => [ 'LEFT JOIN', 'ls_value = actor_name' ] ]
492 );
493 if ( !$res->numRows() ) {
494 break;
495 }
496
497 // Insert new actors for rows that need one
498 $rows = iterator_to_array( $res );
499 $lastRow = end( $rows );
500 $countActors += $this->addActorsForRows(
501 $dbw, 'ls_value', $rows, $complainedAboutUsers, $countErrors
502 );
503
504 // Update the rows
505 $del = [];
506 foreach ( $rows as $row ) {
507 if ( !$row->actor_id ) {
508 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
509 $this->error( "Could not make actor for row with $display\n" );
510 $countErrors++;
511 continue;
512 }
513 $dbw->update(
514 'log_search',
515 [
516 'ls_field' => 'target_author_actor',
517 'ls_value' => $row->actor_id,
518 ],
519 [
520 'ls_field' => $row->ls_field,
521 'ls_value' => $row->ls_value,
522 ],
523 __METHOD__,
524 [ 'IGNORE' ]
525 );
526 $countUpdated += $dbw->affectedRows();
527 $del[] = $row->ls_value;
528 }
529 if ( $del ) {
530 $dbw->delete(
531 'log_search', [ 'ls_field' => 'target_author_ip', 'ls_value' => $del ], __METHOD__
532 );
533 $countUpdated += $dbw->affectedRows();
534 }
535
536 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
537 $this->output( "... $display\n" );
538 wfWaitForSlaves();
539 }
540
541 $this->output(
542 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
543 . "$countErrors error(s)\n"
544 );
545 return $countErrors;
546 }
547 }
548
549 $maintClass = "MigrateActors";
550 require_once RUN_MAINTENANCE_IF_MAIN;