23require_once __DIR__ .
'/Maintenance.php';
38 private $centralLookup;
41 private $movePageFactory;
44 parent::__construct();
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 );
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' );
57 private function initServices() {
59 $this->userFactory = $services->getUserFactory();
60 $this->centralLookup = $services->getCentralIdLookupFactory()->getNonLocalLookup();
61 $this->movePageFactory = $services->getMovePageFactory();
65 $this->initServices();
67 $oldName = $this->
getArg(
'old-name' );
68 $newName = $this->
getArg(
'new-name' );
70 $oldUser = $this->userFactory->newFromName( $oldName );
72 $this->
fatalError(
'The specified old username is invalid' );
75 if ( !$oldUser->isRegistered() ) {
76 $this->
fatalError(
'The user does not exist' );
79 if ( !$this->
getOption(
'force-global-detach' )
80 && $this->centralLookup
81 && $this->centralLookup->isAttached( $oldUser )
83 $this->
fatalError(
'The user is globally attached. Use CentralAuth to rename this account.' );
86 $newUser = $this->userFactory->newFromName( $newName, UserFactory::RIGOR_CREATABLE );
88 $this->
fatalError(
'The specified new username is invalid' );
89 } elseif ( $newUser->isRegistered() ) {
90 $this->
fatalError(
'New username must be free' );
93 if ( $this->
getOption(
'performer' ) ===
null ) {
94 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
100 $this->
fatalError(
'Performer does not exist.' );
109 'reason' => $this->getOption(
'reason' )
113 if ( !$renamer->rename() ) {
116 $this->
output(
"{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" );
120 if ( !$this->
getOption(
'skip-page-moves' ) ) {
121 $numRenames += $this->movePageAndSubpages(
124 $newUser->getUserPage(),
127 $numRenames += $this->movePageAndSubpages(
130 $newUser->getTalkPage(),
134 if ( $numRenames > 0 ) {
135 $this->
output(
"$numRenames user page(s) renamed\n" );
139 private function movePageAndSubpages(
User $performer,
Title $oldTitle,
Title $newTitle, $kind ) {
140 $movePage = $this->movePageFactory->newMovePage(
144 $movePage->setMaximumMovedPages( -1 );
147 )->inContentLanguage()->text();
148 $createRedirect = !$this->
getOption(
'suppress-redirect' );
151 if ( $oldTitle->
exists() ) {
152 $status = $movePage->move( $performer, $logMessage, $createRedirect );
153 if ( $status->isGood() ) {
156 $this->
output(
"Failed to rename $kind page\n" );
157 $this->
error( $status );
161 $batchStatus = $movePage->moveSubpages( $performer, $logMessage, $createRedirect );
162 foreach ( $batchStatus->getValue() as $titleText => $status ) {
163 if ( $status->isGood() ) {
166 $this->
output(
"Failed to rename $kind subpage \"$titleText\"\n" );
167 $this->
error( $status );
176require_once RUN_MAINTENANCE_IF_MAIN;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
__construct()
Default constructor.
execute()
Do the actual work.
Service for page rename actions.