MediaWiki master
resetUserEmail.php
Go to the documentation of this file.
1<?php
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/Maintenance.php';
12// @codeCoverageIgnoreEnd
13
18
26 public function __construct() {
27 parent::__construct();
28
29 $this->addDescription( "Resets a user's email" );
30
31 $this->addArg( 'user', 'Username or user ID, if starts with #' );
32 $this->addArg( 'email', 'Email to assign' );
33
34 $this->addOption( 'no-reset-password', 'Don\'t reset the user\'s password' );
35 $this->addOption( 'email-password', 'Send a temporary password to the user\'s new email address' );
36 }
37
38 public function execute() {
39 $userName = $this->getArg( 0 );
40 if ( preg_match( '/^#\d+$/', $userName ) ) {
41 $user = User::newFromId( (int)substr( $userName, 1 ) );
42 } else {
43 $user = User::newFromName( $userName );
44 }
45 if ( !$user || !$user->isRegistered() || !$user->loadFromId() ) {
46 $this->fatalError( "Error: user '$userName' does not exist\n" );
47 }
48
49 $email = $this->getArg( 1, '' );
50 if ( $email !== '' && !Sanitizer::validateEmail( $email ) ) {
51 $this->fatalError( "Error: email '$email' is not valid\n" );
52 }
53
54 // Code from https://wikitech.wikimedia.org/wiki/Password_reset
55 $user->setEmail( $email );
56 $user->setEmailAuthenticationTimestamp( wfTimestampNow() );
57 $user->saveSettings();
58
59 if ( !$this->hasOption( 'no-reset-password' ) ) {
60 // Kick whomever is currently controlling the account off if possible
61 $password = PasswordFactory::generateRandomPasswordString( 128 );
62 $status = $user->changeAuthenticationData( [
63 'username' => $user->getName(),
64 'password' => $password,
65 'retype' => $password,
66 ] );
67 if ( !$status->isGood() ) {
68 $this->error( "Password couldn't be reset because:" );
69 $this->error( $status );
70 }
71 }
72
73 if ( $this->hasOption( 'email-password' ) ) {
74 $passReset = $this->getServiceContainer()->getPasswordReset();
75 $sysUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
76 $status = $passReset->execute( $sysUser, $user->getName(), $email );
77 if ( !$status->isGood() ) {
78 $this->error( "Email couldn't be sent because:" );
79 $this->error( $status );
80 }
81 }
82 $this->output( "Done!\n" );
83 }
84}
85
86// @codeCoverageIgnoreStart
87$maintClass = ResetUserEmail::class;
88require_once RUN_MAINTENANCE_IF_MAIN;
89// @codeCoverageIgnoreEnd
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.
hasOption( $name)
Checks to see if a particular option was set.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:32
Factory class for creating and checking Password objects.
User class for the MediaWiki software.
Definition User.php:130
Maintenance script that resets user email.
__construct()
Default constructor.
execute()
Do the actual work.
$maintClass