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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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[0]->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 ) {
82 if( $this->hasUniqueIndex() ) {
83 echo wfWikiID()." already has a unique index on its user table.\n";
89 echo "Checking for duplicate accounts...\n";
90 $dupes = $this->getDupes();
91 $count = count( $dupes );
93 echo "Found $count accounts with duplicate records on ".wfWikiID().".\n";
95 $this->reassigned = 0;
97 foreach( $dupes as $name ) {
98 $this->examine( $name, $doDelete );
105 if( $this->reassigned > 0 ) {
107 echo "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n";
109 echo "$this->reassigned duplicate accounts need to have edits reassigned.\n";
113 if( $this->trimmed > 0 ) {
115 echo "$this->trimmed duplicate user records were deleted from ".wfWikiID().".\n";
117 echo "$this->trimmed duplicate user accounts were found on ".wfWikiID()." which can be removed safely.\n";
121 if( $this->failed > 0 ) {
122 echo "Something terribly awry; $this->failed duplicate accounts were not removed.\n";
126 if( $this->trimmed == 0 || $doDelete ) {
127 echo "It is now safe to apply the unique index on user_name.\n";
130 echo "Run this script again with the --fix option to automatically delete them.\n";
136 * We don't want anybody to mess with our stuff...
140 $fname = 'UserDupes::lock';
141 if( $this->newSchema() ) {
142 $set = array( 'user', 'revision' );
144 $set = array( 'user', 'cur', 'old' );
146 $names = array_map( array( $this, 'lockTable' ), $set );
147 $tables = implode( ',', $names );
149 $this->db->query( "LOCK TABLES $tables", $fname );
152 function lockTable( $table ) {
153 return $this->db->tableName( $table ) . ' WRITE';
160 function newSchema() {
161 return class_exists( 'Revision' );
168 $fname = 'UserDupes::unlock';
169 $this->db->query( "UNLOCK TABLES", $fname );
173 * Grab usernames for which multiple records are present in the database.
177 function getDupes() {
178 $fname = 'UserDupes::listDupes';
179 $user = $this->db->tableName( 'user' );
180 $result = $this->db->query(
181 "SELECT user_name,COUNT(*) AS n
184 HAVING n > 1", $fname );
187 while( $row = $this->db->fetchObject( $result ) ) {
188 $list[] = $row->user_name;
190 $this->db->freeResult( $result );
196 * Examine user records for the given name. Try to see which record
197 * will be the one that actually gets used, then check remaining records
198 * for edits. If the dupes have no edits, we can safely remove them.
199 * @param string $name
200 * @param bool $doDelete
203 function examine( $name, $doDelete ) {
204 $fname = 'UserDupes::listDupes';
205 $result = $this->db->select( 'user',
207 array( 'user_name' => $name ),
210 $firstRow = $this->db->fetchObject( $result );
211 $firstId = $firstRow->user_id;
212 echo "Record that will be used for '$name' is user_id=$firstId\n";
214 while( $row = $this->db->fetchObject( $result ) ) {
215 $dupeId = $row->user_id;
216 echo "... dupe id $dupeId: ";
217 $edits = $this->editCount( $dupeId );
220 echo "has $edits edits! ";
222 $this->reassignEdits( $dupeId, $firstId );
223 $newEdits = $this->editCount( $dupeId );
224 if( $newEdits == 0 ) {
225 echo "confirmed cleaned. ";
228 echo "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n";
232 echo "(will need to reassign edits on fix)";
235 echo "ok, no edits. ";
239 $this->trimAccount( $dupeId );
243 $this->db->freeResult( $result );
247 * Count the number of edits attributed to this user.
248 * Does not currently check log table or other things
249 * where it might show up...
254 function editCount( $userid ) {
255 if( $this->newSchema() ) {
256 return $this->editCountOn( 'revision', 'rev_user', $userid );
258 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
259 $this->editCountOn( 'old', 'old_user', $userid );
264 * Count the number of hits on a given table for this account.
265 * @param string $table
266 * @param string $field
271 function editCountOn( $table, $field, $userid ) {
272 $fname = 'UserDupes::editCountOn';
273 return intval( $this->db->selectField(
276 array( $field => $userid ),
285 function reassignEdits( $from, $to ) {
286 $set = $this->newSchema()
287 ? array( 'revision' => 'rev_user' )
288 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
289 foreach( $set as $table => $field ) {
290 $this->reassignEditsOn( $table, $field, $from, $to );
295 * @param string $table
296 * @param string $field
301 function reassignEditsOn( $table, $field, $from, $to ) {
302 $fname = 'UserDupes::reassignEditsOn';
303 echo "reassigning on $table... ";
304 $this->db->update( $table,
305 array( $field => $to ),
306 array( $field => $from ),
312 * Remove a user account line.
316 function trimAccount( $userid ) {
317 $fname = 'UserDupes::trimAccount';
319 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );