19 private $movePageFactory;
22 private $titleFactory;
34 private $suppressRedirect;
37 private $skipPageMoves;
40 parent::__construct();
42 $this->
addDescription(
'Rename users with a name matching a pattern. ' .
43 'This can be used to migrate to a temporary user (IP masking) configuration.' );
44 $this->
addOption(
'from',
'A username pattern where $1 is ' .
45 'the wildcard standing in for any number of characters. All users ' .
46 'matching this pattern will be renamed.',
true,
true );
47 $this->
addOption(
'to',
'A username pattern where $1 is ' .
48 'the part of the username matched by $1 in --from. Users will be ' .
49 ' renamed to this pattern.',
true,
true );
50 $this->
addOption(
'performer',
'Performer of the rename action',
false,
true );
51 $this->
addOption(
'reason',
'Reason of the rename',
false,
true );
52 $this->
addOption(
'suppress-redirect',
'Don\'t create redirects when moving pages' );
53 $this->
addOption(
'skip-page-moves',
'Don\'t move associated user pages' );
54 $this->
addOption(
'dry-run',
'Don\'t actually rename the ' .
55 'users, just report what it would do.' );
59 private function initServices() {
61 if ( $services->getCentralIdLookupFactory()->getNonLocalLookup() ) {
62 $this->
fatalError(
"This script cannot be run when CentralAuth is enabled." );
64 $this->userFactory = $services->getUserFactory();
65 $this->movePageFactory = $services->getMovePageFactory();
66 $this->titleFactory = $services->getTitleFactory();
70 $this->initServices();
75 if ( $this->
getOption(
'performer' ) ===
null ) {
76 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
81 $this->
error(
"Unable to get performer account" );
84 $this->performer = $performer;
86 $this->reason = $this->
getOption(
'reason',
'' );
87 $this->dryRun = $this->
getOption(
'dry-run' );
88 $this->suppressRedirect = $this->
getOption(
'suppress-redirect' );
89 $this->skipPageMoves = $this->
getOption(
'skip-page-moves' );
96 $res = $dbr->newSelectQueryBuilder()
97 ->select( [
'user_name' ] )
99 ->where( $dbr->expr(
'user_name', IExpression::LIKE, $fromPattern->toLikeValue( $dbr ) ) )
100 ->andWhere( $batchConds )
101 ->orderBy(
'user_name' )
102 ->limit( $batchSize )
103 ->caller( __METHOD__ )
106 foreach ( $res as $row ) {
107 $oldName = $row->user_name;
108 $batchConds = [ $dbr->expr(
'user_name',
'>', $oldName ) ];
109 $variablePart = $fromPattern->extract( $oldName );
110 if ( $variablePart ===
null ) {
111 $this->
output(
"Username \"fromName\" matched the LIKE " .
112 "but does not seem to match the pattern" );
115 $newName = $toPattern->generate( $variablePart );
118 $newTitle = $this->titleFactory->makeTitleSafe(
NS_USER, $newName );
119 $newUser = $this->userFactory->newFromName( $newName );
120 if ( !$newTitle || !$newUser ) {
121 $this->
output(
"Cannot rename \"$oldName\" " .
122 "because \"$newName\" is not a valid title\n" );
125 $newName = $newTitle->getText();
128 if ( $newUser->isRegistered() ) {
129 $this->
output(
"Cannot rename \"$oldName\" " .
130 "because \"$newName\" already exists\n" );
134 $numRenamed += $this->renameUser( $oldName, $newName ) ? 1 : 0;
137 }
while ( $res->numRows() === $batchSize );
138 $this->
output(
"Renamed $numRenamed user(s)\n" );
147 private function renameUser( $oldName, $newName ) {
148 $id = $this->userFactory->newFromName( $oldName )->getId();
150 $this->
output(
"Cannot rename non-existent user \"$oldName\"" );
153 if ( $this->dryRun ) {
154 $this->
output(
"$oldName would be renamed to $newName\n" );
162 'reason' => $this->reason
166 if ( !$renamer->rename() ) {
167 $this->
output(
"Unable to rename $oldName" );
170 $this->
output(
"$oldName was successfully renamed to $newName.\n" );
174 if ( $this->skipPageMoves ) {
178 $this->movePageAndSubpages(
NS_USER,
'User', $oldName, $newName );
179 $this->movePageAndSubpages(
NS_USER_TALK,
'User talk', $oldName, $newName );
183 private function movePageAndSubpages( $ns, $nsName, $oldName, $newName ) {
184 $oldTitle = $this->titleFactory->makeTitleSafe( $ns, $oldName );
186 $this->
output(
"[[$nsName:$oldName]] is an invalid title, can't move it.\n" );
189 $newTitle = $this->titleFactory->makeTitleSafe( $ns, $newName );
191 $this->
output(
"[[$nsName:$newName]] is an invalid title, can't move to it.\n" );
195 $movePage = $this->movePageFactory->newMovePage( $oldTitle, $newTitle );
196 $movePage->setMaximumMovedPages( -1 );
199 'renameuser-move-log', $oldName, $newName
200 )->inContentLanguage()->text();
202 if ( $this->dryRun ) {
203 if ( $oldTitle->exists() ) {
204 $this->
output(
"Would move [[$nsName:$oldName]] to [[$nsName:$newName]].\n" );
207 if ( $oldTitle->exists() ) {
208 $status = $movePage->move(
209 $this->performer, $logMessage, !$this->suppressRedirect );
211 $status = Status::newGood();
213 $status->merge( $movePage->moveSubpages(
214 $this->performer, $logMessage, !$this->suppressRedirect ) );
215 if ( !$status->isGood() ) {
216 $this->
output(
"Failed to rename user page: " .
217 $status->getWikiText(
false,
false,
'en' ) .
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.