MediaWiki  master
changePassword.php
Go to the documentation of this file.
1 <?php
27 require_once __DIR__ . '/Maintenance.php';
28 
34 class ChangePassword extends Maintenance {
35  public function __construct() {
36  parent::__construct();
37  $this->addOption( "user", "The username to operate on", false, true );
38  $this->addOption( "userid", "The user id to operate on", false, true );
39  $this->addOption( "password", "The password to use", true, true );
40  $this->addDescription( "Change a user's password" );
41  }
42 
43  public function execute() {
44  if ( $this->hasOption( "user" ) ) {
45  $user = User::newFromName( $this->getOption( 'user' ) );
46  } elseif ( $this->hasOption( "userid" ) ) {
47  $user = User::newFromId( $this->getOption( 'userid' ) );
48  } else {
49  $this->fatalError( "A \"user\" or \"userid\" must be set to change the password for" );
50  }
51  if ( !$user || !$user->getId() ) {
52  $this->fatalError( "No such user: " . $this->getOption( 'user' ) );
53  }
54  $password = $this->getOption( 'password' );
55  $status = $user->changeAuthenticationData( [
56  'username' => $user->getName(),
57  'password' => $password,
58  'retype' => $password,
59  ] );
60  if ( $status->isGood() ) {
61  $this->output( "Password set for " . $user->getName() . "\n" );
62  } else {
63  $this->fatalError( $status->getMessage( false, false, 'en' )->text() );
64  }
65  }
66 }
67 
68 $maintClass = ChangePassword::class;
69 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
hasOption( $name)
Checks to see if a particular option exists.
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:560
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 newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:536
Maintenance script to change the password of a given user.