MediaWiki master
changePassword.php
Go to the documentation of this file.
1<?php
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
32
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( "user", "The username to operate on", false, true );
42 $this->addOption( "userid", "The user id to operate on", false, true );
43 $this->addOption( "password", "The password to use", false, true );
44 // phpcs:ignore Generic.Files.LineLength.TooLong
45 $this->addOption( "passwordstdin", "Makes the script read the password from stdin instead. Cannot be used alongside --password", false, false );
46 $this->addDescription( "Change a user's password" );
47 }
48
49 public function execute() {
50 $user = $this->validateUserOption( "A \"user\" or \"userid\" must be set to change the password for" );
51 // phpcs:ignore Generic.Files.LineLength.TooLong
52 $password = $this->validatePasswordOption( 'Set either --password or --passwordstdin', 'Either --password or --passwordstdin must be set, not both at once' );
53 $status = $user->changeAuthenticationData( [
54 'username' => $user->getName(),
55 'password' => $password,
56 'retype' => $password,
57 ] );
58 if ( $status->isGood() ) {
59 $this->output( "Password set for " . $user->getName() . "\n" );
60 } else {
61 $this->fatalError( $status );
62 }
63 }
64
73 private function validatePasswordOption( $errorMsgNoPassword, $errorMsgBothPasswords ) {
74 if ( $this->hasOption( 'password' ) && $this->hasOption( 'passwordstdin' ) ) {
75 $this->fatalError( $errorMsgBothPasswords );
76 }
77 if ( $this->hasOption( 'password' ) ) {
78 return $this->getOption( 'password' );
79 } elseif ( $this->hasOption( 'passwordstdin' ) ) {
80 return $this->getStdin( Maintenance::STDIN_ALL );
81 } else {
82 $this->fatalError( $errorMsgNoPassword );
83 }
84 }
85}
86
87// @codeCoverageIgnoreStart
88$maintClass = ChangePassword::class;
89require_once RUN_MAINTENANCE_IF_MAIN;
90// @codeCoverageIgnoreEnd
$maintClass
Maintenance script to change the password of a given user.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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.
getOption( $name, $default=null)
Get an option, or return the default.
getStdin( $len=null)
Return input from stdin.
addDescription( $text)
Set the description text.