25 require_once __DIR__ .
'/Maintenance.php';
33 private const SHOWGRANTS_COLUMN_WIDTH = 20;
36 parent::__construct();
38 'Create a bot password for a user. ' .
39 'See https://www.mediawiki.org/wiki/Manual:Bot_passwords for more information.'
43 "Prints a description of available grants and exits."
46 "App id for the new bot password.",
false,
true
49 "CSV list of permissions to grant.",
false,
true
52 "The username to create a bot password for.",
false
55 "A password will be generated if this is omitted." .
56 " If supplied, it must be exactly 32 characters.",
false
66 $username = $this->
getArg( 0 );
67 $password = $this->
getArg( 1 );
69 $grants = explode(
',', $this->
getOption(
'grants' ) );
72 if ( $username ===
null ) {
73 $errors[] =
"Argument <user> required!";
75 if ( $appId ==
null ) {
76 $errors[] =
"Param appid required!";
78 if ( $this->
getOption(
'grants' ) ===
null ) {
79 $errors[] =
"Param grants required!";
81 if ( count( $errors ) > 0 ) {
85 $services = MediaWikiServices::getInstance();
86 $grantsInfo = $services->getGrantsInfo();
87 $invalidGrants = array_diff( $grants, $grantsInfo->getValidGrants() );
88 if ( count( $invalidGrants ) > 0 ) {
90 "These grants are invalid: " . implode(
', ', $invalidGrants ) .
"\n" .
91 "Use the --showgrants option for a full list of valid grant names."
95 $passwordFactory = $services->getPasswordFactory();
98 if ( $userId ===
null ) {
99 $this->
fatalError(
"Cannot create bot password for non-existent user '$username'." );
102 if ( $password ===
null ) {
105 $passwordLength = strlen( $password );
108 " characters. Given password is $passwordLength characters.";
114 'username' => $username,
119 if ( $bp ===
null ) {
120 $this->
fatalError(
"Bot password creation failed." );
123 $passwordInstance = $passwordFactory->newFromPlaintext( $password );
124 $status = $bp->save(
'insert', $passwordInstance );
126 if ( $status->isGood() ) {
127 $this->
output(
"Success.\n" );
128 $this->
output(
"Log in using username:'{$username}@{$appId}' and password:'{$password}'.\n" );
131 "Bot password creation failed. Does this appid already exist for the user perhaps?\n\nErrors:\n" .
132 print_r( $status->getErrors(),
true )
138 $permissions = MediaWikiServices::getInstance()->getGrantsInfo()->getValidGrants();
139 sort( $permissions );
141 $this->
output( str_pad(
'GRANT', self::SHOWGRANTS_COLUMN_WIDTH ) .
" DESCRIPTION\n" );
142 foreach ( $permissions as $permission ) {
144 str_pad( $permission, self::SHOWGRANTS_COLUMN_WIDTH ) .
" " .
152 require_once RUN_MAINTENANCE_IF_MAIN;
static generatePassword( $config)
Returns a (raw, unhashed) random password string.
const PASSWORD_MINLENGTH
Minimum length for a bot password.
static newUnsaved(array $data, $flags=self::READ_NORMAL)
Create an unsaved BotPassword.
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static getRightDescription( $right)
Get the description of a given right.
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.