MediaWiki  master
cleanupUsersWithNoId.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
37  private $prefix, $table, $assign;
38  private $triedCreations = [];
39 
40  public function __construct() {
41  parent::__construct();
42  $this->addDescription( 'Cleans up tables that have valid usernames with no user ID' );
43  $this->addOption( 'prefix', 'Interwiki prefix to apply to the usernames', true, true, 'p' );
44  $this->addOption( 'table', 'Only clean up this table', false, true );
45  $this->addOption( 'assign', 'Assign edits to existing local users if they exist', false, false );
46  $this->setBatchSize( 100 );
47  }
48 
49  protected function getUpdateKey() {
50  return __CLASS__;
51  }
52 
53  protected function doDBUpdates() {
54  $this->prefix = $this->getOption( 'prefix' );
55  $this->table = $this->getOption( 'table', null );
56  $this->assign = $this->getOption( 'assign' );
57 
58  $this->cleanup(
59  'revision', 'rev_id', 'rev_user', 'rev_user_text',
60  [ 'rev_user' => 0 ], [ 'rev_timestamp', 'rev_id' ]
61  );
62  $this->cleanup(
63  'archive', 'ar_id', 'ar_user', 'ar_user_text',
64  [], [ 'ar_id' ]
65  );
66  $this->cleanup(
67  'logging', 'log_id', 'log_user', 'log_user_text',
68  [ 'log_user' => 0 ], [ 'log_timestamp', 'log_id' ]
69  );
70  $this->cleanup(
71  'image', 'img_name', 'img_user', 'img_user_text',
72  [ 'img_user' => 0 ], [ 'img_timestamp', 'img_name' ]
73  );
74  $this->cleanup(
75  'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text',
76  [], [ 'oi_name', 'oi_timestamp' ]
77  );
78  $this->cleanup(
79  'filearchive', 'fa_id', 'fa_user', 'fa_user_text',
80  [], [ 'fa_id' ]
81  );
82  $this->cleanup(
83  'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text',
84  [], [ 'ipb_id' ]
85  );
86  $this->cleanup(
87  'recentchanges', 'rc_id', 'rc_user', 'rc_user_text',
88  [], [ 'rc_id' ]
89  );
90 
91  return true;
92  }
93 
101  private function makeNextCond( $dbw, $indexFields, $row ) {
102  $next = '';
103  $display = [];
104  for ( $i = count( $indexFields ) - 1; $i >= 0; $i-- ) {
105  $field = $indexFields[$i];
106  $display[] = $field . '=' . $row->$field;
107  $value = $dbw->addQuotes( $row->$field );
108  if ( $next === '' ) {
109  $next = "$field > $value";
110  } else {
111  $next = "$field > $value OR $field = $value AND ($next)";
112  }
113  }
114  $display = implode( ' ', array_reverse( $display ) );
115  return [ $next, $display ];
116  }
117 
128  protected function cleanup(
129  $table, $primaryKey, $idField, $nameField, array $conds, array $orderby
130  ) {
131  if ( $this->table !== null && $this->table !== $table ) {
132  return;
133  }
134 
135  $dbw = $this->getDB( DB_MASTER );
136  if ( !$dbw->fieldExists( $table, $idField ) ||
137  !$dbw->fieldExists( $table, $nameField )
138  ) {
139  $this->output( "Skipping $table, fields $idField and/or $nameField do not exist\n" );
140  return;
141  }
142 
143  $primaryKey = (array)$primaryKey;
144  $pkFilter = array_flip( $primaryKey );
145  $this->output( "Beginning cleanup of $table\n" );
146 
147  $next = '1=1';
148  $countAssigned = 0;
149  $countPrefixed = 0;
150  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
151  while ( true ) {
152  // Fetch the rows needing update
153  $res = $dbw->select(
154  $table,
155  array_merge( $primaryKey, [ $idField, $nameField ], $orderby ),
156  array_merge( $conds, [ $next ] ),
157  __METHOD__,
158  [
159  'ORDER BY' => $orderby,
160  'LIMIT' => $this->mBatchSize,
161  ]
162  );
163  if ( !$res->numRows() ) {
164  break;
165  }
166 
167  // Update the existing rows
168  foreach ( $res as $row ) {
169  $name = $row->$nameField;
170  if ( $row->$idField || !User::isUsableName( $name ) ) {
171  continue;
172  }
173 
174  $id = 0;
175  if ( $this->assign ) {
176  $id = User::idFromName( $name );
177  if ( !$id ) {
178  // See if any extension wants to create it.
179  if ( !isset( $this->triedCreations[$name] ) ) {
180  $this->triedCreations[$name] = true;
181  if ( !$this->getHookRunner()->onImportHandleUnknownUser( $name ) ) {
182  $id = User::idFromName( $name, User::READ_LATEST );
183  }
184  }
185  }
186  }
187  if ( $id ) {
188  $set = [ $idField => $id ];
189  $counter = &$countAssigned;
190  } else {
191  $set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
192  $counter = &$countPrefixed;
193  }
194 
195  $dbw->update(
196  $table,
197  $set,
198  array_intersect_key( (array)$row, $pkFilter ) + [
199  $idField => 0,
200  $nameField => $name,
201  ],
202  __METHOD__
203  );
204  $counter += $dbw->affectedRows();
205  }
206 
207  list( $next, $display ) = $this->makeNextCond( $dbw, $orderby, $row );
208  $this->output( "... $display\n" );
209  $lbFactory->waitForReplication();
210  }
211 
212  $this->output(
213  "Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
214  );
215  }
216 }
217 
218 $maintClass = CleanupUsersWithNoId::class;
219 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
$maintClass
$maintClass
Definition: cleanupUsersWithNoId.php:218
CleanupUsersWithNoId\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: cleanupUsersWithNoId.php:53
CleanupUsersWithNoId\$assign
$assign
Definition: cleanupUsersWithNoId.php:37
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:149
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
CleanupUsersWithNoId\makeNextCond
makeNextCond( $dbw, $indexFields, $row)
Calculate a "next" condition and progress display string.
Definition: cleanupUsersWithNoId.php:101
CleanupUsersWithNoId\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: cleanupUsersWithNoId.php:49
$res
$res
Definition: testCompression.php:57
CleanupUsersWithNoId\__construct
__construct()
Default constructor.
Definition: cleanupUsersWithNoId.php:40
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
CleanupUsersWithNoId\$table
$table
Definition: cleanupUsersWithNoId.php:37
CleanupUsersWithNoId
Maintenance script that cleans up tables that have valid usernames with no user ID.
Definition: cleanupUsersWithNoId.php:36
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:241
DB_MASTER
const DB_MASTER
Definition: defines.php:26
CleanupUsersWithNoId\cleanup
cleanup( $table, $primaryKey, $idField, $nameField, array $conds, array $orderby)
Cleanup a table.
Definition: cleanupUsersWithNoId.php:128
Maintenance\getHookRunner
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Maintenance.php:1688
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1350
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:277
User\idFromName
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:883
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
CleanupUsersWithNoId\$triedCreations
$triedCreations
Definition: cleanupUsersWithNoId.php:38
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:991
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:370
CleanupUsersWithNoId\$prefix
$prefix
Definition: cleanupUsersWithNoId.php:37