MediaWiki master
renameUser.php
Go to the documentation of this file.
1<?php
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
30
31class RenameUser extends Maintenance {
33 private $userFactory;
34
36 private $renameUserFactory;
37
38 public function __construct() {
39 parent::__construct();
40
41 $this->addDescription( 'Rename a user' );
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 );
46 $this->addOption( 'force-global-detach',
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' );
50 }
51
52 private function initServices() {
53 $services = $this->getServiceContainer();
54 $this->userFactory = $services->getUserFactory();
55 $this->renameUserFactory = $services->getRenameUserFactory();
56 }
57
58 public function execute() {
59 $this->initServices();
60
61 $oldName = $this->getArg( 'old-name' );
62 $newName = $this->getArg( 'new-name' );
63 $reason = $this->getOption( 'reason' ) ?? '';
64
65 $oldUser = $this->userFactory->newFromName( $oldName );
66 $newUser = $this->userFactory->newFromName( $newName );
67
68 if ( !$oldUser ) {
69 $this->fatalError( 'The specified old username is invalid.' );
70 } elseif ( !$oldUser->isRegistered() ) {
71 $this->fatalError( 'The user does not exist.' );
72 }
73 if ( !$newUser ) {
74 $this->fatalError( 'The specified new username is invalid.' );
75 } elseif ( $newUser->isRegistered() ) {
76 $this->fatalError( 'New username must be free.' );
77 }
78
79 if ( $this->getOption( 'performer' ) === null ) {
80 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
81 } else {
82 $performer = $this->userFactory->newFromName( $this->getOption( 'performer' ) );
83 }
84
85 if ( !( $performer instanceof User ) || !$performer->isRegistered() ) {
86 $this->fatalError( 'Performer does not exist.' );
87 }
88
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' ),
93 ] );
94 $status = $rename->renameUnsafe();
95
96 if ( $status->isGood() ) {
97 $this->output( "Successfully renamed user.\n" );
98 } else {
99 if ( $status->isOK() ) {
100 $this->output( "Successfully renamed user with some warnings.\n" );
101 foreach ( $status->getMessages() as $msg ) {
102 $this->output( ' - ' . wfMessage( $msg )->plain() );
103 }
104 } else {
105 $out = "Failed to rename user:\n";
106 foreach ( $status->getMessages() as $msg ) {
107 $out = $out . ' - ' . wfMessage( $msg )->plain() . "\n";
108 }
109 $this->fatalError( $out );
110 }
111 }
112 }
113}
114
115// @codeCoverageIgnoreStart
116$maintClass = RenameUser::class;
117require_once RUN_MAINTENANCE_IF_MAIN;
118// @codeCoverageIgnoreEnd
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.
Create User objects.
User class for the MediaWiki software.
Definition User.php:123
__construct()
Default constructor.
execute()
Do the actual work.
$maintClass