MediaWiki  1.23.1
createAndPromote.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
34 
35  static $permitRoles = array( 'sysop', 'bureaucrat', 'bot' );
36 
37  public function __construct() {
38  parent::__construct();
39  $this->mDescription = "Create a new user account and/or grant it additional rights";
40  $this->addOption( "force", "If acccount exists already, just grant it rights or change password." );
41  foreach ( self::$permitRoles as $role ) {
42  $this->addOption( $role, "Add the account to the {$role} group" );
43  }
44  $this->addArg( "username", "Username of new user" );
45  $this->addArg( "password", "Password to set (not required if --force is used)", false );
46  }
47 
48  public function execute() {
49  $username = $this->getArg( 0 );
50  $password = $this->getArg( 1 );
51  $force = $this->hasOption( 'force' );
52  $inGroups = array();
53 
54  $user = User::newFromName( $username );
55  if ( !is_object( $user ) ) {
56  $this->error( "invalid username.", true );
57  }
58 
59  $exists = ( 0 !== $user->idForName() );
60 
61  if ( $exists && !$force ) {
62  $this->error( "Account exists. Perhaps you want the --force option?", true );
63  } elseif ( !$exists && !$password ) {
64  $this->error( "Argument <password> required!", false );
65  $this->maybeHelp( true );
66  } elseif ( $exists ) {
67  $inGroups = $user->getGroups();
68  }
69 
70  $promotions = array_diff( array_filter( self::$permitRoles, array( $this, 'hasOption' ) ), $inGroups );
71 
72  if ( $exists && !$password && count( $promotions ) === 0 ) {
73  $this->output( "Account exists and nothing to do.\n" );
74  return;
75  } elseif ( count( $promotions ) !== 0 ) {
76  $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
77  if ( $exists ) {
78  $this->output( wfWikiID() . ": Promoting $promoText" );
79  } else {
80  $this->output( wfWikiID() . ": Creating and promoting $promoText" );
81  }
82  }
83 
84  if ( $password ) {
85  # Try to set the password
86  try {
87  $user->setPassword( $password );
88  if ( $exists ) {
89  $this->output( "Password set.\n" );
90  $user->saveSettings();
91  }
92  } catch ( PasswordError $pwe ) {
93  $this->error( $pwe->getText(), true );
94  }
95  }
96 
97  if ( !$exists ) {
98  # Insert the account into the database
99  $user->addToDatabase();
100  $user->saveSettings();
101  }
102 
103  # Promote user
104  array_map( array( $user, 'addGroup' ), $promotions );
105 
106  if ( !$exists ) {
107  # Increment site_stats.ss_users
108  $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
109  $ssu->doUpdate();
110  }
111 
112  $this->output( "done.\n" );
113  }
114 }
115 
116 $maintClass = "CreateAndPromote";
117 require_once RUN_MAINTENANCE_IF_MAIN;
CreateAndPromote\__construct
__construct()
Default constructor.
Definition: createAndPromote.php:37
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Maintenance\maybeHelp
maybeHelp( $force=false)
Maybe show the help.
Definition: Maintenance.php:710
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
PasswordError
Thrown by User::setPassword() on error.
Definition: User.php:45
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:388
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
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:1530
MWException\getText
getText()
Get the text to display when reporting the error on the command line.
Definition: MWException.php:158
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SiteStatsUpdate
Class for handling updates to the site_stats table.
Definition: SiteStatsUpdate.php:24
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:3604
$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:237
$password
return false to override stock group addition can be modified try getUserPermissionsErrors userCan checks are continued by internal code can override on output return false to not delete it return false to override the default password checks this Boolean value will be checked to determine if the password was valid return false to implement your own hashing method & $password
Definition: hooks.txt:2697
$maintClass
$maintClass
Definition: createAndPromote.php:116
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:207
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:48
CreateAndPromote\$permitRoles
static $permitRoles
Definition: createAndPromote.php:35
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246