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