30 parent::__construct();
32 $this->addDescription(
'Find and fix invalid actor IDs.' );
33 $this->addOption(
'field',
'The name of a database field to process',
35 $this->addOption(
'type',
'Which type of invalid actors to find or fix, '
36 .
'missing or broken (with empty actor_name which can\'t be associated '
37 .
'with an existing user).',
39 $this->addOption(
'skip',
'A comma-separated list of actor IDs to skip.',
41 $this->addOption(
'overwrite-with',
'Replace invalid actors with this user. '
42 .
'Typically, this would be "Unknown user", but it could be any reserved '
43 .
'system user (per $wgReservedUsernames) or locally registered user. '
44 .
'If not given, invalid actors will only be listed, not fixed. '
45 .
'You will be prompted for confirmation before data is written. ',
48 $this->setBatchSize( 1000 );
54 private function getTables() {
56 'ar_actor' => [
'archive',
'ar_actor',
'ar_id' ],
57 'img_actor' => [
'image',
'img_actor',
'img_name' ],
58 'oi_actor' => [
'oldimage',
'oi_actor',
'oi_archive_name' ],
59 'fa_actor' => [
'filearchive',
'fa_actor',
'fa_id' ],
60 'fr_actor' => [
'filerevision',
'fr_actor',
'fr_id' ],
61 'rc_actor' => [
'recentchanges',
'rc_actor',
'rc_id' ],
62 'log_actor' => [
'logging',
'log_actor',
'log_id' ],
63 'rev_actor' => [
'revision',
'rev_actor',
'rev_id' ],
64 'bl_by_actor' => [
'block',
'bl_by_actor',
'bl_id' ],
72 private function getTableInfo( $field ) {
73 $tables = $this->getTables();
74 return $tables[$field] ??
null;
86 private function getNewActorId() {
87 $name = $this->getOption(
'overwrite-with' );
89 if ( $name ===
null ) {
93 $user = $this->userFactory->newFromName( $name );
96 $this->fatalError(
"Not a valid user name: '$name'" );
99 if ( $user->isRegistered() ) {
100 $this->output(
"Using existing user: '$user'\n" );
101 } elseif ( !$this->userNameUtils->isUsable( $user->getName() ) ) {
102 $this->output(
"Using system user: '{$user->getName()}'\n" );
104 $this->fatalError(
"Unknown user: '{$user->getName()}'" );
107 $dbw = $this->getPrimaryDB();
110 $actorId = $this->actorNormalization->acquireActorId( $user, $dbw );
111 }
catch ( CannotCreateActorException ) {
112 $this->fatalError(
"Failed to acquire an actor ID for user '$user'" );
115 $this->output(
"Replacement actor ID is $actorId.\n" );
120 $services = $this->getServiceContainer();
121 $this->userFactory = $services->getUserFactory();
122 $this->userNameUtils = $services->getUserNameUtils();
123 $this->actorNormalization = $services->getActorNormalization();
125 $field = $this->getOption(
'field' );
126 if ( !$this->getTableInfo( $field ) ) {
127 $this->fatalError(
"Unknown field: $field.\n" );
130 $type = $this->getOption(
'type',
'missing' );
131 if ( $type !==
'missing' && $type !==
'broken' ) {
132 $this->fatalError(
"Unknown type: $type.\n" );
135 $skip = $this->parseIntList( $this->getOption(
'skip',
'' ) );
136 $overwrite = $this->getNewActorId();
138 $bad = $this->findBadActors( $field, $type, $skip );
140 if ( $bad && $overwrite ) {
141 $this->output(
"\n" );
142 $this->output(
"Do you want to OVERWRITE the listed actor IDs?\n" );
143 $this->output(
"Information about the invalid IDs will be lost!\n" );
144 $this->output(
"\n" );
145 $confirm = static::readconsole(
'Type "yes" to continue: ' );
147 if ( $confirm ===
'yes' ) {
148 $this->overwriteActorIDs( $field, array_keys( $bad ), $overwrite );
150 $this->fatalError(
'Aborted.' );
154 $this->output(
"Done.\n" );
166 private function findBadActors( $field, $type, $skip ) {
167 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
168 $this->output(
"Finding invalid actor IDs in $table.$actorField...\n" );
170 $dbr = $this->getAnalyticsReplica();
188 $queryBuilder = $dbr->newSelectQueryBuilder()
189 ->select( [ $actorField, $idField ] )
191 ->leftJoin(
'actor',
null, [
"$actorField = actor_id" ] )
192 ->where( $type ==
'missing' ? [
'actor_id' =>
null ] : [
'actor_name' =>
'' ] )
193 ->limit( $this->getBatchSize() );
196 $queryBuilder->andWhere( $dbr->expr( $actorField,
'!=', $skip ) );
199 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
200 $count = $res->numRows();
205 $this->output(
"\t\tID\tACTOR\n" );
208 foreach ( $res as $row ) {
209 $id = $row->$idField;
210 $actor = (int)( $row->$actorField );
213 $this->output(
"\t\t$id\t$actor\n" );
216 $this->output(
"\tFound $count invalid actor IDs.\n" );
218 if ( $count >= $this->getBatchSize() ) {
219 $this->output(
"\tBatch size reached, run again after fixing the current batch.\n" );
234 private function overwriteActorIDs( $field, array $ids,
int $overwrite ) {
235 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
237 $count = count( $ids );
238 $this->output(
"OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
240 $dbw = $this->getPrimaryDB();
242 $dbw->newUpdateQueryBuilder()
244 ->set( [ $actorField => $overwrite ] )
245 ->where( [ $idField => $ids ] )
246 ->caller( __METHOD__ )->execute();
248 $count = $dbw->affectedRows();
250 $this->waitForReplication();
251 $this->output(
"\tUpdated $count rows.\n" );