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 require_once __DIR__
. '/Maintenance.php';
24 use MediaWiki\Page\MovePageFactory
;
25 use MediaWiki\RenameUser\RenameuserSQL
;
26 use MediaWiki\Title\Title
;
27 use MediaWiki\User\CentralId\CentralIdLookup
;
28 use MediaWiki\User\User
;
29 use MediaWiki\User\UserFactory
;
31 class RenameUser
extends Maintenance
{
32 /** @var UserFactory */
35 /** @var CentralIdLookup|null */
36 private $centralLookup;
38 /** @var MovePageFactory */
39 private $movePageFactory;
41 public function __construct() {
42 parent
::__construct();
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' );
55 private function initServices() {
56 $services = $this->getServiceContainer();
57 $this->userFactory
= $services->getUserFactory();
58 $this->centralLookup
= $services->getCentralIdLookupFactory()->getNonLocalLookup();
59 $this->movePageFactory
= $services->getMovePageFactory();
62 public function execute() {
63 $this->initServices();
65 $oldName = $this->getArg( 'old-name' );
66 $newName = $this->getArg( 'new-name' );
68 $oldUser = $this->userFactory
->newFromName( $oldName );
70 $this->fatalError( 'The specified old username is invalid' );
73 if ( !$oldUser->isRegistered() ) {
74 $this->fatalError( 'The user does not exist' );
77 if ( !$this->getOption( 'force-global-detach' )
78 && $this->centralLookup
79 && $this->centralLookup
->isAttached( $oldUser )
81 $this->fatalError( 'The user is globally attached. Use CentralAuth to rename this account.' );
84 $newUser = $this->userFactory
->newFromName( $newName, UserFactory
::RIGOR_CREATABLE
);
86 $this->fatalError( 'The specified new username is invalid' );
87 } elseif ( $newUser->isRegistered() ) {
88 $this->fatalError( 'New username must be free' );
91 if ( $this->getOption( 'performer' ) === null ) {
92 $performer = User
::newSystemUser( User
::MAINTENANCE_SCRIPT_USER
, [ 'steal' => true ] );
94 $performer = $this->userFactory
->newFromName( $this->getOption( 'performer' ) );
97 if ( !( $performer instanceof User
) ||
!$performer->isRegistered() ) {
98 $this->fatalError( 'Performer does not exist.' );
101 $renamer = new RenameuserSQL(
107 'reason' => $this->getOption( 'reason' )
111 if ( !$renamer->rename() ) {
112 $this->fatalError( 'Renaming failed.' );
114 $this->output( "{$oldUser->getName()} was successfully renamed to {$newUser->getName()}.\n" );
118 if ( !$this->getOption( 'skip-page-moves' ) ) {
119 $numRenames +
= $this->movePageAndSubpages(
121 $oldUser->getUserPage(),
122 $newUser->getUserPage(),
125 $numRenames +
= $this->movePageAndSubpages(
127 $oldUser->getTalkPage(),
128 $newUser->getTalkPage(),
132 if ( $numRenames > 0 ) {
133 $this->output( "$numRenames user page(s) renamed\n" );
137 private function movePageAndSubpages( User
$performer, Title
$oldTitle, Title
$newTitle, $kind ) {
138 $movePage = $this->movePageFactory
->newMovePage(
142 $movePage->setMaximumMovedPages( -1 );
143 $logMessage = wfMessage(
144 'renameuser-move-log', $oldTitle->getText(), $newTitle->getText()
145 )->inContentLanguage()->text();
146 $createRedirect = !$this->getOption( 'suppress-redirect' );
149 if ( $oldTitle->exists() ) {
150 $status = $movePage->move( $performer, $logMessage, $createRedirect );
151 if ( $status->isGood() ) {
154 $this->output( "Failed to rename $kind page: " .
155 $status->getWikiText( false, false, 'en' ) .
160 $batchStatus = $movePage->moveSubpages( $performer, $logMessage, $createRedirect );
161 foreach ( $batchStatus->getValue() as $titleText => $status ) {
162 if ( $status->isGood() ) {
165 $this->output( "Failed to rename $kind subpage \"$titleText\": " .
166 $status->getWikiText( false, false, 'en' ) . "\n" );
173 $maintClass = RenameUser
::class;
174 require_once RUN_MAINTENANCE_IF_MAIN
;