]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - maintenance/changePassword.php
MediaWiki 1.30.2-scripts
[autoinstalls/mediawiki.git] / maintenance / changePassword.php
index 568952b9e29bc0e866d6c0576102e3d54bf880fa..9fa66324f5a1ef3f6a507f21fef7d159894e6de8 100644 (file)
  * @ingroup Maintenance
  */
 
-require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+require_once __DIR__ . '/Maintenance.php';
 
+/**
+ * Maintenance script to change the password of a given user.
+ *
+ * @ingroup Maintenance
+ */
 class ChangePassword extends Maintenance {
        public function __construct() {
                parent::__construct();
-               $this->addOption( "user", "The username to operate on", true, true );
+               $this->addOption( "user", "The username to operate on", false, true );
+               $this->addOption( "userid", "The user id to operate on", false, true );
                $this->addOption( "password", "The password to use", true, true );
-               $this->mDescription = "Change a user's password";
+               $this->addDescription( "Change a user's password" );
        }
 
        public function execute() {
-               $user = User::newFromName( $this->getOption( 'user' ) );
-               if ( !$user->getId() ) {
+               if ( $this->hasOption( "user" ) ) {
+                       $user = User::newFromName( $this->getOption( 'user' ) );
+               } elseif ( $this->hasOption( "userid" ) ) {
+                       $user = User::newFromId( $this->getOption( 'userid' ) );
+               } else {
+                       $this->error( "A \"user\" or \"userid\" must be set to change the password for", true );
+               }
+               if ( !$user || !$user->getId() ) {
                        $this->error( "No such user: " . $this->getOption( 'user' ), true );
                }
+               $password = $this->getOption( 'password' );
                try {
-                       $user->setPassword( $this->getOption( 'password' ) );
+                       $status = $user->changeAuthenticationData( [
+                               'username' => $user->getName(),
+                               'password' => $password,
+                               'retype' => $password,
+                       ] );
+                       if ( !$status->isGood() ) {
+                               throw new PasswordError( $status->getWikiText( null, null, 'en' ) );
+                       }
                        $user->saveSettings();
                        $this->output( "Password set for " . $user->getName() . "\n" );
                } catch ( PasswordError $pwe ) {
@@ -50,4 +70,4 @@ class ChangePassword extends Maintenance {
 }
 
 $maintClass = "ChangePassword";
-require_once( RUN_MAINTENANCE_IF_MAIN );
+require_once RUN_MAINTENANCE_IF_MAIN;