1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
<?php
namespace MediaWiki\Specials;
use MediaWiki\CommentStore\CommentStore;
use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\MainConfigNames;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\RenameUser\RenameUserFactory;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\TitleFactory;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserNamePrefixSearch;
use OOUI\FieldLayout;
use OOUI\HtmlSnippet;
use OOUI\MessageWidget;
use UserBlockedError;
use Wikimedia\Rdbms\IConnectionProvider;
/**
* Rename a user account.
*
* @ingroup SpecialPage
*/
class SpecialRenameUser extends SpecialPage {
private IConnectionProvider $dbConns;
private PermissionManager $permissionManager;
private TitleFactory $titleFactory;
private UserFactory $userFactory;
private UserNamePrefixSearch $userNamePrefixSearch;
private RenameUserFactory $renameUserFactory;
/**
* @param IConnectionProvider $dbConns
* @param PermissionManager $permissionManager
* @param TitleFactory $titleFactory
* @param UserFactory $userFactory
* @param UserNamePrefixSearch $userNamePrefixSearch
* @param RenameUserFactory $renameUserFactory
*/
public function __construct(
IConnectionProvider $dbConns,
PermissionManager $permissionManager,
TitleFactory $titleFactory,
UserFactory $userFactory,
UserNamePrefixSearch $userNamePrefixSearch,
RenameUserFactory $renameUserFactory
) {
parent::__construct( 'Renameuser', $userFactory->isUserTableShared() ? 'renameuser-global' : 'renameuser' );
$this->dbConns = $dbConns;
$this->permissionManager = $permissionManager;
$this->titleFactory = $titleFactory;
$this->userFactory = $userFactory;
$this->userNamePrefixSearch = $userNamePrefixSearch;
$this->renameUserFactory = $renameUserFactory;
}
public function doesWrites() {
return true;
}
/**
* Show the special page
*
* @param null|string $par Parameter passed to the page
*/
public function execute( $par ) {
$this->setHeaders();
$this->addHelpLink( 'Help:Renameuser' );
$this->checkPermissions();
$this->checkReadOnly();
$performer = $this->getUser();
$block = $performer->getBlock();
if ( $block ) {
throw new UserBlockedError( $block );
}
$out = $this->getOutput();
$out->addWikiMsg( 'renameuser-summary' );
$this->useTransactionalTimeLimit();
$request = $this->getRequest();
// This works as "/" is not valid in usernames
$userNames = $par !== null ? explode( '/', $par, 2 ) : [];
// Get the old name, applying minimal validation or canonicalization
$oldName = $request->getText( 'oldusername', $userNames[0] ?? '' );
$oldName = trim( str_replace( '_', ' ', $oldName ) );
$oldTitle = $this->titleFactory->makeTitle( NS_USER, $oldName );
// Get the new name and canonicalize it
$origNewName = $request->getText( 'newusername', $userNames[1] ?? '' );
$origNewName = trim( str_replace( '_', ' ', $origNewName ) );
// Force uppercase of new username, otherwise wikis
// with wgCapitalLinks=false can create lc usernames
$newTitle = $this->titleFactory->makeTitleSafe( NS_USER, $this->getContentLanguage()->ucfirst( $origNewName ) );
$newName = $newTitle ? $newTitle->getText() : '';
$reason = $request->getText( 'reason' );
$moveChecked = $request->getBool( 'movepages', !$request->wasPosted() );
$suppressChecked = $request->getCheck( 'suppressredirect' );
if ( $oldName !== '' && $newName !== '' && !$request->getCheck( 'confirmaction' ) ) {
$warnings = $this->getWarnings( $oldName, $newName );
} else {
$warnings = [];
}
$this->showForm( $oldName, $newName, $warnings, $reason, $moveChecked, $suppressChecked );
if ( $request->getText( 'wpEditToken' ) === '' ) {
# They probably haven't even submitted the form, so don't go further.
return;
}
if ( $warnings ) {
# Let user read warnings
return;
}
if (
!$request->wasPosted() ||
!$performer->matchEditToken( $request->getVal( 'wpEditToken' ) )
) {
$out->addHTML( Html::errorBox( $out->msg( 'renameuser-error-request' )->parse() ) );
return;
}
if ( !$newTitle ) {
$out->addHTML( Html::errorBox(
$out->msg( 'renameusererrorinvalid' )->params( $request->getText( 'newusername' ) )->parse()
) );
return;
}
if ( $oldName === $newName ) {
$out->addHTML( Html::errorBox( $out->msg( 'renameuser-error-same-user' )->parse() ) );
return;
}
// Suppress username validation of old username
$oldUser = $this->userFactory->newFromName( $oldName, $this->userFactory::RIGOR_NONE );
$newUser = $this->userFactory->newFromName( $newName, $this->userFactory::RIGOR_CREATABLE );
// It won't be an object if for instance "|" is supplied as a value
if ( !$oldUser ) {
$out->addHTML( Html::errorBox(
$out->msg( 'renameusererrorinvalid' )->params( $oldTitle->getText() )->parse()
) );
return;
}
if ( !$newUser ) {
$out->addHTML( Html::errorBox(
$out->msg( 'renameusererrorinvalid' )->params( $newTitle->getText() )->parse()
) );
return;
}
// Check for the existence of lowercase old username in database.
// Until r19631 it was possible to rename a user to a name with first character as lowercase
if ( $oldName !== $this->getContentLanguage()->ucfirst( $oldName ) ) {
// old username was entered as lowercase -> check for existence in table 'user'
$dbr = $this->dbConns->getReplicaDatabase();
$uid = $dbr->newSelectQueryBuilder()
->select( 'user_id' )
->from( 'user' )
->where( [ 'user_name' => $oldName ] )
->caller( __METHOD__ )
->fetchField();
if ( $uid === false ) {
if ( !$this->getConfig()->get( MainConfigNames::CapitalLinks ) ) {
$uid = 0; // We are on a lowercase wiki but lowercase username does not exist
} else {
// We are on a standard uppercase wiki, use normal
$uid = $oldUser->idForName();
$oldTitle = $this->titleFactory->makeTitleSafe( NS_USER, $oldUser->getName() );
if ( !$oldTitle ) {
$out->addHTML( Html::errorBox(
$out->msg( 'renameusererrorinvalid' )->params( $oldName )->parse()
) );
return;
}
$oldName = $oldTitle->getText();
}
}
} else {
// old username was entered as uppercase -> standard procedure
$uid = $oldUser->idForName();
}
if ( $uid === 0 ) {
$out->addHTML( Html::errorBox(
$out->msg( 'renameusererrordoesnotexist' )->params( $oldName )->parse()
) );
return;
}
if ( $newUser->idForName() !== 0 ) {
$out->addHTML( Html::errorBox(
$out->msg( 'renameusererrorexists' )->params( $newName )->parse()
) );
return;
}
// Check user rights again
// This is needed because SpecialPage::__construct only supports
// checking for one right, but both renameuser and -global is required
// to rename a global user.
if ( !$this->permissionManager->userHasRight( $performer, 'renameuser' ) ) {
$this->displayRestrictionError();
}
if ( $this->userFactory->isUserTableShared()
&& !$this->permissionManager->userHasRight( $performer, 'renameuser-global' ) ) {
$out->addHTML( Html::errorBox( $out->msg( 'renameuser-error-global-rights' )->parse() ) );
return;
}
// Give other affected extensions a chance to validate or abort
if ( !$this->getHookRunner()->onRenameUserAbort( $uid, $oldName, $newName ) ) {
return;
}
$rename = $this->renameUserFactory->newRenameUser( $performer, $oldUser, $newName, $reason, [
'movePages' => $moveChecked,
'suppressRedirect' => $suppressChecked,
] );
$status = $rename->rename();
if ( $status->isGood() ) {
// Output success message stuff :)
$out->addHTML(
Html::successBox(
$out->msg( 'renameusersuccess' )
->params( $oldTitle->getText(), $newTitle->getText() )
->parse()
)
);
} else {
// Output errors stuff
$outHtml = '';
foreach ( $status->getMessages() as $msg ) {
$outHtml = $outHtml . $out->msg( $msg )->parse() . '<br/>';
}
if ( $status->isOK() ) {
$out->addHTML( Html::warningBox( $outHtml ) );
} else {
$out->addHTML( Html::errorBox( $outHtml ) );
}
}
}
private function getWarnings( $oldName, $newName ) {
$warnings = [];
$oldUser = $this->userFactory->newFromName( $oldName, $this->userFactory::RIGOR_NONE );
if ( $oldUser && !$oldUser->isTemp() && $oldUser->getBlock() ) {
$warnings[] = [
'renameuser-warning-currentblock',
SpecialPage::getTitleFor( 'Log', 'block' )->getFullURL( [ 'page' => $oldName ] )
];
}
$this->getHookRunner()->onRenameUserWarning( $oldName, $newName, $warnings );
return $warnings;
}
private function showForm( $oldName, $newName, $warnings, $reason, $moveChecked, $suppressChecked ) {
$performer = $this->getUser();
$formDescriptor = [
'oldusername' => [
'type' => 'user',
'name' => 'oldusername',
'label-message' => 'renameuserold',
'default' => $oldName,
'required' => true,
'excludetemp' => true,
],
'newusername' => [
'type' => 'text',
'name' => 'newusername',
'label-message' => 'renameusernew',
'default' => $newName,
'required' => true,
],
'reason' => [
'type' => 'text',
'name' => 'reason',
'label-message' => 'renameuserreason',
'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
'maxlength-unit' => 'codepoints',
'infusable' => true,
'default' => $reason,
'required' => true,
],
];
if ( $this->permissionManager->userHasRight( $performer, 'move' ) ) {
$formDescriptor['confirm'] = [
'type' => 'check',
'id' => 'movepages',
'name' => 'movepages',
'label-message' => 'renameusermove',
'default' => $moveChecked,
];
}
if ( $this->permissionManager->userHasRight( $performer, 'suppressredirect' ) ) {
$formDescriptor['suppressredirect'] = [
'type' => 'check',
'id' => 'suppressredirect',
'name' => 'suppressredirect',
'label-message' => 'renameusersuppress',
'default' => $suppressChecked,
];
}
if ( $warnings ) {
$warningsHtml = [];
foreach ( $warnings as $warning ) {
$warningsHtml[] = is_array( $warning ) ?
$this->msg( $warning[0] )->params( array_slice( $warning, 1 ) )->parse() :
$this->msg( $warning )->parse();
}
$formDescriptor['renameuserwarnings'] = [
'type' => 'info',
'label-message' => 'renameuserwarnings',
'raw' => true,
'rawrow' => true,
'default' => new FieldLayout(
new MessageWidget( [
'label' => new HtmlSnippet(
'<ul><li>'
. implode( '</li><li>', $warningsHtml )
. '</li></ul>'
),
'type' => 'warning',
] )
),
];
$formDescriptor['confirmaction'] = [
'type' => 'check',
'name' => 'confirmaction',
'id' => 'confirmaction',
'label-message' => 'renameuserconfirm',
];
}
$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
->setMethod( 'post' )
->setId( 'renameuser' )
->setSubmitTextMsg( 'renameusersubmit' );
$this->getOutput()->addHTML( $htmlForm->prepareForm()->getHTML( false ) );
}
/**
* Return an array of subpages beginning with $search that this special page will accept.
*
* @param string $search Prefix to search for
* @param int $limit Maximum number of results to return (usually 10)
* @param int $offset Number of results to skip (usually 0)
* @return string[] Matching subpages
*/
public function prefixSearchSubpages( $search, $limit, $offset ) {
$user = $this->userFactory->newFromName( $search );
if ( !$user ) {
// No prefix suggestion for invalid user
return [];
}
// Autocomplete subpage as user list - public to allow caching
return $this->userNamePrefixSearch->search( 'public', $search, $limit, $offset );
}
protected function getGroupName() {
return 'users';
}
}
/**
* Retain the old class name for backwards compatibility.
* @deprecated since 1.41
*/
class_alias( SpecialRenameUser::class, 'SpecialRenameuser' );
|