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
34 private $outputCallback;
36 function __construct( &$database, $outputCallback ) {
37 $this->db = $database;
38 $this->outputCallback = $outputCallback;
42 * Output some text via the output callback provided
43 * @param $str String Text to print
45 private function out( $str ) {
46 call_user_func( $this->outputCallback, $str );
50 * Check if this database's user table has already had a unique
51 * user_name index applied.
54 function hasUniqueIndex() {
55 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
57 $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
61 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
62 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
63 # it's obviously some good stuff!
64 return ( $info[0]->Non_unique == 0 );
68 * Checks the database for duplicate user account records
69 * and remove them in preparation for application of a unique
70 * index on the user_name field. Returns true if the table is
71 * clean or if duplicates have been resolved automatically.
73 * May return false if there are unresolvable problems.
74 * Status information will be echo'd to stdout.
78 function clearDupes() {
79 return $this->checkDupes( true );
83 * Checks the database for duplicate user account records
84 * in preparation for application of a unique index on the
85 * user_name field. Returns true if the table is clean or
86 * if duplicates can be resolved automatically.
88 * Returns false if there are duplicates and resolution was
89 * not requested. (If doing resolution, edits may be reassigned.)
90 * Status information will be echo'd to stdout.
92 * @param $doDelete bool: pass true to actually remove things
93 * from the database; false to just check.
96 function checkDupes( $doDelete = false ) {
97 if ( $this->hasUniqueIndex() ) {
98 echo wfWikiID() . " already has a unique index on its user table.\n";
104 $this->out( "Checking for duplicate accounts...\n" );
105 $dupes = $this->getDupes();
106 $count = count( $dupes );
108 $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
110 $this->reassigned = 0;
112 foreach ( $dupes as $name ) {
113 $this->examine( $name, $doDelete );
120 if ( $this->reassigned > 0 ) {
122 $this->out( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
124 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
128 if ( $this->trimmed > 0 ) {
130 $this->out( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
132 $this->out( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
136 if ( $this->failed > 0 ) {
137 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
141 if ( $this->trimmed == 0 || $doDelete ) {
142 $this->out( "It is now safe to apply the unique index on user_name.\n" );
145 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
151 * We don't want anybody to mess with our stuff...
155 if ( $this->newSchema() ) {
156 $set = array( 'user', 'revision' );
158 $set = array( 'user', 'cur', 'old' );
160 $names = array_map( array( $this, 'lockTable' ), $set );
161 $tables = implode( ',', $names );
163 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
166 function lockTable( $table ) {
167 return $this->db->tableName( $table ) . ' WRITE';
174 function newSchema() {
175 return class_exists( 'Revision' );
182 $this->db->query( "UNLOCK TABLES", __METHOD__ );
186 * Grab usernames for which multiple records are present in the database.
190 function getDupes() {
191 $user = $this->db->tableName( 'user' );
192 $result = $this->db->query(
193 "SELECT user_name,COUNT(*) AS n
196 HAVING n > 1", __METHOD__ );
199 foreach ( $result as $row ) {
200 $list[] = $row->user_name;
206 * Examine user records for the given name. Try to see which record
207 * will be the one that actually gets used, then check remaining records
208 * for edits. If the dupes have no edits, we can safely remove them.
209 * @param $name string
210 * @param $doDelete bool
213 function examine( $name, $doDelete ) {
214 $result = $this->db->select( 'user',
216 array( 'user_name' => $name ),
219 $firstRow = $this->db->fetchObject( $result );
220 $firstId = $firstRow->user_id;
221 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
223 foreach ( $result as $row ) {
224 $dupeId = $row->user_id;
225 $this->out( "... dupe id $dupeId: " );
226 $edits = $this->editCount( $dupeId );
229 $this->out( "has $edits edits! " );
231 $this->reassignEdits( $dupeId, $firstId );
232 $newEdits = $this->editCount( $dupeId );
233 if ( $newEdits == 0 ) {
234 $this->out( "confirmed cleaned. " );
237 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
241 $this->out( "(will need to reassign edits on fix)" );
244 $this->out( "ok, no edits. " );
248 $this->trimAccount( $dupeId );
255 * Count the number of edits attributed to this user.
256 * Does not currently check log table or other things
257 * where it might show up...
262 function editCount( $userid ) {
263 if ( $this->newSchema() ) {
264 return $this->editCountOn( 'revision', 'rev_user', $userid );
266 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
267 $this->editCountOn( 'old', 'old_user', $userid );
272 * Count the number of hits on a given table for this account.
273 * @param $table string
274 * @param $field string
279 function editCountOn( $table, $field, $userid ) {
280 return intval( $this->db->selectField(
283 array( $field => $userid ),
292 function reassignEdits( $from, $to ) {
293 $set = $this->newSchema()
294 ? array( 'revision' => 'rev_user' )
295 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
296 foreach ( $set as $table => $field ) {
297 $this->reassignEditsOn( $table, $field, $from, $to );
302 * @param $table string
303 * @param $field string
308 function reassignEditsOn( $table, $field, $from, $to ) {
309 $this->out( "reassigning on $table... " );
310 $this->db->update( $table,
311 array( $field => $to ),
312 array( $field => $from ),
314 $this->out( "ok. " );
318 * Remove a user account line.
322 function trimAccount( $userid ) {
323 $this->out( "deleting..." );
324 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );