Part of bug 26280: added license headers to PHP files in maintenance
[lhc/web/wiklou.git] / maintenance / userDupes.inc
1 <?php
2 /**
3 * Helper class for update.php and upgrade1_5.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 */
26
27 /**
28 * Look for duplicate user table entries and optionally prune them.
29 * @ingroup Maintenance
30 */
31 class UserDupes {
32 var $db;
33 var $reassigned;
34 var $trimmed;
35 var $failed;
36
37 function UserDupes( &$database ) {
38 $this->db =& $database;
39 }
40
41 /**
42 * Check if this database's user table has already had a unique
43 * user_name index applied.
44 * @return bool
45 */
46 function hasUniqueIndex() {
47 $fname = 'UserDupes::hasUniqueIndex';
48 $info = $this->db->indexInfo( 'user', 'user_name', $fname );
49 if ( !$info ) {
50 wfOut( "WARNING: doesn't seem to have user_name index at all!\n" );
51 return false;
52 }
53
54 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
55 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
56 # it's obviously some good stuff!
57 return ( $info[0]->Non_unique == 0 );
58 }
59
60 /**
61 * Checks the database for duplicate user account records
62 * and remove them in preparation for application of a unique
63 * index on the user_name field. Returns true if the table is
64 * clean or if duplicates have been resolved automatically.
65 *
66 * May return false if there are unresolvable problems.
67 * Status information will be echo'd to stdout.
68 *
69 * @return bool
70 */
71 function clearDupes() {
72 return $this->checkDupes( true );
73 }
74
75 /**
76 * Checks the database for duplicate user account records
77 * in preparation for application of a unique index on the
78 * user_name field. Returns true if the table is clean or
79 * if duplicates can be resolved automatically.
80 *
81 * Returns false if there are duplicates and resolution was
82 * not requested. (If doing resolution, edits may be reassigned.)
83 * Status information will be echo'd to stdout.
84 *
85 * @param $doDelete bool: pass true to actually remove things
86 * from the database; false to just check.
87 * @return bool
88 */
89 function checkDupes( $doDelete = false ) {
90 if ( $this->hasUniqueIndex() ) {
91 echo wfWikiID() . " already has a unique index on its user table.\n";
92 return true;
93 }
94
95 $this->lock();
96
97 wfOut( "Checking for duplicate accounts...\n" );
98 $dupes = $this->getDupes();
99 $count = count( $dupes );
100
101 wfOut( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
102 $this->trimmed = 0;
103 $this->reassigned = 0;
104 $this->failed = 0;
105 foreach ( $dupes as $name ) {
106 $this->examine( $name, $doDelete );
107 }
108
109 $this->unlock();
110
111 wfOut( "\n" );
112
113 if ( $this->reassigned > 0 ) {
114 if ( $doDelete ) {
115 wfOut( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
116 } else {
117 wfOut( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
118 }
119 }
120
121 if ( $this->trimmed > 0 ) {
122 if ( $doDelete ) {
123 wfOut( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
124 } else {
125 wfOut( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
126 }
127 }
128
129 if ( $this->failed > 0 ) {
130 wfOut( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
131 return false;
132 }
133
134 if ( $this->trimmed == 0 || $doDelete ) {
135 wfOut( "It is now safe to apply the unique index on user_name.\n" );
136 return true;
137 } else {
138 wfOut( "Run this script again with the --fix option to automatically delete them.\n" );
139 return false;
140 }
141 }
142
143 /**
144 * We don't want anybody to mess with our stuff...
145 * @access private
146 */
147 function lock() {
148 $fname = 'UserDupes::lock';
149 if ( $this->newSchema() ) {
150 $set = array( 'user', 'revision' );
151 } else {
152 $set = array( 'user', 'cur', 'old' );
153 }
154 $names = array_map( array( $this, 'lockTable' ), $set );
155 $tables = implode( ',', $names );
156
157 $this->db->query( "LOCK TABLES $tables", $fname );
158 }
159
160 function lockTable( $table ) {
161 return $this->db->tableName( $table ) . ' WRITE';
162 }
163
164 /**
165 * @return bool
166 * @access private
167 */
168 function newSchema() {
169 return class_exists( 'Revision' );
170 }
171
172 /**
173 * @access private
174 */
175 function unlock() {
176 $fname = 'UserDupes::unlock';
177 $this->db->query( "UNLOCK TABLES", $fname );
178 }
179
180 /**
181 * Grab usernames for which multiple records are present in the database.
182 * @return array
183 * @access private
184 */
185 function getDupes() {
186 $fname = 'UserDupes::listDupes';
187 $user = $this->db->tableName( 'user' );
188 $result = $this->db->query(
189 "SELECT user_name,COUNT(*) AS n
190 FROM $user
191 GROUP BY user_name
192 HAVING n > 1", $fname );
193
194 $list = array();
195 foreach ( $result as $row ) {
196 $list[] = $row->user_name;
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 foreach ( $result as $row ) {
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 }
250
251 /**
252 * Count the number of edits attributed to this user.
253 * Does not currently check log table or other things
254 * where it might show up...
255 * @param $userid int
256 * @return int
257 * @access private
258 */
259 function editCount( $userid ) {
260 if ( $this->newSchema() ) {
261 return $this->editCountOn( 'revision', 'rev_user', $userid );
262 } else {
263 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
264 $this->editCountOn( 'old', 'old_user', $userid );
265 }
266 }
267
268 /**
269 * Count the number of hits on a given table for this account.
270 * @param $table string
271 * @param $field string
272 * @param $userid int
273 * @return int
274 * @access private
275 */
276 function editCountOn( $table, $field, $userid ) {
277 $fname = 'UserDupes::editCountOn';
278 return intval( $this->db->selectField(
279 $table,
280 'COUNT(*)',
281 array( $field => $userid ),
282 $fname ) );
283 }
284
285 /**
286 * @param $from int
287 * @param $to int
288 * @access private
289 */
290 function reassignEdits( $from, $to ) {
291 $set = $this->newSchema()
292 ? array( 'revision' => 'rev_user' )
293 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
294 foreach ( $set as $table => $field ) {
295 $this->reassignEditsOn( $table, $field, $from, $to );
296 }
297 }
298
299 /**
300 * @param $table string
301 * @param $field string
302 * @param $from int
303 * @param $to int
304 * @access private
305 */
306 function reassignEditsOn( $table, $field, $from, $to ) {
307 $fname = 'UserDupes::reassignEditsOn';
308 wfOut( "reassigning on $table... " );
309 $this->db->update( $table,
310 array( $field => $to ),
311 array( $field => $from ),
312 $fname );
313 wfOut( "ok. " );
314 }
315
316 /**
317 * Remove a user account line.
318 * @param $userid int
319 * @access private
320 */
321 function trimAccount( $userid ) {
322 $fname = 'UserDupes::trimAccount';
323 wfOut( "deleting..." );
324 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
325 wfOut( " ok" );
326 }
327
328 }