MediaWiki  master
createAndPromote.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
30 
37  private static $permitRoles = [ 'sysop', 'bureaucrat', 'interface-admin', 'bot' ];
38 
39  public function __construct() {
40  parent::__construct();
41  $this->addDescription( 'Create a new user account and/or grant it additional rights' );
42  $this->addOption(
43  'force',
44  'If account exists already, just grant it rights or change password.'
45  );
46  foreach ( self::$permitRoles as $role ) {
47  $this->addOption( $role, "Add the account to the {$role} group" );
48  }
49 
50  $this->addOption(
51  'custom-groups',
52  'Comma-separated list of groups to add the user to',
53  false,
54  true
55  );
56 
57  $this->addArg( "username", "Username of new user" );
58  $this->addArg( "password", "Password to set", false );
59  }
60 
61  public function execute() {
62  $username = $this->getArg( 0 );
63  $password = $this->getArg( 1 );
64  $force = $this->hasOption( 'force' );
65  $inGroups = [];
66  $services = MediaWikiServices::getInstance();
67 
68  $user = $services->getUserFactory()->newFromName( $username );
69  if ( !is_object( $user ) ) {
70  $this->fatalError( "invalid username." );
71  }
72 
73  $exists = ( $user->idForName() !== 0 );
74 
75  if ( $exists && !$force ) {
76  $this->fatalError( "Account exists. Perhaps you want the --force option?" );
77  } elseif ( !$exists && !$password ) {
78  $this->error( "Argument <password> required!" );
79  $this->maybeHelp( true );
80  } elseif ( $exists ) {
81  $inGroups = $services->getUserGroupManager()->getUserGroups( $user );
82  }
83 
84  $groups = array_filter( self::$permitRoles, [ $this, 'hasOption' ] );
85  if ( $this->hasOption( 'custom-groups' ) ) {
86  $allGroups = array_fill_keys( $services->getUserGroupManager()->listAllGroups(), true );
87  $customGroupsText = $this->getOption( 'custom-groups' );
88  if ( $customGroupsText !== '' ) {
89  $customGroups = explode( ',', $customGroupsText );
90  foreach ( $customGroups as $customGroup ) {
91  if ( isset( $allGroups[$customGroup] ) ) {
92  $groups[] = trim( $customGroup );
93  } else {
94  $this->output( "$customGroup is not a valid group, ignoring!\n" );
95  }
96  }
97  }
98  }
99 
100  $promotions = array_diff(
101  $groups,
102  $inGroups
103  );
104 
105  if ( $exists && !$password && count( $promotions ) === 0 ) {
106  $this->output( "Account exists and nothing to do.\n" );
107 
108  return;
109  } elseif ( count( $promotions ) !== 0 ) {
110  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
111  $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
112  if ( $exists ) {
113  $this->output( "$dbDomain: Promoting $promoText" );
114  } else {
115  $this->output( "$dbDomain: Creating and promoting $promoText" );
116  }
117  }
118 
119  if ( !$exists ) {
120  // Create the user via AuthManager as there may be various side
121  // effects that are performed by the configured AuthManager chain.
122  $status = MediaWikiServices::getInstance()->getAuthManager()->autoCreateUser(
123  $user,
124  MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_MAINT,
125  false
126  );
127  if ( !$status->isGood() ) {
128  $this->fatalError( $status->getMessage( false, false, 'en' )->text() );
129  }
130  }
131 
132  if ( $password ) {
133  # Try to set the password
134  try {
135  $status = $user->changeAuthenticationData( [
136  'username' => $user->getName(),
137  'password' => $password,
138  'retype' => $password,
139  ] );
140  if ( !$status->isGood() ) {
141  throw new PasswordError( $status->getMessage( false, false, 'en' )->text() );
142  }
143  if ( $exists ) {
144  $this->output( "Password set.\n" );
145  $user->saveSettings();
146  }
147  } catch ( PasswordError $pwe ) {
148  $this->fatalError( $pwe->getText() );
149  }
150  }
151 
152  $userGroupManager = $services->getUserGroupManager();
153  # Promote user
154  $userGroupManager->addUserToMultipleGroups( $user, $promotions );
155 
156  if ( !$exists ) {
157  # Increment site_stats.ss_users
158  $ssu = SiteStatsUpdate::factory( [ 'users' => 1 ] );
159  $ssu->doUpdate();
160  }
161 
162  $this->output( "done.\n" );
163  }
164 }
165 
166 $maintClass = CreateAndPromote::class;
167 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to create an account and grant it rights.
execute()
Do the actual work.
__construct()
Default constructor.
getText()
Format plain text message for the current exception object.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
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.
maybeHelp( $force=false)
Maybe show the help.
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.
Service locator for MediaWiki core services.
Helper tools for dealing with other locally-hosted wikis.
Definition: WikiMap.php:33
The MediaWiki class is the helper class for the index.php entry point.
Definition: MediaWiki.php:43
Show an error when any operation involving passwords fails to run.
static factory(array $deltas)