Merge "Make users API cache mode public if blockinfo is not queried"
[lhc/web/wiklou.git] / maintenance / userDupes.inc
1 <?php
2 /**
3 * Helper class for update.php.
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://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 string $str 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 bool $doDelete 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 "
129 . "reassigned to a canonical record id.\n" );
130 } else {
131 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
132 }
133 }
134
135 if ( $this->trimmed > 0 ) {
136 if ( $doDelete ) {
137 $this->out( "$this->trimmed duplicate user records were deleted from "
138 . wfWikiID() . ".\n" );
139 } else {
140 $this->out( "$this->trimmed duplicate user accounts were found on "
141 . wfWikiID() . " which can be removed safely.\n" );
142 }
143 }
144
145 if ( $this->failed > 0 ) {
146 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
147 return false;
148 }
149
150 if ( $this->trimmed == 0 || $doDelete ) {
151 $this->out( "It is now safe to apply the unique index on user_name.\n" );
152 return true;
153 } else {
154 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
155 return false;
156 }
157 }
158
159 /**
160 * We don't want anybody to mess with our stuff...
161 * @access private
162 */
163 function lock() {
164 $set = array( 'user', 'revision' );
165 $names = array_map( array( $this, 'lockTable' ), $set );
166 $tables = implode( ',', $names );
167
168 $this->db->query( "LOCK TABLES $tables", __METHOD__ );
169 }
170
171 function lockTable( $table ) {
172 return $this->db->tableName( $table ) . ' WRITE';
173 }
174
175 /**
176 * @access private
177 */
178 function unlock() {
179 $this->db->query( "UNLOCK TABLES", __METHOD__ );
180 }
181
182 /**
183 * Grab usernames for which multiple records are present in the database.
184 * @return array
185 * @access private
186 */
187 function getDupes() {
188 $user = $this->db->tableName( 'user' );
189 $result = $this->db->query(
190 "SELECT user_name,COUNT(*) AS n
191 FROM $user
192 GROUP BY user_name
193 HAVING n > 1", __METHOD__ );
194
195 $list = array();
196 foreach ( $result as $row ) {
197 $list[] = $row->user_name;
198 }
199 return $list;
200 }
201
202 /**
203 * Examine user records for the given name. Try to see which record
204 * will be the one that actually gets used, then check remaining records
205 * for edits. If the dupes have no edits, we can safely remove them.
206 * @param string $name
207 * @param bool $doDelete
208 * @access private
209 */
210 function examine( $name, $doDelete ) {
211 $result = $this->db->select( 'user',
212 array( 'user_id' ),
213 array( 'user_name' => $name ),
214 __METHOD__ );
215
216 $firstRow = $this->db->fetchObject( $result );
217 $firstId = $firstRow->user_id;
218 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
219
220 foreach ( $result as $row ) {
221 $dupeId = $row->user_id;
222 $this->out( "... dupe id $dupeId: " );
223 $edits = $this->editCount( $dupeId );
224 if ( $edits > 0 ) {
225 $this->reassigned++;
226 $this->out( "has $edits edits! " );
227 if ( $doDelete ) {
228 $this->reassignEdits( $dupeId, $firstId );
229 $newEdits = $this->editCount( $dupeId );
230 if ( $newEdits == 0 ) {
231 $this->out( "confirmed cleaned. " );
232 } else {
233 $this->failed++;
234 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
235 continue;
236 }
237 } else {
238 $this->out( "(will need to reassign edits on fix)" );
239 }
240 } else {
241 $this->out( "ok, no edits. " );
242 }
243 $this->trimmed++;
244 if ( $doDelete ) {
245 $this->trimAccount( $dupeId );
246 }
247 $this->out( "\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 int $userid
256 * @return int
257 * @access private
258 */
259 function editCount( $userid ) {
260 return intval( $this->db->selectField(
261 'revision',
262 'COUNT(*)',
263 array( 'rev_user' => $userid ),
264 __METHOD__ ) );
265 }
266
267 /**
268 * @param int $from
269 * @param int $to
270 * @access private
271 */
272 function reassignEdits( $from, $to ) {
273 $this->out( 'reassigning... ' );
274 $this->db->update( 'revision',
275 array( 'rev_user' => $to ),
276 array( 'rev_user' => $from ),
277 __METHOD__ );
278 $this->out( "ok. " );
279 }
280
281 /**
282 * Remove a user account line.
283 * @param int $userid
284 * @access private
285 */
286 function trimAccount( $userid ) {
287 $this->out( "deleting..." );
288 $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );
289 $this->out( " ok" );
290 }
291
292 }