3 * Helper class for update.php.
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * http://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 $str String 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 $doDelete bool: 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 reassigned to a canonical record id.\n" );
130 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
134 if ( $this->trimmed > 0 ) {
136 $this->out( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
138 $this->out( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
142 if ( $this->failed > 0 ) {
143 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
147 if ( $this->trimmed == 0 || $doDelete ) {
148 $this->out( "It is now safe to apply the unique index on user_name.\n" );
151 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
157 * We don't want anybody to mess with our stuff...
161 $set = array( 'user', 'revision' );
162 $names = array_map( array( $this, 'lockTable' ), $set );
163 $tables = implode( ',', $names );
165 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
168 function lockTable( $table ) {
169 return $this->db->tableName( $table ) . ' WRITE';
176 $this->db->query( "UNLOCK TABLES", __METHOD__ );
180 * Grab usernames for which multiple records are present in the database.
184 function getDupes() {
185 $user = $this->db->tableName( 'user' );
186 $result = $this->db->query(
187 "SELECT user_name,COUNT(*) AS n
190 HAVING n > 1", __METHOD__ );
193 foreach ( $result as $row ) {
194 $list[] = $row->user_name;
200 * Examine user records for the given name. Try to see which record
201 * will be the one that actually gets used, then check remaining records
202 * for edits. If the dupes have no edits, we can safely remove them.
203 * @param $name string
204 * @param $doDelete bool
207 function examine( $name, $doDelete ) {
208 $result = $this->db->select( 'user',
210 array( 'user_name' => $name ),
213 $firstRow = $this->db->fetchObject( $result );
214 $firstId = $firstRow->user_id;
215 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
217 foreach ( $result as $row ) {
218 $dupeId = $row->user_id;
219 $this->out( "... dupe id $dupeId: " );
220 $edits = $this->editCount( $dupeId );
223 $this->out( "has $edits edits! " );
225 $this->reassignEdits( $dupeId, $firstId );
226 $newEdits = $this->editCount( $dupeId );
227 if ( $newEdits == 0 ) {
228 $this->out( "confirmed cleaned. " );
231 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
235 $this->out( "(will need to reassign edits on fix)" );
238 $this->out( "ok, no edits. " );
242 $this->trimAccount( $dupeId );
249 * Count the number of edits attributed to this user.
250 * Does not currently check log table or other things
251 * where it might show up...
256 function editCount( $userid ) {
257 return intval( $this->db->selectField(
260 array( 'rev_user' => $userid ),
269 function reassignEdits( $from, $to ) {
270 $this->out( 'reassigning... ' );
271 $this->db->update( 'revision',
272 array( 'rev_user' => $to ),
273 array( 'rev_user' => $from ),
275 $this->out( "ok. " );
279 * Remove a user account line.
283 function trimAccount( $userid ) {
284 $this->out( "deleting..." );
285 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );