69 $username = $this->
getArg( 0 );
70 $password = $this->
getArg( 1 );
75 $user = $services->getUserFactory()->newFromName( $username );
76 if ( !is_object( $user ) ) {
80 $exists = ( $user->idForName() !== 0 );
82 if ( $exists && !$force ) {
83 $this->
fatalError(
'Account exists. Perhaps you want the --force option?' );
84 } elseif ( !$exists && !$password ) {
85 $this->
error(
'Argument <password> required!' );
87 } elseif ( $exists ) {
88 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
91 $groups = array_filter( self::$permitRoles, [ $this,
'hasOption' ] );
92 if ( $this->
hasOption(
'custom-groups' ) ) {
93 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
94 $customGroupsText = $this->
getOption(
'custom-groups' );
95 if ( $customGroupsText !==
'' ) {
96 $customGroups = explode(
',', $customGroupsText );
97 foreach ( $customGroups as $customGroup ) {
98 if ( isset( $allGroups[$customGroup] ) ) {
99 $groups[] = trim( $customGroup );
101 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
107 $promotions = array_diff(
112 if ( $exists && !$password && count( $promotions ) === 0 ) {
113 $this->
output(
"Account exists and nothing to do.\n" );
116 } elseif ( count( $promotions ) !== 0 ) {
117 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
118 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
120 $this->
output(
"$dbDomain: Promoting $promoText" );
122 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
131 $status = $user->checkPasswordValidity( $password );
133 if ( !$status->isGood() ) {
134 $this->
fatalError( $status->getMessage(
false,
false,
'en' )->text() );
142 MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_MAINT,
145 if ( !$status->isGood() ) {
146 $this->
fatalError( $status->getMessage(
false,
false,
'en' )->text() );
153 $userGroupManager = $services->getUserGroupManager();
154 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
155 $reason = $this->
getOption(
'reason' ) ?:
'';
156 $this->addLogEntry( $user, $inGroups, array_merge( $inGroups, $promotions ), $reason );
160 # Try to set the password
162 $status = $user->changeAuthenticationData( [
163 'username' => $user->getName(),
164 'password' => $password,
165 'retype' => $password,
167 if ( !$status->isGood() ) {
168 throw new PasswordError( $status->getMessage(
false,
false,
'en' )->text() );
171 $this->
output(
"Password set.\n" );
172 $user->saveSettings();
175 $this->
fatalError(
'Setting the password failed: ' . $pwe->getMessage() );
180 # Increment site_stats.ss_users
181 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
185 $this->
output(
"done.\n" );
198 private function addLogEntry( $user, array $oldGroups, array $newGroups,
string $reason ) {