47 private $userNameUtils;
52 private $loadBalancer;
62 private $actorNormalization;
68 parent::__construct();
71 $this->
addOption(
'field',
'The name of a database field to process',
73 $this->
addOption(
'type',
'Which type of invalid actors to find or fix, '
74 .
'missing or broken (with empty actor_name which can\'t be associated '
75 .
'with an existing user).',
77 $this->
addOption(
'skip',
'A comma-separated list of actor IDs to skip.',
79 $this->
addOption(
'overwrite-with',
'Replace invalid actors with this user. '
80 .
'Typically, this would be "Unknown user", but it could be any reserved '
81 .
'system user (per $wgReservedUsernames) or locally registered user. '
82 .
'If not given, invalid actors will only be listed, not fixed. '
83 .
'You will be prompted for confirmation before data is written. ',
96 $services = MediaWikiServices::getInstance();
98 $this->userFactory = $userFactory ?? $this->userFactory ?? $services->getUserFactory();
99 $this->userNameUtils = $userNameUtils ?? $this->userNameUtils ?? $services->getUserNameUtils();
100 $this->loadBalancer = $loadBalancer ?? $this->loadBalancer ?? $services->getDBLoadBalancer();
101 $this->lbFactory = $lbFactory ?? $this->lbFactory ?? $services->getDBLoadBalancerFactory();
102 $this->actorNormalization = $actorNormalization ?? $this->actorNormalization ??
103 $services->getActorNormalization();
109 private function getTables() {
110 if ( !$this->tables ) {
112 'ar_actor' => [
'archive',
'ar_actor',
'ar_id' ],
113 'ipb_by_actor' => [
'ipblocks',
'ipb_by_actor',
'ipb_id' ],
114 'img_actor' => [
'image',
'img_actor',
'img_name' ],
115 'oi_actor' => [
'oldimage',
'oi_actor',
'oi_archive_name' ],
116 'fa_actor' => [
'filearchive',
'fa_actor',
'fa_id' ],
117 'rc_actor' => [
'recentchanges',
'rc_actor',
'rc_id' ],
118 'log_actor' => [
'logging',
'log_actor',
'log_id' ],
119 'rev_actor' => [
'revision',
'rev_actor',
'rev_id' ],
121 $this->tables = $tables;
123 return $this->tables;
130 private function getTableInfo( $field ) {
131 $tables = $this->getTables();
132 return $tables[$field] ??
null;
144 private function getNewActorId() {
145 $name = $this->
getOption(
'overwrite-with' );
147 if ( $name ===
null ) {
151 $user = $this->userFactory->newFromName( $name );
154 $this->
fatalError(
"Not a valid user name: '$name'" );
157 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
159 if ( $user->isRegistered() ) {
160 $this->
output(
"Using existing user: '$user'\n" );
161 } elseif ( !$this->userNameUtils->isValid( $name ) ) {
162 $this->
fatalError(
"Not a valid user name: '$name'" );
163 } elseif ( !$this->userNameUtils->isUsable( $name ) ) {
164 $this->
output(
"Using system user: '$name'\n" );
169 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
170 $actorId = $this->actorNormalization->acquireActorId( $user, $dbw );
173 $this->
fatalError(
"Failed to acquire an actor ID for user '$user'" );
176 $this->
output(
"Replacement actor ID is $actorId.\n" );
184 if ( !$this->getTableInfo( $field ) ) {
185 $this->
fatalError(
"Unknown field: $field.\n" );
189 if (
$type !==
'missing' &&
$type !==
'broken' ) {
190 $this->
fatalError(
"Unknown type: $type.\n" );
194 $overwrite = $this->getNewActorId();
196 $bad = $this->findBadActors( $field,
$type, $skip );
198 if ( $bad && $overwrite ) {
200 $this->
output(
"Do you want to OVERWRITE the listed actor IDs?\n" );
201 $this->
output(
"Information about the invalid IDs will be lost!\n" );
205 if ( $confirm ===
'yes' ) {
206 $this->overwriteActorIDs( $field, array_keys( $bad ), $overwrite );
212 $this->
output(
"Done.\n" );
224 private function findBadActors( $field,
$type, $skip ) {
225 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
226 $this->
output(
"Finding invalid actor IDs in $table.$actorField...\n" );
228 $dbr = $this->loadBalancer->getConnectionRef(
230 [
'maintenance',
'vslow',
'slow' ]
249 $conds =
$type ==
'missing'
250 ? [
'actor_id' => null ]
251 : [
'actor_name' =>
'' ];
254 $conds[] = $actorField .
' NOT IN ( ' .
$dbr->makeList( $skip ) .
' ) ';
257 $queryBuilder =
$dbr->newSelectQueryBuilder();
258 $queryBuilder->table( $table )
259 ->fields( [ $actorField, $idField ] )
261 ->leftJoin(
'actor',
null, [
"$actorField = actor_id" ] )
263 ->caller( __METHOD__ );
265 $res = $queryBuilder->fetchResultSet();
266 $count =
$res->numRows();
271 $this->
output(
"\t\tID\tACTOR\n" );
274 foreach (
$res as $row ) {
275 $id = $row->$idField;
276 $actor = (int)( $row->$actorField );
279 $this->
output(
"\t\t$id\t$actor\n" );
282 $this->
output(
"\tFound $count invalid actor IDs.\n" );
285 $this->
output(
"\tBatch size reached, run again after fixing the current batch.\n" );
300 private function overwriteActorIDs( $field, array $ids,
int $overwrite ) {
301 [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
303 $count = count( $ids );
304 $this->
output(
"OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
306 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
308 $dbw->update( $table, [ $actorField => $overwrite ], [ $idField => $ids ], __METHOD__ );
310 $count = $dbw->affectedRows();
312 $this->lbFactory->waitForReplication();
313 $this->
output(
"\tUpdated $count rows.\n" );
initializeServices(?UserFactory $userFactory=null, ?UserNameUtils $userNameUtils=null, ?LoadBalancer $loadBalancer=null, ?LBFactory $lbFactory=null, ?ActorNormalization $actorNormalization=null)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
static readconsole( $prompt='> ')
Prompt the console for input.
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.
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.