MediaWiki
master
Toggle main menu visibility
renameUsersMatchingPattern.php
Go to the documentation of this file.
1
<?php
2
3
use
MediaWiki\Maintenance\Maintenance
;
4
use
MediaWiki\RenameUser\RenameUserFactory
;
5
use
MediaWiki\Title\TitleFactory
;
6
use MediaWiki\User\TempUser\Pattern;
7
use MediaWiki\User\User;
8
use MediaWiki\User\UserFactory;
9
use
Wikimedia\Rdbms\IExpression
;
10
11
// @codeCoverageIgnoreStart
12
require_once __DIR__ .
'/Maintenance.php'
;
13
// @codeCoverageIgnoreEnd
14
15
class
RenameUsersMatchingPattern
extends
Maintenance
{
17
private
$userFactory;
18
20
private
$renameUserFactory;
21
23
private
$titleFactory;
24
26
private
$performer;
27
29
private
$reason;
30
32
private
$dryRun;
33
35
private
$suppressRedirect;
36
38
private
$skipPageMoves;
39
40
public
function
__construct
() {
41
parent::__construct();
42
43
$this->addDescription(
'Rename users with a name matching a pattern. '
.
44
'This can be used to migrate to a temporary user (IP masking) configuration.'
);
45
$this->addOption(
'from'
,
'A username pattern where $1 is '
.
46
'the wildcard standing in for any number of characters. All users '
.
47
'matching this pattern will be renamed.'
,
true
,
true
);
48
$this->addOption(
'to'
,
'A username pattern where $1 is '
.
49
'the part of the username matched by $1 in --from. Users will be '
.
50
' renamed to this pattern.'
,
true
,
true
);
51
$this->addOption(
'performer'
,
'Performer of the rename action'
,
false
,
true
);
52
$this->addOption(
'reason'
,
'Reason of the rename'
,
false
,
true
);
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
$this->addOption(
'dry-run'
,
'Don\'t actually rename the '
.
56
'users, just report what it would do.'
);
57
$this->setBatchSize( 1000 );
58
}
59
60
private
function
initServices() {
61
$services = $this->getServiceContainer();
62
if
( $services->getCentralIdLookupFactory()->getNonLocalLookup() ) {
63
$this->fatalError(
"This script cannot be run when CentralAuth is enabled."
);
64
}
65
$this->userFactory = $services->getUserFactory();
66
$this->renameUserFactory = $services->getRenameUserFactory();
67
$this->titleFactory = $services->getTitleFactory();
68
}
69
71
public
function
execute
() {
72
$this->initServices();
73
74
$fromPattern =
new
Pattern
(
'from'
, $this->getOption(
'from'
) );
75
$toPattern =
new
Pattern
(
'to'
, $this->getOption(
'to'
) );
76
77
if
( $this->getOption(
'performer'
) ===
null
) {
78
$performer =
User::newSystemUser
(
User::MAINTENANCE_SCRIPT_USER
, [
'steal'
=>
true
] );
79
}
else
{
80
$performer = $this->userFactory->newFromName( $this->getOption(
'performer'
) );
81
}
82
if
( !$performer ) {
83
$this->fatalError(
"Unable to get performer account"
);
84
}
85
$this->performer = $performer;
86
87
$this->reason = $this->getOption(
'reason'
,
''
);
88
$this->dryRun = $this->getOption(
'dry-run'
);
89
$this->suppressRedirect = $this->getOption(
'suppress-redirect'
);
90
$this->skipPageMoves = $this->getOption(
'skip-page-moves'
);
91
92
$dbr = $this->getReplicaDB();
93
$batchConds = [];
94
$batchSize = $this->getBatchSize();
95
$numRenamed = 0;
96
do
{
97
$res = $dbr->newSelectQueryBuilder()
98
->select( [
'user_name'
] )
99
->from(
'user'
)
100
->where( $dbr->expr(
'user_name'
,
IExpression::LIKE
, $fromPattern->toLikeValue( $dbr ) ) )
101
->andWhere( $batchConds )
102
->orderBy(
'user_name'
)
103
->limit( $batchSize )
104
->caller( __METHOD__ )
105
->fetchResultSet();
106
107
foreach
( $res as $row ) {
108
$oldName = $row->user_name;
109
$batchConds = [ $dbr->expr(
'user_name'
,
'>'
, $oldName ) ];
110
$variablePart = $fromPattern->extract( $oldName );
111
if
( $variablePart ===
null
) {
112
$this->output(
"Username \"fromName\" matched the LIKE "
.
113
"but does not seem to match the pattern"
);
114
continue
;
115
}
116
$newName = $toPattern->generate( $variablePart );
117
118
// Canonicalize
119
$newTitle = $this->titleFactory->makeTitleSafe(
NS_USER
, $newName );
120
$newUser = $this->userFactory->newFromName( $newName );
121
if
( !$newTitle || !$newUser ) {
122
$this->output(
"Cannot rename \"$oldName\" "
.
123
"because \"$newName\" is not a valid title\n"
);
124
continue
;
125
}
126
$newName = $newTitle->getText();
127
128
// Check destination existence
129
if
( $newUser->isRegistered() ) {
130
$this->output(
"Cannot rename \"$oldName\" "
.
131
"because \"$newName\" already exists\n"
);
132
continue
;
133
}
134
135
$numRenamed += $this->renameUser( $oldName, $newName ) ? 1 : 0;
136
$this->waitForReplication();
137
}
138
}
while
( $res->numRows() === $batchSize );
139
$this->output(
"Renamed $numRenamed user(s)\n"
);
140
return
true
;
141
}
142
148
private
function
renameUser( $oldName, $newName ) {
149
$oldUser = $this->userFactory->newFromName( $oldName );
150
if
( !$oldUser ) {
151
$this->output(
"Invalid user name \"$oldName\""
);
152
return
false
;
153
}
154
155
$id = $oldUser->getId();
156
if
( !$id ) {
157
$this->output(
"Cannot rename non-existent user \"$oldName\""
);
158
return
false
;
159
}
160
161
if
( $this->dryRun ) {
162
$this->output(
"$oldName would be renamed to $newName\n"
);
163
}
else
{
164
$rename = $this->renameUserFactory->newRenameUser( $this->performer, $oldUser, $newName, $this->reason, [
165
'forceGlobalDetach'
=> $this->getOption(
'force-global-detach'
),
166
'movePages'
=> !$this->getOption(
'skip-page-moves'
),
167
'suppressRedirect'
=> $this->getOption(
'suppress-redirect'
),
168
] );
169
$status = $rename->renameGlobal();
170
171
if
( $status->isGood() ) {
172
$this->output(
"$oldName was successfully renamed to $newName.\n"
);
173
}
else
{
174
if
( $status->isOK() ) {
175
$this->output(
"$oldName was renamed to $newName.\n"
);
176
}
else
{
177
$this->output(
"Unable to rename $oldName.\n"
);
178
}
179
foreach
( $status->getMessages() as $msg ) {
180
$this->output(
' - '
.
wfMessage
( $msg )->text() );
181
}
182
}
183
}
184
return
true
;
185
}
186
}
187
188
// @codeCoverageIgnoreStart
189
$maintClass
= RenameUsersMatchingPattern::class;
190
require_once RUN_MAINTENANCE_IF_MAIN;
191
// @codeCoverageIgnoreEnd
NS_USER
const NS_USER
Definition
Defines.php:53
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition
GlobalFunctions.php:548
$maintClass
$maintClass
Definition
addChangeTag.php:56
MediaWiki\RenameUser\RenameUserFactory
Definition
RenameUserFactory.php:18
MediaWiki\Title\TitleFactory
Creates Title objects.
Definition
TitleFactory.php:26
MediaWiki\User\TempUser\Pattern
Helper for TempUserConfig representing string patterns with "$1" indicating variable substitution.
Definition
Pattern.php:16
MediaWiki\User\UserFactory
Create User objects.
Definition
UserFactory.php:34
MediaWiki\User\User
User class for the MediaWiki software.
Definition
User.php:129
MediaWiki\User\User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition
User.php:808
MediaWiki\User\User\MAINTENANCE_SCRIPT_USER
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition
User.php:164
RenameUsersMatchingPattern
Definition
renameUsersMatchingPattern.php:15
RenameUsersMatchingPattern\__construct
__construct()
Definition
renameUsersMatchingPattern.php:40
RenameUsersMatchingPattern\execute
execute()
Definition
renameUsersMatchingPattern.php:71
Wikimedia\Rdbms\IExpression
Definition
IExpression.php:10
Wikimedia\Rdbms\IExpression\LIKE
const LIKE
Definition
IExpression.php:14
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
renameUsersMatchingPattern.php
Generated on
for MediaWiki by
1.18.0