Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.25% |
82 / 87 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
RenameUser | |
94.25% |
82 / 87 |
|
50.00% |
2 / 4 |
21.08 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
initServices | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
98.00% |
49 / 50 |
|
0.00% |
0 / 1 |
14 | |||
movePageAndSubpages | |
82.61% |
19 / 23 |
|
0.00% |
0 / 1 |
5.13 |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @ingroup Maintenance |
19 | * @author Martin Urbanec <martin.urbanec@wikimedia.cz> |
20 | */ |
21 | |
22 | // @codeCoverageIgnoreStart |
23 | require_once __DIR__ . '/Maintenance.php'; |
24 | // @codeCoverageIgnoreEnd |
25 | |
26 | use MediaWiki\Maintenance\Maintenance; |
27 | use MediaWiki\Page\MovePageFactory; |
28 | use MediaWiki\RenameUser\RenameuserSQL; |
29 | use MediaWiki\Title\Title; |
30 | use MediaWiki\User\CentralId\CentralIdLookup; |
31 | use MediaWiki\User\User; |
32 | use MediaWiki\User\UserFactory; |
33 | |
34 | class RenameUser extends Maintenance { |
35 | /** @var UserFactory */ |
36 | private $userFactory; |
37 | |
38 | /** @var CentralIdLookup|null */ |
39 | private $centralLookup; |
40 | |
41 | /** @var MovePageFactory */ |
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; |
177 | require_once RUN_MAINTENANCE_IF_MAIN; |
178 | // @codeCoverageIgnoreEnd |