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