MediaWiki  master
findMissingActors.php
Go to the documentation of this file.
1 <?php
28 
29 require_once __DIR__ . '/Maintenance.php';
30 
37 
41  private $userFactory;
42 
46  private $userNameUtils;
47 
51  private $loadBalancer;
52 
56  private $lbFactory;
57 
61  private $actorNormalization;
62 
64  private $tables;
65 
66  public function __construct() {
67  parent::__construct();
68 
69  $this->addDescription( 'Find and fix invalid actor IDs.' );
70  $this->addOption( 'field', 'The name of a database field to process',
71  true, true );
72  $this->addOption( 'type', 'Which type of invalid actors to find or fix, '
73  . 'missing or broken (with empty actor_name which can\'t be associated '
74  . 'with an existing user).',
75  false, true );
76  $this->addOption( 'skip', 'A comma-separated list of actor IDs to skip.',
77  false, true );
78  $this->addOption( 'overwrite-with', 'Replace invalid 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. ',
83  false, true );
84 
85  $this->setBatchSize( 1000 );
86  }
87 
88  public function initializeServices(
89  ?UserFactory $userFactory = null,
90  ?UserNameUtils $userNameUtils = null,
91  ?LoadBalancer $loadBalancer = null,
92  ?LBFactory $lbFactory = null,
93  ?ActorNormalization $actorNormalization = null
94  ) {
95  $services = $this->getServiceContainer();
96 
97  $this->userFactory = $userFactory ?? $this->userFactory ?? $services->getUserFactory();
98  $this->userNameUtils = $userNameUtils ?? $this->userNameUtils ?? $services->getUserNameUtils();
99  $this->loadBalancer = $loadBalancer ?? $this->loadBalancer ?? $services->getDBLoadBalancer();
100  $this->lbFactory = $lbFactory ?? $this->lbFactory ?? $services->getDBLoadBalancerFactory();
101  $this->actorNormalization = $actorNormalization ?? $this->actorNormalization ??
102  $services->getActorNormalization();
103  }
104 
108  private function getTables() {
109  if ( !$this->tables ) {
110  $tables = [
111  'ar_actor' => [ 'archive', 'ar_actor', 'ar_id' ],
112  'ipb_by_actor' => [ 'ipblocks', 'ipb_by_actor', 'ipb_id' ], // no index on ipb_by_actor!
113  'img_actor' => [ 'image', 'img_actor', 'img_name' ],
114  'oi_actor' => [ 'oldimage', 'oi_actor', 'oi_archive_name' ], // no index on oi_archive_name!
115  'fa_actor' => [ 'filearchive', 'fa_actor', 'fa_id' ],
116  'rc_actor' => [ 'recentchanges', 'rc_actor', 'rc_id' ],
117  'log_actor' => [ 'logging', 'log_actor', 'log_id' ],
118  'rev_actor' => [ 'revision', 'rev_actor', 'rev_id' ],
119  ];
120  $this->tables = $tables;
121  }
122  return $this->tables;
123  }
124 
129  private function getTableInfo( $field ) {
130  $tables = $this->getTables();
131  return $tables[$field] ?? null;
132  }
133 
143  private function getNewActorId() {
144  $name = $this->getOption( 'overwrite-with' );
145 
146  if ( $name === null ) {
147  return null;
148  }
149 
150  $user = $this->userFactory->newFromName( $name );
151 
152  if ( !$user ) {
153  $this->fatalError( "Not a valid user name: '$name'" );
154  }
155 
156  $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
157 
158  if ( $user->isRegistered() ) {
159  $this->output( "Using existing user: '$user'\n" );
160  } elseif ( !$this->userNameUtils->isValid( $name ) ) {
161  $this->fatalError( "Not a valid user name: '$name'" );
162  } elseif ( !$this->userNameUtils->isUsable( $name ) ) {
163  $this->output( "Using system user: '$name'\n" );
164  } else {
165  $this->fatalError( "Unknown user: '$name'" );
166  }
167 
168  $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
169  $actorId = $this->actorNormalization->acquireActorId( $user, $dbw );
170 
171  if ( !$actorId ) {
172  $this->fatalError( "Failed to acquire an actor ID for user '$user'" );
173  }
174 
175  $this->output( "Replacement actor ID is $actorId.\n" );
176  return $actorId;
177  }
178 
179  public function execute() {
180  $this->initializeServices();
181 
182  $field = $this->getOption( 'field' );
183  if ( !$this->getTableInfo( $field ) ) {
184  $this->fatalError( "Unknown field: $field.\n" );
185  }
186 
187  $type = $this->getOption( 'type', 'missing' );
188  if ( $type !== 'missing' && $type !== 'broken' ) {
189  $this->fatalError( "Unknown type: $type.\n" );
190  }
191 
192  $skip = $this->parseIntList( $this->getOption( 'skip', '' ) );
193  $overwrite = $this->getNewActorId();
194 
195  $bad = $this->findBadActors( $field, $type, $skip );
196 
197  if ( $bad && $overwrite ) {
198  $this->output( "\n" );
199  $this->output( "Do you want to OVERWRITE the listed actor IDs?\n" );
200  $this->output( "Information about the invalid IDs will be lost!\n" );
201  $this->output( "\n" );
202  $confirm = self::readconsole( 'Type "yes" to continue: ' );
203 
204  if ( $confirm === 'yes' ) {
205  $this->overwriteActorIDs( $field, array_keys( $bad ), $overwrite );
206  } else {
207  $this->fatalError( 'Aborted.' );
208  }
209  }
210 
211  $this->output( "Done.\n" );
212  }
213 
223  private function findBadActors( $field, $type, $skip ) {
224  [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
225  $this->output( "Finding invalid actor IDs in $table.$actorField...\n" );
226 
227  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA, 'vslow' );
228 
229  /*
230  We are building an SQL query like this one here, performing a left join
231  to detect rows in $table that lack a matching row in the actor table.
232 
233  In this example, $field is 'log_actor', so $table is 'logging',
234  $actorField is 'log_actor', and $idField is 'log_id'.
235  Further, $skip is [ 1, 2, 3, 4 ] and the batch size is 1000.
236 
237  SELECT log_id
238  FROM logging
239  LEFT JOIN actor ON log_actor = actor_id
240  WHERE actor_id IS NULL
241  AND log_actor NOT IN (1, 2, 3, 4)
242  LIMIT 1000;
243  */
244 
245  $conds = $type == 'missing'
246  ? [ 'actor_id' => null ]
247  : [ 'actor_name' => '' ];
248 
249  if ( $skip ) {
250  $conds[] = $actorField . ' NOT IN ( ' . $dbr->makeList( $skip ) . ' ) ';
251  }
252 
253  $queryBuilder = $dbr->newSelectQueryBuilder();
254  $queryBuilder->table( $table )
255  ->fields( [ $actorField, $idField ] )
256  ->conds( $conds )
257  ->leftJoin( 'actor', null, [ "$actorField = actor_id" ] )
258  ->limit( $this->getBatchSize() )
259  ->caller( __METHOD__ );
260 
261  $res = $queryBuilder->fetchResultSet();
262  $count = $res->numRows();
263 
264  $bad = [];
265 
266  if ( $count ) {
267  $this->output( "\t\tID\tACTOR\n" );
268  }
269 
270  foreach ( $res as $row ) {
271  $id = $row->$idField;
272  $actor = (int)( $row->$actorField );
273 
274  $bad[$id] = $actor;
275  $this->output( "\t\t$id\t$actor\n" );
276  }
277 
278  $this->output( "\tFound $count invalid actor IDs.\n" );
279 
280  if ( $count >= $this->getBatchSize() ) {
281  $this->output( "\tBatch size reached, run again after fixing the current batch.\n" );
282  }
283 
284  return $bad;
285  }
286 
296  private function overwriteActorIDs( $field, array $ids, int $overwrite ) {
297  [ $table, $actorField, $idField ] = $this->getTableInfo( $field );
298 
299  $count = count( $ids );
300  $this->output( "OVERWRITING $count actor IDs in $table.$actorField with $overwrite...\n" );
301 
302  $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
303 
304  $dbw->update( $table, [ $actorField => $overwrite ], [ $idField => $ids ], __METHOD__ );
305 
306  $count = $dbw->affectedRows();
307 
308  $this->lbFactory->waitForReplication();
309  $this->output( "\tUpdated $count rows.\n" );
310 
311  return $count;
312  }
313 
314 }
315 
316 $maintClass = FindMissingActors::class;
317 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script for finding and replacing invalid actor IDs, see T261325 and T307738.
execute()
Do the actual work.
initializeServices(?UserFactory $userFactory=null, ?UserNameUtils $userNameUtils=null, ?LoadBalancer $loadBalancer=null, ?LBFactory $lbFactory=null, ?ActorNormalization $actorNormalization=null)
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
output( $out, $channel=null)
Throw some output to the user.
static readconsole( $prompt='> ')
Prompt the console for input.
getServiceContainer()
Returns the main service container.
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)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Creates User objects.
Definition: UserFactory.php:41
UserNameUtils service.
Service for dealing with the actor table.
Shared interface for rigor levels when dealing with User methods.
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28