27require_once __DIR__ .
'/../Maintenance.php';
40 parent::__construct();
41 $this->
addDescription(
'Migrates actors from pre-1.31 columns to the \'actor\' table' );
42 $this->
addOption(
'tables',
'List of tables to process, comma-separated',
false,
true );
51 return $this->tables ===
null || in_array( $table, $this->tables,
true );
57 $this->tables = explode(
',',
$tables );
60 if ( $this->
doTable(
'user' ) ) {
61 $this->
output(
"Creating actor entries for all registered users\n" );
64 $max = $dbw->selectField(
'user',
'MAX(user_id)',
'', __METHOD__ );
66 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
67 while ( $end < $max ) {
69 $end = min( $start + $this->mBatchSize, $max );
70 $this->
output(
"... $start - $end\n" );
74 [
'actor_user' =>
'user_id',
'actor_name' =>
'user_name' ],
75 [
"user_id >= $start",
"user_id <= $end" ],
78 [
'ORDER BY' => [
'user_id' ] ]
80 $count += $dbw->affectedRows();
81 $lbFactory->waitForReplication();
83 $this->
output(
"Completed actor creation, added $count new actor(s)\n" );
85 $this->
output(
"Checking that actors exist for all registered users\n" );
87 $anyMissing =
$dbr->selectField(
90 [
'actor_id' =>
null ],
93 [
'actor' => [
'LEFT JOIN',
'actor_user = user_id' ] ]
96 $this->
error(
'Some users lack actors; run without --tables or include `user` in --tables.' );
99 $this->
output(
"Ok, continuing.\n" );
104 'revision',
'rev_id', [
'revactor_timestamp' =>
'rev_timestamp',
'revactor_page' =>
'rev_page' ],
105 'rev_user',
'rev_user_text',
'revactor_rev',
'revactor_actor'
107 $errors += $this->
migrate(
'archive',
'ar_id',
'ar_user',
'ar_user_text',
'ar_actor' );
108 $errors += $this->
migrate(
'ipblocks',
'ipb_id',
'ipb_by',
'ipb_by_text',
'ipb_by_actor' );
109 $errors += $this->
migrate(
'image',
'img_name',
'img_user',
'img_user_text',
'img_actor' );
111 'oldimage', [
'oi_name',
'oi_timestamp' ],
'oi_user',
'oi_user_text',
'oi_actor'
113 $errors += $this->
migrate(
'filearchive',
'fa_id',
'fa_user',
'fa_user_text',
'fa_actor' );
114 $errors += $this->
migrate(
'recentchanges',
'rc_id',
'rc_user',
'rc_user_text',
'rc_actor' );
115 $errors += $this->
migrate(
'logging',
'log_id',
'log_user',
'log_user_text',
'log_actor' );
119 return $errors === 0;
132 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
133 $field = $primaryKey[$i];
134 $display[] = $field .
'=' . $row->$field;
135 $value = $dbw->addQuotes( $row->$field );
136 if ( $next ===
'' ) {
137 $next =
"$field > $value";
139 $next =
"$field > $value OR $field = $value AND ($next)";
142 $display = implode(
' ', array_reverse( $display ) );
143 return [ $next, $display ];
154 $idSubquery = $dbw->buildSelectSubquery(
157 [
"$userField = actor_user" ],
160 $nameSubquery = $dbw->buildSelectSubquery(
163 [
"$nameField = actor_name" ],
166 return "CASE WHEN $userField = 0 OR $userField IS NULL THEN $nameSubquery ELSE $idSubquery END";
181 IDatabase $dbw, $nameField, array &$rows, array &$complainedAboutUsers, &$countErrors
187 foreach ( $rows as $index => $row ) {
188 $keep[$index] =
true;
189 if ( $row->actor_id ===
null ) {
193 $name = $row->$nameField;
195 if ( !isset( $complainedAboutUsers[$name] ) ) {
196 $complainedAboutUsers[$name] =
true;
198 "User name \"$name\" is usable, cannot create an anonymous actor for it."
199 .
" Run maintenance/cleanupUsersWithNoId.php to fix this situation.\n"
202 unset( $keep[$index] );
205 $needActors[$name] = 0;
209 $rows = array_intersect_key( $rows, $keep );
214 array_map(
function ( $v ) {
218 }, array_keys( $needActors ) ),
226 [
'actor_id',
'actor_name' ],
227 [
'actor_name' => array_map(
'strval', array_keys( $needActors ) ) ],
230 foreach (
$res as $row ) {
231 $needActors[$row->actor_name] = $row->actor_id;
233 foreach ( $rows as $row ) {
234 if ( $row->actor_id ===
null ) {
235 $row->actor_id = $needActors[$row->$nameField];
256 protected function migrate( $table, $primaryKey, $userField, $nameField, $actorField ) {
257 if ( !$this->
doTable( $table ) ) {
258 $this->
output(
"Skipping $table, not included in --tables\n" );
263 if ( !$dbw->fieldExists( $table, $userField, __METHOD__ ) ) {
264 $this->
output(
"No need to migrate $table.$userField, field does not exist\n" );
268 $complainedAboutUsers = [];
270 $primaryKey = (array)$primaryKey;
271 $pkFilter = array_flip( $primaryKey );
273 "Beginning migration of $table.$userField and $table.$nameField to $table.$actorField\n"
275 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
276 $lbFactory->waitForReplication();
287 array_merge( $primaryKey, [ $userField, $nameField,
'actor_id' => $actorIdSubquery ] ),
294 'ORDER BY' => $primaryKey,
295 'LIMIT' => $this->mBatchSize,
298 if ( !
$res->numRows() ) {
303 $rows = iterator_to_array(
$res );
304 $lastRow = end( $rows );
306 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
310 foreach ( $rows as $row ) {
311 if ( !$row->actor_id ) {
312 list( , $display ) = $this->
makeNextCond( $dbw, $primaryKey, $row );
314 "Could not make actor for row with $display "
315 .
"$userField={$row->$userField} $nameField={$row->$nameField}\n"
323 $actorField => $row->actor_id,
325 array_intersect_key( (array)$row, $pkFilter ) + [
330 $countUpdated += $dbw->affectedRows();
333 list( $next, $display ) = $this->
makeNextCond( $dbw, $primaryKey, $lastRow );
334 $this->
output(
"... $display\n" );
335 $lbFactory->waitForReplication();
339 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
340 .
"$countErrors error(s)\n"
363 $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
365 if ( !$this->
doTable( $table ) ) {
366 $this->
output(
"Skipping $table, not included in --tables\n" );
371 if ( !$dbw->fieldExists( $table, $userField, __METHOD__ ) ) {
372 $this->
output(
"No need to migrate $table.$userField, field does not exist\n" );
376 $complainedAboutUsers = [];
378 $newTable = $table .
'_actor_temp';
380 "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
382 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
392 [ $table, $newTable ],
393 [ $primaryKey, $userField, $nameField,
'actor_id' => $actorIdSubquery ] + $extra,
394 [ $newPrimaryKey =>
null ] + $next,
397 'ORDER BY' => $primaryKey,
398 'LIMIT' => $this->mBatchSize,
401 $newTable => [
'LEFT JOIN',
"{$primaryKey}={$newPrimaryKey}" ],
404 if ( !
$res->numRows() ) {
409 $rows = iterator_to_array(
$res );
410 $lastRow = end( $rows );
412 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
418 foreach ( $rows as $row ) {
419 if ( !$row->actor_id ) {
420 list( , $display ) = $this->
makeNextCond( $dbw, [ $primaryKey ], $row );
422 "Could not make actor for row with $display "
423 .
"$userField={$row->$userField} $nameField={$row->$nameField}\n"
429 $newPrimaryKey => $row->$primaryKey,
430 $actorField => $row->actor_id,
432 foreach ( $extra as $to => $from ) {
434 $ins[$to] = $row->$to;
439 $dbw->insert( $newTable, $inserts, __METHOD__ );
440 $countUpdated += $dbw->affectedRows();
445 list( $n, $display ) = $this->
makeNextCond( $dbw, [ $primaryKey ], $lastRow );
447 $this->
output(
"... $display\n" );
451 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
452 .
"$countErrors error(s)\n"
462 if ( !$this->
doTable(
'log_search' ) ) {
463 $this->
output(
"Skipping log_search, not included in --tables\n" );
467 $complainedAboutUsers = [];
469 $primaryKey = [
'ls_value',
'ls_log_id' ];
470 $this->
output(
"Beginning migration of log_search\n" );
471 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
472 $lbFactory->waitForReplication();
479 $anyBad = $dbw->selectField(
482 [
'ls_field' =>
'target_author_actor',
'ls_value' =>
'' ],
487 $this->
output(
"... Deleting bogus rows due to T215525\n" );
490 [
'ls_field' =>
'target_author_actor',
'ls_value' =>
'' ],
493 $ct = $dbw->affectedRows();
494 $this->
output(
"... Deleted $ct bogus row(s) from T215525\n" );
495 $lbFactory->waitForReplication();
502 [
'log_search',
'actor' ],
503 [
'ls_value',
'ls_log_id',
'actor_id' ],
505 'ls_field' =>
'target_author_id',
510 'ORDER BY' => $primaryKey,
511 'LIMIT' => $this->mBatchSize,
513 [
'actor' => [
'LEFT JOIN',
'actor_user = ' . $dbw->buildIntegerCast(
'ls_value' ) ] ]
515 if ( !
$res->numRows() ) {
521 foreach (
$res as $row ) {
523 if ( !$row->actor_id ) {
524 list( , $display ) = $this->
makeNextCond( $dbw, $primaryKey, $row );
525 $this->
error(
"No actor for target_author_id row with $display\n" );
530 'ls_field' =>
'target_author_actor',
531 'ls_value' => $row->actor_id,
532 'ls_log_id' => $row->ls_log_id,
535 $dbw->insert(
'log_search', $ins, __METHOD__, [
'IGNORE' ] );
536 $countInserted += $dbw->affectedRows();
538 list( $next, $display ) = $this->
makeNextCond( $dbw, $primaryKey, $lastRow );
539 $this->
output(
"... target_author_id, $display\n" );
540 $lbFactory->waitForReplication();
547 [
'log_search',
'actor' ],
548 [
'ls_value',
'ls_log_id',
'actor_id' ],
550 'ls_field' =>
'target_author_ip',
555 'ORDER BY' => $primaryKey,
556 'LIMIT' => $this->mBatchSize,
558 [
'actor' => [
'LEFT JOIN',
'ls_value = actor_name' ] ]
560 if ( !
$res->numRows() ) {
565 $rows = iterator_to_array(
$res );
566 $lastRow = end( $rows );
568 $dbw,
'ls_value', $rows, $complainedAboutUsers, $countErrors
573 foreach ( $rows as $row ) {
574 if ( !$row->actor_id ) {
575 list( , $display ) = $this->
makeNextCond( $dbw, $primaryKey, $row );
576 $this->
error(
"Could not make actor for target_author_ip row with $display\n" );
581 'ls_field' =>
'target_author_actor',
582 'ls_value' => $row->actor_id,
583 'ls_log_id' => $row->ls_log_id,
586 $dbw->insert(
'log_search', $ins, __METHOD__, [
'IGNORE' ] );
587 $countInserted += $dbw->affectedRows();
589 list( $next, $display ) = $this->
makeNextCond( $dbw, $primaryKey, $lastRow );
590 $this->
output(
"... target_author_ip, $display\n" );
591 $lbFactory->waitForReplication();
595 "Completed migration, inserted $countInserted row(s) with $countActors new actor(s), "
596 .
"$countErrors error(s)\n"
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
error( $err, $die=0)
Throw an error to the user.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Set the batch size.
Maintenance script that migrates actors from pre-1.31 columns to the 'actor' table.
addActorsForRows(IDatabase $dbw, $nameField, array &$rows, array &$complainedAboutUsers, &$countErrors)
Add actors for anons in a set of rows.
doDBUpdates()
Do the actual work.
migrate( $table, $primaryKey, $userField, $nameField, $actorField)
Migrate actors in a table.
makeActorIdSubquery( $dbw, $userField, $nameField)
Make the subqueries for actor_id
migrateLogSearch()
Migrate actors in the log_search table.
__construct()
Default constructor.
makeNextCond( $dbw, $primaryKey, $row)
Calculate a "next" condition and a display string.
migrateToTemp( $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField)
Migrate actors in a table to a temporary table.
getUpdateKey()
Get the update key name to go in the update log table.
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...