3 * Creates an account and grants it rights.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @author Pablo Castellano <pablo@anche.no>
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script to create an account and grant it rights.
31 * @ingroup Maintenance
33 class CreateAndPromote
extends Maintenance
{
34 private static $permitRoles = array( 'sysop', 'bureaucrat', 'bot' );
36 public function __construct() {
37 parent
::__construct();
38 $this->mDescription
= "Create a new user account and/or grant it additional rights";
41 'If acccount exists already, just grant it rights or change password.'
43 foreach ( self
::$permitRoles as $role ) {
44 $this->addOption( $role, "Add the account to the {$role} group" );
49 'Comma-separated list of groups to add the user to',
54 $this->addArg( "username", "Username of new user" );
55 $this->addArg( "password", "Password to set (not required if --force is used)", false );
58 public function execute() {
59 $username = $this->getArg( 0 );
60 $password = $this->getArg( 1 );
61 $force = $this->hasOption( 'force' );
64 $user = User
::newFromName( $username );
65 if ( !is_object( $user ) ) {
66 $this->error( "invalid username.", true );
69 $exists = ( 0 !== $user->idForName() );
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();
80 $groups = array_filter( self
::$permitRoles, array( $this, 'hasOption' ) );
81 if ( $this->hasOption( 'custom-groups' ) ) {
82 $customGroupsText = $this->getOption( 'custom-groups' );
83 if ( $customGroupsText !== '' ) {
84 $customGroups = explode( ',', $customGroupsText );
85 foreach ( $customGroups as $customGroup ) {
86 $groups[] = trim( $customGroup );
91 $promotions = array_diff(
96 if ( $exists && !$password && count( $promotions ) === 0 ) {
97 $this->output( "Account exists and nothing to do.\n" );
100 } elseif ( count( $promotions ) !== 0 ) {
101 $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
103 $this->output( wfWikiID() . ": Promoting $promoText" );
105 $this->output( wfWikiID() . ": Creating and promoting $promoText" );
110 # Insert the account into the database
111 $user->addToDatabase();
112 $user->saveSettings();
116 # Try to set the password
118 $user->setPassword( $password );
120 $this->output( "Password set.\n" );
121 $user->saveSettings();
123 } catch ( PasswordError
$pwe ) {
124 $this->error( $pwe->getText(), true );
129 array_map( array( $user, 'addGroup' ), $promotions );
132 # Increment site_stats.ss_users
133 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
137 $this->output( "done.\n" );
141 $maintClass = "CreateAndPromote";
142 require_once RUN_MAINTENANCE_IF_MAIN
;