]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/userDupes.inc
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / userDupes.inc
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21  * @file
22  * @ingroup Maintenance
23  */
24
25 /**
26  * Look for duplicate user table entries and optionally prune them.
27  * @ingroup Maintenance
28  */
29 class UserDupes {
30         var $db;
31         var $reassigned;
32         var $trimmed;
33         var $failed;
34         private $outputCallback;
35
36         function __construct( &$database, $outputCallback ) {
37                 $this->db = $database;
38                 $this->outputCallback = $outputCallback;
39         }
40
41         /**
42          * Output some text via the output callback provided
43          * @param $str String Text to print
44          */
45         private function out( $str ) {
46                 call_user_func( $this->outputCallback, $str );
47         }
48
49         /**
50          * Check if this database's user table has already had a unique
51          * user_name index applied.
52          * @return bool
53          */
54         function hasUniqueIndex() {
55                 $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
56                 if ( !$info ) {
57                         $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
58                         return false;
59                 }
60
61                 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
62                 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
63                 # it's obviously some good stuff!
64                 return ( $info[0]->Non_unique == 0 );
65         }
66
67         /**
68          * Checks the database for duplicate user account records
69          * and remove them in preparation for application of a unique
70          * index on the user_name field. Returns true if the table is
71          * clean or if duplicates have been resolved automatically.
72          *
73          * May return false if there are unresolvable problems.
74          * Status information will be echo'd to stdout.
75          *
76          * @return bool
77          */
78         function clearDupes() {
79                 return $this->checkDupes( true );
80         }
81
82         /**
83          * Checks the database for duplicate user account records
84          * in preparation for application of a unique index on the
85          * user_name field. Returns true if the table is clean or
86          * if duplicates can be resolved automatically.
87          *
88          * Returns false if there are duplicates and resolution was
89          * not requested. (If doing resolution, edits may be reassigned.)
90          * Status information will be echo'd to stdout.
91          *
92          * @param $doDelete bool: pass true to actually remove things
93          *                  from the database; false to just check.
94          * @return bool
95          */
96         function checkDupes( $doDelete = false ) {
97                 if ( $this->hasUniqueIndex() ) {
98                         echo wfWikiID() . " already has a unique index on its user table.\n";
99                         return true;
100                 }
101
102                 $this->lock();
103
104                 $this->out( "Checking for duplicate accounts...\n" );
105                 $dupes = $this->getDupes();
106                 $count = count( $dupes );
107
108                 $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
109                 $this->trimmed    = 0;
110                 $this->reassigned = 0;
111                 $this->failed     = 0;
112                 foreach ( $dupes as $name ) {
113                         $this->examine( $name, $doDelete );
114                 }
115
116                 $this->unlock();
117
118                 $this->out( "\n" );
119
120                 if ( $this->reassigned > 0 ) {
121                         if ( $doDelete ) {
122                                 $this->out( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
123                         } else {
124                                 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
125                         }
126                 }
127
128                 if ( $this->trimmed > 0 ) {
129                         if ( $doDelete ) {
130                                 $this->out( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
131                         } else {
132                                 $this->out( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
133                         }
134                 }
135
136                 if ( $this->failed > 0 ) {
137                         $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
138                         return false;
139                 }
140
141                 if ( $this->trimmed == 0 || $doDelete ) {
142                         $this->out( "It is now safe to apply the unique index on user_name.\n" );
143                         return true;
144                 } else {
145                         $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
146                         return false;
147                 }
148         }
149
150         /**
151          * We don't want anybody to mess with our stuff...
152          * @access private
153          */
154         function lock() {
155                 if ( $this->newSchema() ) {
156                         $set = array( 'user', 'revision' );
157                 } else {
158                         $set = array( 'user', 'cur', 'old' );
159                 }
160                 $names = array_map( array( $this, 'lockTable' ), $set );
161                 $tables = implode( ',', $names );
162
163                 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
164         }
165
166         function lockTable( $table ) {
167                 return $this->db->tableName( $table ) . ' WRITE';
168         }
169
170         /**
171          * @return bool
172          * @access private
173          */
174         function newSchema() {
175                 return class_exists( 'Revision' );
176         }
177
178         /**
179          * @access private
180          */
181         function unlock() {
182                 $this->db->query( "UNLOCK TABLES", __METHOD__ );
183         }
184
185         /**
186          * Grab usernames for which multiple records are present in the database.
187          * @return array
188          * @access private
189          */
190         function getDupes() {
191                 $user = $this->db->tableName( 'user' );
192                 $result = $this->db->query(
193                          "SELECT user_name,COUNT(*) AS n
194                                 FROM $user
195                         GROUP BY user_name
196                           HAVING n > 1", __METHOD__ );
197
198                 $list = array();
199                 foreach ( $result as $row ) {
200                         $list[] = $row->user_name;
201                 }
202                 return $list;
203         }
204
205         /**
206          * Examine user records for the given name. Try to see which record
207          * will be the one that actually gets used, then check remaining records
208          * for edits. If the dupes have no edits, we can safely remove them.
209          * @param $name string
210          * @param $doDelete bool
211          * @access private
212          */
213         function examine( $name, $doDelete ) {
214                 $result = $this->db->select( 'user',
215                         array( 'user_id' ),
216                         array( 'user_name' => $name ),
217                         __METHOD__ );
218
219                 $firstRow = $this->db->fetchObject( $result );
220                 $firstId  = $firstRow->user_id;
221                 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
222
223                 foreach ( $result as $row ) {
224                         $dupeId = $row->user_id;
225                         $this->out( "... dupe id $dupeId: " );
226                         $edits = $this->editCount( $dupeId );
227                         if ( $edits > 0 ) {
228                                 $this->reassigned++;
229                                 $this->out( "has $edits edits! " );
230                                 if ( $doDelete ) {
231                                         $this->reassignEdits( $dupeId, $firstId );
232                                         $newEdits = $this->editCount( $dupeId );
233                                         if ( $newEdits == 0 ) {
234                                                 $this->out( "confirmed cleaned. " );
235                                         } else {
236                                                 $this->failed++;
237                                                 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
238                                                 continue;
239                                         }
240                                 } else {
241                                         $this->out( "(will need to reassign edits on fix)" );
242                                 }
243                         } else {
244                                 $this->out( "ok, no edits. " );
245                         }
246                         $this->trimmed++;
247                         if ( $doDelete ) {
248                                 $this->trimAccount( $dupeId );
249                         }
250                         $this->out( "\n" );
251                 }
252         }
253
254         /**
255          * Count the number of edits attributed to this user.
256          * Does not currently check log table or other things
257          * where it might show up...
258          * @param $userid int
259          * @return int
260          * @access private
261          */
262         function editCount( $userid ) {
263                 if ( $this->newSchema() ) {
264                         return $this->editCountOn( 'revision', 'rev_user', $userid );
265                 } else {
266                         return $this->editCountOn( 'cur', 'cur_user', $userid ) +
267                                 $this->editCountOn( 'old', 'old_user', $userid );
268                 }
269         }
270
271         /**
272          * Count the number of hits on a given table for this account.
273          * @param $table string
274          * @param $field string
275          * @param $userid int
276          * @return int
277          * @access private
278          */
279         function editCountOn( $table, $field, $userid ) {
280                 return intval( $this->db->selectField(
281                         $table,
282                         'COUNT(*)',
283                         array( $field => $userid ),
284                         __METHOD__ ) );
285         }
286
287         /**
288          * @param $from int
289          * @param $to int
290          * @access private
291          */
292         function reassignEdits( $from, $to ) {
293                 $set = $this->newSchema()
294                         ? array( 'revision' => 'rev_user' )
295                         : array( 'cur' => 'cur_user', 'old' => 'old_user' );
296                 foreach ( $set as $table => $field ) {
297                         $this->reassignEditsOn( $table, $field, $from, $to );
298                 }
299         }
300
301         /**
302          * @param $table string
303          * @param $field string
304          * @param $from int
305          * @param $to int
306          * @access private
307          */
308         function reassignEditsOn( $table, $field, $from, $to ) {
309                 $this->out( "reassigning on $table... " );
310                 $this->db->update( $table,
311                         array( $field => $to ),
312                         array( $field => $from ),
313                         __METHOD__ );
314                 $this->out( "ok. " );
315         }
316
317         /**
318          * Remove a user account line.
319          * @param $userid int
320          * @access private
321          */
322         function trimAccount( $userid ) {
323                 $this->out( "deleting..." );
324                 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );
325                 $this->out( " ok" );
326         }
327
328 }