* (bug 2223) Add unique index on user_name field to prevent duplicate accounts
[lhc/web/wiklou.git] / maintenance / userDupes.inc
1 <?php
2 # Copyright (C) 2005 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Look for duplicate user table entries and optionally prune them.
22 */
23 class UserDupes {
24 var $db;
25 var $unresolvable;
26 var $trimmed;
27
28 function UserDupes( &$database ) {
29 $this->db =& $database;
30 }
31
32 /**
33 * Check if this database's user table has already had a unique
34 * user_name index applied.
35 * @return bool
36 */
37 function hasUniqueIndex() {
38 $fname = 'UserDupes::hasUniqueIndex';
39 #if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
40 $info = $this->db->indexInfo( 'user', 'user_name', $fname );
41 if( !$info ) {
42 echo "WARNING: doesn't seem to have user_name index at all!\n";
43 return false;
44 }
45
46 # Confusingly, 'Non_unique' is 0 for *unique* indexes,
47 # and 1 for *non-unique* indexes. Pass the crack, MySQL,
48 # it's obviously some good stuff!
49 return ( $info->Non_unique == 0 );
50 }
51
52 /**
53 * Checks the database for duplicate user account records
54 * and remove them in preparation for application of a unique
55 * index on the user_name field. Returns true if the table is
56 * clean or if duplicates have been resolved automatically.
57 *
58 * May return false if there are unresolvable problems.
59 * Status information will be echo'd to stdout.
60 *
61 * @return bool
62 */
63 function clearDupes() {
64 return $this->checkDupes( true );
65 }
66
67 /**
68 * Checks the database for duplicate user account records
69 * in preparation for application of a unique index on the
70 * user_name field. Returns true if the table is clean or
71 * if duplicates can be resolved automatically.
72 *
73 * May return false if there are unresolvable problems.
74 * Status information will be echo'd to stdout.
75 *
76 * @param bool $doDelete pass true to actually remove things
77 * from the database; false to just check.
78 * @return bool
79 */
80 function checkDupes( $doDelete=false ) {
81 global $wgDBname;
82
83 if( $this->hasUniqueIndex() ) {
84 echo "$wgDBname already has a unique index on its user table.\n";
85 return true;
86 }
87
88 $this->lock();
89
90 echo "Checking for duplicate accounts...\n";
91 $dupes = $this->getDupes();
92 $count = count( $dupes );
93
94 echo "Found $count accounts with duplicate records on $wgDBname.\n";
95 $this->trimmed = 0;
96 $this->unresolvable = 0;
97 foreach( $dupes as $name ) {
98 $this->examine( $name, $doDelete );
99 }
100
101 $this->unlock();
102
103 if( $this->trimmed > 0 ) {
104 echo "\n";
105 if( $doDelete ) {
106 echo "$this->trimmed duplicate user records were deleted from $wgDBname.\n";
107 if( $this->unresolvable == 0 ) {
108 echo "It is now safe to apply the unique index on user_name.\n";
109 }
110 } else {
111 echo "$this->trimmed duplicate user accounts were found on $wgDBname which can be removed safely.\n";
112 echo "Run this script again with the --fix option to automatically delete them.\n";
113 }
114 }
115
116 if( $this->unresolvable > 0 ) {
117 echo "\n";
118 echo "There were $this->unresolvable unresolvable accounts on $wgDBname.\n";
119 echo "These accounts have edits credited to duplicate records,\n";
120 echo "and need to be cleaned up manually before the unique index\n";
121 echo "for user_name can be applied.\n";
122 return false;
123 } else {
124 return true;
125 }
126 }
127
128 /**
129 * We don't want anybody to mess with our stuff...
130 * @access private
131 */
132 function lock() {
133 $fname = 'UserDupes::lock';
134 global $wgVersion;
135 if( version_compare( $wgVersion, '1.5alpha', 'ge' ) ) {
136 $set = array( 'user', 'revision' );
137 } else {
138 $set = array( 'user', 'cur', 'old' );
139 }
140 $names = array_map( array( $this, 'lockTable' ), $set );
141 $tables = implode( ',', $names );
142
143 $result = $this->db->query( "LOCK TABLES $tables", $fname );
144 }
145
146 function lockTable( $table ) {
147 return $this->db->tableName( $table ) . ' WRITE';
148 }
149
150 /**
151 * @access private
152 */
153 function unlock() {
154 $fname = 'UserDupes::unlock';
155 $result = $this->db->query( "UNLOCK TABLES", $fname );
156 }
157
158 /**
159 * Grab usernames for which multiple records are present in the database.
160 * @return array
161 * @access private
162 */
163 function getDupes() {
164 $fname = 'UserDupes::listDupes';
165 $user = $this->db->tableName( 'user' );
166 $result = $this->db->query(
167 "SELECT user_name,COUNT(*) AS n
168 FROM $user
169 GROUP BY user_name
170 HAVING n > 1", $fname );
171
172 $list = array();
173 while( $row = $this->db->fetchObject( $result ) ) {
174 $list[] = $row->user_name;
175 }
176 $this->db->freeResult( $result );
177
178 return $list;
179 }
180
181 /**
182 * Examine user records for the given name. Try to see which record
183 * will be the one that actually gets used, then check remaining records
184 * for edits. If the dupes have no edits, we can safely remove them.
185 * @param string $name
186 * @param bool $doDelete
187 * @access private
188 */
189 function examine( $name, $doDelete ) {
190 $fname = 'UserDupes::listDupes';
191 $result = $this->db->select( 'user',
192 array( 'user_id' ),
193 array( 'user_name' => $name ),
194 $fname );
195
196 $firstRow = $this->db->fetchObject( $result );
197 $firstId = $firstRow->user_id;
198 echo "Record that will be used for '$name' is user_id=$firstId\n";
199
200 while( $row = $this->db->fetchObject( $result ) ) {
201 $dupeId = $row->user_id;
202 echo "... dupe id $dupeId: ";
203 $edits = $this->editCount( $dupeId );
204 if( $edits > 0 ) {
205 $this->unresolvable++;
206 echo "has $edits edits! MANUAL INTERVENTION REQUIRED.\n";
207 continue;
208 } else {
209 $this->trimmed++;
210 echo "ok, no edits. ";
211 if( $doDelete ) {
212 $this->trimAccount( $dupeId );
213 }
214 echo "\n";
215 }
216 }
217 $this->db->freeResult( $result );
218 }
219
220 /**
221 * Count the number of edits attributed to this user.
222 * Does not currently check log table or other things
223 * where it might show up...
224 * @param int $userid
225 * @return int
226 * @access private
227 */
228 function editCount( $userid ) {
229 global $wgVersion;
230 if( version_compare( $wgVersion, '1.5alpha', 'ge' ) ) {
231 return $this->editCountOn( 'revision', 'rev_user', $userid );
232 } else {
233 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
234 $this->editCountOn( 'old', 'old_user', $userid );
235 }
236 }
237
238 /**
239 * Count the number of hits on a given table for this account.
240 * @param string $table
241 * @param string $field
242 * @param int $userid
243 * @return int
244 * @access private
245 */
246 function editCountOn( $table, $field, $userid ) {
247 $fname = 'UserDupes::editCountOn';
248 return IntVal( $this->db->selectField(
249 $table,
250 'COUNT(*)',
251 array( $field => $userid ),
252 $fname ) );
253 }
254
255 /**
256 * Remove a user account line.
257 * @param int $userid
258 * @access private
259 */
260 function trimAccount( $userid ) {
261 $fname = 'UserDupes::trimAccount';
262 echo "deleting...";
263 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
264 echo " ok";
265 }
266
267 }
268
269
270 ?>