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\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; |
32 | |
33 | class RenameUser extends Maintenance { |
34 | /** @var UserFactory */ |
35 | private $userFactory; |
36 | |
37 | /** @var CentralIdLookup|null */ |
38 | private $centralLookup; |
39 | |
40 | /** @var MovePageFactory */ |
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; |
176 | require_once RUN_MAINTENANCE_IF_MAIN; |
177 | // @codeCoverageIgnoreEnd |