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.
29 * Checks the database for duplicate user account records
30 * and remove them in preparation for application of a unique
31 * index on the user_name field. Returns true if the table is
32 * clean or if duplicates have been resolved automatically.
34 * May return false if there are unresolvable problems.
35 * Status information will be echo'd to stdout.
39 function clearDupes() {
40 return $this->checkDupes( true );
44 * Checks the database for duplicate user account records
45 * in preparation for application of a unique index on the
46 * user_name field. Returns true if the table is clean or
47 * if duplicates can be resolved automatically.
49 * May return false if there are unresolvable problems.
50 * Status information will be echo'd to stdout.
52 * @param bool $doDelete pass true to actually remove things
53 * from the database; false to just check.
56 function checkDupes( $doDelete=false ) {
59 $this->db =& wfGetDB( DB_MASTER );
62 echo "Checking for duplicate accounts...\n";
63 $dupes = $this->getDupes();
64 $count = count( $dupes );
66 echo "Found $count accounts with duplicate records on $wgDBname.\n";
68 $this->unresolvable = 0;
69 foreach( $dupes as $name ) {
70 $this->examine( $name, $doDelete );
75 if( $this->trimmed > 0 ) {
78 echo "$this->trimmed duplicate user records were deleted from $wgDBname.\n";
79 if( $this->unresolvable == 0 ) {
80 echo "It is now safe to apply the unique index on user_name.\n";
83 echo "$this->trimmed duplicate user accounts were found on $wgDBname which can be removed safely.\n";
84 echo "Run this script again with the --fix option to automatically delete them.\n";
88 if( $this->unresolvable > 0 ) {
90 echo "There were $this->unresolvable unresolvable accounts on $wgDBname.\n";
91 echo "These accounts have edits credited to duplicate records,\n";
92 echo "and need to be cleaned up manually before the unique index\n";
93 echo "for user_name can be applied.\n";
101 * We don't want anybody to mess with our stuff...
105 $fname = 'UserDupes::lock';
107 if( version_compare( $wgVersion, '1.5', 'gte' ) ) {
108 $set = array( 'user', 'cur', 'old' );
110 $set = array( 'user', 'revision' );
112 $names = array_map( array( $this, 'lockTable' ), $set );
113 $tables = implode( ',', $names );
115 $result = $this->db->query( "LOCK TABLES $tables", $fname );
118 function lockTable( $table ) {
119 return $this->db->tableName( $table ) . ' WRITE';
126 $fname = 'UserDupes::unlock';
127 $result = $this->db->query( "UNLOCK TABLES", $fname );
131 * Grab usernames for which multiple records are present in the database.
135 function getDupes() {
136 $fname = 'UserDupes::listDupes';
137 $user = $this->db->tableName( 'user' );
138 $result = $this->db->query(
139 "SELECT user_name,COUNT(*) AS n
142 HAVING n > 1", $fname );
145 while( $row = $this->db->fetchObject( $result ) ) {
146 $list[] = $row->user_name;
148 $this->db->freeResult( $result );
154 * Examine user records for the given name. Try to see which record
155 * will be the one that actually gets used, then check remaining records
156 * for edits. If the dupes have no edits, we can safely remove them.
157 * @param string $name
158 * @param bool $doDelete
161 function examine( $name, $doDelete ) {
162 $fname = 'UserDupes::listDupes';
163 $result = $this->db->select( 'user',
165 array( 'user_name' => $name ),
168 $firstRow = $this->db->fetchObject( $result );
169 $firstId = $firstRow->user_id;
170 echo "Record that will be used for '$name' is user_id=$firstId\n";
172 while( $row = $this->db->fetchObject( $result ) ) {
173 $dupeId = $row->user_id;
174 echo "... dupe id $dupeId: ";
175 $edits = $this->editCount( $dupeId );
177 $this->unresolvable++;
178 echo "has $edits edits! MANUAL INTERVENTION REQUIRED.\n";
182 echo "ok, no edits. ";
184 $this->trimAccount( $dupeId );
189 $this->db->freeResult( $result );
193 * Count the number of edits attributed to this user.
194 * Does not currently check log table or other things
195 * where it might show up...
200 function editCount( $userid ) {
202 if( version_compare( $wgVersion, '1.5alpha', 'ge' ) ) {
203 return $this->editCountOn( 'revision', 'rev_user', $userid );
205 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
206 $this->editCountOn( 'old', 'old_user', $userid );
211 * Count the number of hits on a given table for this account.
212 * @param string $table
213 * @param string $field
218 function editCountOn( $table, $field, $userid ) {
219 $fname = 'UserDupes::editCountOn';
220 return IntVal( $this->db->selectField(
223 array( $field => $userid ),
228 * Remove a user account line.
232 function trimAccount( $userid ) {
233 $fname = 'UserDupes::trimAccount';
235 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );