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