Revision: Remove some unnecessary temporary variables for returns
[mediawiki.git] / maintenance / UserDupes.php
blob02293bf7df06348759f5267ded11ab79cc38f566
1 <?php
2 /**
3 * Helper class for update.php.
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
24 * @ingroup Maintenance
27 use Wikimedia\Rdbms\IMaintainableDatabase;
29 /**
30 * Look for duplicate user table entries and optionally prune them.
32 * This is still used by our MysqlUpdater at:
33 * includes/installer/MysqlUpdater.php
35 * @ingroup Maintenance
37 class UserDupes {
38 /**
39 * @var IMaintainableDatabase
41 private $db;
42 /** @var int */
43 private $reassigned;
44 /** @var int */
45 private $trimmed;
46 /** @var int */
47 private $failed;
48 /** @var callable */
49 private $outputCallback;
51 /**
52 * @param IMaintainableDatabase &$database
53 * @param callable $outputCallback
55 public function __construct( &$database, $outputCallback ) {
56 $this->db = $database;
57 $this->outputCallback = $outputCallback;
60 /**
61 * Output some text via the output callback provided
62 * @param string $str Text to print
64 private function out( $str ) {
65 call_user_func( $this->outputCallback, $str );
68 /**
69 * Check if this database's user table has already had a unique
70 * user_name index applied.
71 * @return bool
73 public function hasUniqueIndex() {
74 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
75 if ( !$info ) {
76 $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
78 return false;
81 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
82 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
83 # it's obviously some good stuff!
84 return ( $info[0]->Non_unique == 0 );
87 /**
88 * Checks the database for duplicate user account records
89 * and remove them in preparation for application of a unique
90 * index on the user_name field. Returns true if the table is
91 * clean or if duplicates have been resolved automatically.
93 * May return false if there are unresolvable problems.
94 * Status information will be echo'd to stdout.
96 * @return bool
98 public function clearDupes() {
99 return $this->checkDupes( true );
103 * Checks the database for duplicate user account records
104 * in preparation for application of a unique index on the
105 * user_name field. Returns true if the table is clean or
106 * if duplicates can be resolved automatically.
108 * Returns false if there are duplicates and resolution was
109 * not requested. (If doing resolution, edits may be reassigned.)
110 * Status information will be echo'd to stdout.
112 * @param bool $doDelete Pass true to actually remove things
113 * from the database; false to just check.
114 * @return bool
116 private function checkDupes( $doDelete = false ) {
117 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
118 if ( $this->hasUniqueIndex() ) {
119 echo "$dbDomain already has a unique index on its user table.\n";
121 return true;
124 $this->lock();
126 $this->out( "Checking for duplicate accounts...\n" );
127 $dupes = $this->getDupes();
128 $count = count( $dupes );
130 $this->out( "Found $count accounts with duplicate records on $dbDomain.\n" );
131 $this->trimmed = 0;
132 $this->reassigned = 0;
133 $this->failed = 0;
134 foreach ( $dupes as $name ) {
135 $this->examine( $name, $doDelete );
138 $this->unlock();
140 $this->out( "\n" );
142 // @phan-suppress-next-line PhanSuspiciousValueComparison
143 if ( $this->reassigned > 0 ) {
144 if ( $doDelete ) {
145 $this->out( "$this->reassigned duplicate accounts had edits "
146 . "reassigned to a canonical record id.\n" );
147 } else {
148 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
152 // @phan-suppress-next-line PhanSuspiciousValueComparison
153 if ( $this->trimmed > 0 ) {
154 if ( $doDelete ) {
155 $this->out(
156 "$this->trimmed duplicate user records were deleted from $dbDomain.\n" );
157 } else {
158 $this->out(
159 "$this->trimmed duplicate user accounts were found on $dbDomain " .
160 "which can be removed safely.\n"
165 // @phan-suppress-next-line PhanSuspiciousValueComparison
166 if ( $this->failed > 0 ) {
167 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
169 return false;
172 // @phan-suppress-next-line PhanSuspiciousValueComparison
173 if ( $this->trimmed == 0 || $doDelete ) {
174 $this->out( "It is now safe to apply the unique index on user_name.\n" );
176 return true;
177 } else {
178 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
180 return false;
185 * We don't want anybody to mess with our stuff...
187 private function lock() {
188 $set = [ 'user', 'revision' ];
189 $names = array_map( [ $this, 'lockTable' ], $set );
190 $tables = implode( ',', $names );
192 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
195 private function lockTable( $table ) {
196 return $this->db->tableName( $table ) . ' WRITE';
200 * @private
202 private function unlock() {
203 $this->db->query( "UNLOCK TABLES", __METHOD__ );
207 * Grab usernames for which multiple records are present in the database.
208 * @return array
210 private function getDupes() {
211 $user = $this->db->tableName( 'user' );
212 $result = $this->db->query(
213 "SELECT user_name,COUNT(*) AS n
214 FROM $user
215 GROUP BY user_name
216 HAVING n > 1", __METHOD__ );
218 $list = [];
219 foreach ( $result as $row ) {
220 $list[] = $row->user_name;
223 return $list;
227 * Examine user records for the given name. Try to see which record
228 * will be the one that actually gets used, then check remaining records
229 * for edits. If the dupes have no edits, we can safely remove them.
230 * @param string $name
231 * @param bool $doDelete
233 private function examine( $name, $doDelete ) {
234 $result = $this->db->select( 'user',
235 [ 'user_id' ],
236 [ 'user_name' => $name ],
237 __METHOD__ );
239 $firstRow = $this->db->fetchObject( $result );
240 $firstId = $firstRow->user_id;
241 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
243 foreach ( $result as $row ) {
244 $dupeId = $row->user_id;
245 $this->out( "... dupe id $dupeId: " );
246 $edits = $this->editCount( $dupeId );
247 if ( $edits > 0 ) {
248 $this->reassigned++;
249 $this->out( "has $edits edits! " );
250 if ( $doDelete ) {
251 $this->reassignEdits( $dupeId, $firstId );
252 $newEdits = $this->editCount( $dupeId );
253 if ( $newEdits == 0 ) {
254 $this->out( "confirmed cleaned. " );
255 } else {
256 $this->failed++;
257 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
258 continue;
260 } else {
261 $this->out( "(will need to reassign edits on fix)" );
263 } else {
264 $this->out( "ok, no edits. " );
266 $this->trimmed++;
267 if ( $doDelete ) {
268 $this->trimAccount( $dupeId );
270 $this->out( "\n" );
275 * Count the number of edits attributed to this user.
276 * Does not currently check log table or other things
277 * where it might show up...
278 * @param int $userid
279 * @return int
281 private function editCount( $userid ) {
282 return intval( $this->db->selectField(
283 'revision',
284 'COUNT(*)',
285 [ 'rev_user' => $userid ],
286 __METHOD__ ) );
290 * @param int $from
291 * @param int $to
293 private function reassignEdits( $from, $to ) {
294 $this->out( 'reassigning... ' );
295 $this->db->update( 'revision',
296 [ 'rev_user' => $to ],
297 [ 'rev_user' => $from ],
298 __METHOD__ );
299 $this->out( "ok. " );
303 * Remove a user account line.
304 * @param int $userid
306 private function trimAccount( $userid ) {
307 $this->out( "deleting..." );
308 $this->db->delete( 'user', [ 'user_id' => $userid ], __METHOD__ );
309 $this->out( " ok" );