MediaWiki  1.23.8
userDupes.inc
Go to the documentation of this file.
1 <?php
35 class UserDupes {
36  private $db;
37  private $reassigned;
38  private $trimmed;
39  private $failed;
40  private $outputCallback;
41 
42  function __construct( &$database, $outputCallback ) {
43  $this->db = $database;
44  $this->outputCallback = $outputCallback;
45  }
46 
51  private function out( $str ) {
52  call_user_func( $this->outputCallback, $str );
53  }
54 
60  function hasUniqueIndex() {
61  $info = $this->db->indexInfo( 'user', 'user_name', __METHOD__ );
62  if ( !$info ) {
63  $this->out( "WARNING: doesn't seem to have user_name index at all!\n" );
64  return false;
65  }
66 
67  # Confusingly, 'Non_unique' is 0 for *unique* indexes,
68  # and 1 for *non-unique* indexes. Pass the crack, MySQL,
69  # it's obviously some good stuff!
70  return ( $info[0]->Non_unique == 0 );
71  }
72 
84  function clearDupes() {
85  return $this->checkDupes( true );
86  }
87 
102  function checkDupes( $doDelete = false ) {
103  if ( $this->hasUniqueIndex() ) {
104  echo wfWikiID() . " already has a unique index on its user table.\n";
105  return true;
106  }
107 
108  $this->lock();
109 
110  $this->out( "Checking for duplicate accounts...\n" );
111  $dupes = $this->getDupes();
112  $count = count( $dupes );
113 
114  $this->out( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
115  $this->trimmed = 0;
116  $this->reassigned = 0;
117  $this->failed = 0;
118  foreach ( $dupes as $name ) {
119  $this->examine( $name, $doDelete );
120  }
121 
122  $this->unlock();
123 
124  $this->out( "\n" );
125 
126  if ( $this->reassigned > 0 ) {
127  if ( $doDelete ) {
128  $this->out( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
129  } else {
130  $this->out( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
131  }
132  }
133 
134  if ( $this->trimmed > 0 ) {
135  if ( $doDelete ) {
136  $this->out( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
137  } else {
138  $this->out( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
139  }
140  }
141 
142  if ( $this->failed > 0 ) {
143  $this->out( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
144  return false;
145  }
146 
147  if ( $this->trimmed == 0 || $doDelete ) {
148  $this->out( "It is now safe to apply the unique index on user_name.\n" );
149  return true;
150  } else {
151  $this->out( "Run this script again with the --fix option to automatically delete them.\n" );
152  return false;
153  }
154  }
155 
160  function lock() {
161  $set = array( 'user', 'revision' );
162  $names = array_map( array( $this, 'lockTable' ), $set );
163  $tables = implode( ',', $names );
164 
165  $this->db->query( "LOCK TABLES $tables", __METHOD__ );
166  }
167 
168  function lockTable( $table ) {
169  return $this->db->tableName( $table ) . ' WRITE';
170  }
171 
175  function unlock() {
176  $this->db->query( "UNLOCK TABLES", __METHOD__ );
177  }
178 
184  function getDupes() {
185  $user = $this->db->tableName( 'user' );
186  $result = $this->db->query(
187  "SELECT user_name,COUNT(*) AS n
188  FROM $user
189  GROUP BY user_name
190  HAVING n > 1", __METHOD__ );
191 
192  $list = array();
193  foreach ( $result as $row ) {
194  $list[] = $row->user_name;
195  }
196  return $list;
197  }
198 
207  function examine( $name, $doDelete ) {
208  $result = $this->db->select( 'user',
209  array( 'user_id' ),
210  array( 'user_name' => $name ),
211  __METHOD__ );
212 
213  $firstRow = $this->db->fetchObject( $result );
214  $firstId = $firstRow->user_id;
215  $this->out( "Record that will be used for '$name' is user_id=$firstId\n" );
216 
217  foreach ( $result as $row ) {
218  $dupeId = $row->user_id;
219  $this->out( "... dupe id $dupeId: " );
220  $edits = $this->editCount( $dupeId );
221  if ( $edits > 0 ) {
222  $this->reassigned++;
223  $this->out( "has $edits edits! " );
224  if ( $doDelete ) {
225  $this->reassignEdits( $dupeId, $firstId );
226  $newEdits = $this->editCount( $dupeId );
227  if ( $newEdits == 0 ) {
228  $this->out( "confirmed cleaned. " );
229  } else {
230  $this->failed++;
231  $this->out( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
232  continue;
233  }
234  } else {
235  $this->out( "(will need to reassign edits on fix)" );
236  }
237  } else {
238  $this->out( "ok, no edits. " );
239  }
240  $this->trimmed++;
241  if ( $doDelete ) {
242  $this->trimAccount( $dupeId );
243  }
244  $this->out( "\n" );
245  }
246  }
247 
256  function editCount( $userid ) {
257  return intval( $this->db->selectField(
258  'revision',
259  'COUNT(*)',
260  array( 'rev_user' => $userid ),
261  __METHOD__ ) );
262  }
263 
269  function reassignEdits( $from, $to ) {
270  $this->out( 'reassigning... ' );
271  $this->db->update( 'revision',
272  array( 'rev_user' => $to ),
273  array( 'rev_user' => $from ),
274  __METHOD__ );
275  $this->out( "ok. " );
276  }
277 
283  function trimAccount( $userid ) {
284  $this->out( "deleting..." );
285  $this->db->delete( 'user', array( 'user_id' => $userid ), __METHOD__ );
286  $this->out( " ok" );
287  }
288 
289 }
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
UserDupes\lock
lock()
We don't want anybody to mess with our stuff...
Definition: userDupes.inc:160
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$tables
namespace and then decline to actually register it RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist & $tables
Definition: hooks.txt:815
UserDupes\$reassigned
$reassigned
Definition: userDupes.inc:37
UserDupes\lockTable
lockTable( $table)
Definition: userDupes.inc:168
$from
$from
Definition: importImages.php:90
UserDupes\clearDupes
clearDupes()
Checks the database for duplicate user account records and remove them in preparation for application...
Definition: userDupes.inc:84
UserDupes\$trimmed
$trimmed
Definition: userDupes.inc:38
UserDupes\editCount
editCount( $userid)
Count the number of edits attributed to this user.
Definition: userDupes.inc:256
UserDupes\checkDupes
checkDupes( $doDelete=false)
Checks the database for duplicate user account records in preparation for application of a unique ind...
Definition: userDupes.inc:102
UserDupes\unlock
unlock()
Definition: userDupes.inc:175
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
UserDupes\examine
examine( $name, $doDelete)
Examine user records for the given name.
Definition: userDupes.inc:207
UserDupes\hasUniqueIndex
hasUniqueIndex()
Check if this database's user table has already had a unique user_name index applied.
Definition: userDupes.inc:60
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3613
UserDupes\trimAccount
trimAccount( $userid)
Remove a user account line.
Definition: userDupes.inc:283
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
UserDupes\getDupes
getDupes()
Grab usernames for which multiple records are present in the database.
Definition: userDupes.inc:184
UserDupes\$outputCallback
$outputCallback
Definition: userDupes.inc:40
UserDupes\$failed
$failed
Definition: userDupes.inc:39
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
UserDupes\out
out( $str)
Output some text via the output callback provided.
Definition: userDupes.inc:51
UserDupes
Look for duplicate user table entries and optionally prune them.
Definition: userDupes.inc:35
$count
$count
Definition: UtfNormalTest2.php:96
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
UserDupes\reassignEdits
reassignEdits( $from, $to)
Definition: userDupes.inc:269
UserDupes\__construct
__construct(&$database, $outputCallback)
Definition: userDupes.inc:42
UserDupes\$db
$db
Definition: userDupes.inc:36