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
45 private $outputCallback;
48 * @param IMaintainableDatabase &$database
49 * @param callback $outputCallback
51 public function __construct( &$database, $outputCallback ) {
52 $this->db = $database;
53 $this->outputCallback = $outputCallback;
57 * Output some text via the output callback provided
58 * @param string $str Text to print
60 private function out( $str ) {
61 call_user_func( $this->outputCallback, $str );
65 * Check if this database's user table has already had a unique
66 * user_name index applied.
69 public function hasUniqueIndex() {
70 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
72 $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
77 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
78 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
79 # it's obviously some good stuff!
80 return ( $info[0]->Non_unique == 0 );
84 * Checks the database for duplicate user account records
85 * and remove them in preparation for application of a unique
86 * index on the user_name field. Returns true if the table is
87 * clean or if duplicates have been resolved automatically.
89 * May return false if there are unresolvable problems.
90 * Status information will be echo'd to stdout.
94 public function clearDupes() {
95 return $this->checkDupes( true );
99 * Checks the database for duplicate user account records
100 * in preparation for application of a unique index on the
101 * user_name field. Returns true if the table is clean or
102 * if duplicates can be resolved automatically.
104 * Returns false if there are duplicates and resolution was
105 * not requested. (If doing resolution, edits may be reassigned.)
106 * Status information will be echo'd to stdout.
108 * @param bool $doDelete Pass true to actually remove things
109 * from the database; false to just check.
112 private function checkDupes( $doDelete = false ) {
113 if ( $this->hasUniqueIndex() ) {
114 echo wfWikiID() . " already has a unique index on its user table.\n";
121 $this->out( "Checking for duplicate accounts...\n" );
122 $dupes = $this->getDupes();
123 $count = count( $dupes );
125 $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
127 $this->reassigned = 0;
129 foreach ( $dupes as $name ) {
130 $this->examine( $name, $doDelete );
137 if ( $this->reassigned > 0 ) {
139 $this->out( "$this->reassigned duplicate accounts had edits "
140 . "reassigned to a canonical record id.\n" );
142 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
146 if ( $this->trimmed > 0 ) {
148 $this->out( "$this->trimmed duplicate user records were deleted from "
149 . wfWikiID() . ".\n" );
151 $this->out( "$this->trimmed duplicate user accounts were found on "
152 . wfWikiID() . " which can be removed safely.\n" );
156 if ( $this->failed > 0 ) {
157 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
162 if ( $this->trimmed == 0 || $doDelete ) {
163 $this->out( "It is now safe to apply the unique index on user_name.\n" );
167 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
174 * We don't want anybody to mess with our stuff...
176 private function lock() {
177 $set = [ 'user', 'revision' ];
178 $names = array_map( [ $this, 'lockTable' ], $set );
179 $tables = implode( ',', $names );
181 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
184 private function lockTable( $table ) {
185 return $this->db->tableName( $table ) . ' WRITE';
191 private function unlock() {
192 $this->db->query( "UNLOCK TABLES", __METHOD__ );
196 * Grab usernames for which multiple records are present in the database.
199 private function getDupes() {
200 $user = $this->db->tableName( 'user' );
201 $result = $this->db->query(
202 "SELECT user_name,COUNT(*) AS n
205 HAVING n > 1", __METHOD__ );
208 foreach ( $result as $row ) {
209 $list[] = $row->user_name;
216 * Examine user records for the given name. Try to see which record
217 * will be the one that actually gets used, then check remaining records
218 * for edits. If the dupes have no edits, we can safely remove them.
219 * @param string $name
220 * @param bool $doDelete
222 private function examine( $name, $doDelete ) {
223 $result = $this->db->select( 'user',
225 [ 'user_name' => $name ],
228 $firstRow = $this->db->fetchObject( $result );
229 $firstId = $firstRow->user_id;
230 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
232 foreach ( $result as $row ) {
233 $dupeId = $row->user_id;
234 $this->out( "... dupe id $dupeId: " );
235 $edits = $this->editCount( $dupeId );
238 $this->out( "has $edits edits! " );
240 $this->reassignEdits( $dupeId, $firstId );
241 $newEdits = $this->editCount( $dupeId );
242 if ( $newEdits == 0 ) {
243 $this->out( "confirmed cleaned. " );
246 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
250 $this->out( "(will need to reassign edits on fix)" );
253 $this->out( "ok, no edits. " );
257 $this->trimAccount( $dupeId );
264 * Count the number of edits attributed to this user.
265 * Does not currently check log table or other things
266 * where it might show up...
270 private function editCount( $userid ) {
271 return intval( $this->db->selectField(
274 [ 'rev_user' => $userid ],
282 private function reassignEdits( $from, $to ) {
283 $this->out( 'reassigning... ' );
284 $this->db->update( 'revision',
285 [ 'rev_user' => $to ],
286 [ 'rev_user' => $from ],
288 $this->out( "ok. " );
292 * Remove a user account line.
295 private function trimAccount( $userid ) {
296 $this->out( "deleting..." );
297 $this->db->delete( 'user', [ 'user_id' => $userid ], __METHOD__ );