MediaWiki  1.34.0
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;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:539
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
ChangePassword\execute
execute()
Do the actual work.
Definition: changePassword.php:43
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
ChangePassword
Maintenance script to change the password of a given user.
Definition: changePassword.php:34
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$status
return $status
Definition: SyntaxHighlight.php:347
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
ChangePassword\__construct
__construct()
Default constructor.
Definition: changePassword.php:35
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
$maintClass
$maintClass
Definition: changePassword.php:68