43 parent::__construct();
46 $this->
addOption(
'field',
'The name of a database field to process',
48 $this->
addOption(
'type',
'Which type of invalid actors to find or fix, '
49 .
'missing or broken (with empty actor_name which can\'t be associated '
50 .
'with an existing user).',
52 $this->
addOption(
'skip',
'A comma-separated list of actor IDs to skip.',
54 $this->
addOption(
'overwrite-with',
'Replace invalid actors with this user. '
55 .
'Typically, this would be "Unknown user", but it could be any reserved '
56 .
'system user (per $wgReservedUsernames) or locally registered user. '
57 .
'If not given, invalid actors will only be listed, not fixed. '
58 .
'You will be prompted for confirmation before data is written. ',
67 private function getTables() {
69 'ar_actor' => [
'archive',
'ar_actor',
'ar_id' ],
70 'img_actor' => [
'image',
'img_actor',
'img_name' ],
71 'oi_actor' => [
'oldimage',
'oi_actor',
'oi_archive_name' ],
72 'fa_actor' => [
'filearchive',
'fa_actor',
'fa_id' ],
73 'rc_actor' => [
'recentchanges',
'rc_actor',
'rc_id' ],
74 'log_actor' => [
'logging',
'log_actor',
'log_id' ],
75 'rev_actor' => [
'revision',
'rev_actor',
'rev_id' ],
76 'bl_by_actor' => [
'block',
'bl_by_actor',
'bl_id' ],
84 private function getTableInfo( $field ) {
85 $tables = $this->getTables();
86 return $tables[$field] ??
null;
98 private function getNewActorId() {
99 $name = $this->
getOption(
'overwrite-with' );
101 if ( $name ===
null ) {
105 $user = $this->userFactory->newFromName( $name );
108 $this->
fatalError(
"Not a valid user name: '$name'" );
111 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
113 if ( $user->isRegistered() ) {
114 $this->
output(
"Using existing user: '$user'\n" );
115 } elseif ( !$this->userNameUtils->isValid( $name ) ) {
116 $this->
fatalError(
"Not a valid user name: '$name'" );
117 } elseif ( !$this->userNameUtils->isUsable( $name ) ) {
118 $this->
output(
"Using system user: '$name'\n" );
124 $actorId = $this->actorNormalization->acquireActorId( $user, $dbw );
127 $this->
fatalError(
"Failed to acquire an actor ID for user '$user'" );
130 $this->
output(
"Replacement actor ID is $actorId.\n" );
136 $this->userFactory = $services->getUserFactory();
137 $this->userNameUtils = $services->getUserNameUtils();
138 $this->actorNormalization = $services->getActorNormalization();
142 if ( !$this->getTableInfo( $field ) ) {
143 $this->
fatalError(
"Unknown field: $field.\n" );
146 $type = $this->
getOption(
'type',
'missing' );
147 if ( $type !==
'missing' && $type !==
'broken' ) {
148 $this->
fatalError(
"Unknown type: $type.\n" );
152 $overwrite = $this->getNewActorId();
154 $bad = $this->findBadActors( $field, $type, $skip );
156 if ( $bad && $overwrite ) {
158 $this->
output(
"Do you want to OVERWRITE the listed actor IDs?\n" );
159 $this->
output(
"Information about the invalid IDs will be lost!\n" );
163 if ( $confirm ===
'yes' ) {
164 $this->overwriteActorIDs( $field, array_keys( $bad ), $overwrite );
170 $this->
output(
"Done.\n" );
182 private function findBadActors( $field, $type, $skip ) {
183 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
184 $this->
output(
"Finding invalid actor IDs in $table.$actorField...\n" );
204 $queryBuilder = $dbr->newSelectQueryBuilder()
205 ->select( [ $actorField, $idField ] )
207 ->leftJoin(
'actor',
null, [
"$actorField = actor_id" ] )
208 ->where( $type ==
'missing' ? [
'actor_id' =>
null ] : [
'actor_name' =>
'' ] )
212 $queryBuilder->andWhere( $dbr->expr( $actorField,
'!=', $skip ) );
215 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
216 $count = $res->numRows();
221 $this->
output(
"\t\tID\tACTOR\n" );
224 foreach ( $res as $row ) {
225 $id = $row->$idField;
226 $actor = (int)( $row->$actorField );
229 $this->
output(
"\t\t$id\t$actor\n" );
232 $this->
output(
"\tFound $count invalid actor IDs.\n" );
235 $this->
output(
"\tBatch size reached, run again after fixing the current batch.\n" );
250 private function overwriteActorIDs( $field, array $ids,
int $overwrite ) {
251 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
253 $count = count( $ids );
254 $this->
output(
"OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
258 $dbw->newUpdateQueryBuilder()
260 ->set( [ $actorField => $overwrite ] )
261 ->where( [ $idField => $ids ] )
262 ->caller( __METHOD__ )->execute();
264 $count = $dbw->affectedRows();
267 $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.