* API: All pages list
[mediawiki.git] / maintenance / userDupes.inc
blobf66051d45f946a00f1c1171f50a3b1dc44ffcb98
1 <?php
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
20 /**
21  * Look for duplicate user table entries and optionally prune them.
22  */
23 class UserDupes {
24         var $db;
25         var $reassigned;
26         var $trimmed;
27         var $failed;
29         function UserDupes( &$database ) {
30                 $this->db =& $database;
31         }
33         /**
34          * Check if this database's user table has already had a unique
35          * user_name index applied.
36          * @return bool
37          */
38         function hasUniqueIndex() {
39                 $fname = 'UserDupes::hasUniqueIndex';
40                 $info = $this->db->indexInfo( 'user', 'user_name', $fname );
41                 if( !$info ) {
42                         echo "WARNING: doesn't seem to have user_name index at all!\n";
43                         return false;
44                 }
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 );
50         }
52         /**
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.
57          *
58          * May return false if there are unresolvable problems.
59          * Status information will be echo'd to stdout.
60          *
61          * @return bool
62          */
63         function clearDupes() {
64                 return $this->checkDupes( true );
65         }
67         /**
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.
72          *
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.
76          *
77          * @param bool $doDelete pass true to actually remove things
78          *                       from the database; false to just check.
79          * @return bool
80          */
81         function checkDupes( $doDelete = false ) {
82                 global $wgDBname;
84                 if( $this->hasUniqueIndex() ) {
85                         echo "$wgDBname already has a unique index on its user table.\n";
86                         return true;
87                 }
89                 $this->lock();
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";
96                 $this->trimmed    = 0;
97                 $this->reassigned = 0;
98                 $this->failed     = 0;
99                 foreach( $dupes as $name ) {
100                         $this->examine( $name, $doDelete );
101                 }
103                 $this->unlock();
105                 echo "\n";
107                 if( $this->reassigned > 0 ) {
108                         if( $doDelete ) {
109                                 echo "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n";
110                         } else {
111                                 echo "$this->reassigned duplicate accounts need to have edits reassigned.\n";
112                         }
113                 }
115                 if( $this->trimmed > 0 ) {
116                         if( $doDelete ) {
117                                 echo "$this->trimmed duplicate user records were deleted from $wgDBname.\n";
118                         } else {
119                                 echo "$this->trimmed duplicate user accounts were found on $wgDBname which can be removed safely.\n";
120                         }
121                 }
123                 if( $this->failed > 0 ) {
124                         echo "Something terribly awry; $this->failed duplicate accounts were not removed.\n";
125                         return false;
126                 }
128                 if( $this->trimmed == 0 || $doDelete ) {
129                         echo "It is now safe to apply the unique index on user_name.\n";
130                         return true;
131                 } else {
132                         echo "Run this script again with the --fix option to automatically delete them.\n";
133                         return false;
134                 }
135         }
137         /**
138          * We don't want anybody to mess with our stuff...
139          * @access private
140          */
141         function lock() {
142                 $fname = 'UserDupes::lock';
143                 if( $this->newSchema() ) {
144                         $set = array( 'user', 'revision' );
145                 } else {
146                         $set = array( 'user', 'cur', 'old' );
147                 }
148                 $names = array_map( array( $this, 'lockTable' ), $set );
149                 $tables = implode( ',', $names );
151                 $result = $this->db->query( "LOCK TABLES $tables", $fname );
152         }
154         function lockTable( $table ) {
155                 return $this->db->tableName( $table ) . ' WRITE';
156         }
158         /**
159          * @return bool
160          * @access private
161          */
162         function newSchema() {
163                 return class_exists( 'Revision' );
164         }
166         /**
167          * @access private
168          */
169         function unlock() {
170                 $fname = 'UserDupes::unlock';
171                 $result = $this->db->query( "UNLOCK TABLES", $fname );
172         }
174         /**
175          * Grab usernames for which multiple records are present in the database.
176          * @return array
177          * @access private
178          */
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
184                             FROM $user
185                         GROUP BY user_name
186                           HAVING n > 1", $fname );
188                 $list = array();
189                 while( $row = $this->db->fetchObject( $result ) ) {
190                         $list[] = $row->user_name;
191                 }
192                 $this->db->freeResult( $result );
194                 return $list;
195         }
197         /**
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
203          * @access private
204          */
205         function examine( $name, $doDelete ) {
206                 $fname = 'UserDupes::listDupes';
207                 $result = $this->db->select( 'user',
208                         array( 'user_id' ),
209                         array( 'user_name' => $name ),
210                         $fname );
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 );
220                         if( $edits > 0 ) {
221                                 $this->reassigned++;
222                                 echo "has $edits edits! ";
223                                 if( $doDelete ) {
224                                         $this->reassignEdits( $dupeId, $firstId );
225                                         $newEdits = $this->editCount( $dupeId );
226                                         if( $newEdits == 0 ) {
227                                                 echo "confirmed cleaned. ";
228                                         } else {
229                                                 $this->failed++;
230                                                 echo "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n";
231                                                 continue;
232                                         }
233                                 } else {
234                                         echo "(will need to reassign edits on fix)";
235                                 }
236                         } else {
237                                 echo "ok, no edits. ";
238                         }
239                         $this->trimmed++;
240                         if( $doDelete ) {
241                                 $this->trimAccount( $dupeId );
242                         }
243                         echo "\n";
244                 }
245                 $this->db->freeResult( $result );
246         }
248         /**
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...
252          * @param int $userid
253          * @return int
254          * @access private
255          */
256         function editCount( $userid ) {
257                 if( $this->newSchema() ) {
258                         return $this->editCountOn( 'revision', 'rev_user', $userid );
259                 } else {
260                         return $this->editCountOn( 'cur', 'cur_user', $userid ) +
261                                 $this->editCountOn( 'old', 'old_user', $userid );
262                 }
263         }
265         /**
266          * Count the number of hits on a given table for this account.
267          * @param string $table
268          * @param string $field
269          * @param int $userid
270          * @return int
271          * @access private
272          */
273         function editCountOn( $table, $field, $userid ) {
274                 $fname = 'UserDupes::editCountOn';
275                 return intval( $this->db->selectField(
276                         $table,
277                         'COUNT(*)',
278                         array( $field => $userid ),
279                         $fname ) );
280         }
282         /**
283          * @param int $from
284          * @param int $to
285          * @access private
286          */
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 );
293                 }
294         }
296         /**
297          * @param string $table
298          * @param string $field
299          * @param int $from
300          * @param int $to
301          * @access private
302          */
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 ),
309                         $fname );
310                 echo "ok. ";
311         }
313         /**
314          * Remove a user account line.
315          * @param int $userid
316          * @access private
317          */
318         function trimAccount( $userid ) {
319                 $fname = 'UserDupes::trimAccount';
320                 echo "deleting...";
321                 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
322                 echo " ok";
323         }