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
22 * @ingroup Maintenance
26 * Look for duplicate user table entries and optionally prune them.
27 * @ingroup Maintenance
35 function UserDupes( &$database ) {
36 $this->db =& $database;
40 * Check if this database's user table has already had a unique
41 * user_name index applied.
44 function hasUniqueIndex() {
45 $fname = 'UserDupes::hasUniqueIndex';
46 $info = $this->db->indexInfo( 'user', 'user_name', $fname );
48 wfOut( "WARNING: doesn't seem to have user_name index at all!\n" );
52 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
53 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
54 # it's obviously some good stuff!
55 return ( $info[0]->Non_unique == 0 );
59 * Checks the database for duplicate user account records
60 * and remove them in preparation for application of a unique
61 * index on the user_name field. Returns true if the table is
62 * clean or if duplicates have been resolved automatically.
64 * May return false if there are unresolvable problems.
65 * Status information will be echo'd to stdout.
69 function clearDupes() {
70 return $this->checkDupes( true );
74 * Checks the database for duplicate user account records
75 * in preparation for application of a unique index on the
76 * user_name field. Returns true if the table is clean or
77 * if duplicates can be resolved automatically.
79 * Returns false if there are duplicates and resolution was
80 * not requested. (If doing resolution, edits may be reassigned.)
81 * Status information will be echo'd to stdout.
83 * @param $doDelete bool: pass true to actually remove things
84 * from the database; false to just check.
87 function checkDupes( $doDelete = false ) {
88 if ( $this->hasUniqueIndex() ) {
89 echo wfWikiID() . " already has a unique index on its user table.\n";
95 wfOut( "Checking for duplicate accounts...\n" );
96 $dupes = $this->getDupes();
97 $count = count( $dupes );
99 wfOut( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
101 $this->reassigned = 0;
103 foreach ( $dupes as $name ) {
104 $this->examine( $name, $doDelete );
111 if ( $this->reassigned > 0 ) {
113 wfOut( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
115 wfOut( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
119 if ( $this->trimmed > 0 ) {
121 wfOut( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
123 wfOut( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
127 if ( $this->failed > 0 ) {
128 wfOut( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
132 if ( $this->trimmed == 0 || $doDelete ) {
133 wfOut( "It is now safe to apply the unique index on user_name.\n" );
136 wfOut( "Run this script again with the --fix option to automatically delete them.\n" );
142 * We don't want anybody to mess with our stuff...
146 $fname = 'UserDupes::lock';
147 if ( $this->newSchema() ) {
148 $set = array( 'user', 'revision' );
150 $set = array( 'user', 'cur', 'old' );
152 $names = array_map( array( $this, 'lockTable' ), $set );
153 $tables = implode( ',', $names );
155 $this->db->query( "LOCK TABLES $tables", $fname );
158 function lockTable( $table ) {
159 return $this->db->tableName( $table ) . ' WRITE';
166 function newSchema() {
167 return class_exists( 'Revision' );
174 $fname = 'UserDupes::unlock';
175 $this->db->query( "UNLOCK TABLES", $fname );
179 * Grab usernames for which multiple records are present in the database.
183 function getDupes() {
184 $fname = 'UserDupes::listDupes';
185 $user = $this->db->tableName( 'user' );
186 $result = $this->db->query(
187 "SELECT user_name,COUNT(*) AS n
190 HAVING n > 1", $fname );
193 foreach ( $result as $row ) {
194 $list[] = $row->user_name;
200 * Examine user records for the given name. Try to see which record
201 * will be the one that actually gets used, then check remaining records
202 * for edits. If the dupes have no edits, we can safely remove them.
203 * @param $name string
204 * @param $doDelete bool
207 function examine( $name, $doDelete ) {
208 $fname = 'UserDupes::listDupes';
209 $result = $this->db->select( 'user',
211 array( 'user_name' => $name ),
214 $firstRow = $this->db->fetchObject( $result );
215 $firstId = $firstRow->user_id;
216 wfOut( "Record that will be used for '$name' is user_id=$firstId\n" );
218 foreach ( $result as $row ) {
219 $dupeId = $row->user_id;
220 wfOut( "... dupe id $dupeId: " );
221 $edits = $this->editCount( $dupeId );
224 wfOut( "has $edits edits! " );
226 $this->reassignEdits( $dupeId, $firstId );
227 $newEdits = $this->editCount( $dupeId );
228 if ( $newEdits == 0 ) {
229 wfOut( "confirmed cleaned. " );
232 wfOut( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
236 wfOut( "(will need to reassign edits on fix)" );
239 wfOut( "ok, no edits. " );
243 $this->trimAccount( $dupeId );
250 * Count the number of edits attributed to this user.
251 * Does not currently check log table or other things
252 * where it might show up...
257 function editCount( $userid ) {
258 if ( $this->newSchema() ) {
259 return $this->editCountOn( 'revision', 'rev_user', $userid );
261 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
262 $this->editCountOn( 'old', 'old_user', $userid );
267 * Count the number of hits on a given table for this account.
268 * @param $table string
269 * @param $field string
274 function editCountOn( $table, $field, $userid ) {
275 $fname = 'UserDupes::editCountOn';
276 return intval( $this->db->selectField(
279 array( $field => $userid ),
288 function reassignEdits( $from, $to ) {
289 $set = $this->newSchema()
290 ? array( 'revision' => 'rev_user' )
291 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
292 foreach ( $set as $table => $field ) {
293 $this->reassignEditsOn( $table, $field, $from, $to );
298 * @param $table string
299 * @param $field string
304 function reassignEditsOn( $table, $field, $from, $to ) {
305 $fname = 'UserDupes::reassignEditsOn';
306 wfOut( "reassigning on $table... " );
307 $this->db->update( $table,
308 array( $field => $to ),
309 array( $field => $from ),
315 * Remove a user account line.
319 function trimAccount( $userid ) {
320 $fname = 'UserDupes::trimAccount';
321 wfOut( "deleting..." );
322 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );