41 private ?array $tables;
44 parent::__construct();
47 $this->
addOption(
'field',
'The name of a database field to process',
49 $this->
addOption(
'type',
'Which type of invalid actors to find or fix, '
50 .
'missing or broken (with empty actor_name which can\'t be associated '
51 .
'with an existing user).',
53 $this->
addOption(
'skip',
'A comma-separated list of actor IDs to skip.',
55 $this->
addOption(
'overwrite-with',
'Replace invalid actors with this user. '
56 .
'Typically, this would be "Unknown user", but it could be any reserved '
57 .
'system user (per $wgReservedUsernames) or locally registered user. '
58 .
'If not given, invalid actors will only be listed, not fixed. '
59 .
'You will be prompted for confirmation before data is written. ',
68 private function getTables() {
69 if ( !$this->tables ) {
71 'ar_actor' => [
'archive',
'ar_actor',
'ar_id' ],
72 'img_actor' => [
'image',
'img_actor',
'img_name' ],
73 'oi_actor' => [
'oldimage',
'oi_actor',
'oi_archive_name' ],
74 'fa_actor' => [
'filearchive',
'fa_actor',
'fa_id' ],
75 'rc_actor' => [
'recentchanges',
'rc_actor',
'rc_id' ],
76 'log_actor' => [
'logging',
'log_actor',
'log_id' ],
77 'rev_actor' => [
'revision',
'rev_actor',
'rev_id' ],
80 ->get( MainConfigNames::BlockTargetMigrationStage );
82 $tables[
'ipb_by_actor'] = [
'ipblocks',
'ipb_by_actor',
'ipb_id' ];
85 $tables[
'bl_by_actor'] = [
'block',
'bl_by_actor',
'bl_id' ];
87 $this->tables = $tables;
96 private function getTableInfo( $field ) {
97 $tables = $this->getTables();
98 return $tables[$field] ??
null;
110 private function getNewActorId() {
111 $name = $this->
getOption(
'overwrite-with' );
113 if ( $name ===
null ) {
117 $user = $this->userFactory->newFromName( $name );
120 $this->
fatalError(
"Not a valid user name: '$name'" );
123 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
125 if ( $user->isRegistered() ) {
126 $this->
output(
"Using existing user: '$user'\n" );
127 } elseif ( !$this->userNameUtils->isValid( $name ) ) {
128 $this->
fatalError(
"Not a valid user name: '$name'" );
129 } elseif ( !$this->userNameUtils->isUsable( $name ) ) {
130 $this->
output(
"Using system user: '$name'\n" );
136 $actorId = $this->actorNormalization->acquireActorId( $user, $dbw );
139 $this->
fatalError(
"Failed to acquire an actor ID for user '$user'" );
142 $this->
output(
"Replacement actor ID is $actorId.\n" );
148 $this->userFactory = $services->getUserFactory();
149 $this->userNameUtils = $services->getUserNameUtils();
150 $this->actorNormalization = $services->getActorNormalization();
154 if ( !$this->getTableInfo( $field ) ) {
155 $this->
fatalError(
"Unknown field: $field.\n" );
158 $type = $this->
getOption(
'type',
'missing' );
159 if ( $type !==
'missing' && $type !==
'broken' ) {
160 $this->
fatalError(
"Unknown type: $type.\n" );
164 $overwrite = $this->getNewActorId();
166 $bad = $this->findBadActors( $field, $type, $skip );
168 if ( $bad && $overwrite ) {
170 $this->
output(
"Do you want to OVERWRITE the listed actor IDs?\n" );
171 $this->
output(
"Information about the invalid IDs will be lost!\n" );
175 if ( $confirm ===
'yes' ) {
176 $this->overwriteActorIDs( $field, array_keys( $bad ), $overwrite );
182 $this->
output(
"Done.\n" );
194 private function findBadActors( $field, $type, $skip ) {
195 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
196 $this->
output(
"Finding invalid actor IDs in $table.$actorField...\n" );
216 $queryBuilder = $dbr->newSelectQueryBuilder()
217 ->select( [ $actorField, $idField ] )
219 ->leftJoin(
'actor',
null, [
"$actorField = actor_id" ] )
220 ->where( $type ==
'missing' ? [
'actor_id' =>
null ] : [
'actor_name' =>
'' ] )
224 $queryBuilder->andWhere( $dbr->expr( $actorField,
'!=', $skip ) );
227 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
228 $count = $res->numRows();
233 $this->
output(
"\t\tID\tACTOR\n" );
236 foreach ( $res as $row ) {
237 $id = $row->$idField;
238 $actor = (int)( $row->$actorField );
241 $this->
output(
"\t\t$id\t$actor\n" );
244 $this->
output(
"\tFound $count invalid actor IDs.\n" );
247 $this->
output(
"\tBatch size reached, run again after fixing the current batch.\n" );
262 private function overwriteActorIDs( $field, array $ids,
int $overwrite ) {
263 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
265 $count = count( $ids );
266 $this->
output(
"OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
270 $dbw->newUpdateQueryBuilder()
272 ->set( [ $actorField => $overwrite ] )
273 ->where( [ $idField => $ids ] )
274 ->caller( __METHOD__ )->execute();
276 $count = $dbw->affectedRows();
279 $this->
output(
"\tUpdated $count rows.\n" );
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
static readconsole( $prompt='> ')
Prompt the console for input.
getServiceContainer()
Returns the main service container.
parseIntList( $text)
Utility function to parse a string (perhaps from a command line option) into a list of integers (perh...
addDescription( $text)
Set the description text.
setDBProvider(IConnectionProvider $dbProvider)
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.