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
37 function UserDupes( &$database ) {
38 $this->db =& $database;
42 * Check if this database's user table has already had a unique
43 * user_name index applied.
46 function hasUniqueIndex() {
47 $fname = 'UserDupes::hasUniqueIndex';
48 $info = $this->db->indexInfo( 'user', 'user_name', $fname );
50 wfOut( "WARNING: doesn't seem to have user_name index at all!\n" );
54 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
55 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
56 # it's obviously some good stuff!
57 return ( $info[0]->Non_unique == 0 );
61 * Checks the database for duplicate user account records
62 * and remove them in preparation for application of a unique
63 * index on the user_name field. Returns true if the table is
64 * clean or if duplicates have been resolved automatically.
66 * May return false if there are unresolvable problems.
67 * Status information will be echo'd to stdout.
71 function clearDupes() {
72 return $this->checkDupes( true );
76 * Checks the database for duplicate user account records
77 * in preparation for application of a unique index on the
78 * user_name field. Returns true if the table is clean or
79 * if duplicates can be resolved automatically.
81 * Returns false if there are duplicates and resolution was
82 * not requested. (If doing resolution, edits may be reassigned.)
83 * Status information will be echo'd to stdout.
85 * @param $doDelete bool: pass true to actually remove things
86 * from the database; false to just check.
89 function checkDupes( $doDelete = false ) {
90 if ( $this->hasUniqueIndex() ) {
91 echo wfWikiID() . " already has a unique index on its user table.\n";
97 wfOut( "Checking for duplicate accounts...\n" );
98 $dupes = $this->getDupes();
99 $count = count( $dupes );
101 wfOut( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
103 $this->reassigned = 0;
105 foreach ( $dupes as $name ) {
106 $this->examine( $name, $doDelete );
113 if ( $this->reassigned > 0 ) {
115 wfOut( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
117 wfOut( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
121 if ( $this->trimmed > 0 ) {
123 wfOut( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
125 wfOut( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
129 if ( $this->failed > 0 ) {
130 wfOut( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
134 if ( $this->trimmed == 0 || $doDelete ) {
135 wfOut( "It is now safe to apply the unique index on user_name.\n" );
138 wfOut( "Run this script again with the --fix option to automatically delete them.\n" );
144 * We don't want anybody to mess with our stuff...
148 $fname = 'UserDupes::lock';
149 if ( $this->newSchema() ) {
150 $set = array( 'user', 'revision' );
152 $set = array( 'user', 'cur', 'old' );
154 $names = array_map( array( $this, 'lockTable' ), $set );
155 $tables = implode( ',', $names );
157 $this->db->query( "LOCK TABLES $tables", $fname );
160 function lockTable( $table ) {
161 return $this->db->tableName( $table ) . ' WRITE';
168 function newSchema() {
169 return class_exists( 'Revision' );
176 $fname = 'UserDupes::unlock';
177 $this->db->query( "UNLOCK TABLES", $fname );
181 * Grab usernames for which multiple records are present in the database.
185 function getDupes() {
186 $fname = 'UserDupes::listDupes';
187 $user = $this->db->tableName( 'user' );
188 $result = $this->db->query(
189 "SELECT user_name,COUNT(*) AS n
192 HAVING n > 1", $fname );
195 foreach ( $result as $row ) {
196 $list[] = $row->user_name;
202 * Examine user records for the given name. Try to see which record
203 * will be the one that actually gets used, then check remaining records
204 * for edits. If the dupes have no edits, we can safely remove them.
205 * @param $name string
206 * @param $doDelete bool
209 function examine( $name, $doDelete ) {
210 $fname = 'UserDupes::listDupes';
211 $result = $this->db->select( 'user',
213 array( 'user_name' => $name ),
216 $firstRow = $this->db->fetchObject( $result );
217 $firstId = $firstRow->user_id;
218 wfOut( "Record that will be used for '$name' is user_id=$firstId\n" );
220 foreach ( $result as $row ) {
221 $dupeId = $row->user_id;
222 wfOut( "... dupe id $dupeId: " );
223 $edits = $this->editCount( $dupeId );
226 wfOut( "has $edits edits! " );
228 $this->reassignEdits( $dupeId, $firstId );
229 $newEdits = $this->editCount( $dupeId );
230 if ( $newEdits == 0 ) {
231 wfOut( "confirmed cleaned. " );
234 wfOut( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
238 wfOut( "(will need to reassign edits on fix)" );
241 wfOut( "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 if ( $this->newSchema() ) {
261 return $this->editCountOn( 'revision', 'rev_user', $userid );
263 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
264 $this->editCountOn( 'old', 'old_user', $userid );
269 * Count the number of hits on a given table for this account.
270 * @param $table string
271 * @param $field string
276 function editCountOn( $table, $field, $userid ) {
277 $fname = 'UserDupes::editCountOn';
278 return intval( $this->db->selectField(
281 array( $field => $userid ),
290 function reassignEdits( $from, $to ) {
291 $set = $this->newSchema()
292 ? array( 'revision' => 'rev_user' )
293 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
294 foreach ( $set as $table => $field ) {
295 $this->reassignEditsOn( $table, $field, $from, $to );
300 * @param $table string
301 * @param $field string
306 function reassignEditsOn( $table, $field, $from, $to ) {
307 $fname = 'UserDupes::reassignEditsOn';
308 wfOut( "reassigning on $table... " );
309 $this->db->update( $table,
310 array( $field => $to ),
311 array( $field => $from ),
317 * Remove a user account line.
321 function trimAccount( $userid ) {
322 $fname = 'UserDupes::trimAccount';
323 wfOut( "deleting..." );
324 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );