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_PRIMARY );
136  if ( !$dbw->fieldExists( $table, $idField, __METHOD__ ) ||
137  !$dbw->fieldExists( $table, $nameField, __METHOD__ )
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  $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
152  while ( true ) {
153  // Fetch the rows needing update
154  $res = $dbw->select(
155  $table,
156  array_merge( $primaryKey, [ $idField, $nameField ], $orderby ),
157  array_merge( $conds, [ $next ] ),
158  __METHOD__,
159  [
160  'ORDER BY' => $orderby,
161  'LIMIT' => $this->mBatchSize,
162  ]
163  );
164  if ( !$res->numRows() ) {
165  break;
166  }
167 
168  // Update the existing rows
169  foreach ( $res as $row ) {
170  $name = $row->$nameField;
171  if ( $row->$idField || !$userNameUtils->isUsable( $name ) ) {
172  continue;
173  }
174 
175  $id = 0;
176  if ( $this->assign ) {
177  $id = User::idFromName( $name );
178  if ( !$id ) {
179  // See if any extension wants to create it.
180  if ( !isset( $this->triedCreations[$name] ) ) {
181  $this->triedCreations[$name] = true;
182  if ( !$this->getHookRunner()->onImportHandleUnknownUser( $name ) ) {
183  $id = User::idFromName( $name, User::READ_LATEST );
184  }
185  }
186  }
187  }
188  if ( $id ) {
189  $set = [ $idField => $id ];
190  $counter = &$countAssigned;
191  } else {
192  $set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
193  $counter = &$countPrefixed;
194  }
195 
196  $dbw->update(
197  $table,
198  $set,
199  array_intersect_key( (array)$row, $pkFilter ) + [
200  $idField => 0,
201  $nameField => $name,
202  ],
203  __METHOD__
204  );
205  $counter += $dbw->affectedRows();
206  }
207 
208  list( $next, $display ) = $this->makeNextCond( $dbw, $orderby, $row );
209  $this->output( "... $display\n" );
210  $lbFactory->waitForReplication();
211  }
212 
213  $this->output(
214  "Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
215  );
216  }
217 }
218 
219 $maintClass = CleanupUsersWithNoId::class;
220 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
$maintClass
Definition: cleanupUsersWithNoId.php:219
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:180
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:328
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:248
CleanupUsersWithNoId\cleanup
cleanup( $table, $primaryKey, $idField, $nameField, array $conds, array $orderby)
Cleanup a table.
Definition: cleanupUsersWithNoId.php:128
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\getHookRunner
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Maintenance.php:1611
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1349
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:285
User\idFromName
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:923
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
CleanupUsersWithNoId\$triedCreations
$triedCreations
Definition: cleanupUsersWithNoId.php:38
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:374
CleanupUsersWithNoId\$prefix
$prefix
Definition: cleanupUsersWithNoId.php:37