26 require_once __DIR__ .
'/Maintenance.php';
36 parent::__construct();
37 $this->
addDescription(
'Migrates actors from pre-1.31 columns to the \'actor\' table' );
50 "...cannot update while \$wgActorTableSchemaMigrationStage lacks SCHEMA_COMPAT_WRITE_NEW\n"
55 $this->
output(
"Creating actor entries for all registered users\n" );
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" );
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' );
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' );
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 ];
130 $idSubquery = $dbw->buildSelectSubquery(
133 [
"$userField = actor_user" ],
136 $nameSubquery = $dbw->buildSelectSubquery(
139 [
"$nameField = actor_name" ],
142 return "CASE WHEN $userField = 0 OR $userField IS NULL THEN $nameSubquery ELSE $idSubquery END";
161 foreach (
$rows as $index => $row ) {
162 $keep[$index] =
true;
163 if ( $row->actor_id ===
null ) {
167 $name = $row->$nameField;
169 if ( !isset( $complainedAboutUsers[
$name] ) ) {
170 $complainedAboutUsers[
$name] =
true;
172 "User name \"$name\" is usable, cannot create an anonymous actor for it."
173 .
" Run maintenance/cleanupUsersWithNoId.php to fix this situation.\n"
176 unset( $keep[$index] );
179 $needActors[
$name] = 0;
188 array_map(
function ( $v ) {
192 }, array_keys( $needActors ) ),
199 [
'actor_id',
'actor_name' ],
200 [
'actor_name' => array_keys( $needActors ) ],
203 foreach (
$res as $row ) {
204 $needActors[$row->actor_name] = $row->actor_id;
207 if ( $row->actor_id ===
null ) {
208 $row->actor_id = $needActors[$row->$nameField];
229 protected function migrate( $table, $primaryKey, $userField, $nameField, $actorField ) {
230 $complainedAboutUsers = [];
232 $primaryKey = (
array)$primaryKey;
233 $pkFilter = array_flip( $primaryKey );
235 "Beginning migration of $table.$userField and $table.$nameField to $table.$actorField\n"
249 array_merge( $primaryKey, [ $userField, $nameField,
'actor_id' => $actorIdSubquery ] ),
256 'ORDER BY' => $primaryKey,
257 'LIMIT' => $this->mBatchSize,
260 if ( !
$res->numRows() ) {
266 $lastRow = end(
$rows );
268 $dbw, $nameField,
$rows, $complainedAboutUsers, $countErrors
273 if ( !$row->actor_id ) {
276 "Could not make actor for row with $display "
277 .
"$userField={$row->$userField} $nameField={$row->$nameField}\n"
285 $actorField => $row->actor_id,
287 array_intersect_key( (
array)$row, $pkFilter ) + [
292 $countUpdated += $dbw->affectedRows();
295 list( $next, $display ) = $this->
makeNextCond( $dbw, $primaryKey, $lastRow );
296 $this->
output(
"... $display\n" );
301 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
302 .
"$countErrors error(s)\n"
324 $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
326 $complainedAboutUsers = [];
328 $newTable = $table .
'_actor_temp';
330 "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
343 [ $table, $newTable ],
344 [ $primaryKey, $userField, $nameField,
'actor_id' => $actorIdSubquery ] + $extra,
345 [ $newPrimaryKey =>
null ] + $next,
348 'ORDER BY' => $primaryKey,
349 'LIMIT' => $this->mBatchSize,
352 $newTable => [
'LEFT JOIN',
"{$primaryKey}={$newPrimaryKey}" ],
355 if ( !
$res->numRows() ) {
361 $lastRow = end(
$rows );
363 $dbw, $nameField,
$rows, $complainedAboutUsers, $countErrors
371 if ( !$row->actor_id ) {
374 "Could not make actor for row with $display "
375 .
"$userField={$row->$userField} $nameField={$row->$nameField}\n"
381 $newPrimaryKey => $row->$primaryKey,
382 $actorField => $row->actor_id,
384 foreach ( $extra
as $to => $from ) {
385 $ins[$to] = $row->$to;
388 $updates[] = $row->$primaryKey;
391 $dbw->insert( $newTable, $inserts, __METHOD__ );
392 $countUpdated += $dbw->affectedRows();
397 list( $n, $display ) = $this->
makeNextCond( $dbw, [ $primaryKey ], $lastRow );
399 $this->
output(
"... $display\n" );
403 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
404 .
"$countErrors error(s)\n"
414 $complainedAboutUsers = [];
416 $primaryKey = [
'ls_field',
'ls_value' ];
417 $pkFilter = array_flip( $primaryKey );
418 $this->
output(
"Beginning migration of log_search\n" );
431 'ls' => $dbw->buildSelectSubquery(
435 'ls_field' =>
'target_author_id',
441 'ORDER BY' => [
'ls_value' ],
442 'LIMIT' => $this->mBatchSize,
448 'ls_field' => $dbw->addQuotes(
'target_author_id' ),
455 [
'actor' => [
'LEFT JOIN',
'ls_value = ' . $dbw->buildStringCast(
'actor_user' ) ] ]
457 if ( !
$res->numRows() ) {
463 foreach (
$res as $row ) {
465 if ( !$row->actor_id ) {
467 $this->
error(
"No actor for row with $display\n" );
474 'ls_field' =>
'target_author_actor',
475 'ls_value' => $row->actor_id,
478 'ls_field' => $row->ls_field,
479 'ls_value' => $row->ls_value,
484 $countUpdated += $dbw->affectedRows();
485 $del[] = $row->ls_value;
489 'log_search', [
'ls_field' =>
'target_author_id',
'ls_value' => $del ], __METHOD__
491 $countUpdated += $dbw->affectedRows();
494 list( $next, $display ) = $this->
makeNextCond( $dbw, $primaryKey, $lastRow );
495 $this->
output(
"... $display\n" );
504 'ls' => $dbw->buildSelectSubquery(
508 'ls_field' =>
'target_author_ip',
514 'ORDER BY' => [
'ls_value' ],
515 'LIMIT' => $this->mBatchSize,
521 'ls_field' => $dbw->addQuotes(
'target_author_ip' ),
528 [
'actor' => [
'LEFT JOIN',
'ls_value = actor_name' ] ]
530 if ( !
$res->numRows() ) {
536 $lastRow = end(
$rows );
538 $dbw,
'ls_value',
$rows, $complainedAboutUsers, $countErrors
544 if ( !$row->actor_id ) {
546 $this->
error(
"Could not make actor for row with $display\n" );
553 'ls_field' =>
'target_author_actor',
554 'ls_value' => $row->actor_id,
557 'ls_field' => $row->ls_field,
558 'ls_value' => $row->ls_value,
563 $countUpdated += $dbw->affectedRows();
564 $del[] = $row->ls_value;
568 'log_search', [
'ls_field' =>
'target_author_ip',
'ls_value' => $del ], __METHOD__
570 $countUpdated += $dbw->affectedRows();
573 list( $next, $display ) = $this->
makeNextCond( $dbw, $primaryKey, $lastRow );
574 $this->
output(
"... $display\n" );
579 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
580 .
"$countErrors error(s)\n"