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
28 * Look for duplicate user table entries and optionally prune them.
30 * This is still used by our MysqlUpdater at:
31 * includes/installer/MysqlUpdater.php
33 * @ingroup Maintenance
40 private $outputCallback;
42 function __construct( &$database, $outputCallback ) {
43 $this->db = $database;
44 $this->outputCallback = $outputCallback;
48 * Output some text via the output callback provided
49 * @param string $str Text to print
51 private function out( $str ) {
52 call_user_func( $this->outputCallback, $str );
56 * Check if this database's user table has already had a unique
57 * user_name index applied.
60 function hasUniqueIndex() {
61 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
63 $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
67 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
68 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
69 # it's obviously some good stuff!
70 return ( $info[0]->Non_unique == 0 );
74 * Checks the database for duplicate user account records
75 * and remove them in preparation for application of a unique
76 * index on the user_name field. Returns true if the table is
77 * clean or if duplicates have been resolved automatically.
79 * May return false if there are unresolvable problems.
80 * Status information will be echo'd to stdout.
84 function clearDupes() {
85 return $this->checkDupes( true );
89 * Checks the database for duplicate user account records
90 * in preparation for application of a unique index on the
91 * user_name field. Returns true if the table is clean or
92 * if duplicates can be resolved automatically.
94 * Returns false if there are duplicates and resolution was
95 * not requested. (If doing resolution, edits may be reassigned.)
96 * Status information will be echo'd to stdout.
98 * @param bool $doDelete Pass true to actually remove things
99 * from the database; false to just check.
102 function checkDupes( $doDelete = false ) {
103 if ( $this->hasUniqueIndex() ) {
104 echo wfWikiID() . " already has a unique index on its user table.\n";
110 $this->out( "Checking for duplicate accounts...\n" );
111 $dupes = $this->getDupes();
112 $count = count( $dupes );
114 $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
116 $this->reassigned = 0;
118 foreach ( $dupes as $name ) {
119 $this->examine( $name, $doDelete );
126 if ( $this->reassigned > 0 ) {
128 $this->out( "$this->reassigned duplicate accounts had edits "
129 . "reassigned to a canonical record id.\n" );
131 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
135 if ( $this->trimmed > 0 ) {
137 $this->out( "$this->trimmed duplicate user records were deleted from "
138 . wfWikiID() . ".\n" );
140 $this->out( "$this->trimmed duplicate user accounts were found on "
141 . wfWikiID() . " which can be removed safely.\n" );
145 if ( $this->failed > 0 ) {
146 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
150 if ( $this->trimmed == 0 || $doDelete ) {
151 $this->out( "It is now safe to apply the unique index on user_name.\n" );
154 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
160 * We don't want anybody to mess with our stuff...
164 $set = array( 'user', 'revision' );
165 $names = array_map( array( $this, 'lockTable' ), $set );
166 $tables = implode( ',', $names );
168 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
171 function lockTable( $table ) {
172 return $this->db->tableName( $table ) . ' WRITE';
179 $this->db->query( "UNLOCK TABLES", __METHOD__ );
183 * Grab usernames for which multiple records are present in the database.
187 function getDupes() {
188 $user = $this->db->tableName( 'user' );
189 $result = $this->db->query(
190 "SELECT user_name,COUNT(*) AS n
193 HAVING n > 1", __METHOD__ );
196 foreach ( $result as $row ) {
197 $list[] = $row->user_name;
203 * Examine user records for the given name. Try to see which record
204 * will be the one that actually gets used, then check remaining records
205 * for edits. If the dupes have no edits, we can safely remove them.
206 * @param string $name
207 * @param bool $doDelete
210 function examine( $name, $doDelete ) {
211 $result = $this->db->select( 'user',
213 array( 'user_name' => $name ),
216 $firstRow = $this->db->fetchObject( $result );
217 $firstId = $firstRow->user_id;
218 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
220 foreach ( $result as $row ) {
221 $dupeId = $row->user_id;
222 $this->out( "... dupe id $dupeId: " );
223 $edits = $this->editCount( $dupeId );
226 $this->out( "has $edits edits! " );
228 $this->reassignEdits( $dupeId, $firstId );
229 $newEdits = $this->editCount( $dupeId );
230 if ( $newEdits == 0 ) {
231 $this->out( "confirmed cleaned. " );
234 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
238 $this->out( "(will need to reassign edits on fix)" );
241 $this->out( "ok, no edits. " );
245 $this->trimAccount( $dupeId );
252 * Count the number of edits attributed to this user.
253 * Does not currently check log table or other things
254 * where it might show up...
259 function editCount( $userid ) {
260 return intval( $this->db->selectField(
263 array( 'rev_user' => $userid ),
272 function reassignEdits( $from, $to ) {
273 $this->out( 'reassigning... ' );
274 $this->db->update( 'revision',
275 array( 'rev_user' => $to ),
276 array( 'rev_user' => $from ),
278 $this->out( "ok. " );
282 * Remove a user account line.
286 function trimAccount( $userid ) {
287 $this->out( "deleting..." );
288 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );