Update git submodules
[mediawiki.git] / maintenance / cleanupUsersWithNoId.php
blobfd3401263810b6af9be2fb11ab9df9f4211cb414
1 <?php
2 /**
3 * Cleanup tables that have valid usernames with no user ID
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\IDatabase;
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that cleans up tables that have valid usernames with no
30 * user ID.
32 * @ingroup Maintenance
33 * @since 1.31
35 class CleanupUsersWithNoId extends LoggedUpdateMaintenance {
36 private $prefix, $table, $assign;
37 private $triedCreations = [];
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 );
48 protected function getUpdateKey() {
49 return __CLASS__;
52 protected function doDBUpdates() {
53 $this->prefix = $this->getOption( 'prefix' );
54 $this->table = $this->getOption( 'table', null );
55 $this->assign = $this->getOption( 'assign' );
57 $this->cleanup(
58 'revision', 'rev_id', 'rev_user', 'rev_user_text',
59 [ 'rev_user' => 0 ], [ 'rev_timestamp', 'rev_id' ]
61 $this->cleanup(
62 'archive', 'ar_id', 'ar_user', 'ar_user_text',
63 [], [ 'ar_id' ]
65 $this->cleanup(
66 'logging', 'log_id', 'log_user', 'log_user_text',
67 [ 'log_user' => 0 ], [ 'log_timestamp', 'log_id' ]
69 $this->cleanup(
70 'image', 'img_name', 'img_user', 'img_user_text',
71 [ 'img_user' => 0 ], [ 'img_timestamp', 'img_name' ]
73 $this->cleanup(
74 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text',
75 [], [ 'oi_name', 'oi_timestamp' ]
77 $this->cleanup(
78 'filearchive', 'fa_id', 'fa_user', 'fa_user_text',
79 [], [ 'fa_id' ]
81 $this->cleanup(
82 'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text',
83 [], [ 'ipb_id' ]
85 $this->cleanup(
86 'recentchanges', 'rc_id', 'rc_user', 'rc_user_text',
87 [], [ 'rc_id' ]
90 return true;
93 /**
94 * Calculate a "next" condition and progress display string
95 * @param IDatabase $dbw
96 * @param string[] $indexFields Fields in the index being ordered by
97 * @param stdClass $row Database row
98 * @return string[] [ string $next, string $display ]
100 private function makeNextCond( $dbw, $indexFields, $row ) {
101 $display = [];
102 $conds = [];
103 foreach ( $indexFields as $field ) {
104 $display[] = $field . '=' . $row->$field;
105 $conds[ $field ] = $row->$field;
107 $display = implode( ' ', $display );
108 $conds = $dbw->buildComparison( '>', $conds );
109 return [ $conds, $display ];
113 * Cleanup a table
115 * @param string $table Table to migrate
116 * @param string|string[] $primaryKey Primary key of the table.
117 * @param string $idField User ID field name
118 * @param string $nameField User name field name
119 * @param array $conds Query conditions
120 * @param string[] $orderby Fields to order by
122 protected function cleanup(
123 $table, $primaryKey, $idField, $nameField, array $conds, array $orderby
125 if ( $this->table !== null && $this->table !== $table ) {
126 return;
129 $dbw = $this->getDB( DB_PRIMARY );
130 if ( !$dbw->fieldExists( $table, $idField, __METHOD__ ) ||
131 !$dbw->fieldExists( $table, $nameField, __METHOD__ )
133 $this->output( "Skipping $table, fields $idField and/or $nameField do not exist\n" );
134 return;
137 $primaryKey = (array)$primaryKey;
138 $pkFilter = array_fill_keys( $primaryKey, true );
139 $this->output( "Beginning cleanup of $table\n" );
141 $next = '1=1';
142 $countAssigned = 0;
143 $countPrefixed = 0;
144 $userNameUtils = $this->getServiceContainer()->getUserNameUtils();
145 $userIdentityLookup = $this->getServiceContainer()->getUserIdentityLookup();
146 while ( true ) {
147 // Fetch the rows needing update
148 $res = $dbw->newSelectQueryBuilder()
149 ->select( array_merge( $primaryKey, [ $idField, $nameField ], $orderby ) )
150 ->from( $table )
151 ->where( array_merge( $conds, [ $next ] ) )
152 ->orderBy( $orderby )
153 ->limit( $this->mBatchSize )
154 ->caller( __METHOD__ )
155 ->fetchResultSet();
156 if ( !$res->numRows() ) {
157 break;
160 // Update the existing rows
161 foreach ( $res as $row ) {
162 $name = $row->$nameField;
163 if ( $row->$idField || !$userNameUtils->isUsable( $name ) ) {
164 continue;
167 $id = 0;
168 if ( $this->assign ) {
169 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
170 if ( !$userIdentity || !$userIdentity->isRegistered() ) {
171 // See if any extension wants to create it.
172 if ( !isset( $this->triedCreations[$name] ) ) {
173 $this->triedCreations[$name] = true;
174 if ( !$this->getHookRunner()->onImportHandleUnknownUser( $name ) ) {
175 $userIdentity = $userIdentityLookup
176 ->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
177 $id = $userIdentity ? $userIdentity->getId() : 0;
182 if ( $id ) {
183 $set = [ $idField => $id ];
184 $counter = &$countAssigned;
185 } else {
186 $set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
187 $counter = &$countPrefixed;
190 $dbw->update(
191 $table,
192 $set,
193 array_intersect_key( (array)$row, $pkFilter ) + [
194 $idField => 0,
195 $nameField => $name,
197 __METHOD__
199 $counter += $dbw->affectedRows();
202 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable row is set
203 [ $next, $display ] = $this->makeNextCond( $dbw, $orderby, $row );
204 $this->output( "... $display\n" );
205 $this->waitForReplication();
208 $this->output(
209 "Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
214 $maintClass = CleanupUsersWithNoId::class;
215 require_once RUN_MAINTENANCE_IF_MAIN;