29require_once __DIR__ .
'/Maintenance.php';
67 parent::__construct();
70 $this->
addOption(
'field',
'The name of a database field to process',
72 $this->
addOption(
'skip',
'A comma-separated list of actor IDs to skip.',
74 $this->
addOption(
'overwrite-with',
'Replace missing actors with this user. '
75 .
'Typically, this would be "Unknown user", but it could be any reserved '
76 .
'system user (per $wgReservedUsernames) or locally registered user. '
77 .
'If not given, invalid actors will only be listed, not fixed. '
78 .
'You will be prompted for confirmation before data is written. ',
91 $services = MediaWikiServices::getInstance();
93 $this->userFactory =
$userFactory ?? $this->userFactory ?? $services->getUserFactory();
94 $this->userNameUtils =
$userNameUtils ?? $this->userNameUtils ?? $services->getUserNameUtils();
95 $this->loadBalancer =
$loadBalancer ?? $this->loadBalancer ?? $services->getDBLoadBalancer();
96 $this->lbFactory =
$lbFactory ?? $this->lbFactory ?? $services->getDBLoadBalancerFactory();
98 $services->getActorNormalization();
107 if ( !$this->tables ) {
109 'ar_actor' => [
'archive',
'ar_actor',
'ar_id' ],
110 'ipb_by_actor' => [
'ipblocks',
'ipb_by_actor',
'ipb_id' ],
111 'img_actor' => [
'image',
'img_actor',
'img_name' ],
112 'oi_actor' => [
'oldimage',
'oi_actor',
'oi_archive_name' ],
113 'fa_actor' => [
'filearchive',
'fa_actor',
'fa_id' ],
114 'rc_actor' => [
'recentchanges',
'rc_actor',
'rc_id' ],
115 'log_actor' => [
'logging',
'log_actor',
'log_id' ],
119 $tables[
'revactor_actor'] = [
'revision_actor_temp',
'revactor_actor',
'revactor_rev' ];
122 $tables[
'rev_actor'] = [
'revision',
'rev_actor',
'rev_id' ];
135 return $tables[$field] ??
null;
148 $name = $this->
getOption(
'overwrite-with' );
150 if ( $name ===
null ) {
154 $user = $this->userFactory->newFromName( $name );
157 $this->
fatalError(
"Not a valid user name: '$user'" );
160 $name = $this->userNameUtils->getCanonical( $name, UserNameUtils::RIGOR_NONE );
162 if ( $user->isRegistered() ) {
163 $this->
output(
"Using existing user: '$user'\n" );
164 } elseif ( !$this->userNameUtils->isValid( $name ) ) {
165 $this->
fatalError(
"Not a valid user name: '$name'" );
166 } elseif ( !$this->userNameUtils->isUsable( $name ) ) {
167 $this->
output(
"Using system user: '$name'\n" );
172 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
173 $actorId = $this->actorNormalization->acquireActorId( $user, $dbw );
176 $this->
fatalError(
"Failed to acquire an actor ID for user '$user'" );
179 $this->
output(
"Replacement actor ID is $actorId.\n" );
188 $this->
fatalError(
"Unknown field: $field.\n" );
196 if ( $bad && $overwrite ) {
198 $this->
output(
"Do you want to OVERWRITE the listed actor IDs?\n" );
199 $this->
output(
"Information about the invalid IDs will be lost!\n" );
201 $confirm = $this->
readconsole(
'Type "yes" to continue: ' );
203 if ( $confirm ===
'yes' ) {
210 $this->
output(
"Done.\n" );
222 [ $table, $actorField, $idField ] = $this->
getTableInfo( $field );
223 $this->
output(
"Finding invalid actor IDs in $table.$actorField...\n" );
225 $dbr = $this->loadBalancer->getConnectionRef(
227 [
'maintenance',
'vslow',
'slow' ]
246 $conds = [
'actor_id' => null ];
249 $conds[] = $actorField .
' NOT IN ( ' .
$dbr->makeList( $skip ) .
' ) ';
252 $queryBuilder =
$dbr->newSelectQueryBuilder();
253 $queryBuilder->table( $table )
254 ->fields( [ $actorField, $idField ] )
256 ->leftJoin(
'actor',
null, [
"$actorField = actor_id" ] )
258 ->caller( __METHOD__ );
260 $res = $queryBuilder->fetchResultSet();
261 $count =
$res->numRows();
266 $this->
output(
"\t\tID\tACTOR\n" );
269 foreach (
$res as $row ) {
270 $id = $row->$idField;
271 $actor = (int)( $row->$actorField );
274 $this->
output(
"\t\t$id\t$actor\n" );
277 $this->
output(
"\tFound $count invalid actor IDs.\n" );
280 $this->
output(
"\tBatch size reached, run again after fixing the current batch.\n" );
296 [ $table, $actorField, $idField ] = $this->
getTableInfo( $field );
298 $count = count( $ids );
299 $this->
output(
"OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
301 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
303 $dbw->update( $table, [ $actorField => $overwrite ], [ $idField => $ids ], __METHOD__ );
305 $count = $dbw->affectedRows();
307 $this->lbFactory->waitForReplication();
308 $this->
output(
"\tUpdated $count rows.\n" );
316require_once RUN_MAINTENANCE_IF_MAIN;
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage, for migration from the temporary table revision_actor_temp to the...
const SCHEMA_COMPAT_WRITE_TEMP
const SCHEMA_COMPAT_WRITE_NEW
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.
UserNameUtils null $userNameUtils
ActorNormalization $actorNormalization
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, ?ActorNormalization $actorNormalization=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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.