MediaWiki REL1_34
findMissingActors.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
34
39
43 private $lbFactory;
44
45 private const TABLES = [
46 // 'rev_actor' => [ 'revision', 'rev_actor', 'rev_id' ], // not yet used in 1.35
47 'revactor_actor' => [ 'revision_actor_temp', 'revactor_actor', 'revactor_rev' ],
48 'ar_actor' => [ 'archive', 'ar_actor', 'ar_id' ],
49 'ipb_by_actor' => [ 'ipblocks', 'ipb_by_actor', 'ipb_id' ], // no index on ipb_by_actor!
50 'img_actor' => [ 'image', 'img_actor', 'img_name' ],
51 'oi_actor' => [ 'oldimage', 'oi_actor', 'oi_archive_name' ], // no index on oi_archive_name!
52 'fa_actor' => [ 'filearchive', 'fa_actor', 'fa_id' ],
53 'rc_actor' => [ 'recentchanges', 'rc_actor', 'rc_id' ],
54 'log_actor' => [ 'logging', 'log_actor', 'log_id' ],
55 ];
56
57 public function __construct() {
58 parent::__construct();
59
60 $this->addDescription( 'Find and fix invalid actor IDs.' );
61 $this->addOption( 'field', 'The name of a database field to process. '
62 . 'Possible values: ' . implode( ', ', array_keys( self::TABLES ) ),
63 true, true );
64 $this->addOption( 'skip', 'A comma-separated list of actor IDs to skip.',
65 false, true );
66 $this->addOption( 'overwrite-with', 'Replace missing actors with this user. '
67 . 'Typically, this would be "Unknown user", but it could be any reserved '
68 . 'system user (per $wgReservedUsernames) or locally registered user. '
69 . 'If not given, invalid actors will only be listed, not fixed. '
70 . 'You will be prompted for confirmation before data is written. ',
71 false, true );
72
73 $this->setBatchSize( 1000 );
74 }
75
80 public function initializeServices(
81 $userFactory = null,
82 $userNameUtils = null,
84 ?LBFactory $lbFactory = null
85 ) {
86 $services = MediaWikiServices::getInstance();
87
88 $this->loadBalancer = $loadBalancer ?? $this->loadBalancer ?? $services->getDBLoadBalancer();
89 $this->lbFactory = $lbFactory ?? $this->lbFactory ?? $services->getDBLoadBalancerFactory();
90 }
91
101 private function getNewActorId() {
102 $name = $this->getOption( 'overwrite-with' );
103
104 if ( $name === null ) {
105 return null;
106 }
107
108 $user = User::newFromName( $name );
109
110 if ( !$user ) {
111 $this->fatalError( "Not a valid user name: '$user'" );
112 }
113
114 $name = User::getCanonicalName( $name );
115
116 if ( $user->isRegistered() ) {
117 $this->output( "Using existing user: '$user'\n" );
118 } elseif ( !User::isValidUserName( $name ) ) {
119 $this->fatalError( "Not a valid user name: '$name'" );
120 } elseif ( !User::isUsableName( $name ) ) {
121 $this->output( "Using system user: '$name'\n" );
122 } else {
123 $this->fatalError( "Unknown user: '$name'" );
124 }
125
126 // Supply write connection to assign an actor ID if needed.
127 $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
128 $actorId = $user->getActorId( $dbw );
129
130 if ( !$actorId ) {
131 $this->fatalError( "Failed to acquire an actor ID for user '$user'" );
132 }
133
134 $this->output( "Replacement actor ID is $actorId.\n" );
135 return $actorId;
136 }
137
141 public function execute() {
142 $this->initializeServices();
143
144 $field = $this->getOption( 'field' );
145 if ( !isset( self::TABLES[$field] ) ) {
146 $this->fatalError( "Unknown field: $field.\n" );
147 }
148
149 $skip = $this->parseIntList( $this->getOption( 'skip', '' ) );
150 $overwrite = $this->getNewActorId();
151
152 $bad = $this->findBadActors( $field, $skip );
153
154 if ( $bad && $overwrite ) {
155 $this->output( "\n" );
156 $this->output( "Do you want to OVERWRITE the listed actor IDs?\n" );
157 $this->output( "Information about the invalid IDs will be lost!\n" );
158 $this->output( "\n" );
159 $confirm = $this->readconsole( 'Type "yes" to continue: ' );
160
161 if ( $confirm === 'yes' ) {
162 $this->overwriteActorIDs( $field, array_keys( $bad ), $overwrite );
163 } else {
164 $this->fatalError( 'Aborted.' );
165 }
166 }
167
168 $this->output( "Done.\n" );
169 }
170
179 private function findBadActors( $field, $skip ) {
180 [ $table, $actorField, $idField ] = self::TABLES[$field];
181 $this->output( "Finding invalid actor IDs in $table.$actorField...\n" );
182
183 $dbr = $this->loadBalancer->getConnectionRef(
185 [ 'maintenance', 'vslow', 'slow' ]
186 );
187
188 /*
189 We are building an SQL query like this one here, performing a left join
190 to detect rows in $table that lack a matching row in the actor table.
191
192 In this example, $field is 'log_actor', so $table is 'logging',
193 $actorField is 'log_actor', and $idField is 'log_id'.
194 Further, $skip is [ 1, 2, 3, 4 ] and the batch size is 1000.
195
196 SELECT log_id
197 FROM logging
198 JOIN actor ON log_actor = actor_id
199 WHERE actor_id IS NULL
200 AND log_actor NOT IN (1, 2, 3, 4)
201 LIMIT 1000;
202 */
203
204 $conds = [ 'actor_id' => null ];
205
206 if ( $skip ) {
207 $conds[] = $actorField . ' NOT IN ( ' . $dbr->makeList( $skip ) . ' ) ';
208 }
209
210 $queryBuilder = $dbr->newSelectQueryBuilder();
211 $queryBuilder->table( $table )
212 ->fields( [ $actorField, $idField ] )
213 ->conds( $conds )
214 ->leftJoin( 'actor', null, [ "$actorField = actor_id" ] )
215 ->limit( $this->getBatchSize() )
216 ->caller( __METHOD__ );
217
218 $res = $queryBuilder->fetchResultSet();
219 $count = $res->numRows();
220
221 $bad = [];
222
223 if ( $count ) {
224 $this->output( "\t\tID\tACTOR\n" );
225 }
226
227 foreach ( $res as $row ) {
228 $id = $row->$idField;
229 $actor = (int)( $row->$actorField );
230
231 $bad[$id] = $actor;
232 $this->output( "\t\t$id\t$actor\n" );
233 }
234
235 $this->output( "\tFound $count invalid actor IDs.\n" );
236
237 if ( $count >= $this->getBatchSize() ) {
238 $this->output( "\tBatch size reached, run again after fixing the current batch.\n" );
239 }
240
241 return $bad;
242 }
243
253 private function overwriteActorIDs( $field, array $ids, int $overwrite ) {
254 [ $table, $actorField, $idField ] = self::TABLES[$field];
255
256 $count = count( $ids );
257 $this->output( "OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
258
259 $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
260
261 $dbw->update( $table, [ $actorField => $overwrite ], [ $idField => $ids ], __METHOD__ );
262
263 $count = $dbw->affectedRows();
264
265 $this->lbFactory->waitForReplication();
266 $this->output( "\tUpdated $count rows.\n" );
267
268 return $count;
269 }
270
271}
272
273$maintClass = FindMissingActors::class;
274require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
Maintenance script for finding and replacing invalid actor IDs, see T261325.
initializeServices( $userFactory=null, $userNameUtils=null, ?LoadBalancer $loadBalancer=null, ?LBFactory $lbFactory=null)
The $userFactory and $userNameUtils only exist to prevent the function signature having a breaking ch...
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,...
getNewActorId()
Returns the actor ID of the user specified with the –overwrite-with option, or null if –overwrite-wit...
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
An interface for generating database load balancers.
Definition LBFactory.php:40
Database connection, tracking, load balancing, and transaction manager for a cluster.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26