Merge "Use and forward returntoquery parameter in Special:ChangePassword"
[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 *
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;
41
42 function __construct( &$database, $outputCallback ) {
43 $this->db = $database;
44 $this->outputCallback = $outputCallback;
45 }
46
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 }
54
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 }
66
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 }
72
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 }
87
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 }
107
108 $this->lock();
109
110 $this->out( "Checking for duplicate accounts...\n" );
111 $dupes = $this->getDupes();
112 $count = count( $dupes );
113
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 }
121
122 $this->unlock();
123
124 $this->out( "\n" );
125
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 }
133
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 }
141
142 if ( $this->failed > 0 ) {
143 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
144 return false;
145 }
146
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 }
155
156 /**
157 * We don't want anybody to mess with our stuff...
158 * @access private
159 */
160 function lock() {
161 if ( $this->newSchema() ) {
162 $set = array( 'user', 'revision' );
163 } else {
164 $set = array( 'user', 'cur', 'old' );
165 }
166 $names = array_map( array( $this, 'lockTable' ), $set );
167 $tables = implode( ',', $names );
168
169 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
170 }
171
172 function lockTable( $table ) {
173 return $this->db->tableName( $table ) . ' WRITE';
174 }
175
176 /**
177 * @return bool
178 * @access private
179 */
180 function newSchema() {
181 return MWInit::classExists( 'Revision' );
182 }
183
184 /**
185 * @access private
186 */
187 function unlock() {
188 $this->db->query( "UNLOCK TABLES", __METHOD__ );
189 }
190
191 /**
192 * Grab usernames for which multiple records are present in the database.
193 * @return array
194 * @access private
195 */
196 function getDupes() {
197 $user = $this->db->tableName( 'user' );
198 $result = $this->db->query(
199 "SELECT user_name,COUNT(*) AS n
200 FROM $user
201 GROUP BY user_name
202 HAVING n > 1", __METHOD__ );
203
204 $list = array();
205 foreach ( $result as $row ) {
206 $list[] = $row->user_name;
207 }
208 return $list;
209 }
210
211 /**
212 * Examine user records for the given name. Try to see which record
213 * will be the one that actually gets used, then check remaining records
214 * for edits. If the dupes have no edits, we can safely remove them.
215 * @param $name string
216 * @param $doDelete bool
217 * @access private
218 */
219 function examine( $name, $doDelete ) {
220 $result = $this->db->select( 'user',
221 array( 'user_id' ),
222 array( 'user_name' => $name ),
223 __METHOD__ );
224
225 $firstRow = $this->db->fetchObject( $result );
226 $firstId = $firstRow->user_id;
227 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
228
229 foreach ( $result as $row ) {
230 $dupeId = $row->user_id;
231 $this->out( "... dupe id $dupeId: " );
232 $edits = $this->editCount( $dupeId );
233 if ( $edits > 0 ) {
234 $this->reassigned++;
235 $this->out( "has $edits edits! " );
236 if ( $doDelete ) {
237 $this->reassignEdits( $dupeId, $firstId );
238 $newEdits = $this->editCount( $dupeId );
239 if ( $newEdits == 0 ) {
240 $this->out( "confirmed cleaned. " );
241 } else {
242 $this->failed++;
243 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
244 continue;
245 }
246 } else {
247 $this->out( "(will need to reassign edits on fix)" );
248 }
249 } else {
250 $this->out( "ok, no edits. " );
251 }
252 $this->trimmed++;
253 if ( $doDelete ) {
254 $this->trimAccount( $dupeId );
255 }
256 $this->out( "\n" );
257 }
258 }
259
260 /**
261 * Count the number of edits attributed to this user.
262 * Does not currently check log table or other things
263 * where it might show up...
264 * @param $userid int
265 * @return int
266 * @access private
267 */
268 function editCount( $userid ) {
269 if ( $this->newSchema() ) {
270 return $this->editCountOn( 'revision', 'rev_user', $userid );
271 } else {
272 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
273 $this->editCountOn( 'old', 'old_user', $userid );
274 }
275 }
276
277 /**
278 * Count the number of hits on a given table for this account.
279 * @param $table string
280 * @param $field string
281 * @param $userid int
282 * @return int
283 * @access private
284 */
285 function editCountOn( $table, $field, $userid ) {
286 return intval( $this->db->selectField(
287 $table,
288 'COUNT(*)',
289 array( $field => $userid ),
290 __METHOD__ ) );
291 }
292
293 /**
294 * @param $from int
295 * @param $to int
296 * @access private
297 */
298 function reassignEdits( $from, $to ) {
299 $set = $this->newSchema()
300 ? array( 'revision' => 'rev_user' )
301 : array( 'cur' => 'cur_user', 'old' => 'old_user' );
302 foreach ( $set as $table => $field ) {
303 $this->reassignEditsOn( $table, $field, $from, $to );
304 }
305 }
306
307 /**
308 * @param $table string
309 * @param $field string
310 * @param $from int
311 * @param $to int
312 * @access private
313 */
314 function reassignEditsOn( $table, $field, $from, $to ) {
315 $this->out( "reassigning on $table... " );
316 $this->db->update( $table,
317 array( $field => $to ),
318 array( $field => $from ),
319 __METHOD__ );
320 $this->out( "ok. " );
321 }
322
323 /**
324 * Remove a user account line.
325 * @param $userid int
326 * @access private
327 */
328 function trimAccount( $userid ) {
329 $this->out( "deleting..." );
330 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );
331 $this->out( " ok" );
332 }
333
334 }