* Fixed 4 fatal parsing errors introduced in the last checkin.
[mediawiki.git] / maintenance / userDupes.inc
blob10704dff016bda980855645f7990f2437eb93d64
1 <?php
2 # Copyright (C) 2005 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4
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.
9
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.
14
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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
28         
29         function UserDupes( &$database ) {
30                 $this->db =& $database;
31         }
32         
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                 }
45                 
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         }
51         
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         }
66         
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;
83                 
84                 if( $this->hasUniqueIndex() ) {
85                         echo "$wgDBname already has a unique index on its user table.\n";
86                         return true;
87                 }
88                 
89                 $this->lock();
90                 
91                 echo "Checking for duplicate accounts...\n";
92                 $dupes = $this->getDupes();
93                 $count = count( $dupes );
94                 
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                 }
102                 
103                 $this->unlock();
104                 
105                 echo "\n";
106                 
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                 }
114                 
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                 }
122                 
123                 if( $this->failed > 0 ) {
124                         echo "Something terribly awry; $this->failed duplicate accounts were not removed.\n";
125                         return false;
126                 }
127                 
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         }
136         
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 );
150                 
151                 $result = $this->db->query( "LOCK TABLES $tables", $fname );
152         }
153         
154         function lockTable( $table ) {
155                 return $this->db->tableName( $table ) . ' WRITE';
156         }
157         
158         /**
159          * @return bool
160          * @access private
161          */
162         function newSchema() {
163                 global $wgVersion;
164                 return version_compare( $wgVersion, '1.5alpha', 'ge' );
165         }
166         
167         /**
168          * @access private
169          */
170         function unlock() {
171                 $fname = 'UserDupes::unlock';
172                 $result = $this->db->query( "UNLOCK TABLES", $fname );
173         }
174         
175         /**
176          * Grab usernames for which multiple records are present in the database.
177          * @return array
178          * @access private
179          */
180         function getDupes() {
181                 $fname = 'UserDupes::listDupes';
182                 $user = $this->db->tableName( 'user' );
183                 $result = $this->db->query(
184                          "SELECT user_name,COUNT(*) AS n
185                             FROM $user
186                         GROUP BY user_name
187                           HAVING n > 1", $fname );
188                 
189                 $list = array();
190                 while( $row = $this->db->fetchObject( $result ) ) {
191                         $list[] = $row->user_name;
192                 }
193                 $this->db->freeResult( $result );
194                 
195                 return $list;
196         }
197         
198         /**
199          * Examine user records for the given name. Try to see which record
200          * will be the one that actually gets used, then check remaining records
201          * for edits. If the dupes have no edits, we can safely remove them.
202          * @param string $name
203          * @param bool $doDelete
204          * @access private
205          */
206         function examine( $name, $doDelete ) {
207                 $fname = 'UserDupes::listDupes';
208                 $result = $this->db->select( 'user',
209                         array( 'user_id' ),
210                         array( 'user_name' => $name ),
211                         $fname );
212                 
213                 $firstRow = $this->db->fetchObject( $result );
214                 $firstId  = $firstRow->user_id;
215                 echo "Record that will be used for '$name' is user_id=$firstId\n";
216                 
217                 while( $row = $this->db->fetchObject( $result ) ) {
218                         $dupeId = $row->user_id;
219                         echo "... dupe id $dupeId: ";
220                         $edits = $this->editCount( $dupeId );
221                         if( $edits > 0 ) {
222                                 $this->reassigned++;
223                                 echo "has $edits edits! ";
224                                 if( $doDelete ) {
225                                         $this->reassignEdits( $dupeId, $firstId );
226                                         $newEdits = $this->editCount( $dupeId );
227                                         if( $newEdits == 0 ) {
228                                                 echo "confirmed cleaned. ";
229                                         } else {
230                                                 $this->failed++;
231                                                 echo "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n";
232                                                 continue;
233                                         }
234                                 } else {
235                                         echo "(will need to reassign edits on fix)";
236                                 }
237                         } else {
238                                 echo "ok, no edits. ";
239                         }
240                         $this->trimmed++;
241                         if( $doDelete ) {
242                                 $this->trimAccount( $dupeId );
243                         }
244                         echo "\n";
245                 }
246                 $this->db->freeResult( $result );
247         }
248         
249         /**
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...
253          * @param int $userid
254          * @return int
255          * @access private
256          */
257         function editCount( $userid ) {
258                 if( $this->newSchema() ) {
259                         return $this->editCountOn( 'revision', 'rev_user', $userid );
260                 } else {
261                         return $this->editCountOn( 'cur', 'cur_user', $userid ) +
262                                 $this->editCountOn( 'old', 'old_user', $userid );
263                 }
264         }
265         
266         /**
267          * Count the number of hits on a given table for this account.
268          * @param string $table
269          * @param string $field
270          * @param int $userid
271          * @return int
272          * @access private
273          */
274         function editCountOn( $table, $field, $userid ) {
275                 $fname = 'UserDupes::editCountOn';
276                 return IntVal( $this->db->selectField(
277                         $table,
278                         'COUNT(*)',
279                         array( $field => $userid ),
280                         $fname ) );
281         }
282         
283         /**
284          * @param int $from
285          * @param int $to
286          * @access private
287          */
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 );
294                 }
295         }
296         
297         /**
298          * @param string $table
299          * @param string $field
300          * @param int $from
301          * @param int $to
302          * @access private
303          */
304         function reassignEditsOn( $table, $field, $from, $to ) {
305                 $fname = 'UserDupes::reassignEditsOn';
306                 echo "reassigning on $table... ";
307                 $result = $this->db->update( $table,
308                         array( $field => $to ),
309                         array( $field => $from ),
310                         $fname );
311                 echo "ok. ";
312         }
313         
314         /**
315          * Remove a user account line.
316          * @param int $userid
317          * @access private
318          */
319         function trimAccount( $userid ) {
320                 $fname = 'UserDupes::trimAccount';
321                 echo "deleting...";
322                 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
323                 echo " ok";
324         }
325