3 * Migrate actors from pre-1.31 columns to the 'actor' table
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.
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.
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
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\IDatabase
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that migrates actors from pre-1.31 columns to the
32 * @ingroup Maintenance
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 );
41 protected function getUpdateKey() {
45 protected function doDBUpdates() {
46 global $wgActorTableSchemaMigrationStage;
48 if ( !( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) ) {
50 "...cannot update while \$wgActorTableSchemaMigrationStage lacks SCHEMA_COMPAT_WRITE_NEW\n"
55 $this->output( "Creating actor entries for all registered users\n" );
57 $dbw = $this->getDB( DB_MASTER
);
58 $max = $dbw->selectField( 'user', 'MAX(user_id)', '', __METHOD__
);
60 while ( $end < $max ) {
62 $end = min( $start +
$this->mBatchSize
, $max );
63 $this->output( "... $start - $end\n" );
67 [ 'actor_user' => 'user_id', 'actor_name' => 'user_name' ],
68 [ "user_id >= $start", "user_id <= $end" ],
71 [ 'ORDER BY' => [ 'user_id' ] ]
73 $count +
= $dbw->affectedRows();
76 $this->output( "Completed actor creation, added $count new actor(s)\n" );
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'
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'
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' );
93 $errors +
= $this->migrateLogSearch();
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 ]
105 private function makeNextCond( $dbw, $primaryKey, $row ) {
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";
115 $next = "$field > $value OR $field = $value AND ($next)";
118 $display = implode( ' ', array_reverse( $display ) );
119 return [ $next, $display ];
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
131 private function addActorsForRows(
132 IDatabase
$dbw, $nameField, array &$rows, array &$complainedAboutUsers, &$countErrors
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;
149 "User name \"$name\" is usable, cannot create an anonymous actor for it."
150 . " Run maintenance/cleanupUsersWithNoId.php to fix this situation.\n"
153 unset( $keep[$index] );
156 $needActors[$name] = 0;
160 $rows = array_intersect_key( $rows, $keep );
165 array_map( function ( $v ) {
169 }, array_keys( $needActors ) ),
172 $countActors +
= $dbw->affectedRows();
176 [ 'actor_id', 'actor_name' ],
177 [ 'actor_name' => array_keys( $needActors ) ],
180 foreach ( $res as $row ) {
181 $needActors[$row->actor_name
] = $row->actor_id
;
183 foreach ( $rows as $row ) {
184 if ( $row->actor_id
=== null ) {
185 $row->actor_id
= $needActors[$row->$nameField];
194 * Migrate actors in a table.
196 * Assumes any row with the actor field non-zero have already been migrated.
197 * Blanks the name field when migrating.
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
206 protected function migrate( $table, $primaryKey, $userField, $nameField, $actorField ) {
207 $complainedAboutUsers = [];
209 $primaryKey = (array)$primaryKey;
210 $pkFilter = array_flip( $primaryKey );
212 "Beginning migration of $table.$userField and $table.$nameField to $table.$actorField\n"
216 $dbw = $this->getDB( DB_MASTER
);
222 // Fetch the rows needing update
225 array_merge( $primaryKey, [ $userField, $nameField, 'actor_id' ] ),
232 'ORDER BY' => $primaryKey,
233 'LIMIT' => $this->mBatchSize
,
238 "$userField != 0 AND actor_user = $userField OR "
239 . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
243 if ( !$res->numRows() ) {
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
254 // Update the existing rows
255 foreach ( $rows as $row ) {
256 if ( !$row->actor_id
) {
257 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
259 "Could not make actor for row with $display "
260 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
268 $actorField => $row->actor_id
,
270 array_intersect_key( (array)$row, $pkFilter ) +
[
275 $countUpdated +
= $dbw->affectedRows();
278 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
279 $this->output( "... $display\n" );
284 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
285 . "$countErrors error(s)\n"
291 * Migrate actors in a table to a temporary table.
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
296 * Blanks the name field when migrating.
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
306 protected function migrateToTemp(
307 $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
309 $complainedAboutUsers = [];
311 $newTable = $table . '_actor_temp';
313 "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
317 $dbw = $this->getDB( DB_MASTER
);
323 // Fetch the rows needing update
325 [ $table, $newTable, 'actor' ],
326 [ $primaryKey, $userField, $nameField, 'actor_id' ] +
$extra,
327 [ $newPrimaryKey => null ] +
$next,
330 'ORDER BY' => $primaryKey,
331 'LIMIT' => $this->mBatchSize
,
334 $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ],
337 "$userField != 0 AND actor_user = $userField OR "
338 . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
342 if ( !$res->numRows() ) {
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
357 foreach ( $rows as $row ) {
358 if ( !$row->actor_id
) {
359 list( , $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $row );
361 "Could not make actor for row with $display "
362 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
368 $newPrimaryKey => $row->$primaryKey,
369 $actorField => $row->actor_id
,
371 foreach ( $extra as $to => $from ) {
372 $ins[$to] = $row->$to; // It's aliased
375 $updates[] = $row->$primaryKey;
377 $this->beginTransaction( $dbw, __METHOD__
);
378 $dbw->insert( $newTable, $inserts, __METHOD__
);
379 $countUpdated +
= $dbw->affectedRows();
380 $this->commitTransaction( $dbw, __METHOD__
);
383 // Calculate the "next" condition
384 list( $n, $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $lastRow );
386 $this->output( "... $display\n" );
390 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
391 . "$countErrors error(s)\n"
397 * Migrate actors in the log_search table.
398 * @return int Number of errors
400 protected function migrateLogSearch() {
401 $complainedAboutUsers = [];
403 $primaryKey = [ 'ls_field', 'ls_value' ];
404 $pkFilter = array_flip( $primaryKey );
405 $this->output( "Beginning migration of log_search\n" );
408 $dbw = $this->getDB( DB_MASTER
);
415 // Fetch the rows needing update
417 [ 'log_search', 'actor' ],
418 [ 'ls_field', 'ls_value', 'actor_id' ],
420 'ls_field' => 'target_author_id',
426 'ORDER BY' => [ 'ls_value' ],
427 'LIMIT' => $this->mBatchSize
,
429 [ 'actor' => [ 'LEFT JOIN', 'ls_value = ' . $dbw->buildStringCast( 'actor_user' ) ] ]
431 if ( !$res->numRows() ) {
437 foreach ( $res as $row ) {
439 if ( !$row->actor_id
) {
440 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
441 $this->error( "No actor for row with $display\n" );
448 'ls_field' => 'target_author_actor',
449 'ls_value' => $row->actor_id
,
452 'ls_field' => $row->ls_field
,
453 'ls_value' => $row->ls_value
,
458 $countUpdated +
= $dbw->affectedRows();
459 $del[] = $row->ls_value
;
463 'log_search', [ 'ls_field' => 'target_author_id', 'ls_value' => $del ], __METHOD__
465 $countUpdated +
= $dbw->affectedRows();
468 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
469 $this->output( "... $display\n" );
475 // Fetch the rows needing update
477 [ 'log_search', 'actor' ],
478 [ 'ls_field', 'ls_value', 'actor_id' ],
480 'ls_field' => 'target_author_ip',
486 'ORDER BY' => [ 'ls_value' ],
487 'LIMIT' => $this->mBatchSize
,
489 [ 'actor' => [ 'LEFT JOIN', 'ls_value = actor_name' ] ]
491 if ( !$res->numRows() ) {
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
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" );
514 'ls_field' => 'target_author_actor',
515 'ls_value' => $row->actor_id
,
518 'ls_field' => $row->ls_field
,
519 'ls_value' => $row->ls_value
,
524 $countUpdated +
= $dbw->affectedRows();
525 $del[] = $row->ls_value
;
529 'log_search', [ 'ls_field' => 'target_author_ip', 'ls_value' => $del ], __METHOD__
531 $countUpdated +
= $dbw->affectedRows();
534 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
535 $this->output( "... $display\n" );
540 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
541 . "$countErrors error(s)\n"
547 $maintClass = "MigrateActors";
548 require_once RUN_MAINTENANCE_IF_MAIN
;