3 * Helper class for update.php and upgrade1_5.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 if ( $this->newSchema() ) {
162 $set = array( 'user', 'revision' );
164 $set = array( 'user', 'cur', 'old' );
166 $names = array_map( array( $this, 'lockTable' ), $set );
167 $tables = implode( ',', $names );
169 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
172 function lockTable( $table ) {
173 return $this->db->tableName( $table ) . ' WRITE';
180 function newSchema() {
181 return MWInit::classExists( 'Revision' );
188 $this->db->query( "UNLOCK TABLES", __METHOD__ );
192 * Grab usernames for which multiple records are present in the database.
196 function getDupes() {
197 $user = $this->db->tableName( 'user' );
198 $result = $this->db->query(
199 "SELECT user_name,COUNT(*) AS n
202 HAVING n > 1", __METHOD__ );
205 foreach ( $result as $row ) {
206 $list[] = $row->user_name;
212 * Examine user records for the given name. Try to see which record
213 * will be the one that actually gets used, then check remaining records
214 * for edits. If the dupes have no edits, we can safely remove them.
215 * @param $name string
216 * @param $doDelete bool
219 function examine( $name, $doDelete ) {
220 $result = $this->db->select( 'user',
222 array( 'user_name' => $name ),
225 $firstRow = $this->db->fetchObject( $result );
226 $firstId = $firstRow->user_id;
227 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
229 foreach ( $result as $row ) {
230 $dupeId = $row->user_id;
231 $this->out( "... dupe id $dupeId: " );
232 $edits = $this->editCount( $dupeId );
235 $this->out( "has $edits edits! " );
237 $this->reassignEdits( $dupeId, $firstId );
238 $newEdits = $this->editCount( $dupeId );
239 if ( $newEdits == 0 ) {
240 $this->out( "confirmed cleaned. " );
243 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
247 $this->out( "(will need to reassign edits on fix)" );
250 $this->out( "ok, no edits. " );
254 $this->trimAccount( $dupeId );
261 * Count the number of edits attributed to this user.
262 * Does not currently check log table or other things
263 * where it might show up...
268 function editCount( $userid ) {
269 if ( $this->newSchema() ) {
270 return $this->editCountOn( 'revision', 'rev_user', $userid );
272 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
273 $this->editCountOn( 'old', 'old_user', $userid );
278 * Count the number of hits on a given table for this account.
279 * @param $table string
280 * @param $field string
285 function editCountOn( $table, $field, $userid ) {
286 return intval( $this->db->selectField(
289 array( $field => $userid ),
298 function reassignEdits( $from, $to ) {
299 $set = $this->newSchema()
300 ? array( 'revision' => 'rev_user' )
301 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
302 foreach ( $set as $table => $field ) {
303 $this->reassignEditsOn( $table, $field, $from, $to );
308 * @param $table string
309 * @param $field string
314 function reassignEditsOn( $table, $field, $from, $to ) {
315 $this->out( "reassigning on $table... " );
316 $this->db->update( $table,
317 array( $field => $to ),
318 array( $field => $from ),
320 $this->out( "ok. " );
324 * Remove a user account line.
328 function trimAccount( $userid ) {
329 $this->out( "deleting..." );
330 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );