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.
29 * @ingroup Maintenance
36 private $outputCallback;
38 function __construct( &$database, $outputCallback ) {
39 $this->db = $database;
40 $this->outputCallback = $outputCallback;
44 * Output some text via the output callback provided
45 * @param $str String Text to print
47 private function out( $str ) {
48 call_user_func( $this->outputCallback, $str );
52 * Check if this database's user table has already had a unique
53 * user_name index applied.
56 function hasUniqueIndex() {
57 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
59 $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
63 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
64 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
65 # it's obviously some good stuff!
66 return ( $info[0]->Non_unique == 0 );
70 * Checks the database for duplicate user account records
71 * and remove them in preparation for application of a unique
72 * index on the user_name field. Returns true if the table is
73 * clean or if duplicates have been resolved automatically.
75 * May return false if there are unresolvable problems.
76 * Status information will be echo'd to stdout.
80 function clearDupes() {
81 return $this->checkDupes( true );
85 * Checks the database for duplicate user account records
86 * in preparation for application of a unique index on the
87 * user_name field. Returns true if the table is clean or
88 * if duplicates can be resolved automatically.
90 * Returns false if there are duplicates and resolution was
91 * not requested. (If doing resolution, edits may be reassigned.)
92 * Status information will be echo'd to stdout.
94 * @param $doDelete bool: pass true to actually remove things
95 * from the database; false to just check.
98 function checkDupes( $doDelete = false ) {
99 if ( $this->hasUniqueIndex() ) {
100 echo wfWikiID() . " already has a unique index on its user table.\n";
106 $this->out( "Checking for duplicate accounts...\n" );
107 $dupes = $this->getDupes();
108 $count = count( $dupes );
110 $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
112 $this->reassigned = 0;
114 foreach ( $dupes as $name ) {
115 $this->examine( $name, $doDelete );
122 if ( $this->reassigned > 0 ) {
124 $this->out( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
126 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
130 if ( $this->trimmed > 0 ) {
132 $this->out( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
134 $this->out( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
138 if ( $this->failed > 0 ) {
139 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
143 if ( $this->trimmed == 0 || $doDelete ) {
144 $this->out( "It is now safe to apply the unique index on user_name.\n" );
147 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
153 * We don't want anybody to mess with our stuff...
157 if ( $this->newSchema() ) {
158 $set = array( 'user', 'revision' );
160 $set = array( 'user', 'cur', 'old' );
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 function newSchema() {
177 return MWInit::classExists( 'Revision' );
184 $this->db->query( "UNLOCK TABLES", __METHOD__ );
188 * Grab usernames for which multiple records are present in the database.
192 function getDupes() {
193 $user = $this->db->tableName( 'user' );
194 $result = $this->db->query(
195 "SELECT user_name,COUNT(*) AS n
198 HAVING n > 1", __METHOD__ );
201 foreach ( $result as $row ) {
202 $list[] = $row->user_name;
208 * Examine user records for the given name. Try to see which record
209 * will be the one that actually gets used, then check remaining records
210 * for edits. If the dupes have no edits, we can safely remove them.
211 * @param $name string
212 * @param $doDelete bool
215 function examine( $name, $doDelete ) {
216 $result = $this->db->select( 'user',
218 array( 'user_name' => $name ),
221 $firstRow = $this->db->fetchObject( $result );
222 $firstId = $firstRow->user_id;
223 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
225 foreach ( $result as $row ) {
226 $dupeId = $row->user_id;
227 $this->out( "... dupe id $dupeId: " );
228 $edits = $this->editCount( $dupeId );
231 $this->out( "has $edits edits! " );
233 $this->reassignEdits( $dupeId, $firstId );
234 $newEdits = $this->editCount( $dupeId );
235 if ( $newEdits == 0 ) {
236 $this->out( "confirmed cleaned. " );
239 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
243 $this->out( "(will need to reassign edits on fix)" );
246 $this->out( "ok, no edits. " );
250 $this->trimAccount( $dupeId );
257 * Count the number of edits attributed to this user.
258 * Does not currently check log table or other things
259 * where it might show up...
264 function editCount( $userid ) {
265 if ( $this->newSchema() ) {
266 return $this->editCountOn( 'revision', 'rev_user', $userid );
268 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
269 $this->editCountOn( 'old', 'old_user', $userid );
274 * Count the number of hits on a given table for this account.
275 * @param $table string
276 * @param $field string
281 function editCountOn( $table, $field, $userid ) {
282 return intval( $this->db->selectField(
285 array( $field => $userid ),
294 function reassignEdits( $from, $to ) {
295 $set = $this->newSchema()
296 ? array( 'revision' => 'rev_user' )
297 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
298 foreach ( $set as $table => $field ) {
299 $this->reassignEditsOn( $table, $field, $from, $to );
304 * @param $table string
305 * @param $field string
310 function reassignEditsOn( $table, $field, $from, $to ) {
311 $this->out( "reassigning on $table... " );
312 $this->db->update( $table,
313 array( $field => $to ),
314 array( $field => $from ),
316 $this->out( "ok. " );
320 * Remove a user account line.
324 function trimAccount( $userid ) {
325 $this->out( "deleting..." );
326 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );