28require_once __DIR__ .
'/Maintenance.php';
59 'revactor_actor' => [
'revision_actor_temp',
'revactor_actor',
'revactor_rev' ],
60 'ar_actor' => [
'archive',
'ar_actor',
'ar_id' ],
61 'ipb_by_actor' => [
'ipblocks',
'ipb_by_actor',
'ipb_id' ],
62 'img_actor' => [
'image',
'img_actor',
'img_name' ],
63 'oi_actor' => [
'oldimage',
'oi_actor',
'oi_archive_name' ],
64 'fa_actor' => [
'filearchive',
'fa_actor',
'fa_id' ],
65 'rc_actor' => [
'recentchanges',
'rc_actor',
'rc_id' ],
66 'log_actor' => [
'logging',
'log_actor',
'log_id' ],
70 parent::__construct();
73 $this->
addOption(
'field',
'The name of a database field to process. '
74 .
'Possible values: ' . implode(
', ', array_keys( self::TABLES ) ),
76 $this->
addOption(
'skip',
'A comma-separated list of actor IDs to skip.',
78 $this->
addOption(
'overwrite-with',
'Replace missing actors with this user. '
79 .
'Typically, this would be "Unknown user", but it could be any reserved '
80 .
'system user (per $wgReservedUsernames) or locally registered user. '
81 .
'If not given, invalid actors will only be listed, not fixed. '
82 .
'You will be prompted for confirmation before data is written. ',
94 $services = MediaWikiServices::getInstance();
96 $this->userFactory =
$userFactory ?? $this->userFactory ?? $services->getUserFactory();
97 $this->userNameUtils =
$userNameUtils ?? $this->userNameUtils ?? $services->getUserNameUtils();
98 $this->loadBalancer =
$loadBalancer ?? $this->loadBalancer ?? $services->getDBLoadBalancer();
99 $this->lbFactory =
$lbFactory ?? $this->lbFactory ?? $services->getDBLoadBalancerFactory();
112 $name = $this->
getOption(
'overwrite-with' );
114 if ( $name ===
null ) {
118 $user = $this->userFactory->newFromName( $name );
121 $this->
fatalError(
"Not a valid user name: '$user'" );
124 $name = $this->userNameUtils->getCanonical( $name, UserNameUtils::RIGOR_NONE );
126 if ( $user->isRegistered() ) {
127 $this->
output(
"Using existing user: '$user'\n" );
128 } elseif ( !$this->userNameUtils->isValid( $name ) ) {
129 $this->
fatalError(
"Not a valid user name: '$name'" );
130 } elseif ( !$this->userNameUtils->isUsable( $name ) ) {
131 $this->
output(
"Using system user: '$name'\n" );
137 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
138 $actorId = $user->getActorId( $dbw );
141 $this->
fatalError(
"Failed to acquire an actor ID for user '$user'" );
144 $this->
output(
"Replacement actor ID is $actorId.\n" );
155 if ( !isset( self::TABLES[$field] ) ) {
156 $this->
fatalError(
"Unknown field: $field.\n" );
164 if ( $bad && $overwrite ) {
166 $this->
output(
"Do you want to OVERWRITE the listed actor IDs?\n" );
167 $this->
output(
"Information about the invalid IDs will be lost!\n" );
169 $confirm = $this->
readconsole(
'Type "yes" to continue: ' );
171 if ( $confirm ===
'yes' ) {
178 $this->
output(
"Done.\n" );
190 [ $table, $actorField, $idField ] = self::TABLES[$field];
191 $this->
output(
"Finding invalid actor IDs in $table.$actorField...\n" );
193 $dbr = $this->loadBalancer->getConnectionRef(
195 [
'maintenance',
'vslow',
'slow' ]
214 $conds = [
'actor_id' => null ];
217 $conds[] = $actorField .
' NOT IN ( ' .
$dbr->makeList( $skip ) .
' ) ';
220 $queryBuilder =
$dbr->newSelectQueryBuilder();
221 $queryBuilder->table( $table )
222 ->fields( [ $actorField, $idField ] )
224 ->leftJoin(
'actor',
null, [
"$actorField = actor_id" ] )
226 ->caller( __METHOD__ );
228 $res = $queryBuilder->fetchResultSet();
229 $count =
$res->numRows();
234 $this->
output(
"\t\tID\tACTOR\n" );
237 foreach (
$res as $row ) {
238 $id = $row->$idField;
239 $actor = (int)( $row->$actorField );
242 $this->
output(
"\t\t$id\t$actor\n" );
245 $this->
output(
"\tFound $count invalid actor IDs.\n" );
248 $this->
output(
"\tBatch size reached, run again after fixing the current batch.\n" );
264 [ $table, $actorField, $idField ] = self::TABLES[$field];
266 $count = count( $ids );
267 $this->
output(
"OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
269 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
271 $dbw->update( $table, [ $actorField => $overwrite ], [ $idField => $ids ], __METHOD__ );
273 $count = $dbw->affectedRows();
275 $this->lbFactory->waitForReplication();
276 $this->
output(
"\tUpdated $count rows.\n" );
const RUN_MAINTENANCE_IF_MAIN
Maintenance script for finding and replacing invalid actor IDs, see T261325.
findBadActors( $field, $skip)
Find rows that have bad actor IDs.
execute()
Do the actual work.All child classes will need to implement thisbool|null|void True for success,...
UserNameUtils null $userNameUtils
getNewActorId()
Returns the actor ID of the user specified with the –overwrite-with option, or null if –overwrite-wit...
initializeServices(?UserFactory $userFactory=null, ?UserNameUtils $userNameUtils=null, ?LoadBalancer $loadBalancer=null, ?LBFactory $lbFactory=null)
UserFactory null $userFactory
overwriteActorIDs( $field, array $ids, int $overwrite)
Overwrite the actor ID in a given set of rows.
__construct()
Default constructor.
LoadBalancer null $loadBalancer
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.
getBatchSize()
Returns batch size.
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.
setBatchSize( $s=0)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.