MediaWiki REL1_39
cleanupUsersWithNoId.php
Go to the documentation of this file.
1<?php
26
27require_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_fill_keys( $primaryKey, true );
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->newSelectQueryBuilder()
155 ->select( array_merge( $primaryKey, [ $idField, $nameField ], $orderby ) )
156 ->from( $table )
157 ->where( array_merge( $conds, [ $next ] ) )
158 ->orderBy( $orderby )
159 ->limit( $this->mBatchSize )
160 ->caller( __METHOD__ )
161 ->fetchResultSet();
162 if ( !$res->numRows() ) {
163 break;
164 }
165
166 // Update the existing rows
167 foreach ( $res as $row ) {
168 $name = $row->$nameField;
169 if ( $row->$idField || !$userNameUtils->isUsable( $name ) ) {
170 continue;
171 }
172
173 $id = 0;
174 if ( $this->assign ) {
175 $id = User::idFromName( $name );
176 if ( !$id ) {
177 // See if any extension wants to create it.
178 if ( !isset( $this->triedCreations[$name] ) ) {
179 $this->triedCreations[$name] = true;
180 if ( !$this->getHookRunner()->onImportHandleUnknownUser( $name ) ) {
181 $id = User::idFromName( $name, User::READ_LATEST );
182 }
183 }
184 }
185 }
186 if ( $id ) {
187 $set = [ $idField => $id ];
188 $counter = &$countAssigned;
189 } else {
190 $set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
191 $counter = &$countPrefixed;
192 }
193
194 $dbw->update(
195 $table,
196 $set,
197 array_intersect_key( (array)$row, $pkFilter ) + [
198 $idField => 0,
199 $nameField => $name,
200 ],
201 __METHOD__
202 );
203 $counter += $dbw->affectedRows();
204 }
205
206 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable row is set
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;
219require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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.
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.
getHookRunner()
Get a HookRunner for running core hooks.
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)
Service locator for MediaWiki core services.
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition User.php:936
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
const DB_PRIMARY
Definition defines.php:28