3
if ( !defined( 'MEDIAWIKI' ) ) {
4
echo "RenameUser extension\n";
9
wfLoadExtensionMessages( 'Renameuser' );
12
* Special page allows authorised users to rename
15
class SpecialRenameuser extends SpecialPage {
20
public function __construct() {
21
parent::__construct( 'Renameuser', 'renameuser' );
25
* Show the special page
27
* @param mixed $par Parameter passed to the page
29
public function execute( $par ) {
30
global $wgOut, $wgUser, $wgTitle, $wgRequest, $wgContLang, $wgLang;
31
global $wgVersion, $wgMaxNameChars, $wgCapitalLinks;
34
$wgOut->addWikiMsg( 'renameuser-summary' );
36
if( !$wgUser->isAllowed( 'renameuser' ) ) {
37
$wgOut->permissionRequired( 'renameuser' );
41
$wgOut->readOnlyPage();
45
$showBlockLog = $wgRequest->getBool( 'submit-showBlockLog' );
46
$oldnamePar = trim( str_replace( '_', ' ', $wgRequest->getText( 'oldusername' ) ) );
47
$oldusername = Title::makeTitle( NS_USER, $oldnamePar );
48
// Force uppercase of newusername, otherwise wikis with wgCapitalLinks=false can create lc usernames
49
$newusername = Title::makeTitleSafe( NS_USER, $wgContLang->ucfirst( $wgRequest->getText( 'newusername' ) ) );
50
$oun = is_object( $oldusername ) ? $oldusername->getText() : '';
51
$nun = is_object( $newusername ) ? $newusername->getText() : '';
52
$token = $wgUser->editToken();
53
$reason = $wgRequest->getText( 'reason' );
54
// If nothing given for these flags, assume they are checked
55
// unless this is a POST submission.
57
if( $wgRequest->wasPosted() ) {
58
if( !$wgRequest->getCheck( 'movepages' ) ) {
59
$move_checked = false;
63
if( $oun && $nun && !$wgRequest->getCheck( 'confirmaction' ) ) {
64
wfRunHooks( 'RenameUserWarning', array( $oun, $nun, &$warnings ) );
68
<!-- Current contributions limit is " . RENAMEUSER_CONTRIBLIMIT . " -->" .
69
Xml::openElement( 'form', array( 'method' => 'post', 'action' => $wgTitle->getLocalUrl(), 'id' => 'renameuser' ) ) .
70
Xml::openElement( 'fieldset' ) .
71
Xml::element( 'legend', null, wfMsg( 'renameuser' ) ) .
72
Xml::openElement( 'table', array( 'id' => 'mw-renameuser-table' ) ) .
74
<td class='mw-label'>" .
75
Xml::label( wfMsg( 'renameuserold' ), 'oldusername' ) .
77
<td class='mw-input'>" .
78
Xml::input( 'oldusername', 20, $oun, array( 'type' => 'text', 'tabindex' => '1' ) ) . ' ' .
82
<td class='mw-label'>" .
83
Xml::label( wfMsg( 'renameusernew' ), 'newusername' ) .
85
<td class='mw-input'>" .
86
Xml::input( 'newusername', 20, $nun, array( 'type' => 'text', 'tabindex' => '2' ) ) .
90
<td class='mw-label'>" .
91
Xml::label( wfMsg( 'renameuserreason' ), 'reason' ) .
93
<td class='mw-input'>" .
94
Xml::input( 'reason', 40, $reason, array( 'type' => 'text', 'tabindex' => '3', 'maxlength' => 255 ) ) .
98
if( $wgUser->isAllowed( 'move' ) ) {
103
<td class='mw-input'>" .
104
Xml::checkLabel( wfMsg( 'renameusermove' ), 'movepages', 'movepages',
105
$move_checked, array( 'tabindex' => '4' ) ) .
111
$warningsHtml = array();
112
foreach( $warnings as $warning )
113
$warningsHtml[] = is_array( $warning ) ?
114
call_user_func_array( 'wfMsgWikiHtml', $warning ) :
115
wfMsgHtml( $warning );
118
<td class='mw-label'>" . wfMsgWikiHtml( 'renameuserwarnings' ) . "
120
<td class='mw-input'>" .
121
'<ul style="color: red; font-weight: bold"><li>'.
122
implode( '</li><li>', $warningsHtml ).'</li></ul>'.
130
<td class='mw-input'>" .
131
Xml::checkLabel( wfMsg( 'renameuserconfirm' ), 'confirmaction', 'confirmaction',
132
false, array( 'tabindex' => '6' ) ) .
141
<td class='mw-submit'>" .
142
Xml::submitButton( wfMsg( 'renameusersubmit' ), array( 'name' => 'submit',
143
'tabindex' => '7', 'id' => 'submit' ) ) .
145
Xml::submitButton( wfMsg( 'blocklogpage' ), array ( 'name' => 'submit-showBlockLog',
146
'id' => 'submit-showBlockLog', 'tabindex' => '8' ) ) .
149
Xml::closeElement( 'table' ) .
150
Xml::closeElement( 'fieldset' ) .
151
Xml::hidden( 'token', $token ) .
152
Xml::closeElement( 'form' ) . "\n"
155
// Show block log if requested
156
if( $showBlockLog && is_object( $oldusername ) ) {
157
$this->showLogExtract( $oldusername, 'block', $wgOut ) ;
161
if( $wgRequest->getText( 'token' ) === '' ) {
162
# They probably haven't even submitted the form, so don't go further.
164
} elseif( $warnings ) {
165
# Let user read warnings
167
} elseif( !$wgRequest->wasPosted() || !$wgUser->matchEditToken( $wgRequest->getVal( 'token' ) ) ) {
168
$wgOut->addWikiText( "<div class=\"errorbox\">" . wfMsg( 'renameuser-error-request' ) . "</div>" );
170
} elseif( !is_object( $oldusername ) ) {
172
"<div class=\"errorbox\">"
173
. wfMsg( 'renameusererrorinvalid', $wgRequest->getText( 'oldusername' ) )
177
} elseif( !is_object( $newusername ) ) {
179
"<div class=\"errorbox\">"
180
. wfMsg( 'renameusererrorinvalid', $wgRequest->getText( 'newusername' ) )
184
} elseif( $oldusername->getText() == $newusername->getText() ) {
185
$wgOut->addWikiText( "<div class=\"errorbox\">" . wfMsg( 'renameuser-error-same-user' ) . "</div>" );
189
// Suppress username validation of old username
190
$olduser = User::newFromName( $oldusername->getText(), false );
191
$newuser = User::newFromName( $newusername->getText(), 'creatable' );
193
// It won't be an object if for instance "|" is supplied as a value
194
if( !is_object( $olduser ) ) {
195
$wgOut->addWikiText( "<div class=\"errorbox\">" . wfMsg( 'renameusererrorinvalid',
196
$oldusername->getText() ) . "</div>" );
199
if( !is_object( $newuser ) || !User::isCreatableName( $newuser->getName() ) ) {
200
$wgOut->addWikiText( "<div class=\"errorbox\">" . wfMsg( 'renameusererrorinvalid',
201
$newusername->getText() ) . "</div>" );
205
// Check for the existence of lowercase oldusername in database.
206
// Until r19631 it was possible to rename a user to a name with first character as lowercase
207
if( $oldusername->getText() !== $wgContLang->ucfirst( $oldusername->getText() ) ) {
208
// oldusername was entered as lowercase -> check for existence in table 'user'
209
$dbr = wfGetDB( DB_SLAVE );
210
$uid = $dbr->selectField( 'user', 'user_id',
211
array( 'user_name' => $oldusername->getText() ),
213
if( $uid === false ) {
214
if( !$wgCapitalLinks ) {
215
$uid = 0; // We are on a lowercase wiki but lowercase username does not exists
217
// We are on a standard uppercase wiki, use normal
218
$uid = $olduser->idForName();
219
$oldusername = Title::makeTitleSafe( NS_USER, $olduser->getName() );
223
// oldusername was entered as upperase -> standard procedure
224
$uid = $olduser->idForName();
228
$wgOut->addWikiText( "<div class=\"errorbox\">" . wfMsg( 'renameusererrordoesnotexist' ,
229
$oldusername->getText() ) . "</div>" );
233
if( $newuser->idForName() != 0 ) {
234
$wgOut->addWikiText( "<div class=\"errorbox\">" . wfMsg( 'renameusererrorexists',
235
$newusername->getText() ) . "</div>" );
239
// Always get the edits count, it will be used for the log message
240
$contribs = User::edits( $uid );
243
if( !$wgUser->isAllowed( 'siteadmin' ) ) {
244
if ( RENAMEUSER_CONTRIBLIMIT != 0 && $contribs > RENAMEUSER_CONTRIBLIMIT ) {
245
$wgOut->addWikiText( "<div class=\"errorbox\">" .
246
wfMsg( 'renameusererrortoomany',
247
$oldusername->getText(),
248
$wgLang->formatNum( $contribs ),
249
$wgLang->formatNum( RENAMEUSER_CONTRIBLIMIT )
256
// Give other affected extensions a chance to validate or abort
257
if( !wfRunHooks( 'RenameUserAbort', array( $uid, $oldusername->getText(), $newusername->getText() ) ) ) {
261
// Do the heavy lifting...
262
$rename = new RenameuserSQL( $oldusername->getText(), $newusername->getText(), $uid );
263
if( !$rename->rename() ) {
267
// If this user is renaming his/herself, make sure that Title::moveTo()
268
// doesn't make a bunch of null move edits under the old name!
269
if( $wgUser->getId() == $uid ) {
270
$wgUser->setName( $newusername->getText() );
274
$log = new LogPage( 'renameuser' );
275
$log->addEntry( 'renameuser', $oldusername, wfMsgExt( 'renameuser-log', array( 'parsemag', 'content' ),
276
$wgContLang->formatNum( $contribs ), $reason ), $newusername->getText() );
278
// Move any user pages
279
if( $wgRequest->getCheck( 'movepages' ) && $wgUser->isAllowed( 'move' ) ) {
280
$dbr = wfGetDB( DB_SLAVE );
281
$oldkey = $oldusername->getDBkey();
282
$pages = $dbr->select(
284
array( 'page_namespace', 'page_title' ),
286
'page_namespace IN (' . NS_USER . ',' . NS_USER_TALK . ')',
287
'(page_title LIKE ' .
288
$dbr->addQuotes( $dbr->escapeLike( $oldusername->getDBkey() ) . '/%' ) .
289
' OR page_title = ' . $dbr->addQuotes( $oldusername->getDBkey() ) . ')'
295
$skin =& $wgUser->getSkin();
296
while ( $row = $dbr->fetchObject( $pages ) ) {
297
$oldPage = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
298
$newPage = Title::makeTitleSafe( $row->page_namespace,
299
preg_replace( '!^[^/]+!', $newusername->getDBkey(), $row->page_title ) );
300
# Do not autodelete or anything, title must not exist
301
if ( $newPage->exists() && !$oldPage->isValidMoveTarget( $newPage ) ) {
302
$link = $skin->makeKnownLinkObj( $newPage );
303
$output .= '<li class="mw-renameuser-pe">' . wfMsgHtml( 'renameuser-page-exists', $link ) . '</li>';
305
$success = $oldPage->moveTo( $newPage, false, wfMsgForContent( 'renameuser-move-log',
306
$oldusername->getText(), $newusername->getText() ) );
307
if( $success === true ) {
308
$oldLink = $skin->makeKnownLinkObj( $oldPage, '', 'redirect=no' );
309
$newLink = $skin->makeKnownLinkObj( $newPage );
310
$output .= '<li class="mw-renameuser-pm">' . wfMsgHtml( 'renameuser-page-moved', $oldLink, $newLink ) . '</li>';
312
$oldLink = $skin->makeKnownLinkObj( $oldPage );
313
$newLink = $skin->makeLinkObj( $newPage );
314
$output .= '<li class="mw-renameuser-pu">' . wfMsgHtml( 'renameuser-page-unmoved', $oldLink, $newLink ) . '</li>';
319
$wgOut->addHTML( '<ul>' . $output . '</ul>' );
322
// Output success message stuff :)
323
$wgOut->addWikiText( "<div class=\"successbox\">" . wfMsg( 'renameusersuccess', $oldusername->getText(),
324
$newusername->getText() ) . "</div><br style=\"clear:both\" />" );
327
function showLogExtract( $username, $type, &$out ) {
328
# Show relevant lines from the logs:
329
$out->addHTML( Xml::element( 'h2', null, LogPage::logName( $type ) ) . "\n" );
330
LogEventsList::showLogExtract( $out, $type, $username->getPrefixedText() );
334
class RenameuserSQL {
361
* The the tables => fields to be updated
371
* @param string $old The old username
372
* @param string $new The new username
374
function RenameuserSQL($old, $new, $uid) {
379
$this->tables = array(); // Immediate updates
380
$this->tables['image'] = array('img_user_text','img_user');
381
$this->tables['oldimage'] = array('oi_user_text','oi_user');
382
# FIXME: $this->tables['filearchive'] = array('fa_user_text','fa_user'); (not indexed yet)
383
$this->tablesJob = array(); // Slow updates
384
// If this user has a large number of edits, use the jobqueue
385
if( User::edits($this->uid) > RENAMEUSER_CONTRIBJOB ) {
386
$this->tablesJob['revision'] = array('rev_user_text','rev_user','rev_timestamp');
387
$this->tablesJob['archive'] = array('ar_user_text','ar_user','ar_timestamp');
389
$this->tables['revision'] = array('rev_user_text','rev_user');
390
$this->tables['archive'] = array('ar_user_text','ar_user');
392
// Recent changes is pretty hot, deadlocks occur if done all at once
393
if( wfQueriesMustScale() ) {
394
$this->tablesJob['recentchanges'] = array('rc_user_text','rc_user','rc_timestamp');
396
$this->tables['recentchanges'] = array('rc_user_text','rc_user');
401
* Do the rename operation
404
global $wgMemc, $wgDBname, $wgAuth;
406
wfProfileIn( __METHOD__ );
408
wfRunHooks( 'RenameUserPreRename', array( $this->uid, $this->old, $this->new ) );
410
$dbw = wfGetDB( DB_MASTER );
411
// Rename and touch the user before re-attributing edits,
412
// this avoids users still being logged in and making new edits while
413
// being renamed, which leaves edits at the old name.
414
$dbw->update( 'user',
415
array( 'user_name' => $this->new, 'user_touched' => $dbw->timestamp() ),
416
array( 'user_name' => $this->old ),
419
if( !$dbw->affectedRows() ) {
422
// Reset token to break login with central auth systems.
423
// Again, avoids user being logged in with old name.
424
$user = User::newFromId( $this->uid );
425
$authUser = $wgAuth->getUserInstance( $user );
426
$authUser->resetAuthToken();
428
// Delete from memcached.
430
$wgMemc->delete( wfMemcKey( 'user', 'id', $this->uid ) );
432
// Update ipblock list if this user has a block in there.
433
$dbw->update( 'ipblocks',
434
array( 'ipb_address' => $this->new ),
435
array( 'ipb_user' => $this->uid, 'ipb_address' => $this->old ),
437
// Update this users block/rights log. Ideally, the logs would be historical,
438
// but it is really annoying when users have "clean" block logs by virtue of
439
// being renamed, which makes admin tasks more of a pain...
440
$oldTitle = Title::makeTitle( NS_USER, $this->old );
441
$newTitle = Title::makeTitle( NS_USER, $this->new );
442
$dbw->update( 'logging',
443
array( 'log_title' => $newTitle->getDBKey() ),
444
array( 'log_type' => array( 'block', 'rights' ),
445
'log_namespace' => NS_USER,
446
'log_title' => $oldTitle->getDBKey() ),
448
// Do immediate updates!
449
foreach( $this->tables as $table => $fieldSet ) {
450
list($nameCol,$userCol) = $fieldSet;
451
$dbw->update( $table,
452
array( $nameCol => $this->new ),
453
array( $nameCol => $this->old, $userCol => $this->uid ),
457
// Construct jobqueue updates...
458
// FIXME: if a bureaucrat renames a user in error, he/she
459
// must be careful to wait until the rename finishes before
460
// renaming back. This is due to the fact the the job "queue"
461
// is not really FIFO, so we might end up with a bunch of edits
462
// randomly mixed between the two new names. Some sort of rename
463
// lock might be in order...
464
foreach( $this->tablesJob as $table => $params ) {
465
$userTextC = $params[0]; // some *_user_text column
466
$userIDC = $params[1]; // some *_user column
467
$timestampC = $params[2]; // some *_timestamp column
469
$res = $dbw->select( $table,
470
array( $userTextC, $timestampC ),
471
array( $userTextC => $this->old, $userIDC => $this->uid ),
473
array( 'ORDER BY' => "$timestampC ASC" )
476
global $wgUpdateRowsPerJob;
478
$batchSize = 500; // Lets not flood the job table!
479
$jobSize = $wgUpdateRowsPerJob; // How many rows per job?
481
$jobParams = array();
482
$jobParams['table'] = $table;
483
$jobParams['column'] = $userTextC;
484
$jobParams['uidColumn'] = $userIDC;
485
$jobParams['timestampColumn'] = $timestampC;
486
$jobParams['oldname'] = $this->old;
487
$jobParams['newname'] = $this->new;
488
$jobParams['userID'] = $this->uid;
489
// Timestamp column data for index optimizations
490
$jobParams['minTimestamp'] = '0';
491
$jobParams['maxTimestamp'] = '0';
492
$jobParams['count'] = 0;
494
// Insert into queue!
499
for ( $i = 0; $i < $batchSize; $i++ ) {
500
$row = $dbw->fetchObject( $res );
502
# If there are any job rows left, add it to the queue as one job
504
$jobParams['count'] = $jobRows;
505
$jobs[] = Job::factory( 'renameUser', $oldTitle, $jobParams );
506
$jobParams['minTimestamp'] = '0';
507
$jobParams['maxTimestamp'] = '0';
508
$jobParams['count'] = 0;
514
# If we are adding the first item, since the ORDER BY is ASC, set
516
if( $jobRows == 0 ) {
517
$jobParams['minTimestamp'] = $row->$timestampC;
519
# Keep updating the last timestamp, so it should be correct when the last item is added.
520
$jobParams['maxTimestamp'] = $row->$timestampC;
521
# Update nice counter
523
# Once a job has $jobSize rows, add it to the queue
524
if( $jobRows >= $jobSize ) {
525
$jobParams['count'] = $jobRows;
526
$jobs[] = Job::factory( 'renameUser', $oldTitle, $jobParams );
527
$jobParams['minTimestamp'] = '0';
528
$jobParams['maxTimestamp'] = '0';
529
$jobParams['count'] = 0;
533
Job::batchInsert( $jobs );
535
$dbw->freeResult( $res );
538
// Clear caches and inform authentication plugins
539
$user = User::newFromId( $this->uid );
540
$wgAuth->updateExternalDB( $user );
541
wfRunHooks( 'RenameUserComplete', array( $this->uid, $this->old, $this->new ) );
543
wfProfileOut( __METHOD__ );