2 # Copyright (C) 2005 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Look for duplicate user table entries and optionally prune them.
28 function UserDupes( &$database ) {
29 $this->db =& $database;
33 * Check if this database's user table has already had a unique
34 * user_name index applied.
37 function hasUniqueIndex() {
38 $fname = 'UserDupes::hasUniqueIndex';
39 #if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
40 $info = $this->db->indexInfo( 'user', 'user_name', $fname );
42 echo "WARNING: doesn't seem to have user_name index at all!\n";
46 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
47 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
48 # it's obviously some good stuff!
49 return ( $info->Non_unique == 0 );
53 * Checks the database for duplicate user account records
54 * and remove them in preparation for application of a unique
55 * index on the user_name field. Returns true if the table is
56 * clean or if duplicates have been resolved automatically.
58 * May return false if there are unresolvable problems.
59 * Status information will be echo'd to stdout.
63 function clearDupes() {
64 return $this->checkDupes( true );
68 * Checks the database for duplicate user account records
69 * in preparation for application of a unique index on the
70 * user_name field. Returns true if the table is clean or
71 * if duplicates can be resolved automatically.
73 * May return false if there are unresolvable problems.
74 * Status information will be echo'd to stdout.
76 * @param bool $doDelete pass true to actually remove things
77 * from the database; false to just check.
80 function checkDupes( $doDelete=false ) {
83 if( $this->hasUniqueIndex() ) {
84 echo "$wgDBname already has a unique index on its user table.\n";
90 echo "Checking for duplicate accounts...\n";
91 $dupes = $this->getDupes();
92 $count = count( $dupes );
94 echo "Found $count accounts with duplicate records on $wgDBname.\n";
96 $this->unresolvable = 0;
97 foreach( $dupes as $name ) {
98 $this->examine( $name, $doDelete );
103 if( $this->trimmed > 0 ) {
106 echo "$this->trimmed duplicate user records were deleted from $wgDBname.\n";
107 if( $this->unresolvable == 0 ) {
108 echo "It is now safe to apply the unique index on user_name.\n";
111 echo "$this->trimmed duplicate user accounts were found on $wgDBname which can be removed safely.\n";
112 echo "Run this script again with the --fix option to automatically delete them.\n";
116 if( $this->unresolvable > 0 ) {
118 echo "There were $this->unresolvable unresolvable accounts on $wgDBname.\n";
119 echo "These accounts have edits credited to duplicate records,\n";
120 echo "and need to be cleaned up manually before the unique index\n";
121 echo "for user_name can be applied.\n";
129 * We don't want anybody to mess with our stuff...
133 $fname = 'UserDupes::lock';
135 if( version_compare( $wgVersion, '1.5alpha', 'ge' ) ) {
136 $set = array( 'user', 'revision' );
138 $set = array( 'user', 'cur', 'old' );
140 $names = array_map( array( $this, 'lockTable' ), $set );
141 $tables = implode( ',', $names );
143 $result = $this->db->query( "LOCK TABLES $tables", $fname );
146 function lockTable( $table ) {
147 return $this->db->tableName( $table ) . ' WRITE';
154 $fname = 'UserDupes::unlock';
155 $result = $this->db->query( "UNLOCK TABLES", $fname );
159 * Grab usernames for which multiple records are present in the database.
163 function getDupes() {
164 $fname = 'UserDupes::listDupes';
165 $user = $this->db->tableName( 'user' );
166 $result = $this->db->query(
167 "SELECT user_name,COUNT(*) AS n
170 HAVING n > 1", $fname );
173 while( $row = $this->db->fetchObject( $result ) ) {
174 $list[] = $row->user_name;
176 $this->db->freeResult( $result );
182 * Examine user records for the given name. Try to see which record
183 * will be the one that actually gets used, then check remaining records
184 * for edits. If the dupes have no edits, we can safely remove them.
185 * @param string $name
186 * @param bool $doDelete
189 function examine( $name, $doDelete ) {
190 $fname = 'UserDupes::listDupes';
191 $result = $this->db->select( 'user',
193 array( 'user_name' => $name ),
196 $firstRow = $this->db->fetchObject( $result );
197 $firstId = $firstRow->user_id;
198 echo "Record that will be used for '$name' is user_id=$firstId\n";
200 while( $row = $this->db->fetchObject( $result ) ) {
201 $dupeId = $row->user_id;
202 echo "... dupe id $dupeId: ";
203 $edits = $this->editCount( $dupeId );
205 $this->unresolvable++;
206 echo "has $edits edits! MANUAL INTERVENTION REQUIRED.\n";
210 echo "ok, no edits. ";
212 $this->trimAccount( $dupeId );
217 $this->db->freeResult( $result );
221 * Count the number of edits attributed to this user.
222 * Does not currently check log table or other things
223 * where it might show up...
228 function editCount( $userid ) {
230 if( version_compare( $wgVersion, '1.5alpha', 'ge' ) ) {
231 return $this->editCountOn( 'revision', 'rev_user', $userid );
233 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
234 $this->editCountOn( 'old', 'old_user', $userid );
239 * Count the number of hits on a given table for this account.
240 * @param string $table
241 * @param string $field
246 function editCountOn( $table, $field, $userid ) {
247 $fname = 'UserDupes::editCountOn';
248 return IntVal( $this->db->selectField(
251 array( $field => $userid ),
256 * Remove a user account line.
260 function trimAccount( $userid ) {
261 $fname = 'UserDupes::trimAccount';
263 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );