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