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