ApiParse: don't reparse language link titles
[mediawiki.git] / maintenance / renameUser.php
blobf5dbddf61466023b4065b7ccf4a6c182ff48107a
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @ingroup Maintenance
19 * @author Martin Urbanec <martin.urbanec@wikimedia.cz>
22 // @codeCoverageIgnoreStart
23 require_once __DIR__ . '/Maintenance.php';
24 // @codeCoverageIgnoreEnd
26 use MediaWiki\Page\MovePageFactory;
27 use MediaWiki\RenameUser\RenameuserSQL;
28 use MediaWiki\Title\Title;
29 use MediaWiki\User\CentralId\CentralIdLookup;
30 use MediaWiki\User\User;
31 use MediaWiki\User\UserFactory;
33 class RenameUser extends Maintenance {
34 /** @var UserFactory */
35 private $userFactory;
37 /** @var CentralIdLookup|null */
38 private $centralLookup;
40 /** @var MovePageFactory */
41 private $movePageFactory;
43 public function __construct() {
44 parent::__construct();
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' );
57 private function initServices() {
58 $services = $this->getServiceContainer();
59 $this->userFactory = $services->getUserFactory();
60 $this->centralLookup = $services->getCentralIdLookupFactory()->getNonLocalLookup();
61 $this->movePageFactory = $services->getMovePageFactory();
64 public function execute() {
65 $this->initServices();
67 $oldName = $this->getArg( 'old-name' );
68 $newName = $this->getArg( 'new-name' );
70 $oldUser = $this->userFactory->newFromName( $oldName );
71 if ( !$oldUser ) {
72 $this->fatalError( 'The specified old username is invalid' );
75 if ( !$oldUser->isRegistered() ) {
76 $this->fatalError( 'The user does not exist' );
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.' );
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' );
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' ) );
99 if ( !( $performer instanceof User ) || !$performer->isRegistered() ) {
100 $this->fatalError( 'Performer does not exist.' );
103 $renamer = new RenameuserSQL(
104 $oldUser->getName(),
105 $newUser->getName(),
106 $oldUser->getId(),
107 $performer,
109 'reason' => $this->getOption( 'reason' )
113 if ( !$renamer->rename() ) {
114 $this->fatalError( 'Renaming failed.' );
115 } else {
116 $this->output( "{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" );
119 $numRenames = 0;
120 if ( !$this->getOption( 'skip-page-moves' ) ) {
121 $numRenames += $this->movePageAndSubpages(
122 $performer,
123 $oldUser->getUserPage(),
124 $newUser->getUserPage(),
125 'user'
127 $numRenames += $this->movePageAndSubpages(
128 $performer,
129 $oldUser->getTalkPage(),
130 $newUser->getTalkPage(),
131 'user talk',
134 if ( $numRenames > 0 ) {
135 $this->output( "$numRenames user page(s) renamed\n" );
139 private function movePageAndSubpages( User $performer, Title $oldTitle, Title $newTitle, $kind ) {
140 $movePage = $this->movePageFactory->newMovePage(
141 $oldTitle,
142 $newTitle,
144 $movePage->setMaximumMovedPages( -1 );
145 $logMessage = wfMessage(
146 'renameuser-move-log', $oldTitle->getText(), $newTitle->getText()
147 )->inContentLanguage()->text();
148 $createRedirect = !$this->getOption( 'suppress-redirect' );
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 );
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 );
170 return $numRenames;
174 // @codeCoverageIgnoreStart
175 $maintClass = RenameUser::class;
176 require_once RUN_MAINTENANCE_IF_MAIN;
177 // @codeCoverageIgnoreEnd