3 * Helper class for update.php.
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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.
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.
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
24 * @ingroup Maintenance
27 use Wikimedia\Rdbms\IMaintainableDatabase
;
30 * Look for duplicate user table entries and optionally prune them.
32 * This is still used by our MysqlUpdater at:
33 * includes/installer/MysqlUpdater.php
35 * @ingroup Maintenance
39 * @var IMaintainableDatabase
49 private $outputCallback;
52 * @param IMaintainableDatabase &$database
53 * @param callable $outputCallback
55 public function __construct( &$database, $outputCallback ) {
56 $this->db
= $database;
57 $this->outputCallback
= $outputCallback;
61 * Output some text via the output callback provided
62 * @param string $str Text to print
64 private function out( $str ) {
65 call_user_func( $this->outputCallback
, $str );
69 * Check if this database's user table has already had a unique
70 * user_name index applied.
73 public function hasUniqueIndex() {
74 $info = $this->db
->indexInfo( 'user', 'user_name', __METHOD__
);
76 $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
81 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
82 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
83 # it's obviously some good stuff!
84 return ( $info[0]->Non_unique
== 0 );
88 * Checks the database for duplicate user account records
89 * and remove them in preparation for application of a unique
90 * index on the user_name field. Returns true if the table is
91 * clean or if duplicates have been resolved automatically.
93 * May return false if there are unresolvable problems.
94 * Status information will be echo'd to stdout.
98 public function clearDupes() {
99 return $this->checkDupes( true );
103 * Checks the database for duplicate user account records
104 * in preparation for application of a unique index on the
105 * user_name field. Returns true if the table is clean or
106 * if duplicates can be resolved automatically.
108 * Returns false if there are duplicates and resolution was
109 * not requested. (If doing resolution, edits may be reassigned.)
110 * Status information will be echo'd to stdout.
112 * @param bool $doDelete Pass true to actually remove things
113 * from the database; false to just check.
116 private function checkDupes( $doDelete = false ) {
117 $dbDomain = WikiMap
::getCurrentWikiDbDomain()->getId();
118 if ( $this->hasUniqueIndex() ) {
119 echo "$dbDomain already has a unique index on its user table.\n";
126 $this->out( "Checking for duplicate accounts...\n" );
127 $dupes = $this->getDupes();
128 $count = count( $dupes );
130 $this->out( "Found $count accounts with duplicate records on $dbDomain.\n" );
132 $this->reassigned
= 0;
134 foreach ( $dupes as $name ) {
135 $this->examine( $name, $doDelete );
142 // @phan-suppress-next-line PhanSuspiciousValueComparison
143 if ( $this->reassigned
> 0 ) {
145 $this->out( "$this->reassigned duplicate accounts had edits "
146 . "reassigned to a canonical record id.\n" );
148 $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
152 // @phan-suppress-next-line PhanSuspiciousValueComparison
153 if ( $this->trimmed
> 0 ) {
156 "$this->trimmed duplicate user records were deleted from $dbDomain.\n" );
159 "$this->trimmed duplicate user accounts were found on $dbDomain " .
160 "which can be removed safely.\n"
165 // @phan-suppress-next-line PhanSuspiciousValueComparison
166 if ( $this->failed
> 0 ) {
167 $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
172 // @phan-suppress-next-line PhanSuspiciousValueComparison
173 if ( $this->trimmed
== 0 ||
$doDelete ) {
174 $this->out( "It is now safe to apply the unique index on user_name.\n" );
178 $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
185 * We don't want anybody to mess with our stuff...
187 private function lock() {
188 $set = [ 'user', 'revision' ];
189 $names = array_map( [ $this, 'lockTable' ], $set );
190 $tables = implode( ',', $names );
192 $this->db
->query( "LOCK TABLES $tables", __METHOD__
);
195 private function lockTable( $table ) {
196 return $this->db
->tableName( $table ) . ' WRITE';
202 private function unlock() {
203 $this->db
->query( "UNLOCK TABLES", __METHOD__
);
207 * Grab usernames for which multiple records are present in the database.
210 private function getDupes() {
211 $user = $this->db
->tableName( 'user' );
212 $result = $this->db
->query(
213 "SELECT user_name,COUNT(*) AS n
216 HAVING n > 1", __METHOD__
);
219 foreach ( $result as $row ) {
220 $list[] = $row->user_name
;
227 * Examine user records for the given name. Try to see which record
228 * will be the one that actually gets used, then check remaining records
229 * for edits. If the dupes have no edits, we can safely remove them.
230 * @param string $name
231 * @param bool $doDelete
233 private function examine( $name, $doDelete ) {
234 $result = $this->db
->select( 'user',
236 [ 'user_name' => $name ],
239 $firstRow = $this->db
->fetchObject( $result );
240 $firstId = $firstRow->user_id
;
241 $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
243 foreach ( $result as $row ) {
244 $dupeId = $row->user_id
;
245 $this->out( "... dupe id $dupeId: " );
246 $edits = $this->editCount( $dupeId );
249 $this->out( "has $edits edits! " );
251 $this->reassignEdits( $dupeId, $firstId );
252 $newEdits = $this->editCount( $dupeId );
253 if ( $newEdits == 0 ) {
254 $this->out( "confirmed cleaned. " );
257 $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
261 $this->out( "(will need to reassign edits on fix)" );
264 $this->out( "ok, no edits. " );
268 $this->trimAccount( $dupeId );
275 * Count the number of edits attributed to this user.
276 * Does not currently check log table or other things
277 * where it might show up...
281 private function editCount( $userid ) {
282 return intval( $this->db
->selectField(
285 [ 'rev_user' => $userid ],
293 private function reassignEdits( $from, $to ) {
294 $this->out( 'reassigning... ' );
295 $this->db
->update( 'revision',
296 [ 'rev_user' => $to ],
297 [ 'rev_user' => $from ],
299 $this->out( "ok. " );
303 * Remove a user account line.
306 private function trimAccount( $userid ) {
307 $this->out( "deleting..." );
308 $this->db
->delete( 'user', [ 'user_id' => $userid ], __METHOD__
);