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' ] ]
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 = (bool)
$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" );
103 $errors += $this->
migrate(
'revision',
'rev_id',
'rev_user',
'rev_user_text',
'rev_actor' );
104 $errors += $this->
migrate(
'archive',
'ar_id',
'ar_user',
'ar_user_text',
'ar_actor' );
105 $errors += $this->
migrate(
'ipblocks',
'ipb_id',
'ipb_by',
'ipb_by_text',
'ipb_by_actor' );
106 $errors += $this->
migrate(
'image',
'img_name',
'img_user',
'img_user_text',
'img_actor' );
108 'oldimage', [
'oi_name',
'oi_timestamp' ],
'oi_user',
'oi_user_text',
'oi_actor'
110 $errors += $this->
migrate(
'filearchive',
'fa_id',
'fa_user',
'fa_user_text',
'fa_actor' );
111 $errors += $this->
migrate(
'recentchanges',
'rc_id',
'rc_user',
'rc_user_text',
'rc_actor' );
112 $errors += $this->
migrate(
'logging',
'log_id',
'log_user',
'log_user_text',
'log_actor' );
116 return $errors === 0;
126 private function makeNextCond( $dbw, $primaryKey, $row ) {
129 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
130 $field = $primaryKey[$i];
131 $display[] = $field .
'=' . $row->$field;
132 $value = $dbw->
addQuotes( $row->$field );
133 if ( $next ===
'' ) {
134 $next =
"$field > $value";
136 $next =
"$field > $value OR $field = $value AND ($next)";
139 $display = implode(
' ', array_reverse( $display ) );
140 return [ $next, $display ];
150 private function makeActorIdSubquery( $dbw, $userField, $nameField ) {
154 [
"$userField = actor_user" ],
160 [
"$nameField = actor_name" ],
163 return "CASE WHEN $userField = 0 OR $userField IS NULL THEN $nameSubquery ELSE $idSubquery END";
176 private function addActorsForRows(
177 IDatabase $dbw, $nameField, array &$rows, array &$complainedAboutUsers, &$countErrors
181 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
184 foreach ( $rows as $index => $row ) {
185 $keep[$index] =
true;
186 if ( $row->actor_id ===
null ) {
190 $name = $row->$nameField;
191 if ( $userNameUtils->isUsable( $name ) ) {
192 if ( !isset( $complainedAboutUsers[$name] ) ) {
193 $complainedAboutUsers[$name] =
true;
195 "User name \"$name\" is usable, cannot create an anonymous actor for it."
196 .
" Run maintenance/cleanupUsersWithNoId.php to fix this situation.\n"
199 unset( $keep[$index] );
202 $needActors[$name] = 0;
206 $rows = array_intersect_key( $rows, $keep );
211 array_map(
static function ( $v ) {
215 }, array_keys( $needActors ) ),
223 [
'actor_id',
'actor_name' ],
224 [
'actor_name' => array_map(
'strval', array_keys( $needActors ) ) ],
227 foreach (
$res as $row ) {
228 $needActors[$row->actor_name] = $row->actor_id;
230 foreach ( $rows as $row ) {
231 if ( $row->actor_id ===
null ) {
232 $row->actor_id = $needActors[$row->$nameField];
253 protected function migrate( $table, $primaryKey, $userField, $nameField, $actorField ) {
254 if ( !$this->
doTable( $table ) ) {
255 $this->
output(
"Skipping $table, not included in --tables\n" );
260 if ( !$dbw->fieldExists( $table, $userField, __METHOD__ ) ) {
261 $this->
output(
"No need to migrate $table.$userField, field does not exist\n" );
265 $complainedAboutUsers = [];
267 $primaryKey = (array)$primaryKey;
268 $pkFilter = array_fill_keys( $primaryKey,
true );
270 "Beginning migration of $table.$userField and $table.$nameField to $table.$actorField\n"
272 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
273 $lbFactory->waitForReplication();
275 $actorIdSubquery = $this->makeActorIdSubquery( $dbw, $userField, $nameField );
284 array_merge( $primaryKey, [ $userField, $nameField,
'actor_id' => $actorIdSubquery ] ),
291 'ORDER BY' => $primaryKey,
292 'LIMIT' => $this->mBatchSize,
295 if ( !
$res->numRows() ) {
300 $rows = iterator_to_array(
$res );
301 $lastRow = end( $rows );
302 $countActors += $this->addActorsForRows(
303 $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
307 foreach ( $rows as $row ) {
308 if ( !$row->actor_id ) {
309 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
311 "Could not make actor for row with $display "
312 .
"$userField={$row->$userField} $nameField={$row->$nameField}\n"
320 $actorField => $row->actor_id,
322 array_intersect_key( (array)$row, $pkFilter ) + [
330 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
331 $this->
output(
"... $display\n" );
332 $lbFactory->waitForReplication();
336 "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
337 .
"$countErrors error(s)\n"
347 if ( !$this->
doTable(
'log_search' ) ) {
348 $this->
output(
"Skipping log_search, not included in --tables\n" );
352 $complainedAboutUsers = [];
354 $primaryKey = [
'ls_value',
'ls_log_id' ];
355 $this->
output(
"Beginning migration of log_search\n" );
356 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
357 $lbFactory->waitForReplication();
364 $anyBad = (bool)$dbw->
selectField(
'log_search',
'1',
365 [
'ls_field' =>
'target_author_actor',
'ls_value' =>
'' ],
369 $this->
output(
"... Deleting bogus rows due to T215525\n" );
372 [
'ls_field' =>
'target_author_actor',
'ls_value' =>
'' ],
376 $this->
output(
"... Deleted $ct bogus row(s) from T215525\n" );
377 $lbFactory->waitForReplication();
384 [
'log_search',
'actor' ],
385 [
'ls_value',
'ls_log_id',
'actor_id' ],
387 'ls_field' =>
'target_author_id',
392 'ORDER BY' => $primaryKey,
393 'LIMIT' => $this->mBatchSize,
395 [
'actor' => [
'LEFT JOIN',
'actor_user = ' . $dbw->
buildIntegerCast(
'ls_value' ) ] ]
397 if ( !
$res->numRows() ) {
403 foreach (
$res as $row ) {
405 if ( !$row->actor_id ) {
406 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
407 $this->
error(
"No actor for target_author_id row with $display\n" );
412 'ls_field' =>
'target_author_actor',
413 'ls_value' => $row->actor_id,
414 'ls_log_id' => $row->ls_log_id,
417 $dbw->
insert(
'log_search', $ins, __METHOD__, [
'IGNORE' ] );
422 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
423 $this->
output(
"... target_author_id, $display\n" );
424 $lbFactory->waitForReplication();
431 [
'log_search',
'actor' ],
432 [
'ls_value',
'ls_log_id',
'actor_id' ],
434 'ls_field' =>
'target_author_ip',
439 'ORDER BY' => $primaryKey,
440 'LIMIT' => $this->mBatchSize,
442 [
'actor' => [
'LEFT JOIN',
'ls_value = actor_name' ] ]
444 if ( !
$res->numRows() ) {
449 $rows = iterator_to_array(
$res );
450 $lastRow = end( $rows );
451 $countActors += $this->addActorsForRows(
452 $dbw,
'ls_value', $rows, $complainedAboutUsers, $countErrors
457 foreach ( $rows as $row ) {
458 if ( !$row->actor_id ) {
459 list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
460 $this->
error(
"Could not make actor for target_author_ip row with $display\n" );
465 'ls_field' =>
'target_author_actor',
466 'ls_value' => $row->actor_id,
467 'ls_log_id' => $row->ls_log_id,
470 $dbw->
insert(
'log_search', $ins, __METHOD__, [
'IGNORE' ] );
473 list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
474 $this->
output(
"... target_author_ip, $display\n" );
475 $lbFactory->waitForReplication();
479 "Completed migration, inserted $countInserted row(s) with $countActors new actor(s), "
480 .
"$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.
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.
Maintenance script that migrates actors from pre-1.31 columns to the 'actor' table.
doDBUpdates()
Do the actual work.
migrate( $table, $primaryKey, $userField, $nameField, $actorField)
Migrate actors in a table.
migrateLogSearch()
Migrate actors in the log_search table.
__construct()
Default constructor.
getUpdateKey()
Get the update key name to go in the update log table.