MediaWiki  1.29.2
createAndPromote.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
34  private static $permitRoles = [ 'sysop', 'bureaucrat', 'bot' ];
35 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Create a new user account and/or grant it additional rights' );
39  $this->addOption(
40  'force',
41  'If acccount exists already, just grant it rights or change password.'
42  );
43  foreach ( self::$permitRoles as $role ) {
44  $this->addOption( $role, "Add the account to the {$role} group" );
45  }
46 
47  $this->addOption(
48  'custom-groups',
49  'Comma-separated list of groups to add the user to',
50  false,
51  true
52  );
53 
54  $this->addArg( "username", "Username of new user" );
55  $this->addArg( "password", "Password to set (not required if --force is used)", false );
56  }
57 
58  public function execute() {
59  $username = $this->getArg( 0 );
60  $password = $this->getArg( 1 );
61  $force = $this->hasOption( 'force' );
62  $inGroups = [];
63 
65  if ( !is_object( $user ) ) {
66  $this->error( "invalid username.", true );
67  }
68 
69  $exists = ( 0 !== $user->idForName() );
70 
71  if ( $exists && !$force ) {
72  $this->error( "Account exists. Perhaps you want the --force option?", true );
73  } elseif ( !$exists && !$password ) {
74  $this->error( "Argument <password> required!", false );
75  $this->maybeHelp( true );
76  } elseif ( $exists ) {
77  $inGroups = $user->getGroups();
78  }
79 
80  $groups = array_filter( self::$permitRoles, [ $this, 'hasOption' ] );
81  if ( $this->hasOption( 'custom-groups' ) ) {
82  $allGroups = array_flip( User::getAllGroups() );
83  $customGroupsText = $this->getOption( 'custom-groups' );
84  if ( $customGroupsText !== '' ) {
85  $customGroups = explode( ',', $customGroupsText );
86  foreach ( $customGroups as $customGroup ) {
87  if ( isset( $allGroups[$customGroup] ) ) {
88  $groups[] = trim( $customGroup );
89  } else {
90  $this->output( "$customGroup is not a valid group, ignoring!\n" );
91  }
92  }
93  }
94  }
95 
96  $promotions = array_diff(
97  $groups,
98  $inGroups
99  );
100 
101  if ( $exists && !$password && count( $promotions ) === 0 ) {
102  $this->output( "Account exists and nothing to do.\n" );
103 
104  return;
105  } elseif ( count( $promotions ) !== 0 ) {
106  $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
107  if ( $exists ) {
108  $this->output( wfWikiID() . ": Promoting $promoText" );
109  } else {
110  $this->output( wfWikiID() . ": Creating and promoting $promoText" );
111  }
112  }
113 
114  if ( !$exists ) {
115  # Insert the account into the database
116  $user->addToDatabase();
117  $user->saveSettings();
118  }
119 
120  if ( $password ) {
121  # Try to set the password
122  try {
123  $status = $user->changeAuthenticationData( [
124  'username' => $user->getName(),
125  'password' => $password,
126  'retype' => $password,
127  ] );
128  if ( !$status->isGood() ) {
129  throw new PasswordError( $status->getWikiText( null, null, 'en' ) );
130  }
131  if ( $exists ) {
132  $this->output( "Password set.\n" );
133  $user->saveSettings();
134  }
135  } catch ( PasswordError $pwe ) {
136  $this->error( $pwe->getText(), true );
137  }
138  }
139 
140  # Promote user
141  array_map( [ $user, 'addGroup' ], $promotions );
142 
143  if ( !$exists ) {
144  # Increment site_stats.ss_users
145  $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
146  $ssu->doUpdate();
147  }
148 
149  $this->output( "done.\n" );
150  }
151 }
152 
153 $maintClass = "CreateAndPromote";
154 require_once RUN_MAINTENANCE_IF_MAIN;
CreateAndPromote\__construct
__construct()
Default constructor.
Definition: createAndPromote.php:36
Maintenance\maybeHelp
maybeHelp( $force=false)
Maybe show the help.
Definition: Maintenance.php:952
captcha-old.count
count
Definition: captcha-old.py:225
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
PasswordError
Show an error when any operation involving passwords fails to run.
Definition: PasswordError.php:26
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MWException\getText
getText()
Get the text to display when reporting the error on the command line.
Definition: MWException.php:140
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
SiteStatsUpdate
Class for handling updates to the site_stats table.
Definition: SiteStatsUpdate.php:27
CreateAndPromote
Maintenance script to create an account and grant it rights.
Definition: createAndPromote.php:33
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3011
User\getAllGroups
static getAllGroups()
Return the set of defined explicit groups.
Definition: User.php:4860
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$maintClass
$maintClass
Definition: createAndPromote.php:153
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:267
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
CreateAndPromote\execute
execute()
Do the actual work.
Definition: createAndPromote.php:58
CreateAndPromote\$permitRoles
static $permitRoles
Definition: createAndPromote.php:34
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1956
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:306
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:783