MediaWiki  master
resetUserEmail.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32 class ResetUserEmail extends Maintenance {
33  public function __construct() {
34  parent::__construct();
35 
36  $this->addDescription( "Resets a user's email" );
37 
38  $this->addArg( 'user', 'Username or user ID, if starts with #' );
39  $this->addArg( 'email', 'Email to assign' );
40 
41  $this->addOption( 'no-reset-password', 'Don\'t reset the user\'s password' );
42  }
43 
44  public function execute() {
45  $userName = $this->getArg( 0 );
46  if ( preg_match( '/^#\d+$/', $userName ) ) {
47  $user = User::newFromId( substr( $userName, 1 ) );
48  } else {
49  $user = User::newFromName( $userName );
50  }
51  if ( !$user || !$user->getId() || !$user->loadFromId() ) {
52  $this->fatalError( "Error: user '$userName' does not exist\n" );
53  }
54 
55  $email = $this->getArg( 1, '' );
56  if ( $email !== '' && !Sanitizer::validateEmail( $email ) ) {
57  $this->fatalError( "Error: email '$email' is not valid\n" );
58  }
59 
60  // Code from https://wikitech.wikimedia.org/wiki/Password_reset
61  $user->setEmail( $email );
62  $user->setEmailAuthenticationTimestamp( wfTimestampNow() );
63  $user->saveSettings();
64 
65  if ( !$this->hasOption( 'no-reset-password' ) ) {
66  // Kick whomever is currently controlling the account off if possible
68  $status = $user->changeAuthenticationData( [
69  'username' => $user->getName(),
70  'password' => $password,
71  'retype' => $password,
72  ] );
73  if ( !$status->isGood() ) {
74  $this->error( "Password couldn't be reset because:\n"
75  . $status->getMessage( null, null, 'en' )->text() );
76  }
77  }
78  $this->output( "Done!\n" );
79  }
80 }
81 
82 $maintClass = ResetUserEmail::class;
83 require_once RUN_MAINTENANCE_IF_MAIN;
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:636
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:490
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:330
Sanitizer\validateEmail
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1744
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:595
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:60
ResetUserEmail
Maintenance script that resets user email.
Definition: resetUserEmail.php:32
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:250
ResetUserEmail\__construct
__construct()
Default constructor.
Definition: resetUserEmail.php:33
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1678
PasswordFactory\generateRandomPasswordString
static generateRandomPasswordString(int $minLength=10)
Generate a random string suitable for a password.
Definition: PasswordFactory.php:226
ResetUserEmail\execute
execute()
Do the actual work.
Definition: resetUserEmail.php:44
$maintClass
$maintClass
Definition: resetUserEmail.php:82
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:301
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:465
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:436
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:272
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:354