61 $username = $this->
getArg( 0 );
62 $password = $this->
getArg( 1 );
65 $services = MediaWikiServices::getInstance();
67 $user = $services->getUserFactory()->newFromName( $username );
68 if ( !is_object( $user ) ) {
72 $exists = ( $user->idForName() !== 0 );
74 if ( $exists && !$force ) {
75 $this->
fatalError(
"Account exists. Perhaps you want the --force option?" );
76 } elseif ( !$exists && !$password ) {
77 $this->
error(
"Argument <password> required!" );
79 } elseif ( $exists ) {
80 $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
83 $groups = array_filter( self::$permitRoles, [ $this,
'hasOption' ] );
84 if ( $this->
hasOption(
'custom-groups' ) ) {
85 $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(),
true );
86 $customGroupsText = $this->
getOption(
'custom-groups' );
87 if ( $customGroupsText !==
'' ) {
88 $customGroups = explode(
',', $customGroupsText );
89 foreach ( $customGroups as $customGroup ) {
90 if ( isset( $allGroups[$customGroup] ) ) {
91 $groups[] = trim( $customGroup );
93 $this->
output(
"$customGroup is not a valid group, ignoring!\n" );
99 $promotions = array_diff(
104 if ( $exists && !$password && count( $promotions ) === 0 ) {
105 $this->
output(
"Account exists and nothing to do.\n" );
108 } elseif ( count( $promotions ) !== 0 ) {
109 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
110 $promoText =
"User:{$username} into " . implode(
', ', $promotions ) .
"...\n";
112 $this->
output(
"$dbDomain: Promoting $promoText" );
114 $this->
output(
"$dbDomain: Creating and promoting $promoText" );
121 $status = MediaWikiServices::getInstance()->getAuthManager()->autoCreateUser(
123 MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_MAINT,
126 if ( !$status->isGood() ) {
127 $this->
fatalError( $status->getMessage(
false,
false,
'en' )->text() );
132 # Try to set the password
134 $status = $user->changeAuthenticationData( [
135 'username' => $user->getName(),
136 'password' => $password,
137 'retype' => $password,
139 if ( !$status->isGood() ) {
140 throw new PasswordError( $status->getMessage(
false,
false,
'en' )->text() );
143 $this->
output(
"Password set.\n" );
144 $user->saveSettings();
151 $userGroupManager = $services->getUserGroupManager();
153 $userGroupManager->addUserToMultipleGroups( $user, $promotions );
156 # Increment site_stats.ss_users
157 $ssu = SiteStatsUpdate::factory( [
'users' => 1 ] );
161 $this->
output(
"done.\n" );