23require_once __DIR__ .
'/Maintenance.php';
36 private $renameUserFactory;
39 parent::__construct();
42 $this->
addArg(
'old-name',
'Current username of the to-be-renamed user' );
43 $this->
addArg(
'new-name',
'New username of the to-be-renamed user' );
44 $this->
addOption(
'performer',
'Performer of the rename action',
false,
true );
45 $this->
addOption(
'reason',
'Reason of the rename',
false,
true );
47 'Rename the local user even if it is attached to a global account' );
48 $this->
addOption(
'suppress-redirect',
'Don\'t create redirects when moving pages' );
49 $this->
addOption(
'skip-page-moves',
'Don\'t move associated user pages' );
52 private function initServices() {
54 $this->userFactory = $services->getUserFactory();
55 $this->renameUserFactory = $services->getRenameUserFactory();
59 $this->initServices();
61 $oldName = $this->
getArg(
'old-name' );
62 $newName = $this->
getArg(
'new-name' );
63 $reason = $this->
getOption(
'reason' ) ??
'';
65 $oldUser = $this->userFactory->newFromName( $oldName );
66 $newUser = $this->userFactory->newFromName( $newName );
69 $this->
fatalError(
'The specified old username is invalid.' );
70 } elseif ( !$oldUser->isRegistered() ) {
71 $this->
fatalError(
'The user does not exist.' );
74 $this->
fatalError(
'The specified new username is invalid.' );
75 } elseif ( $newUser->isRegistered() ) {
76 $this->
fatalError(
'New username must be free.' );
79 if ( $this->
getOption(
'performer' ) ===
null ) {
80 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
82 $performer = $this->userFactory->newFromName( $this->
getOption(
'performer' ) );
85 if ( !( $performer instanceof
User ) || !$performer->isRegistered() ) {
86 $this->
fatalError(
'Performer does not exist.' );
89 $rename = $this->renameUserFactory->newRenameUser( $performer, $oldUser, $newName, $reason, [
90 'forceGlobalDetach' => $this->
getOption(
'force-global-detach' ),
91 'movePages' => !$this->
getOption(
'skip-page-moves' ),
92 'suppressRedirect' => $this->
getOption(
'suppress-redirect' ),
94 $status = $rename->renameUnsafe();
96 if ( $status->isGood() ) {
97 $this->
output(
"Successfully renamed user.\n" );
99 if ( $status->isOK() ) {
100 $this->
output(
"Successfully renamed user with some warnings.\n" );
101 foreach ( $status->getMessages() as $msg ) {
105 $out =
"Failed to rename user:\n";
106 foreach ( $status->getMessages() as $msg ) {
107 $out = $out .
' - ' .
wfMessage( $msg )->plain() .
"\n";
117require_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.