MediaWiki  1.33.0
cleanupUsersWithNoId.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
36  private $prefix, $table, $assign;
37  private $triedCreations = [];
38 
39  public function __construct() {
40  parent::__construct();
41  $this->addDescription( 'Cleans up tables that have valid usernames with no user ID' );
42  $this->addOption( 'prefix', 'Interwiki prefix to apply to the usernames', true, true, 'p' );
43  $this->addOption( 'table', 'Only clean up this table', false, true );
44  $this->addOption( 'assign', 'Assign edits to existing local users if they exist', false, false );
45  $this->setBatchSize( 100 );
46  }
47 
48  protected function getUpdateKey() {
49  return __CLASS__;
50  }
51 
52  protected function doDBUpdates() {
53  $this->prefix = $this->getOption( 'prefix' );
54  $this->table = $this->getOption( 'table', null );
55  $this->assign = $this->getOption( 'assign' );
56 
57  $this->cleanup(
58  'revision', 'rev_id', 'rev_user', 'rev_user_text',
59  [ 'rev_user' => 0 ], [ 'rev_timestamp', 'rev_id' ]
60  );
61  $this->cleanup(
62  'archive', 'ar_id', 'ar_user', 'ar_user_text',
63  [], [ 'ar_id' ]
64  );
65  $this->cleanup(
66  'logging', 'log_id', 'log_user', 'log_user_text',
67  [ 'log_user' => 0 ], [ 'log_timestamp', 'log_id' ]
68  );
69  $this->cleanup(
70  'image', 'img_name', 'img_user', 'img_user_text',
71  [ 'img_user' => 0 ], [ 'img_timestamp', 'img_name' ]
72  );
73  $this->cleanup(
74  'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text',
75  [], [ 'oi_name', 'oi_timestamp' ]
76  );
77  $this->cleanup(
78  'filearchive', 'fa_id', 'fa_user', 'fa_user_text',
79  [], [ 'fa_id' ]
80  );
81  $this->cleanup(
82  'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text',
83  [], [ 'ipb_id' ]
84  );
85  $this->cleanup(
86  'recentchanges', 'rc_id', 'rc_user', 'rc_user_text',
87  [], [ 'rc_id' ]
88  );
89 
90  return true;
91  }
92 
100  private function makeNextCond( $dbw, $indexFields, $row ) {
101  $next = '';
102  $display = [];
103  for ( $i = count( $indexFields ) - 1; $i >= 0; $i-- ) {
104  $field = $indexFields[$i];
105  $display[] = $field . '=' . $row->$field;
106  $value = $dbw->addQuotes( $row->$field );
107  if ( $next === '' ) {
108  $next = "$field > $value";
109  } else {
110  $next = "$field > $value OR $field = $value AND ($next)";
111  }
112  }
113  $display = implode( ' ', array_reverse( $display ) );
114  return [ $next, $display ];
115  }
116 
127  protected function cleanup(
128  $table, $primaryKey, $idField, $nameField, array $conds, array $orderby
129  ) {
130  if ( $this->table !== null && $this->table !== $table ) {
131  return;
132  }
133 
134  $primaryKey = (array)$primaryKey;
135  $pkFilter = array_flip( $primaryKey );
136  $this->output(
137  "Beginning cleanup of $table\n"
138  );
139 
140  $dbw = $this->getDB( DB_MASTER );
141  $next = '1=1';
142  $countAssigned = 0;
143  $countPrefixed = 0;
144  while ( true ) {
145  // Fetch the rows needing update
146  $res = $dbw->select(
147  $table,
148  array_merge( $primaryKey, [ $idField, $nameField ], $orderby ),
149  array_merge( $conds, [ $next ] ),
150  __METHOD__,
151  [
152  'ORDER BY' => $orderby,
153  'LIMIT' => $this->mBatchSize,
154  ]
155  );
156  if ( !$res->numRows() ) {
157  break;
158  }
159 
160  // Update the existing rows
161  foreach ( $res as $row ) {
162  $name = $row->$nameField;
163  if ( $row->$idField || !User::isUsableName( $name ) ) {
164  continue;
165  }
166 
167  $id = 0;
168  if ( $this->assign ) {
169  $id = (int)User::idFromName( $name );
170  if ( !$id ) {
171  // See if any extension wants to create it.
172  if ( !isset( $this->triedCreations[$name] ) ) {
173  $this->triedCreations[$name] = true;
174  if ( !Hooks::run( 'ImportHandleUnknownUser', [ $name ] ) ) {
175  $id = (int)User::idFromName( $name, User::READ_LATEST );
176  }
177  }
178  }
179  }
180  if ( $id ) {
181  $set = [ $idField => $id ];
182  $counter = &$countAssigned;
183  } else {
184  $set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
185  $counter = &$countPrefixed;
186  }
187 
188  $dbw->update(
189  $table,
190  $set,
191  array_intersect_key( (array)$row, $pkFilter ) + [
192  $idField => 0,
193  $nameField => $name,
194  ],
195  __METHOD__
196  );
197  $counter += $dbw->affectedRows();
198  }
199 
200  list( $next, $display ) = $this->makeNextCond( $dbw, $orderby, $row );
201  $this->output( "... $display\n" );
202  wfWaitForSlaves();
203  }
204 
205  $this->output(
206  "Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
207  );
208  }
209 }
210 
212 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
$maintClass
Definition: cleanupUsersWithNoId.php:211
CleanupUsersWithNoId\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: cleanupUsersWithNoId.php:52
CleanupUsersWithNoId\$assign
$assign
Definition: cleanupUsersWithNoId.php:36
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
CleanupUsersWithNoId\makeNextCond
makeNextCond( $dbw, $indexFields, $row)
Calculate a "next" condition and progress display string.
Definition: cleanupUsersWithNoId.php:100
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
CleanupUsersWithNoId\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: cleanupUsersWithNoId.php:48
CleanupUsersWithNoId\__construct
__construct()
Default constructor.
Definition: cleanupUsersWithNoId.php:39
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2790
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
CleanupUsersWithNoId\$table
$table
Definition: cleanupUsersWithNoId.php:36
CleanupUsersWithNoId
Maintenance script that cleans up tables that have valid usernames with no user ID.
Definition: cleanupUsersWithNoId.php:35
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1700
table
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs table
Definition: deferred.txt:11
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
CleanupUsersWithNoId\cleanup
cleanup( $table, $primaryKey, $idField, $nameField, array $conds, array $orderby)
Cleanup a table.
Definition: cleanupUsersWithNoId.php:127
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
$value
$value
Definition: styleTest.css.php:49
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:283
User\idFromName
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:905
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
CleanupUsersWithNoId\$triedCreations
$triedCreations
Definition: cleanupUsersWithNoId.php:37
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
User\isUsableName
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...
Definition: User.php:1042
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:375
CleanupUsersWithNoId\$prefix
$prefix
Definition: cleanupUsersWithNoId.php:36