MediaWiki master
renameUser.php
Go to the documentation of this file.
1<?php
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
33
34class RenameUser extends Maintenance {
36 private $userFactory;
37
39 private $centralLookup;
40
42 private $movePageFactory;
43
44 public function __construct() {
45 parent::__construct();
46
47 $this->addDescription( 'Rename a user' );
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 );
52 $this->addOption( 'force-global-detach',
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' );
56 }
57
58 private function initServices() {
59 $services = $this->getServiceContainer();
60 $this->userFactory = $services->getUserFactory();
61 $this->centralLookup = $services->getCentralIdLookupFactory()->getNonLocalLookup();
62 $this->movePageFactory = $services->getMovePageFactory();
63 }
64
65 public function execute() {
66 $this->initServices();
67
68 $oldName = $this->getArg( 'old-name' );
69 $newName = $this->getArg( 'new-name' );
70
71 $oldUser = $this->userFactory->newFromName( $oldName );
72 if ( !$oldUser ) {
73 $this->fatalError( 'The specified old username is invalid' );
74 }
75
76 if ( !$oldUser->isRegistered() ) {
77 $this->fatalError( 'The user does not exist' );
78 }
79
80 if ( !$this->getOption( 'force-global-detach' )
81 && $this->centralLookup
82 && $this->centralLookup->isAttached( $oldUser )
83 ) {
84 $this->fatalError( 'The user is globally attached. Use CentralAuth to rename this account.' );
85 }
86
87 $newUser = $this->userFactory->newFromName( $newName, UserFactory::RIGOR_CREATABLE );
88 if ( !$newUser ) {
89 $this->fatalError( 'The specified new username is invalid' );
90 } elseif ( $newUser->isRegistered() ) {
91 $this->fatalError( 'New username must be free' );
92 }
93
94 if ( $this->getOption( 'performer' ) === null ) {
95 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
96 } else {
97 $performer = $this->userFactory->newFromName( $this->getOption( 'performer' ) );
98 }
99
100 if ( !( $performer instanceof User ) || !$performer->isRegistered() ) {
101 $this->fatalError( 'Performer does not exist.' );
102 }
103
104 $renamer = new RenameuserSQL(
105 $oldUser->getName(),
106 $newUser->getName(),
107 $oldUser->getId(),
108 $performer,
109 [
110 'reason' => $this->getOption( 'reason' )
111 ]
112 );
113
114 if ( !$renamer->rename() ) {
115 $this->fatalError( 'Renaming failed.' );
116 } else {
117 $this->output( "{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" );
118 }
119
120 $numRenames = 0;
121 if ( !$this->getOption( 'skip-page-moves' ) ) {
122 $numRenames += $this->movePageAndSubpages(
123 $performer,
124 $oldUser->getUserPage(),
125 $newUser->getUserPage(),
126 'user'
127 );
128 $numRenames += $this->movePageAndSubpages(
129 $performer,
130 $oldUser->getTalkPage(),
131 $newUser->getTalkPage(),
132 'user talk',
133 );
134 }
135 if ( $numRenames > 0 ) {
136 $this->output( "$numRenames user page(s) renamed\n" );
137 }
138 }
139
140 private function movePageAndSubpages( User $performer, Title $oldTitle, Title $newTitle, $kind ) {
141 $movePage = $this->movePageFactory->newMovePage(
142 $oldTitle,
143 $newTitle,
144 );
145 $movePage->setMaximumMovedPages( -1 );
146 $logMessage = wfMessage(
147 'renameuser-move-log', $oldTitle->getText(), $newTitle->getText()
148 )->inContentLanguage()->text();
149 $createRedirect = !$this->getOption( 'suppress-redirect' );
150
151 $numRenames = 0;
152 if ( $oldTitle->exists() ) {
153 $status = $movePage->move( $performer, $logMessage, $createRedirect );
154 if ( $status->isGood() ) {
155 $numRenames++;
156 } else {
157 $this->output( "Failed to rename $kind page\n" );
158 $this->error( $status );
159 }
160 }
161
162 $batchStatus = $movePage->moveSubpages( $performer, $logMessage, $createRedirect );
163 foreach ( $batchStatus->getValue() as $titleText => $status ) {
164 if ( $status->isGood() ) {
165 $numRenames++;
166 } else {
167 $this->output( "Failed to rename $kind subpage \"$titleText\"\n" );
168 $this->error( $status );
169 }
170 }
171 return $numRenames;
172 }
173}
174
175// @codeCoverageIgnoreStart
176$maintClass = RenameUser::class;
177require_once RUN_MAINTENANCE_IF_MAIN;
178// @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.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Class which performs the actual renaming of users.
Represents a title within MediaWiki.
Definition Title.php:78
exists( $flags=0)
Check if page exists.
Definition Title.php:3138
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1016
Find central user IDs associated with local user IDs, e.g.
Create User objects.
User class for the MediaWiki software.
Definition User.php:119
getUserPage()
Get this user's personal page title.
Definition User.php:2742
isRegistered()
Get whether the user is registered.
Definition User.php:2139
getTalkPage()
Get this user's talk page title.
Definition User.php:2751
static newFromName( $name, $validate='valid')
Definition User.php:605
__construct()
Default constructor.
execute()
Do the actual work.
Service for page rename actions.
$maintClass