| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
require_once __DIR__ . '/Maintenance.php'; |
| 24 |
|
| 25 |
|
| 26 |
use MediaWiki\Page\MovePageFactory; |
| 27 |
use MediaWiki\RenameUser\RenameuserSQL; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 30 |
use MediaWiki\User\User; |
| 31 |
use MediaWiki\User\UserFactory; |
| 32 |
|
| 33 |
class RenameUser extends Maintenance { |
| 34 |
|
| 35 |
private $userFactory; |
| 36 |
|
| 37 |
|
| 38 |
private $centralLookup; |
| 39 |
|
| 40 |
|
| 41 |
private $movePageFactory; |
| 42 |
|
| 43 |
public function __construct() { |
| 44 |
parent::__construct(); |
| 45 |
|
| 46 |
$this->addDescription( 'Rename a user' ); |
| 47 |
$this->addArg( 'old-name', 'Current username of the to-be-renamed user' ); |
| 48 |
$this->addArg( 'new-name', 'New username of the to-be-renamed user' ); |
| 49 |
$this->addOption( 'performer', 'Performer of the rename action', false, true ); |
| 50 |
$this->addOption( 'reason', 'Reason of the rename', false, true ); |
| 51 |
$this->addOption( 'force-global-detach', |
| 52 |
'Rename the local user even if it is attached to a global account' ); |
| 53 |
$this->addOption( 'suppress-redirect', 'Don\'t create redirects when moving pages' ); |
| 54 |
$this->addOption( 'skip-page-moves', 'Don\'t move associated user pages' ); |
| 55 |
} |
| 56 |
|
| 57 |
private function initServices() { |
| 58 |
$services = $this->getServiceContainer(); |
| 59 |
$this->userFactory = $services->getUserFactory(); |
| 60 |
$this->centralLookup = $services->getCentralIdLookupFactory()->getNonLocalLookup(); |
| 61 |
$this->movePageFactory = $services->getMovePageFactory(); |
| 62 |
} |
| 63 |
|
| 64 |
public function execute() { |
| 65 |
$this->initServices(); |
| 66 |
|
| 67 |
$oldName = $this->getArg( 'old-name' ); |
| 68 |
$newName = $this->getArg( 'new-name' ); |
| 69 |
|
| 70 |
$oldUser = $this->userFactory->newFromName( $oldName ); |
| 71 |
if ( !$oldUser ) { |
| 72 |
$this->fatalError( 'The specified old username is invalid' ); |
| 73 |
} |
| 74 |
|
| 75 |
if ( !$oldUser->isRegistered() ) { |
| 76 |
$this->fatalError( 'The user does not exist' ); |
| 77 |
} |
| 78 |
|
| 79 |
if ( !$this->getOption( 'force-global-detach' ) |
| 80 |
&& $this->centralLookup |
| 81 |
&& $this->centralLookup->isAttached( $oldUser ) |
| 82 |
) { |
| 83 |
$this->fatalError( 'The user is globally attached. Use CentralAuth to rename this account.' ); |
| 84 |
} |
| 85 |
|
| 86 |
$newUser = $this->userFactory->newFromName( $newName, UserFactory::RIGOR_CREATABLE ); |
| 87 |
if ( !$newUser ) { |
| 88 |
$this->fatalError( 'The specified new username is invalid' ); |
| 89 |
} elseif ( $newUser->isRegistered() ) { |
| 90 |
$this->fatalError( 'New username must be free' ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( $this->getOption( 'performer' ) === null ) { |
| 94 |
$performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 95 |
} else { |
| 96 |
$performer = $this->userFactory->newFromName( $this->getOption( 'performer' ) ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( !( $performer instanceof User ) || !$performer->isRegistered() ) { |
| 100 |
$this->fatalError( 'Performer does not exist.' ); |
| 101 |
} |
| 102 |
|
| 103 |
$renamer = new RenameuserSQL( |
| 104 |
$oldUser->getName(), |
| 105 |
$newUser->getName(), |
| 106 |
$oldUser->getId(), |
| 107 |
$performer, |
| 108 |
[ |
| 109 |
'reason' => $this->getOption( 'reason' ) |
| 110 |
] |
| 111 |
); |
| 112 |
|
| 113 |
if ( !$renamer->rename() ) { |
| 114 |
$this->fatalError( 'Renaming failed.' ); |
| 115 |
} else { |
| 116 |
$this->output( "{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" ); |
| 117 |
} |
| 118 |
|
| 119 |
$numRenames = 0; |
| 120 |
if ( !$this->getOption( 'skip-page-moves' ) ) { |
| 121 |
$numRenames += $this->movePageAndSubpages( |
| 122 |
$performer, |
| 123 |
$oldUser->getUserPage(), |
| 124 |
$newUser->getUserPage(), |
| 125 |
'user' |
| 126 |
); |
| 127 |
$numRenames += $this->movePageAndSubpages( |
| 128 |
$performer, |
| 129 |
$oldUser->getTalkPage(), |
| 130 |
$newUser->getTalkPage(), |
| 131 |
'user talk', |
| 132 |
); |
| 133 |
} |
| 134 |
if ( $numRenames > 0 ) { |
| 135 |
$this->output( "$numRenames user page(s) renamed\n" ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private function movePageAndSubpages( User $performer, Title $oldTitle, Title $newTitle, $kind ) { |
| 140 |
$movePage = $this->movePageFactory->newMovePage( |
| 141 |
$oldTitle, |
| 142 |
$newTitle, |
| 143 |
); |
| 144 |
$movePage->setMaximumMovedPages( -1 ); |
| 145 |
$logMessage = wfMessage( |
| 146 |
'renameuser-move-log', $oldTitle->getText(), $newTitle->getText() |
| 147 |
)->inContentLanguage()->text(); |
| 148 |
$createRedirect = !$this->getOption( 'suppress-redirect' ); |
| 149 |
|
| 150 |
$numRenames = 0; |
| 151 |
if ( $oldTitle->exists() ) { |
| 152 |
$status = $movePage->move( $performer, $logMessage, $createRedirect ); |
| 153 |
if ( $status->isGood() ) { |
| 154 |
$numRenames++; |
| 155 |
} else { |
| 156 |
$this->output( "Failed to rename $kind page\n" ); |
| 157 |
$this->error( $status ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
$batchStatus = $movePage->moveSubpages( $performer, $logMessage, $createRedirect ); |
| 162 |
foreach ( $batchStatus->getValue() as $titleText => $status ) { |
| 163 |
if ( $status->isGood() ) { |
| 164 |
$numRenames++; |
| 165 |
} else { |
| 166 |
$this->output( "Failed to rename $kind subpage \"$titleText\"\n" ); |
| 167 |
$this->error( $status ); |
| 168 |
} |
| 169 |
} |
| 170 |
return $numRenames; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
$maintClass = RenameUser::class; |
| 176 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 177 |
|