MediaWiki REL1_40
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 $display = [];
103 $conds = [];
104 foreach ( $indexFields as $field ) {
105 $display[] = $field . '=' . $row->$field;
106 $conds[ $field ] = $row->$field;
107 }
108 $display = implode( ' ', $display );
109 $conds = $dbw->buildComparison( '>', $conds );
110 return [ $conds, $display ];
111 }
112
123 protected function cleanup(
124 $table, $primaryKey, $idField, $nameField, array $conds, array $orderby
125 ) {
126 if ( $this->table !== null && $this->table !== $table ) {
127 return;
128 }
129
130 $dbw = $this->getDB( DB_PRIMARY );
131 if ( !$dbw->fieldExists( $table, $idField, __METHOD__ ) ||
132 !$dbw->fieldExists( $table, $nameField, __METHOD__ )
133 ) {
134 $this->output( "Skipping $table, fields $idField and/or $nameField do not exist\n" );
135 return;
136 }
137
138 $primaryKey = (array)$primaryKey;
139 $pkFilter = array_fill_keys( $primaryKey, true );
140 $this->output( "Beginning cleanup of $table\n" );
141
142 $next = '1=1';
143 $countAssigned = 0;
144 $countPrefixed = 0;
145 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
146 $userIdentityLookup = MediaWikiServices::getInstance()->getUserIdentityLookup();
147 while ( true ) {
148 // Fetch the rows needing update
149 $res = $dbw->newSelectQueryBuilder()
150 ->select( array_merge( $primaryKey, [ $idField, $nameField ], $orderby ) )
151 ->from( $table )
152 ->where( array_merge( $conds, [ $next ] ) )
153 ->orderBy( $orderby )
154 ->limit( $this->mBatchSize )
155 ->caller( __METHOD__ )
156 ->fetchResultSet();
157 if ( !$res->numRows() ) {
158 break;
159 }
160
161 // Update the existing rows
162 foreach ( $res as $row ) {
163 $name = $row->$nameField;
164 if ( $row->$idField || !$userNameUtils->isUsable( $name ) ) {
165 continue;
166 }
167
168 $id = 0;
169 if ( $this->assign ) {
170 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
171 if ( !$userIdentity || !$userIdentity->isRegistered() ) {
172 // See if any extension wants to create it.
173 if ( !isset( $this->triedCreations[$name] ) ) {
174 $this->triedCreations[$name] = true;
175 if ( !$this->getHookRunner()->onImportHandleUnknownUser( $name ) ) {
176 $userIdentity = $userIdentityLookup
177 ->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
178 $id = $userIdentity ? $userIdentity->getId() : 0;
179 }
180 }
181 }
182 }
183 if ( $id ) {
184 $set = [ $idField => $id ];
185 $counter = &$countAssigned;
186 } else {
187 $set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
188 $counter = &$countPrefixed;
189 }
190
191 $dbw->update(
192 $table,
193 $set,
194 array_intersect_key( (array)$row, $pkFilter ) + [
195 $idField => 0,
196 $nameField => $name,
197 ],
198 __METHOD__
199 );
200 $counter += $dbw->affectedRows();
201 }
202
203 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable row is set
204 [ $next, $display ] = $this->makeNextCond( $dbw, $orderby, $row );
205 $this->output( "... $display\n" );
206 $this->waitForReplication();
207 }
208
209 $this->output(
210 "Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
211 );
212 }
213}
214
215$maintClass = CleanupUsersWithNoId::class;
216require_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.
waitForReplication()
Wait for replica DBs to catch up.
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.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
const DB_PRIMARY
Definition defines.php:28