MediaWiki master
renameUser.php
Go to the documentation of this file.
1<?php
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
32
33class RenameUser extends Maintenance {
35 private $userFactory;
36
38 private $centralLookup;
39
41 private $movePageFactory;
42
43 public function __construct() {
44 parent::__construct();
45
46 $this->addDescription( 'Rename a user' );
47 $this->addArg( 'old-name', 'Current username of the to-be-renamed user' );
48 $this->addArg( 'new-name', 'New username of the to-be-renamed user' );
49 $this->addOption( 'performer', 'Performer of the rename action', false, true );
50 $this->addOption( 'reason', 'Reason of the rename', false, true );
51 $this->addOption( 'force-global-detach',
52 'Rename the local user even if it is attached to a global account' );
53 $this->addOption( 'suppress-redirect', 'Don\'t create redirects when moving pages' );
54 $this->addOption( 'skip-page-moves', 'Don\'t move associated user pages' );
55 }
56
57 private function initServices() {
58 $services = $this->getServiceContainer();
59 $this->userFactory = $services->getUserFactory();
60 $this->centralLookup = $services->getCentralIdLookupFactory()->getNonLocalLookup();
61 $this->movePageFactory = $services->getMovePageFactory();
62 }
63
64 public function execute() {
65 $this->initServices();
66
67 $oldName = $this->getArg( 'old-name' );
68 $newName = $this->getArg( 'new-name' );
69
70 $oldUser = $this->userFactory->newFromName( $oldName );
71 if ( !$oldUser ) {
72 $this->fatalError( 'The specified old username is invalid' );
73 }
74
75 if ( !$oldUser->isRegistered() ) {
76 $this->fatalError( 'The user does not exist' );
77 }
78
79 if ( !$this->getOption( 'force-global-detach' )
80 && $this->centralLookup
81 && $this->centralLookup->isAttached( $oldUser )
82 ) {
83 $this->fatalError( 'The user is globally attached. Use CentralAuth to rename this account.' );
84 }
85
86 $newUser = $this->userFactory->newFromName( $newName, UserFactory::RIGOR_CREATABLE );
87 if ( !$newUser ) {
88 $this->fatalError( 'The specified new username is invalid' );
89 } elseif ( $newUser->isRegistered() ) {
90 $this->fatalError( 'New username must be free' );
91 }
92
93 if ( $this->getOption( 'performer' ) === null ) {
94 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
95 } else {
96 $performer = $this->userFactory->newFromName( $this->getOption( 'performer' ) );
97 }
98
99 if ( !( $performer instanceof User ) || !$performer->isRegistered() ) {
100 $this->fatalError( 'Performer does not exist.' );
101 }
102
103 $renamer = new RenameuserSQL(
104 $oldUser->getName(),
105 $newUser->getName(),
106 $oldUser->getId(),
107 $performer,
108 [
109 'reason' => $this->getOption( 'reason' )
110 ]
111 );
112
113 if ( !$renamer->rename() ) {
114 $this->fatalError( 'Renaming failed.' );
115 } else {
116 $this->output( "{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" );
117 }
118
119 $numRenames = 0;
120 if ( !$this->getOption( 'skip-page-moves' ) ) {
121 $numRenames += $this->movePageAndSubpages(
122 $performer,
123 $oldUser->getUserPage(),
124 $newUser->getUserPage(),
125 'user'
126 );
127 $numRenames += $this->movePageAndSubpages(
128 $performer,
129 $oldUser->getTalkPage(),
130 $newUser->getTalkPage(),
131 'user talk',
132 );
133 }
134 if ( $numRenames > 0 ) {
135 $this->output( "$numRenames user page(s) renamed\n" );
136 }
137 }
138
139 private function movePageAndSubpages( User $performer, Title $oldTitle, Title $newTitle, $kind ) {
140 $movePage = $this->movePageFactory->newMovePage(
141 $oldTitle,
142 $newTitle,
143 );
144 $movePage->setMaximumMovedPages( -1 );
145 $logMessage = wfMessage(
146 'renameuser-move-log', $oldTitle->getText(), $newTitle->getText()
147 )->inContentLanguage()->text();
148 $createRedirect = !$this->getOption( 'suppress-redirect' );
149
150 $numRenames = 0;
151 if ( $oldTitle->exists() ) {
152 $status = $movePage->move( $performer, $logMessage, $createRedirect );
153 if ( $status->isGood() ) {
154 $numRenames++;
155 } else {
156 $this->output( "Failed to rename $kind page\n" );
157 $this->error( $status );
158 }
159 }
160
161 $batchStatus = $movePage->moveSubpages( $performer, $logMessage, $createRedirect );
162 foreach ( $batchStatus->getValue() as $titleText => $status ) {
163 if ( $status->isGood() ) {
164 $numRenames++;
165 } else {
166 $this->output( "Failed to rename $kind subpage \"$titleText\"\n" );
167 $this->error( $status );
168 }
169 }
170 return $numRenames;
171 }
172}
173
174// @codeCoverageIgnoreStart
175$maintClass = RenameUser::class;
176require_once RUN_MAINTENANCE_IF_MAIN;
177// @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...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Creates User objects.
internal since 1.36
Definition User.php:93
getUserPage()
Get this user's personal page title.
Definition User.php:2715
isRegistered()
Get whether the user is registered.
Definition User.php:2114
getTalkPage()
Get this user's talk page title.
Definition User.php:2724
static newFromName( $name, $validate='valid')
Definition User.php:580
__construct()
Default constructor.
execute()
Do the actual work.
Service for page rename actions.
$maintClass