MediaWiki  1.28.1
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->error( "A \"user\" or \"userid\" must be set to change the password for", true );
50  }
51  if ( !$user || !$user->getId() ) {
52  $this->error( "No such user: " . $this->getOption( 'user' ), true );
53  }
54  $password = $this->getOption( 'password' );
55  try {
56  $status = $user->changeAuthenticationData( [
57  'username' => $user->getName(),
58  'password' => $password,
59  'retype' => $password,
60  ] );
61  if ( !$status->isGood() ) {
62  throw new PasswordError( $status->getWikiText( null, null, 'en' ) );
63  }
64  $user->saveSettings();
65  $this->output( "Password set for " . $user->getName() . "\n" );
66  } catch ( PasswordError $pwe ) {
67  $this->error( $pwe->getText(), true );
68  }
69  }
70 }
71 
72 $maintClass = "ChangePassword";
73 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
static newFromId($id)
Static factory method for creation from a given user ID.
Definition: User.php:548
hasOption($name)
Checks to see if a particular param exists.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1936
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
addDescription($text)
Set the description text.
getOption($name, $default=null)
Get an option, or return the default.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
output($out, $channel=null)
Throw some output to the user.
getText()
Get the text to display when reporting the error on the command line.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Show an error when any operation involving passwords fails to run.
error($err, $die=0)
Throw an error to the user.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
Maintenance script to change the password of a given user.