21 private $movePageFactory;
24 private $titleFactory;
36 private $suppressRedirect;
39 private $skipPageMoves;
42 parent::__construct();
44 $this->
addDescription(
'Rename users with a name matching a pattern. ' .
45 'This can be used to migrate to a temporary user (IP masking) configuration.' );
46 $this->
addOption(
'from',
'A username pattern where $1 is ' .
47 'the wildcard standing in for any number of characters. All users ' .
48 'matching this pattern will be renamed.',
true,
true );
49 $this->
addOption(
'to',
'A username pattern where $1 is ' .
50 'the part of the username matched by $1 in --from. Users will be ' .
51 ' renamed to this pattern.',
true,
true );
52 $this->
addOption(
'performer',
'Performer of the rename action',
false,
true );
53 $this->
addOption(
'reason',
'Reason of the rename',
false,
true );
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 $this->
addOption(
'dry-run',
'Don\'t actually rename the ' .
57 'users, just report what it would do.' );
61 private function initServices() {
63 if ( $services->getCentralIdLookupFactory()->getNonLocalLookup() ) {
64 $this->
fatalError(
"This script cannot be run when CentralAuth is enabled." );
66 $this->userFactory = $services->getUserFactory();
67 $this->movePageFactory = $services->getMovePageFactory();
68 $this->titleFactory = $services->getTitleFactory();
72 $this->initServices();
77 if ( $this->
getOption(
'performer' ) ===
null ) {
78 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
83 $this->
error(
"Unable to get performer account" );
86 $this->performer = $performer;
88 $this->reason = $this->
getOption(
'reason',
'' );
89 $this->dryRun = $this->
getOption(
'dry-run' );
90 $this->suppressRedirect = $this->
getOption(
'suppress-redirect' );
91 $this->skipPageMoves = $this->
getOption(
'skip-page-moves' );
98 $res = $dbr->newSelectQueryBuilder()
99 ->select( [
'user_name' ] )
101 ->where( $dbr->expr(
'user_name', IExpression::LIKE, $fromPattern->toLikeValue( $dbr ) ) )
102 ->andWhere( $batchConds )
103 ->orderBy(
'user_name' )
104 ->limit( $batchSize )
105 ->caller( __METHOD__ )
108 foreach ( $res as $row ) {
109 $oldName = $row->user_name;
110 $batchConds = [ $dbr->expr(
'user_name',
'>', $oldName ) ];
111 $variablePart = $fromPattern->extract( $oldName );
112 if ( $variablePart ===
null ) {
113 $this->
output(
"Username \"fromName\" matched the LIKE " .
114 "but does not seem to match the pattern" );
117 $newName = $toPattern->generate( $variablePart );
120 $newTitle = $this->titleFactory->makeTitleSafe(
NS_USER, $newName );
121 $newUser = $this->userFactory->newFromName( $newName );
122 if ( !$newTitle || !$newUser ) {
123 $this->
output(
"Cannot rename \"$oldName\" " .
124 "because \"$newName\" is not a valid title\n" );
127 $newName = $newTitle->getText();
130 if ( $newUser->isRegistered() ) {
131 $this->
output(
"Cannot rename \"$oldName\" " .
132 "because \"$newName\" already exists\n" );
136 $numRenamed += $this->renameUser( $oldName, $newName ) ? 1 : 0;
139 }
while ( $res->numRows() === $batchSize );
140 $this->
output(
"Renamed $numRenamed user(s)\n" );
149 private function renameUser( $oldName, $newName ) {
150 $id = $this->userFactory->newFromName( $oldName )->getId();
152 $this->
output(
"Cannot rename non-existent user \"$oldName\"" );
155 if ( $this->dryRun ) {
156 $this->
output(
"$oldName would be renamed to $newName\n" );
164 'reason' => $this->reason
168 if ( !$renamer->rename() ) {
169 $this->
output(
"Unable to rename $oldName" );
172 $this->
output(
"$oldName was successfully renamed to $newName.\n" );
176 if ( $this->skipPageMoves ) {
180 $this->movePageAndSubpages(
NS_USER,
'User', $oldName, $newName );
181 $this->movePageAndSubpages(
NS_USER_TALK,
'User talk', $oldName, $newName );
185 private function movePageAndSubpages( $ns, $nsName, $oldName, $newName ) {
186 $oldTitle = $this->titleFactory->makeTitleSafe( $ns, $oldName );
188 $this->
output(
"[[$nsName:$oldName]] is an invalid title, can't move it.\n" );
191 $newTitle = $this->titleFactory->makeTitleSafe( $ns, $newName );
193 $this->
output(
"[[$nsName:$newName]] is an invalid title, can't move to it.\n" );
197 $movePage = $this->movePageFactory->newMovePage( $oldTitle, $newTitle );
198 $movePage->setMaximumMovedPages( -1 );
201 'renameuser-move-log', $oldName, $newName
202 )->inContentLanguage()->text();
204 if ( $this->dryRun ) {
205 if ( $oldTitle->exists() ) {
206 $this->
output(
"Would move [[$nsName:$oldName]] to [[$nsName:$newName]].\n" );
209 if ( $oldTitle->exists() ) {
210 $status = $movePage->move(
211 $this->performer, $logMessage, !$this->suppressRedirect );
213 $status = Status::newGood();
215 $status->merge( $movePage->moveSubpages(
216 $this->performer, $logMessage, !$this->suppressRedirect ) );
217 if ( !$status->isGood() ) {
218 $this->
output(
"Failed to rename user page\n" );
219 $this->
error( $status );
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.