MediaWiki master
renameUsersMatchingPattern.php
Go to the documentation of this file.
1<?php
2
10
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
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
70 public function execute() {
71 $this->initServices();
72
73 $fromPattern = new Pattern( 'from', $this->getOption( 'from' ) );
74 $toPattern = new Pattern( 'to', $this->getOption( 'to' ) );
75
76 if ( $this->getOption( 'performer' ) === null ) {
77 $performer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
78 } else {
79 $performer = $this->userFactory->newFromName( $this->getOption( 'performer' ) );
80 }
81 if ( !$performer ) {
82 $this->fatalError( "Unable to get performer account" );
83 }
84 $this->performer = $performer;
85
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' );
90
91 $dbr = $this->getReplicaDB();
92 $batchConds = [];
93 $batchSize = $this->getBatchSize();
94 $numRenamed = 0;
95 do {
96 $res = $dbr->newSelectQueryBuilder()
97 ->select( [ 'user_name' ] )
98 ->from( 'user' )
99 ->where( $dbr->expr( 'user_name', IExpression::LIKE, $fromPattern->toLikeValue( $dbr ) ) )
100 ->andWhere( $batchConds )
101 ->orderBy( 'user_name' )
102 ->limit( $batchSize )
103 ->caller( __METHOD__ )
104 ->fetchResultSet();
105
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" );
113 continue;
114 }
115 $newName = $toPattern->generate( $variablePart );
116
117 // Canonicalize
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" );
123 continue;
124 }
125 $newName = $newTitle->getText();
126
127 // Check destination existence
128 if ( $newUser->isRegistered() ) {
129 $this->output( "Cannot rename \"$oldName\" " .
130 "because \"$newName\" already exists\n" );
131 continue;
132 }
133
134 $numRenamed += $this->renameUser( $oldName, $newName ) ? 1 : 0;
135 $this->waitForReplication();
136 }
137 } while ( $res->numRows() === $batchSize );
138 $this->output( "Renamed $numRenamed user(s)\n" );
139 return true;
140 }
141
147 private function renameUser( $oldName, $newName ) {
148 $oldUser = $this->userFactory->newFromName( $oldName );
149 if ( !$oldUser ) {
150 $this->output( "Invalid user name \"$oldName\"" );
151 return false;
152 }
153
154 $id = $oldUser->getId();
155 if ( !$id ) {
156 $this->output( "Cannot rename non-existent user \"$oldName\"" );
157 return false;
158 }
159
160 if ( $this->dryRun ) {
161 $this->output( "$oldName would be renamed to $newName\n" );
162 } else {
163 $rename = $this->renameUserFactory->newRenameUser( $this->performer, $oldUser, $newName, $this->reason, [
164 'forceGlobalDetach' => $this->getOption( 'force-global-detach' ),
165 'movePages' => !$this->getOption( 'skip-page-moves' ),
166 'suppressRedirect' => $this->getOption( 'suppress-redirect' ),
167 ] );
168 $status = $rename->renameGlobal();
169
170 if ( $status->isGood() ) {
171 $this->output( "$oldName was successfully renamed to $newName.\n" );
172 } else {
173 if ( $status->isOK() ) {
174 $this->output( "$oldName was renamed to $newName.\n" );
175 } else {
176 $this->output( "Unable to rename $oldName.\n" );
177 }
178 foreach ( $status->getMessages() as $msg ) {
179 $this->output( ' - ' . wfMessage( $msg )->text() );
180 }
181 }
182 }
183 return true;
184 }
185}
186
187// @codeCoverageIgnoreStart
188$maintClass = RenameUsersMatchingPattern::class;
189require_once RUN_MAINTENANCE_IF_MAIN;
190// @codeCoverageIgnoreEnd
const NS_USER
Definition Defines.php:67
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...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Creates Title objects.
Helper for TempUserConfig representing string patterns with "$1" indicating variable substitution.
Definition Pattern.php:16
Create User objects.
User class for the MediaWiki software.
Definition User.php:119
static newFromName( $name, $validate='valid')
Definition User.php:605