When saving preferences, check hook and authentication plugin, and actually save...
[mediawiki.git] / maintenance / userDupes.inc
blob00c4e3456aed85b3cc617899a7f5641b87df5637
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[0]->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                 if( $this->hasUniqueIndex() ) {
83                         echo wfWikiID()." already has a unique index on its user table.\n";
84                         return true;
85                 }
87                 $this->lock();
89                 echo "Checking for duplicate accounts...\n";
90                 $dupes = $this->getDupes();
91                 $count = count( $dupes );
93                 echo "Found $count accounts with duplicate records on ".wfWikiID().".\n";
94                 $this->trimmed    = 0;
95                 $this->reassigned = 0;
96                 $this->failed     = 0;
97                 foreach( $dupes as $name ) {
98                         $this->examine( $name, $doDelete );
99                 }
101                 $this->unlock();
103                 echo "\n";
105                 if( $this->reassigned > 0 ) {
106                         if( $doDelete ) {
107                                 echo "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n";
108                         } else {
109                                 echo "$this->reassigned duplicate accounts need to have edits reassigned.\n";
110                         }
111                 }
113                 if( $this->trimmed > 0 ) {
114                         if( $doDelete ) {
115                                 echo "$this->trimmed duplicate user records were deleted from ".wfWikiID().".\n";
116                         } else {
117                                 echo "$this->trimmed duplicate user accounts were found on ".wfWikiID()." which can be removed safely.\n";
118                         }
119                 }
121                 if( $this->failed > 0 ) {
122                         echo "Something terribly awry; $this->failed duplicate accounts were not removed.\n";
123                         return false;
124                 }
126                 if( $this->trimmed == 0 || $doDelete ) {
127                         echo "It is now safe to apply the unique index on user_name.\n";
128                         return true;
129                 } else {
130                         echo "Run this script again with the --fix option to automatically delete them.\n";
131                         return false;
132                 }
133         }
135         /**
136          * We don't want anybody to mess with our stuff...
137          * @access private
138          */
139         function lock() {
140                 $fname = 'UserDupes::lock';
141                 if( $this->newSchema() ) {
142                         $set = array( 'user', 'revision' );
143                 } else {
144                         $set = array( 'user', 'cur', 'old' );
145                 }
146                 $names = array_map( array( $this, 'lockTable' ), $set );
147                 $tables = implode( ',', $names );
149                 $this->db->query( "LOCK TABLES $tables", $fname );
150         }
152         function lockTable( $table ) {
153                 return $this->db->tableName( $table ) . ' WRITE';
154         }
156         /**
157          * @return bool
158          * @access private
159          */
160         function newSchema() {
161                 return class_exists( 'Revision' );
162         }
164         /**
165          * @access private
166          */
167         function unlock() {
168                 $fname = 'UserDupes::unlock';
169                 $this->db->query( "UNLOCK TABLES", $fname );
170         }
172         /**
173          * Grab usernames for which multiple records are present in the database.
174          * @return array
175          * @access private
176          */
177         function getDupes() {
178                 $fname = 'UserDupes::listDupes';
179                 $user = $this->db->tableName( 'user' );
180                 $result = $this->db->query(
181                          "SELECT user_name,COUNT(*) AS n
182                             FROM $user
183                         GROUP BY user_name
184                           HAVING n > 1", $fname );
186                 $list = array();
187                 while( $row = $this->db->fetchObject( $result ) ) {
188                         $list[] = $row->user_name;
189                 }
190                 $this->db->freeResult( $result );
192                 return $list;
193         }
195         /**
196          * Examine user records for the given name. Try to see which record
197          * will be the one that actually gets used, then check remaining records
198          * for edits. If the dupes have no edits, we can safely remove them.
199          * @param string $name
200          * @param bool $doDelete
201          * @access private
202          */
203         function examine( $name, $doDelete ) {
204                 $fname = 'UserDupes::listDupes';
205                 $result = $this->db->select( 'user',
206                         array( 'user_id' ),
207                         array( 'user_name' => $name ),
208                         $fname );
210                 $firstRow = $this->db->fetchObject( $result );
211                 $firstId  = $firstRow->user_id;
212                 echo "Record that will be used for '$name' is user_id=$firstId\n";
214                 while( $row = $this->db->fetchObject( $result ) ) {
215                         $dupeId = $row->user_id;
216                         echo "... dupe id $dupeId: ";
217                         $edits = $this->editCount( $dupeId );
218                         if( $edits > 0 ) {
219                                 $this->reassigned++;
220                                 echo "has $edits edits! ";
221                                 if( $doDelete ) {
222                                         $this->reassignEdits( $dupeId, $firstId );
223                                         $newEdits = $this->editCount( $dupeId );
224                                         if( $newEdits == 0 ) {
225                                                 echo "confirmed cleaned. ";
226                                         } else {
227                                                 $this->failed++;
228                                                 echo "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n";
229                                                 continue;
230                                         }
231                                 } else {
232                                         echo "(will need to reassign edits on fix)";
233                                 }
234                         } else {
235                                 echo "ok, no edits. ";
236                         }
237                         $this->trimmed++;
238                         if( $doDelete ) {
239                                 $this->trimAccount( $dupeId );
240                         }
241                         echo "\n";
242                 }
243                 $this->db->freeResult( $result );
244         }
246         /**
247          * Count the number of edits attributed to this user.
248          * Does not currently check log table or other things
249          * where it might show up...
250          * @param int $userid
251          * @return int
252          * @access private
253          */
254         function editCount( $userid ) {
255                 if( $this->newSchema() ) {
256                         return $this->editCountOn( 'revision', 'rev_user', $userid );
257                 } else {
258                         return $this->editCountOn( 'cur', 'cur_user', $userid ) +
259                                 $this->editCountOn( 'old', 'old_user', $userid );
260                 }
261         }
263         /**
264          * Count the number of hits on a given table for this account.
265          * @param string $table
266          * @param string $field
267          * @param int $userid
268          * @return int
269          * @access private
270          */
271         function editCountOn( $table, $field, $userid ) {
272                 $fname = 'UserDupes::editCountOn';
273                 return intval( $this->db->selectField(
274                         $table,
275                         'COUNT(*)',
276                         array( $field => $userid ),
277                         $fname ) );
278         }
280         /**
281          * @param int $from
282          * @param int $to
283          * @access private
284          */
285         function reassignEdits( $from, $to ) {
286                 $set = $this->newSchema()
287                         ? array( 'revision' => 'rev_user' )
288                         : array( 'cur' => 'cur_user', 'old' => 'old_user' );
289                 foreach( $set as $table => $field ) {
290                         $this->reassignEditsOn( $table, $field, $from, $to );
291                 }
292         }
294         /**
295          * @param string $table
296          * @param string $field
297          * @param int $from
298          * @param int $to
299          * @access private
300          */
301         function reassignEditsOn( $table, $field, $from, $to ) {
302                 $fname = 'UserDupes::reassignEditsOn';
303                 echo "reassigning on $table... ";
304                 $this->db->update( $table,
305                         array( $field => $to ),
306                         array( $field => $from ),
307                         $fname );
308                 echo "ok. ";
309         }
311         /**
312          * Remove a user account line.
313          * @param int $userid
314          * @access private
315          */
316         function trimAccount( $userid ) {
317                 $fname = 'UserDupes::trimAccount';
318                 echo "deleting...";
319                 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
320                 echo " ok";
321         }