22require_once __DIR__ .
'/Maintenance.php';
36 private $centralLookup;
39 private $movePageFactory;
42 parent::__construct();
45 $this->
addArg(
'old-name',
'Current username of the to-be-renamed user' );
46 $this->
addArg(
'new-name',
'New username of the to-be-renamed user' );
47 $this->
addOption(
'performer',
'Performer of the rename action',
false,
true );
48 $this->
addOption(
'reason',
'Reason of the rename',
false,
true );
50 'Rename the local user even if it is attached to a global account' );
51 $this->
addOption(
'suppress-redirect',
'Don\'t create redirects when moving pages' );
52 $this->
addOption(
'skip-page-moves',
'Don\'t move associated user pages' );
55 private function initServices() {
57 $this->userFactory = $services->getUserFactory();
58 $this->centralLookup = $services->getCentralIdLookupFactory()->getNonLocalLookup();
59 $this->movePageFactory = $services->getMovePageFactory();
63 $this->initServices();
65 $oldName = $this->
getArg(
'old-name' );
66 $newName = $this->
getArg(
'new-name' );
68 $oldUser = $this->userFactory->newFromName( $oldName );
70 $this->
fatalError(
'The specified old username is invalid' );
73 if ( !$oldUser->isRegistered() ) {
74 $this->
fatalError(
'The user does not exist' );
77 if ( !$this->
getOption(
'force-global-detach' )
78 && $this->centralLookup
79 && $this->centralLookup->isAttached( $oldUser )
81 $this->
fatalError(
'The user is globally attached. Use CentralAuth to rename this account.' );
84 $newUser = $this->userFactory->newFromName( $newName, UserFactory::RIGOR_CREATABLE );
86 $this->
fatalError(
'The specified new username is invalid' );
87 } elseif ( $newUser->isRegistered() ) {
88 $this->
fatalError(
'New username must be free' );
91 if ( $this->
getOption(
'performer' ) ===
null ) {
92 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
98 $this->
fatalError(
'Performer does not exist.' );
107 'reason' => $this->getOption(
'reason' )
111 if ( !$renamer->rename() ) {
114 $this->
output(
"{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" );
118 if ( !$this->
getOption(
'skip-page-moves' ) ) {
119 $numRenames += $this->movePageAndSubpages(
122 $newUser->getUserPage(),
125 $numRenames += $this->movePageAndSubpages(
128 $newUser->getTalkPage(),
132 if ( $numRenames > 0 ) {
133 $this->
output(
"$numRenames user page(s) renamed\n" );
137 private function movePageAndSubpages(
User $performer,
Title $oldTitle,
Title $newTitle, $kind ) {
138 $movePage = $this->movePageFactory->newMovePage(
142 $movePage->setMaximumMovedPages( -1 );
145 )->inContentLanguage()->text();
146 $createRedirect = !$this->
getOption(
'suppress-redirect' );
149 if ( $oldTitle->
exists() ) {
150 $status = $movePage->move( $performer, $logMessage, $createRedirect );
151 if ( $status->isGood() ) {
154 $this->
output(
"Failed to rename $kind page: " .
155 $status->getWikiText(
false,
false,
'en' ) .
160 $batchStatus = $movePage->moveSubpages( $performer, $logMessage, $createRedirect );
161 foreach ( $batchStatus->getValue() as $titleText => $status ) {
162 if ( $status->isGood() ) {
165 $this->
output(
"Failed to rename $kind subpage \"$titleText\": " .
166 $status->getWikiText(
false,
false,
'en' ) .
"\n" );
174require_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...
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.