9require_once __DIR__ .
'/Maintenance.php';
22 private $renameUserFactory;
25 parent::__construct();
28 $this->
addArg(
'old-name',
'Current username of the to-be-renamed user' );
29 $this->
addArg(
'new-name',
'New username of the to-be-renamed user' );
30 $this->
addOption(
'performer',
'Performer of the rename action',
false,
true );
31 $this->
addOption(
'reason',
'Reason of the rename',
false,
true );
33 'Rename the local user even if it is attached to a global account' );
34 $this->
addOption(
'suppress-redirect',
'Don\'t create redirects when moving pages' );
35 $this->
addOption(
'skip-page-moves',
'Don\'t move associated user pages' );
38 private function initServices() {
40 $this->userFactory = $services->getUserFactory();
41 $this->renameUserFactory = $services->getRenameUserFactory();
45 $this->initServices();
47 $oldName = $this->
getArg(
'old-name' );
48 $newName = $this->
getArg(
'new-name' );
49 $reason = $this->
getOption(
'reason' ) ??
'';
51 $oldUser = $this->userFactory->newFromName( $oldName );
52 $newUser = $this->userFactory->newFromName( $newName );
55 $this->
fatalError(
'The specified old username is invalid.' );
56 } elseif ( !$oldUser->isRegistered() ) {
57 $this->
fatalError(
'The user does not exist.' );
60 $this->
fatalError(
'The specified new username is invalid.' );
61 } elseif ( $newUser->isRegistered() ) {
62 $this->
fatalError(
'New username must be free.' );
65 if ( $this->
getOption(
'performer' ) ===
null ) {
66 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
68 $performer = $this->userFactory->newFromName( $this->
getOption(
'performer' ) );
71 if ( !( $performer instanceof
User ) || !$performer->isRegistered() ) {
72 $this->
fatalError(
'Performer does not exist.' );
75 $rename = $this->renameUserFactory->newRenameUser( $performer, $oldUser, $newName, $reason, [
76 'forceGlobalDetach' => $this->
getOption(
'force-global-detach' ),
77 'movePages' => !$this->
getOption(
'skip-page-moves' ),
78 'suppressRedirect' => $this->
getOption(
'suppress-redirect' ),
80 $status = $rename->renameUnsafe();
82 if ( $status->isGood() ) {
83 $this->
output(
"Successfully renamed user.\n" );
85 if ( $status->isOK() ) {
86 $this->
output(
"Successfully renamed user with some warnings.\n" );
87 foreach ( $status->getMessages() as $msg ) {
91 $out =
"Failed to rename user:\n";
92 foreach ( $status->getMessages() as $msg ) {
93 $out = $out .
' - ' .
wfMessage( $msg )->plain() .
"\n";
103require_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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
__construct()
Default constructor.
execute()
Do the actual work.