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 function UserDupes( &$database ) {
30 $this->db =& $database;
34 * Check if this database's user table has already had a unique
35 * user_name index applied.
38 function hasUniqueIndex() {
39 $fname = 'UserDupes::hasUniqueIndex';
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 * Returns false if there are duplicates and resolution was
74 * not requested. (If doing resolution, edits may be reassigned.)
75 * Status information will be echo'd to stdout.
77 * @param bool $doDelete pass true to actually remove things
78 * from the database; false to just check.
81 function checkDupes( $doDelete = false ) {
84 if( $this->hasUniqueIndex() ) {
85 echo "$wgDBname already has a unique index on its user table.\n";
91 echo "Checking for duplicate accounts...\n";
92 $dupes = $this->getDupes();
93 $count = count( $dupes );
95 echo "Found $count accounts with duplicate records on $wgDBname.\n";
97 $this->reassigned = 0;
99 foreach( $dupes as $name ) {
100 $this->examine( $name, $doDelete );
107 if( $this->reassigned > 0 ) {
109 echo "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n";
111 echo "$this->reassigned duplicate accounts need to have edits reassigned.\n";
115 if( $this->trimmed > 0 ) {
117 echo "$this->trimmed duplicate user records were deleted from $wgDBname.\n";
119 echo "$this->trimmed duplicate user accounts were found on $wgDBname which can be removed safely.\n";
123 if( $this->failed > 0 ) {
124 echo "Something terribly awry; $this->failed duplicate accounts were not removed.\n";
128 if( $this->trimmed == 0 || $doDelete ) {
129 echo "It is now safe to apply the unique index on user_name.\n";
132 echo "Run this script again with the --fix option to automatically delete them.\n";
138 * We don't want anybody to mess with our stuff...
142 $fname = 'UserDupes::lock';
143 if( $this->newSchema() ) {
144 $set = array( 'user', 'revision' );
146 $set = array( 'user', 'cur', 'old' );
148 $names = array_map( array( $this, 'lockTable' ), $set );
149 $tables = implode( ',', $names );
151 $result = $this->db->query( "LOCK TABLES $tables", $fname );
154 function lockTable( $table ) {
155 return $this->db->tableName( $table ) . ' WRITE';
162 function newSchema() {
163 return class_exists( 'Revision' );
170 $fname = 'UserDupes::unlock';
171 $result = $this->db->query( "UNLOCK TABLES", $fname );
175 * Grab usernames for which multiple records are present in the database.
179 function getDupes() {
180 $fname = 'UserDupes::listDupes';
181 $user = $this->db->tableName( 'user' );
182 $result = $this->db->query(
183 "SELECT user_name,COUNT(*) AS n
186 HAVING n > 1", $fname );
189 while( $row = $this->db->fetchObject( $result ) ) {
190 $list[] = $row->user_name;
192 $this->db->freeResult( $result );
198 * Examine user records for the given name. Try to see which record
199 * will be the one that actually gets used, then check remaining records
200 * for edits. If the dupes have no edits, we can safely remove them.
201 * @param string $name
202 * @param bool $doDelete
205 function examine( $name, $doDelete ) {
206 $fname = 'UserDupes::listDupes';
207 $result = $this->db->select( 'user',
209 array( 'user_name' => $name ),
212 $firstRow = $this->db->fetchObject( $result );
213 $firstId = $firstRow->user_id;
214 echo "Record that will be used for '$name' is user_id=$firstId\n";
216 while( $row = $this->db->fetchObject( $result ) ) {
217 $dupeId = $row->user_id;
218 echo "... dupe id $dupeId: ";
219 $edits = $this->editCount( $dupeId );
222 echo "has $edits edits! ";
224 $this->reassignEdits( $dupeId, $firstId );
225 $newEdits = $this->editCount( $dupeId );
226 if( $newEdits == 0 ) {
227 echo "confirmed cleaned. ";
230 echo "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n";
234 echo "(will need to reassign edits on fix)";
237 echo "ok, no edits. ";
241 $this->trimAccount( $dupeId );
245 $this->db->freeResult( $result );
249 * Count the number of edits attributed to this user.
250 * Does not currently check log table or other things
251 * where it might show up...
256 function editCount( $userid ) {
257 if( $this->newSchema() ) {
258 return $this->editCountOn( 'revision', 'rev_user', $userid );
260 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
261 $this->editCountOn( 'old', 'old_user', $userid );
266 * Count the number of hits on a given table for this account.
267 * @param string $table
268 * @param string $field
273 function editCountOn( $table, $field, $userid ) {
274 $fname = 'UserDupes::editCountOn';
275 return intval( $this->db->selectField(
278 array( $field => $userid ),
287 function reassignEdits( $from, $to ) {
288 $set = $this->newSchema()
289 ? array( 'revision' => 'rev_user' )
290 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
291 foreach( $set as $table => $field ) {
292 $this->reassignEditsOn( $table, $field, $from, $to );
297 * @param string $table
298 * @param string $field
303 function reassignEditsOn( $table, $field, $from, $to ) {
304 $fname = 'UserDupes::reassignEditsOn';
305 echo "reassigning on $table... ";
306 $result = $this->db->update( $table,
307 array( $field => $to ),
308 array( $field => $from ),
314 * Remove a user account line.
318 function trimAccount( $userid ) {
319 $fname = 'UserDupes::trimAccount';
321 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );