MediaWiki REL1_34
cleanupUsersWithNoId.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
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 $dbw = $this->getDB( DB_MASTER );
135 if ( !$dbw->fieldExists( $table, $idField ) ||
136 !$dbw->fieldExists( $table, $nameField )
137 ) {
138 $this->output( "Skipping $table, fields $idField and/or $nameField do not exist\n" );
139 return;
140 }
141
142 $primaryKey = (array)$primaryKey;
143 $pkFilter = array_flip( $primaryKey );
144 $this->output( "Beginning cleanup of $table\n" );
145
146 $next = '1=1';
147 $countAssigned = 0;
148 $countPrefixed = 0;
149 while ( true ) {
150 // Fetch the rows needing update
151 $res = $dbw->select(
152 $table,
153 array_merge( $primaryKey, [ $idField, $nameField ], $orderby ),
154 array_merge( $conds, [ $next ] ),
155 __METHOD__,
156 [
157 'ORDER BY' => $orderby,
158 'LIMIT' => $this->mBatchSize,
159 ]
160 );
161 if ( !$res->numRows() ) {
162 break;
163 }
164
165 // Update the existing rows
166 foreach ( $res as $row ) {
167 $name = $row->$nameField;
168 if ( $row->$idField || !User::isUsableName( $name ) ) {
169 continue;
170 }
171
172 $id = 0;
173 if ( $this->assign ) {
174 $id = User::idFromName( $name );
175 if ( !$id ) {
176 // See if any extension wants to create it.
177 if ( !isset( $this->triedCreations[$name] ) ) {
178 $this->triedCreations[$name] = true;
179 if ( !Hooks::run( 'ImportHandleUnknownUser', [ $name ] ) ) {
180 $id = User::idFromName( $name, User::READ_LATEST );
181 }
182 }
183 }
184 }
185 if ( $id ) {
186 $set = [ $idField => $id ];
187 $counter = &$countAssigned;
188 } else {
189 $set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
190 $counter = &$countPrefixed;
191 }
192
193 $dbw->update(
194 $table,
195 $set,
196 array_intersect_key( (array)$row, $pkFilter ) + [
197 $idField => 0,
198 $nameField => $name,
199 ],
200 __METHOD__
201 );
202 $counter += $dbw->affectedRows();
203 }
204
205 list( $next, $display ) = $this->makeNextCond( $dbw, $orderby, $row );
206 $this->output( "... $display\n" );
208 }
209
210 $this->output(
211 "Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
212 );
213 }
214}
215
216$maintClass = CleanupUsersWithNoId::class;
217require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
const RUN_MAINTENANCE_IF_MAIN
Maintenance script that cleans up tables that have valid usernames with no user ID.
getUpdateKey()
Get the update key name to go in the update log table.
makeNextCond( $dbw, $indexFields, $row)
Calculate a "next" condition and progress display string.
cleanup( $table, $primaryKey, $idField, $nameField, array $conds, array $orderby)
Cleanup a table.
__construct()
Default constructor.
doDBUpdates()
Do the actual work.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
output( $out, $channel=null)
Throw some output to the user.
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.
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...
Definition User.php:1008
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition User.php:871
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
const DB_MASTER
Definition defines.php:26