0e315aa9021978d56a23ff23f278d72af46a0204
[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 /**
29 * Checks the database for duplicate user account records
30 * and remove them in preparation for application of a unique
31 * index on the user_name field. Returns true if the table is
32 * clean or if duplicates have been resolved automatically.
33 *
34 * May return false if there are unresolvable problems.
35 * Status information will be echo'd to stdout.
36 *
37 * @return bool
38 */
39 function clearDupes() {
40 return $this->checkDupes( true );
41 }
42
43 /**
44 * Checks the database for duplicate user account records
45 * in preparation for application of a unique index on the
46 * user_name field. Returns true if the table is clean or
47 * if duplicates can be resolved automatically.
48 *
49 * May return false if there are unresolvable problems.
50 * Status information will be echo'd to stdout.
51 *
52 * @param bool $doDelete pass true to actually remove things
53 * from the database; false to just check.
54 * @return bool
55 */
56 function checkDupes( $doDelete=false ) {
57 global $wgDBname;
58
59 $this->db =& wfGetDB( DB_MASTER );
60 $this->lock();
61
62 echo "Checking for duplicate accounts...\n";
63 $dupes = $this->getDupes();
64 $count = count( $dupes );
65
66 echo "Found $count accounts with duplicate records on $wgDBname.\n";
67 $this->trimmed = 0;
68 $this->unresolvable = 0;
69 foreach( $dupes as $name ) {
70 $this->examine( $name, $doDelete );
71 }
72
73 $this->unlock();
74
75 if( $this->trimmed > 0 ) {
76 echo "\n";
77 if( $doDelete ) {
78 echo "$this->trimmed duplicate user records were deleted from $wgDBname.\n";
79 if( $this->unresolvable == 0 ) {
80 echo "It is now safe to apply the unique index on user_name.\n";
81 }
82 } else {
83 echo "$this->trimmed duplicate user accounts were found on $wgDBname which can be removed safely.\n";
84 echo "Run this script again with the --fix option to automatically delete them.\n";
85 }
86 }
87
88 if( $this->unresolvable > 0 ) {
89 echo "\n";
90 echo "There were $this->unresolvable unresolvable accounts on $wgDBname.\n";
91 echo "These accounts have edits credited to duplicate records,\n";
92 echo "and need to be cleaned up manually before the unique index\n";
93 echo "for user_name can be applied.\n";
94 return false;
95 } else {
96 return true;
97 }
98 }
99
100 /**
101 * We don't want anybody to mess with our stuff...
102 * @access private
103 */
104 function lock() {
105 $fname = 'UserDupes::lock';
106 global $wgVersion;
107 if( version_compare( $wgVersion, '1.5alpha', 'ge' ) ) {
108 $set = array( 'user', 'revision' );
109 } else {
110 $set = array( 'user', 'cur', 'old' );
111 }
112 $names = array_map( array( $this, 'lockTable' ), $set );
113 $tables = implode( ',', $names );
114
115 $result = $this->db->query( "LOCK TABLES $tables", $fname );
116 }
117
118 function lockTable( $table ) {
119 return $this->db->tableName( $table ) . ' WRITE';
120 }
121
122 /**
123 * @access private
124 */
125 function unlock() {
126 $fname = 'UserDupes::unlock';
127 $result = $this->db->query( "UNLOCK TABLES", $fname );
128 }
129
130 /**
131 * Grab usernames for which multiple records are present in the database.
132 * @return array
133 * @access private
134 */
135 function getDupes() {
136 $fname = 'UserDupes::listDupes';
137 $user = $this->db->tableName( 'user' );
138 $result = $this->db->query(
139 "SELECT user_name,COUNT(*) AS n
140 FROM $user
141 GROUP BY user_name
142 HAVING n > 1", $fname );
143
144 $list = array();
145 while( $row = $this->db->fetchObject( $result ) ) {
146 $list[] = $row->user_name;
147 }
148 $this->db->freeResult( $result );
149
150 return $list;
151 }
152
153 /**
154 * Examine user records for the given name. Try to see which record
155 * will be the one that actually gets used, then check remaining records
156 * for edits. If the dupes have no edits, we can safely remove them.
157 * @param string $name
158 * @param bool $doDelete
159 * @access private
160 */
161 function examine( $name, $doDelete ) {
162 $fname = 'UserDupes::listDupes';
163 $result = $this->db->select( 'user',
164 array( 'user_id' ),
165 array( 'user_name' => $name ),
166 $fname );
167
168 $firstRow = $this->db->fetchObject( $result );
169 $firstId = $firstRow->user_id;
170 echo "Record that will be used for '$name' is user_id=$firstId\n";
171
172 while( $row = $this->db->fetchObject( $result ) ) {
173 $dupeId = $row->user_id;
174 echo "... dupe id $dupeId: ";
175 $edits = $this->editCount( $dupeId );
176 if( $edits > 0 ) {
177 $this->unresolvable++;
178 echo "has $edits edits! MANUAL INTERVENTION REQUIRED.\n";
179 continue;
180 } else {
181 $this->trimmed++;
182 echo "ok, no edits. ";
183 if( $doDelete ) {
184 $this->trimAccount( $dupeId );
185 }
186 echo "\n";
187 }
188 }
189 $this->db->freeResult( $result );
190 }
191
192 /**
193 * Count the number of edits attributed to this user.
194 * Does not currently check log table or other things
195 * where it might show up...
196 * @param int $userid
197 * @return int
198 * @access private
199 */
200 function editCount( $userid ) {
201 global $wgVersion;
202 if( version_compare( $wgVersion, '1.5alpha', 'ge' ) ) {
203 return $this->editCountOn( 'revision', 'rev_user', $userid );
204 } else {
205 return $this->editCountOn( 'cur', 'cur_user', $userid ) +
206 $this->editCountOn( 'old', 'old_user', $userid );
207 }
208 }
209
210 /**
211 * Count the number of hits on a given table for this account.
212 * @param string $table
213 * @param string $field
214 * @param int $userid
215 * @return int
216 * @access private
217 */
218 function editCountOn( $table, $field, $userid ) {
219 $fname = 'UserDupes::editCountOn';
220 return IntVal( $this->db->selectField(
221 $table,
222 'COUNT(*)',
223 array( $field => $userid ),
224 $fname ) );
225 }
226
227 /**
228 * Remove a user account line.
229 * @param int $userid
230 * @access private
231 */
232 function trimAccount( $userid ) {
233 $fname = 'UserDupes::trimAccount';
234 echo "deleting...";
235 $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
236 echo " ok";
237 }
238
239 }
240
241
242 ?>