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( (int)substr( $userName, 1 ) );
48  } else {
49  $user = User::newFromName( $userName );
50  }
51  if ( !$user || !$user->isRegistered() || !$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( false, false, 'en' )->text() );
76  }
77  }
78  $this->output( "Done!\n" );
79  }
80 }
81 
82 $maintClass = ResetUserEmail::class;
83 require_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...
Definition: Maintenance.php:66
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static generateRandomPasswordString(int $minLength=10)
Generate a random string suitable for a password.
Maintenance script that resets user email.
__construct()
Default constructor.
execute()
Do the actual work.
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1895
static newFromName( $name, $validate='valid')
Definition: User.php:592
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:626
$maintClass