\n to eof
[lhc/web/wiklou.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 return $list;
197 }
198
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 $fname = 'UserDupes::listDupes';
209 $result = $this->db->select( 'user',
210 array( 'user_id' ),
211 array( 'user_name' => $name ),
212 $fname );
213
214 $firstRow = $this->db->fetchObject( $result );
215 $firstId = $firstRow->user_id;
216 wfOut( "Record that will be used for '$name' is user_id=$firstId\n" );
217
218 while ( $row = $this->db->fetchObject( $result ) ) {
219 $dupeId = $row->user_id;
220 wfOut( "... dupe id $dupeId: " );
221 $edits = $this->editCount( $dupeId );
222 if ( $edits > 0 ) {
223 $this->reassigned++;
224 wfOut( "has $edits edits! " );
225 if ( $doDelete ) {
226 $this->reassignEdits( $dupeId, $firstId );
227 $newEdits = $this->editCount( $dupeId );
228 if ( $newEdits == 0 ) {
229 wfOut( "confirmed cleaned. " );
230 } else {
231 $this->failed++;
232 wfOut( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
233 continue;
234 }
235 } else {
236 wfOut( "(will need to reassign edits on fix)" );
237 }
238 } else {
239 wfOut( "ok, no edits. " );
240 }
241 $this->trimmed++;
242 if ( $doDelete ) {
243 $this->trimAccount( $dupeId );
244 }
245 wfOut( "\n" );
246 }
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 $userid int
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 $table string
269 * @param $field string
270 * @param $userid int
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 $from int
285 * @param $to int
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 $table string
299 * @param $field string
300 * @param $from int
301 * @param $to int
302 * @access private
303 */
304 function reassignEditsOn( $table, $field, $from, $to ) {
305 $fname = 'UserDupes::reassignEditsOn';
306 wfOut( "reassigning on $table... " );
307 $this->db->update( $table,
308 array( $field => $to ),
309 array( $field => $from ),
310 $fname );
311 wfOut( "ok. " );
312 }
313
314 /**
315 * Remove a user account line.
316 * @param $userid int
317 * @access private
318 */
319 function trimAccount( $userid ) {
320 $fname = 'UserDupes::trimAccount';
321 wfOut( "deleting..." );
322 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
323 wfOut( " ok" );
324 }
325
326 }