23require_once __DIR__ .
'/Maintenance.php';
39 private $centralLookup;
42 private $movePageFactory;
45 parent::__construct();
48 $this->
addArg(
'old-name',
'Current username of the to-be-renamed user' );
49 $this->
addArg(
'new-name',
'New username of the to-be-renamed user' );
50 $this->
addOption(
'performer',
'Performer of the rename action',
false,
true );
51 $this->
addOption(
'reason',
'Reason of the rename',
false,
true );
53 'Rename the local user even if it is attached to a global account' );
54 $this->
addOption(
'suppress-redirect',
'Don\'t create redirects when moving pages' );
55 $this->
addOption(
'skip-page-moves',
'Don\'t move associated user pages' );
58 private function initServices() {
60 $this->userFactory = $services->getUserFactory();
61 $this->centralLookup = $services->getCentralIdLookupFactory()->getNonLocalLookup();
62 $this->movePageFactory = $services->getMovePageFactory();
66 $this->initServices();
68 $oldName = $this->
getArg(
'old-name' );
69 $newName = $this->
getArg(
'new-name' );
71 $oldUser = $this->userFactory->newFromName( $oldName );
73 $this->
fatalError(
'The specified old username is invalid' );
76 if ( !$oldUser->isRegistered() ) {
77 $this->
fatalError(
'The user does not exist' );
80 if ( !$this->
getOption(
'force-global-detach' )
81 && $this->centralLookup
82 && $this->centralLookup->isAttached( $oldUser )
84 $this->
fatalError(
'The user is globally attached. Use CentralAuth to rename this account.' );
87 $newUser = $this->userFactory->newFromName( $newName, UserFactory::RIGOR_CREATABLE );
89 $this->
fatalError(
'The specified new username is invalid' );
90 } elseif ( $newUser->isRegistered() ) {
91 $this->
fatalError(
'New username must be free' );
94 if ( $this->
getOption(
'performer' ) ===
null ) {
95 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
101 $this->
fatalError(
'Performer does not exist.' );
110 'reason' => $this->getOption(
'reason' )
114 if ( !$renamer->rename() ) {
117 $this->
output(
"{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" );
121 if ( !$this->
getOption(
'skip-page-moves' ) ) {
122 $numRenames += $this->movePageAndSubpages(
125 $newUser->getUserPage(),
128 $numRenames += $this->movePageAndSubpages(
131 $newUser->getTalkPage(),
135 if ( $numRenames > 0 ) {
136 $this->
output(
"$numRenames user page(s) renamed\n" );
140 private function movePageAndSubpages(
User $performer,
Title $oldTitle,
Title $newTitle, $kind ) {
141 $movePage = $this->movePageFactory->newMovePage(
145 $movePage->setMaximumMovedPages( -1 );
148 )->inContentLanguage()->text();
149 $createRedirect = !$this->
getOption(
'suppress-redirect' );
152 if ( $oldTitle->
exists() ) {
153 $status = $movePage->move( $performer, $logMessage, $createRedirect );
154 if ( $status->isGood() ) {
157 $this->
output(
"Failed to rename $kind page\n" );
158 $this->
error( $status );
162 $batchStatus = $movePage->moveSubpages( $performer, $logMessage, $createRedirect );
163 foreach ( $batchStatus->getValue() as $titleText => $status ) {
164 if ( $status->isGood() ) {
167 $this->
output(
"Failed to rename $kind subpage \"$titleText\"\n" );
168 $this->
error( $status );
177require_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.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
__construct()
Default constructor.
execute()
Do the actual work.
Service for page rename actions.