11require_once __DIR__ .
'/Maintenance.php';
31 parent::__construct();
34 "Resets a user's email, or batch resets from a file.\n\n"
35 .
"To reset a single user:\n"
36 .
" resetUserEmail.php <user> <email>\n\n"
37 .
"To batch reset from a file (one \"<username>\t<email>\" pair per line, tab-separated):\n"
38 .
" resetUserEmail.php --file /path/to/resets.tsv\n\n"
39 .
"To batch reset from stdin:\n"
40 .
" resetUserEmail.php --file -"
43 $this->
addArg(
'user',
'Username or user ID, if starts with #',
false );
44 $this->
addArg(
'email',
'Email to assign',
false );
46 $this->
addOption(
'no-reset-password',
'Don\'t reset the user\'s password' );
48 'Send a temporary password to the user\'s new email address'
50 $this->
addOption(
'reason',
'Reason for the email change (ticket number etc)',
false,
true );
53 'File with one "<username>\t<email>" pair per line, tab-separated '
54 .
'(# lines are comments). Use - for stdin.',
64 $userName = $this->
getArg( 0 );
66 if ( $file !==
null && $userName !==
null ) {
67 $this->
fatalError(
'Cannot use both positional arguments and --file' );
70 if ( $file !==
null ) {
71 $this->resetFromFile( $file );
72 } elseif ( $userName !==
null ) {
73 $email = $this->
getArg( 1,
'' );
74 $this->resetSingleUser( $userName, $email );
76 $this->
fatalError(
'Either provide <user> <email> arguments or use --file' );
83 private function resetSingleUser(
string $userName,
string $email ): void {
84 $user = $this->resolveUser( $userName );
86 $this->
fatalError(
"Error: user '$userName' does not exist\n" );
89 if ( $email !==
'' && !Sanitizer::validateEmail( $email ) ) {
90 $this->
fatalError(
"Error: email '$email' is not valid\n" );
93 $this->performReset( $user, $userName, $email );
94 $this->
output(
"Done!\n" );
104 private function resetFromFile(
string $file ): void {
105 $shouldClose = false;
106 if ( $file ===
'-' ) {
107 $handle = $this->getStdin();
109 if ( !is_readable( $file ) ) {
110 $this->fatalError(
"Could not open file: $file" );
112 $handle = fopen( $file,
'r' );
113 if ( $handle ===
false ) {
114 $this->fatalError(
"Could not open file: $file" );
121 if ( $this->hasOption(
'email-password' ) ) {
122 $sysUser = User::newSystemUser(
'Maintenance script', [
'steal' =>
true ] );
123 if ( $sysUser ===
null ) {
124 $this->fatalError(
'Could not create system user for email-password mode' );
131 for ( $lineNum = 1; ; $lineNum++ ) {
132 $rawLine = fgets( $handle );
133 if ( $rawLine ===
false ) {
136 $line = trim( $rawLine );
137 if ( $line ===
'' || preg_match(
'/^#(\s|$)/', $line ) ) {
141 $parts = preg_split(
'/\t/', $line, 2 );
142 if ( count( $parts ) !== 2 ) {
143 $this->error(
"Line $lineNum: expected '<username>\\t<email>' (tab-separated), got: $line" );
148 [ $userName, $email ] = array_map(
'trim', $parts );
151 $user = $this->resolveUser( $userName );
153 $this->error(
"Line $lineNum: user '$userName' does not exist" );
158 if ( $email !==
'' && !Sanitizer::validateEmail( $email ) ) {
159 $this->error(
"Line $lineNum: email '$email' is not valid" );
164 $this->performReset( $user, $userName, $email, $sysUser );
165 $this->output(
"Done: $userName\n" );
168 if ( $processed % $this->getBatchSize() === 0 ) {
169 $this->waitForReplication();
173 if ( $shouldClose ) {
178 "\nBatch complete: $good succeeded, $bad failed out of $processed processed.\n"
188 private function resolveUser(
string $userName ): ?
User {
189 $userFactory = $this->getServiceContainer()->getUserFactory();
190 if ( preg_match(
'/^#\d+$/', $userName ) ) {
191 $user = $userFactory->
newFromId( (
int)substr( $userName, 1 ) );
205 private function performReset(
206 User $user,
string $userName,
string $email, ?
User $sysUser =
null
208 $oldAddr = $user->getEmail();
215 $logger = LoggerFactory::getInstance(
'authentication' );
217 'Changing email address for {user} from {oldemail} to {newemail} via resetUserEmail.php', [
219 'oldemail' => $oldAddr,
220 'newemail' => $email,
221 'reason' => $this->getOption(
'reason',
'' ),
225 if ( !$this->hasOption(
'no-reset-password' ) ) {
227 $password = PasswordFactory::generateRandomPasswordString( 128 );
229 'username' => $user->
getName(),
230 'password' => $password,
231 'retype' => $password,
233 if ( !$status->isGood() ) {
234 $this->error(
"Password couldn't be reset for '$userName' because:" );
235 $this->error( $status );
238 'Scrambling password for {user} via resetUserEmail.php', [
240 'reason' => $this->getOption(
'reason',
'' ),
244 $invalidator = $this->createChild( InvalidateUserSessions::class );
245 $invalidator->setOption(
'user', $user->
getName() );
248 $invalidator->deleteOption(
'file' );
249 $invalidator->execute();
253 if ( $this->hasOption(
'email-password' ) ) {
254 if ( $sysUser ===
null ) {
255 $sysUser = User::newSystemUser(
'Maintenance script', [
'steal' =>
true ] );
256 if ( $sysUser ===
null ) {
257 $this->error(
"Could not create system user for email-password for '$userName'" );
261 $passReset = $this->getServiceContainer()->getPasswordReset();
262 $status = $passReset->execute( $sysUser, $user->
getName(), $email );
263 if ( !$status->isGood() ) {
264 $this->error(
"Email couldn't be sent for '$userName' because:" );
265 $this->error( $status );
268 'Password reset email sent for {user} via resetUserEmail.php', [
270 'reason' => $this->getOption(
'reason',
'' ),
280require_once RUN_MAINTENANCE_IF_MAIN;
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
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.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
Maintenance script that resets user email.
__construct()
Default constructor.
execute()
Do the actual work.