Add new recentchanges field rc_source to replace rc_type
[mediawiki.git] / maintenance / userDupes.inc
blob8bd80c97866755c8e6e357ba79b15c2311302953
1 <?php
2 /**
3  * Helper class for update.php.
4  *
5  * Copyright © 2005 Brion Vibber <brion@pobox.com>
6  * http://www.mediawiki.org/
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  * http://www.gnu.org/copyleft/gpl.html
22  *
23  * @file
24  * @ingroup Maintenance
25  */
27 /**
28  * Look for duplicate user table entries and optionally prune them.
29  *
30  * This is still used by our MysqlUpdater at:
31  * includes/installer/MysqlUpdater.php
32  *
33  * @ingroup Maintenance
34  */
35 class UserDupes {
36         private $db;
37         private $reassigned;
38         private $trimmed;
39         private $failed;
40         private $outputCallback;
42         function __construct( &$database, $outputCallback ) {
43                 $this->db = $database;
44                 $this->outputCallback = $outputCallback;
45         }
47         /**
48          * Output some text via the output callback provided
49          * @param $str String Text to print
50          */
51         private function out( $str ) {
52                 call_user_func( $this->outputCallback, $str );
53         }
55         /**
56          * Check if this database's user table has already had a unique
57          * user_name index applied.
58          * @return bool
59          */
60         function hasUniqueIndex() {
61                 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
62                 if ( !$info ) {
63                         $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
64                         return false;
65                 }
67                 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
68                 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
69                 # it's obviously some good stuff!
70                 return ( $info[0]->Non_unique == 0 );
71         }
73         /**
74          * Checks the database for duplicate user account records
75          * and remove them in preparation for application of a unique
76          * index on the user_name field. Returns true if the table is
77          * clean or if duplicates have been resolved automatically.
78          *
79          * May return false if there are unresolvable problems.
80          * Status information will be echo'd to stdout.
81          *
82          * @return bool
83          */
84         function clearDupes() {
85                 return $this->checkDupes( true );
86         }
88         /**
89          * Checks the database for duplicate user account records
90          * in preparation for application of a unique index on the
91          * user_name field. Returns true if the table is clean or
92          * if duplicates can be resolved automatically.
93          *
94          * Returns false if there are duplicates and resolution was
95          * not requested. (If doing resolution, edits may be reassigned.)
96          * Status information will be echo'd to stdout.
97          *
98          * @param $doDelete bool: pass true to actually remove things
99          *                  from the database; false to just check.
100          * @return bool
101          */
102         function checkDupes( $doDelete = false ) {
103                 if ( $this->hasUniqueIndex() ) {
104                         echo wfWikiID() . " already has a unique index on its user table.\n";
105                         return true;
106                 }
108                 $this->lock();
110                 $this->out( "Checking for duplicate accounts...\n" );
111                 $dupes = $this->getDupes();
112                 $count = count( $dupes );
114                 $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
115                 $this->trimmed = 0;
116                 $this->reassigned = 0;
117                 $this->failed = 0;
118                 foreach ( $dupes as $name ) {
119                         $this->examine( $name, $doDelete );
120                 }
122                 $this->unlock();
124                 $this->out( "\n" );
126                 if ( $this->reassigned > 0 ) {
127                         if ( $doDelete ) {
128                                 $this->out( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
129                         } else {
130                                 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
131                         }
132                 }
134                 if ( $this->trimmed > 0 ) {
135                         if ( $doDelete ) {
136                                 $this->out( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
137                         } else {
138                                 $this->out( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
139                         }
140                 }
142                 if ( $this->failed > 0 ) {
143                         $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
144                         return false;
145                 }
147                 if ( $this->trimmed == 0 || $doDelete ) {
148                         $this->out( "It is now safe to apply the unique index on user_name.\n" );
149                         return true;
150                 } else {
151                         $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
152                         return false;
153                 }
154         }
156         /**
157          * We don't want anybody to mess with our stuff...
158          * @access private
159          */
160         function lock() {
161                 $set = array( 'user', 'revision' );
162                 $names = array_map( array( $this, 'lockTable' ), $set );
163                 $tables = implode( ',', $names );
165                 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
166         }
168         function lockTable( $table ) {
169                 return $this->db->tableName( $table ) . ' WRITE';
170         }
172         /**
173          * @access private
174          */
175         function unlock() {
176                 $this->db->query( "UNLOCK TABLES", __METHOD__ );
177         }
179         /**
180          * Grab usernames for which multiple records are present in the database.
181          * @return array
182          * @access private
183          */
184         function getDupes() {
185                 $user = $this->db->tableName( 'user' );
186                 $result = $this->db->query(
187                          "SELECT user_name,COUNT(*) AS n
188                                 FROM $user
189                         GROUP BY user_name
190                           HAVING n > 1", __METHOD__ );
192                 $list = array();
193                 foreach ( $result as $row ) {
194                         $list[] = $row->user_name;
195                 }
196                 return $list;
197         }
199         /**
200          * Examine user records for the given name. Try to see which record
201          * will be the one that actually gets used, then check remaining records
202          * for edits. If the dupes have no edits, we can safely remove them.
203          * @param $name string
204          * @param $doDelete bool
205          * @access private
206          */
207         function examine( $name, $doDelete ) {
208                 $result = $this->db->select( 'user',
209                         array( 'user_id' ),
210                         array( 'user_name' => $name ),
211                         __METHOD__ );
213                 $firstRow = $this->db->fetchObject( $result );
214                 $firstId = $firstRow->user_id;
215                 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
217                 foreach ( $result as $row ) {
218                         $dupeId = $row->user_id;
219                         $this->out( "... dupe id $dupeId: " );
220                         $edits = $this->editCount( $dupeId );
221                         if ( $edits > 0 ) {
222                                 $this->reassigned++;
223                                 $this->out( "has $edits edits! " );
224                                 if ( $doDelete ) {
225                                         $this->reassignEdits( $dupeId, $firstId );
226                                         $newEdits = $this->editCount( $dupeId );
227                                         if ( $newEdits == 0 ) {
228                                                 $this->out( "confirmed cleaned. " );
229                                         } else {
230                                                 $this->failed++;
231                                                 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
232                                                 continue;
233                                         }
234                                 } else {
235                                         $this->out( "(will need to reassign edits on fix)" );
236                                 }
237                         } else {
238                                 $this->out( "ok, no edits. " );
239                         }
240                         $this->trimmed++;
241                         if ( $doDelete ) {
242                                 $this->trimAccount( $dupeId );
243                         }
244                         $this->out( "\n" );
245                 }
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 $userid int
253          * @return int
254          * @access private
255          */
256         function editCount( $userid ) {
257                 return intval( $this->db->selectField(
258                         'revision',
259                         'COUNT(*)',
260                         array( 'rev_user' => $userid ),
261                         __METHOD__ ) );
262         }
264         /**
265          * @param $from int
266          * @param $to int
267          * @access private
268          */
269         function reassignEdits( $from, $to ) {
270                 $this->out( 'reassigning... ' );
271                 $this->db->update( 'revision',
272                         array( 'rev_user' => $to ),
273                         array( 'rev_user' => $from ),
274                         __METHOD__ );
275                 $this->out( "ok. " );
276         }
278         /**
279          * Remove a user account line.
280          * @param $userid int
281          * @access private
282          */
283         function trimAccount( $userid ) {
284                 $this->out( "deleting..." );
285                 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );
286                 $this->out( " ok" );
287         }