MediaWiki 1.43.1
renameUsersMatchingPattern.php
Go to the documentation of this file.
1<?php
2
11
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/Maintenance.php';
14// @codeCoverageIgnoreEnd
15
16class RenameUsersMatchingPattern extends Maintenance {
18 private $userFactory;
19
21 private $movePageFactory;
22
24 private $titleFactory;
25
27 private $performer;
28
30 private $reason;
31
33 private $dryRun;
34
36 private $suppressRedirect;
37
39 private $skipPageMoves;
40
41 public function __construct() {
42 parent::__construct();
43
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.' );
58 $this->setBatchSize( 1000 );
59 }
60
61 private function initServices() {
62 $services = $this->getServiceContainer();
63 if ( $services->getCentralIdLookupFactory()->getNonLocalLookup() ) {
64 $this->fatalError( "This script cannot be run when CentralAuth is enabled." );
65 }
66 $this->userFactory = $services->getUserFactory();
67 $this->movePageFactory = $services->getMovePageFactory();
68 $this->titleFactory = $services->getTitleFactory();
69 }
70
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 $id = $this->userFactory->newFromName( $oldName )->getId();
150 if ( !$id ) {
151 $this->output( "Cannot rename non-existent user \"$oldName\"" );
152 }
153
154 if ( $this->dryRun ) {
155 $this->output( "$oldName would be renamed to $newName\n" );
156 } else {
157 $renamer = new RenameuserSQL(
158 $oldName,
159 $newName,
160 $id,
161 $this->performer,
162 [
163 'reason' => $this->reason
164 ]
165 );
166
167 if ( !$renamer->rename() ) {
168 $this->output( "Unable to rename $oldName" );
169 return false;
170 } else {
171 $this->output( "$oldName was successfully renamed to $newName.\n" );
172 }
173 }
174
175 if ( $this->skipPageMoves ) {
176 return true;
177 }
178
179 $this->movePageAndSubpages( NS_USER, 'User', $oldName, $newName );
180 $this->movePageAndSubpages( NS_USER_TALK, 'User talk', $oldName, $newName );
181 return true;
182 }
183
184 private function movePageAndSubpages( $ns, $nsName, $oldName, $newName ) {
185 $oldTitle = $this->titleFactory->makeTitleSafe( $ns, $oldName );
186 if ( !$oldTitle ) {
187 $this->output( "[[$nsName:$oldName]] is an invalid title, can't move it.\n" );
188 return true;
189 }
190 $newTitle = $this->titleFactory->makeTitleSafe( $ns, $newName );
191 if ( !$newTitle ) {
192 $this->output( "[[$nsName:$newName]] is an invalid title, can't move to it.\n" );
193 return true;
194 }
195
196 $movePage = $this->movePageFactory->newMovePage( $oldTitle, $newTitle );
197 $movePage->setMaximumMovedPages( -1 );
198
199 $logMessage = wfMessage(
200 'renameuser-move-log', $oldName, $newName
201 )->inContentLanguage()->text();
202
203 if ( $this->dryRun ) {
204 if ( $oldTitle->exists() ) {
205 $this->output( "Would move [[$nsName:$oldName]] to [[$nsName:$newName]].\n" );
206 }
207 } else {
208 if ( $oldTitle->exists() ) {
209 $status = $movePage->move(
210 $this->performer, $logMessage, !$this->suppressRedirect );
211 } else {
212 $status = Status::newGood();
213 }
214 $status->merge( $movePage->moveSubpages(
215 $this->performer, $logMessage, !$this->suppressRedirect ) );
216 if ( !$status->isGood() ) {
217 $this->output( "Failed to rename user page\n" );
218 $this->error( $status );
219 }
220 }
221 return true;
222 }
223}
224
225// @codeCoverageIgnoreStart
226$maintClass = RenameUsersMatchingPattern::class;
227require_once RUN_MAINTENANCE_IF_MAIN;
228// @codeCoverageIgnoreEnd
const NS_USER
Definition Defines.php:67
const NS_USER_TALK
Definition Defines.php:68
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Class which performs the actual renaming of users.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Creates Title objects.
Helper for TempUserConfig representing string patterns with "$1" indicating variable substitution.
Definition Pattern.php:16
Creates User objects.
internal since 1.36
Definition User.php:94
static newFromName( $name, $validate='valid')
Definition User.php:590
Service for page rename actions.