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 echo "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 bool $doDelete 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 echo "Checking for duplicate accounts...\n";
96 $dupes = $this->getDupes();
97 $count = count( $dupes );
99 echo "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 echo "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n";
115 echo "$this->reassigned duplicate accounts need to have edits reassigned.\n";
119 if( $this->trimmed > 0 ) {
121 echo "$this->trimmed duplicate user records were deleted from ".wfWikiID().".\n";
123 echo "$this->trimmed duplicate user accounts were found on ".wfWikiID()." which can be removed safely.\n";
127 if( $this->failed > 0 ) {
128 echo "Something terribly awry; $this->failed duplicate accounts were not removed.\n";
132 if( $this->trimmed == 0 || $doDelete ) {
133 echo "It is now safe to apply the unique index on user_name.\n";
136 echo "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 while( $row = $this->db->fetchObject( $result ) ) {
194 $list[] = $row->user_name;
196 $this->db->freeResult( $result );
202 * Examine user records for the given name. Try to see which record
203 * will be the one that actually gets used, then check remaining records
204 * for edits. If the dupes have no edits, we can safely remove them.
205 * @param string $name
206 * @param bool $doDelete
209 function examine( $name, $doDelete ) {
210 $fname = 'UserDupes::listDupes';
211 $result = $this->db->select( 'user',
213 array( 'user_name' => $name ),
216 $firstRow = $this->db->fetchObject( $result );
217 $firstId = $firstRow->user_id;
218 echo "Record that will be used for '$name' is user_id=$firstId\n";
220 while( $row = $this->db->fetchObject( $result ) ) {
221 $dupeId = $row->user_id;
222 echo "... dupe id $dupeId: ";
223 $edits = $this->editCount( $dupeId );
226 echo "has $edits edits! ";
228 $this->reassignEdits( $dupeId, $firstId );
229 $newEdits = $this->editCount( $dupeId );
230 if( $newEdits == 0 ) {
231 echo "confirmed cleaned. ";
234 echo "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n";
238 echo "(will need to reassign edits on fix)";
241 echo "ok, no edits. ";
245 $this->trimAccount( $dupeId );
249 $this->db->freeResult( $result );
253 * Count the number of edits attributed to this user.
254 * Does not currently check log table or other things
255 * where it might show up...
260 function editCount( $userid ) {
261 if( $this->newSchema() ) {
262 return $this->editCountOn( 'revision', 'rev_user', $userid );
264 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
265 $this->editCountOn( 'old', 'old_user', $userid );
270 * Count the number of hits on a given table for this account.
271 * @param string $table
272 * @param string $field
277 function editCountOn( $table, $field, $userid ) {
278 $fname = 'UserDupes::editCountOn';
279 return intval( $this->db->selectField(
282 array( $field => $userid ),
291 function reassignEdits( $from, $to ) {
292 $set = $this->newSchema()
293 ? array( 'revision' => 'rev_user' )
294 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
295 foreach( $set as $table => $field ) {
296 $this->reassignEditsOn( $table, $field, $from, $to );
301 * @param string $table
302 * @param string $field
307 function reassignEditsOn( $table, $field, $from, $to ) {
308 $fname = 'UserDupes::reassignEditsOn';
309 echo "reassigning on $table... ";
310 $this->db->update( $table,
311 array( $field => $to ),
312 array( $field => $from ),
318 * Remove a user account line.
322 function trimAccount( $userid ) {
323 $fname = 'UserDupes::trimAccount';
325 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );