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
24 * @ingroup Maintenance
27 use Wikimedia\Rdbms\IMaintainableDatabase;
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
39 * @var IMaintainableDatabase
45 private $outputCallback;
48 * UserDupes constructor.
49 * @param IMaintainableDatabase &$database
50 * @param callback $outputCallback
52 public function __construct( &$database, $outputCallback ) {
53 $this->db = $database;
54 $this->outputCallback = $outputCallback;
58 * Output some text via the output callback provided
59 * @param string $str Text to print
61 private function out( $str ) {
62 call_user_func( $this->outputCallback, $str );
66 * Check if this database's user table has already had a unique
67 * user_name index applied.
70 public function hasUniqueIndex() {
71 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
73 $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
78 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
79 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
80 # it's obviously some good stuff!
81 return ( $info[0]->Non_unique == 0 );
85 * Checks the database for duplicate user account records
86 * and remove them in preparation for application of a unique
87 * index on the user_name field. Returns true if the table is
88 * clean or if duplicates have been resolved automatically.
90 * May return false if there are unresolvable problems.
91 * Status information will be echo'd to stdout.
95 public function clearDupes() {
96 return $this->checkDupes( true );
100 * Checks the database for duplicate user account records
101 * in preparation for application of a unique index on the
102 * user_name field. Returns true if the table is clean or
103 * if duplicates can be resolved automatically.
105 * Returns false if there are duplicates and resolution was
106 * not requested. (If doing resolution, edits may be reassigned.)
107 * Status information will be echo'd to stdout.
109 * @param bool $doDelete Pass true to actually remove things
110 * from the database; false to just check.
113 private function checkDupes( $doDelete = false ) {
114 if ( $this->hasUniqueIndex() ) {
115 echo wfWikiID() . " already has a unique index on its user table.\n";
122 $this->out( "Checking for duplicate accounts...\n" );
123 $dupes = $this->getDupes();
124 $count = count( $dupes );
126 $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
128 $this->reassigned = 0;
130 foreach ( $dupes as $name ) {
131 $this->examine( $name, $doDelete );
138 if ( $this->reassigned > 0 ) {
140 $this->out( "$this->reassigned duplicate accounts had edits "
141 . "reassigned to a canonical record id.\n" );
143 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
147 if ( $this->trimmed > 0 ) {
149 $this->out( "$this->trimmed duplicate user records were deleted from "
150 . wfWikiID() . ".\n" );
152 $this->out( "$this->trimmed duplicate user accounts were found on "
153 . wfWikiID() . " which can be removed safely.\n" );
157 if ( $this->failed > 0 ) {
158 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
163 if ( $this->trimmed == 0 || $doDelete ) {
164 $this->out( "It is now safe to apply the unique index on user_name.\n" );
168 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
175 * We don't want anybody to mess with our stuff...
177 private function lock() {
178 $set = [ 'user', 'revision' ];
179 $names = array_map( [ $this, 'lockTable' ], $set );
180 $tables = implode( ',', $names );
182 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
185 private function lockTable( $table ) {
186 return $this->db->tableName( $table ) . ' WRITE';
192 private function unlock() {
193 $this->db->query( "UNLOCK TABLES", __METHOD__ );
197 * Grab usernames for which multiple records are present in the database.
200 private function getDupes() {
201 $user = $this->db->tableName( 'user' );
202 $result = $this->db->query(
203 "SELECT user_name,COUNT(*) AS n
206 HAVING n > 1", __METHOD__ );
209 foreach ( $result as $row ) {
210 $list[] = $row->user_name;
217 * Examine user records for the given name. Try to see which record
218 * will be the one that actually gets used, then check remaining records
219 * for edits. If the dupes have no edits, we can safely remove them.
220 * @param string $name
221 * @param bool $doDelete
223 private function examine( $name, $doDelete ) {
224 $result = $this->db->select( 'user',
226 [ 'user_name' => $name ],
229 $firstRow = $this->db->fetchObject( $result );
230 $firstId = $firstRow->user_id;
231 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
233 foreach ( $result as $row ) {
234 $dupeId = $row->user_id;
235 $this->out( "... dupe id $dupeId: " );
236 $edits = $this->editCount( $dupeId );
239 $this->out( "has $edits edits! " );
241 $this->reassignEdits( $dupeId, $firstId );
242 $newEdits = $this->editCount( $dupeId );
243 if ( $newEdits == 0 ) {
244 $this->out( "confirmed cleaned. " );
247 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
251 $this->out( "(will need to reassign edits on fix)" );
254 $this->out( "ok, no edits. " );
258 $this->trimAccount( $dupeId );
265 * Count the number of edits attributed to this user.
266 * Does not currently check log table or other things
267 * where it might show up...
271 private function editCount( $userid ) {
272 return intval( $this->db->selectField(
275 [ 'rev_user' => $userid ],
283 private function reassignEdits( $from, $to ) {
284 $this->out( 'reassigning... ' );
285 $this->db->update( 'revision',
286 [ 'rev_user' => $to ],
287 [ 'rev_user' => $from ],
289 $this->out( "ok. " );
293 * Remove a user account line.
296 private function trimAccount( $userid ) {
297 $this->out( "deleting..." );
298 $this->db->delete( 'user', [ 'user_id' => $userid ], __METHOD__ );