11 require_once __DIR__ .
'/Maintenance.php';
18 private $movePageFactory;
21 private $titleFactory;
33 private $suppressRedirect;
36 private $skipPageMoves;
39 parent::__construct();
41 $this->
addDescription(
'Rename users with a name matching a pattern. ' .
42 'This can be used to migrate to a temporary user (IP masking) configuration.' );
43 $this->
addOption(
'from',
'A username pattern where $1 is ' .
44 'the wildcard standing in for any number of characters. All users ' .
45 'matching this pattern will be renamed.',
true,
true );
46 $this->
addOption(
'to',
'A username pattern where $1 is ' .
47 'the part of the username matched by $1 in --from. Users will be ' .
48 ' renamed to this pattern.',
true,
true );
49 $this->
addOption(
'performer',
'Performer of the rename action',
false,
true );
50 $this->
addOption(
'reason',
'Reason of the rename',
false,
true );
51 $this->
addOption(
'suppress-redirect',
'Don\'t create redirects when moving pages' );
52 $this->
addOption(
'skip-page-moves',
'Don\'t move associated user pages' );
53 $this->
addOption(
'dry-run',
'Don\'t actually rename the ' .
54 'users, just report what it would do.' );
58 private function initServices() {
60 if ( $services->getCentralIdLookupFactory()->getNonLocalLookup() ) {
61 $this->
fatalError(
"This script cannot be run when CentralAuth is enabled." );
63 $this->userFactory = $services->getUserFactory();
64 $this->movePageFactory = $services->getMovePageFactory();
65 $this->titleFactory = $services->getTitleFactory();
69 $this->initServices();
74 if ( $this->
getOption(
'performer' ) ===
null ) {
75 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
80 $this->
error(
"Unable to get performer account" );
83 $this->performer = $performer;
85 $this->reason = $this->
getOption(
'reason',
'' );
86 $this->dryRun = $this->
getOption(
'dry-run' );
87 $this->suppressRedirect = $this->
getOption(
'suppress-redirect' );
88 $this->skipPageMoves = $this->
getOption(
'skip-page-moves' );
95 $res = $dbr->newSelectQueryBuilder()
96 ->select( [
'user_name' ] )
99 [
'user_name' . $fromPattern->buildLike( $dbr ) ],
102 ->orderBy(
'user_name' )
103 ->limit( $batchSize )
104 ->caller( __METHOD__ )
107 foreach ( $res as $row ) {
108 $oldName = $row->user_name;
109 $batchConds = [
'user_name > ' . $dbr->addQuotes( $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" );
116 $newName = $toPattern->generate( $variablePart );
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" );
126 $newName = $newTitle->getText();
129 if ( $newUser->isRegistered() ) {
130 $this->
output(
"Cannot rename \"$oldName\" " .
131 "because \"$newName\" already exists\n" );
135 $numRenamed += $this->renameUser( $oldName, $newName ) ? 1 : 0;
138 }
while ( $res->numRows() === $batchSize );
139 $this->
output(
"Renamed $numRenamed user(s)\n" );
148 private function renameUser( $oldName, $newName ) {
149 $id = $this->userFactory->newFromName( $oldName )->getId();
151 $this->
output(
"Cannot rename non-existent user \"$oldName\"" );
154 if ( $this->dryRun ) {
155 $this->
output(
"$oldName would be renamed to $newName\n" );
163 'reason' => $this->reason
167 if ( !$renamer->rename() ) {
168 $this->
output(
"Unable to rename $oldName" );
171 $this->
output(
"$oldName was successfully renamed to $newName.\n" );
175 if ( $this->skipPageMoves ) {
179 $this->movePageAndSubpages(
NS_USER,
'User', $oldName, $newName );
180 $this->movePageAndSubpages(
NS_USER_TALK,
'User talk', $oldName, $newName );
184 private function movePageAndSubpages( $ns, $nsName, $oldName, $newName ) {
185 $oldTitle = $this->titleFactory->makeTitleSafe( $ns, $oldName );
187 $this->
output(
"[[$nsName:$oldName]] is an invalid title, can't move it.\n" );
190 $newTitle = $this->titleFactory->makeTitleSafe( $ns, $newName );
192 $this->
output(
"[[$nsName:$newName]] is an invalid title, can't move to it.\n" );
196 $movePage = $this->movePageFactory->newMovePage( $oldTitle, $newTitle );
197 $movePage->setMaximumMovedPages( -1 );
200 'renameuser-move-log', $oldName, $newName
201 )->inContentLanguage()->text();
203 if ( $this->dryRun ) {
204 if ( $oldTitle->exists() ) {
205 $this->
output(
"Would move [[$nsName:$oldName]] to [[$nsName:$newName]].\n" );
208 if ( $oldTitle->exists() ) {
209 $status = $movePage->move(
210 $this->performer, $logMessage, !$this->suppressRedirect );
212 $status = Status::newGood();
214 $status->merge( $movePage->moveSubpages(
215 $this->performer, $logMessage, !$this->suppressRedirect ) );
216 if ( !$status->isGood() ) {
217 $this->
output(
"Failed to rename user page: " .
218 $status->getWikiText(
false,
false,
'en' ) .
227 require_once RUN_MAINTENANCE_IF_MAIN;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
getBatchSize()
Returns batch size.
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.
__construct()
Default constructor.
execute()
Do the actual work.
Service for page rename actions.