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 < MIGRATION_WRITE_NEW
) {
50 "...cannot update while \$wgActorTableSchemaMigrationStage < MIGRATION_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)', false, __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
,
271 array_intersect_key( (array)$row, $pkFilter ) +
[
276 $countUpdated +
= $dbw->affectedRows();
279 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
280 $this->output( "... $display\n" );
285 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
286 . "$countErrors error(s)\n"
292 * Migrate actors in a table to a temporary table.
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
297 * Blanks the name field when migrating.
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
307 protected function migrateToTemp(
308 $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
310 $complainedAboutUsers = [];
312 $newTable = $table . '_actor_temp';
314 "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
318 $dbw = $this->getDB( DB_MASTER
);
324 // Fetch the rows needing update
326 [ $table, $newTable, 'actor' ],
327 [ $primaryKey, $userField, $nameField, 'actor_id' ] +
$extra,
328 [ $newPrimaryKey => null ] +
$next,
331 'ORDER BY' => $primaryKey,
332 'LIMIT' => $this->mBatchSize
,
335 $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ],
338 "$userField != 0 AND actor_user = $userField OR "
339 . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
343 if ( !$res->numRows() ) {
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
358 foreach ( $rows as $row ) {
359 if ( !$row->actor_id
) {
360 list( , $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $row );
362 "Could not make actor for row with $display "
363 . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
369 $newPrimaryKey => $row->$primaryKey,
370 $actorField => $row->actor_id
,
372 foreach ( $extra as $to => $from ) {
373 $ins[$to] = $row->$to; // It's aliased
376 $updates[] = $row->$primaryKey;
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__
);
385 // Calculate the "next" condition
386 list( $n, $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $lastRow );
388 $this->output( "... $display\n" );
393 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
394 . "$countErrors error(s)\n"
400 * Migrate actors in the log_search table.
401 * @return int Number of errors
403 protected function migrateLogSearch() {
404 $complainedAboutUsers = [];
406 $primaryKey = [ 'ls_field', 'ls_value' ];
407 $pkFilter = array_flip( $primaryKey );
408 $this->output( "Beginning migration of log_search\n" );
411 $dbw = $this->getDB( DB_MASTER
);
418 // Fetch the rows needing update
420 [ 'log_search', 'actor' ],
421 [ 'ls_field', 'ls_value', 'actor_id' ],
423 'ls_field' => 'target_author_id',
429 'ORDER BY' => [ 'ls_value' ],
430 'LIMIT' => $this->mBatchSize
,
432 [ 'actor' => [ 'LEFT JOIN', 'ls_value = ' . $dbw->buildStringCast( 'actor_user' ) ] ]
434 if ( !$res->numRows() ) {
440 foreach ( $res as $row ) {
442 if ( !$row->actor_id
) {
443 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
444 $this->error( "No actor for row with $display\n" );
451 'ls_field' => 'target_author_actor',
452 'ls_value' => $row->actor_id
,
455 'ls_field' => $row->ls_field
,
456 'ls_value' => $row->ls_value
,
461 $countUpdated +
= $dbw->affectedRows();
462 $del[] = $row->ls_value
;
466 'log_search', [ 'ls_field' => 'target_author_id', 'ls_value' => $del ], __METHOD__
468 $countUpdated +
= $dbw->affectedRows();
471 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
472 $this->output( "... $display\n" );
478 // Fetch the rows needing update
480 [ 'log_search', 'actor' ],
481 [ 'ls_field', 'ls_value', 'actor_id' ],
483 'ls_field' => 'target_author_ip',
489 'ORDER BY' => [ 'ls_value' ],
490 'LIMIT' => $this->mBatchSize
,
492 [ 'actor' => [ 'LEFT JOIN', 'ls_value = actor_name' ] ]
494 if ( !$res->numRows() ) {
498 // Insert new actors for rows that need one
499 $rows = iterator_to_array( $res );
500 $lastRow = end( $rows );
501 $countActors +
= $this->addActorsForRows(
502 $dbw, 'ls_value', $rows, $complainedAboutUsers, $countErrors
507 foreach ( $rows as $row ) {
508 if ( !$row->actor_id
) {
509 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
510 $this->error( "Could not make actor for row with $display\n" );
517 'ls_field' => 'target_author_actor',
518 'ls_value' => $row->actor_id
,
521 'ls_field' => $row->ls_field
,
522 'ls_value' => $row->ls_value
,
527 $countUpdated +
= $dbw->affectedRows();
528 $del[] = $row->ls_value
;
532 'log_search', [ 'ls_field' => 'target_author_ip', 'ls_value' => $del ], __METHOD__
534 $countUpdated +
= $dbw->affectedRows();
537 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
538 $this->output( "... $display\n" );
543 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
544 . "$countErrors error(s)\n"
550 $maintClass = "MigrateActors";
551 require_once RUN_MAINTENANCE_IF_MAIN
;