3 * Creates an account and grant it administrator 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 administrator rights.
31 * @ingroup Maintenance
33 class CreateAndPromote
extends Maintenance
{
35 static $permitRoles = array( 'sysop', 'bureaucrat' );
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" );
44 $this->addArg( "username", "Username of new user" );
45 $this->addArg( "password", "Password to set (not required if --force is used)", false);
48 public function execute() {
49 $username = $this->getArg( 0 );
50 $password = $this->getArg( 1 );
51 $force = $this->hasOption( 'force' );
54 $user = User
::newFromName( $username );
55 if ( !is_object( $user ) ) {
56 $this->error( "invalid username.", true );
59 $exists = ( 0 !== $user->idForName() );
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();
70 $promotions = array_diff( array_filter( self
::$permitRoles, array( $this, 'hasOption' ) ), $inGroups );
72 if ( $exists && !$password && count( $promotions ) === 0 ) {
73 $this->output( "Account exists and nothing to do.\n" );
75 } elseif ( count( $promotions ) !== 0 ) {
76 $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
78 $this->output( wfWikiID() . ": Promoting $promoText" );
80 $this->output( wfWikiID() . ": Creating and promoting $promoText" );
85 # Try to set the password
87 $user->setPassword( $password );
89 $this->output( "Password set.\n" );
90 $user->saveSettings();
92 } catch ( PasswordError
$pwe ) {
93 $this->error( $pwe->getText(), true );
98 # Insert the account into the database
99 $user->addToDatabase();
100 $user->saveSettings();
104 array_map( array( $user, 'addGroup' ), $promotions );
107 # Increment site_stats.ss_users
108 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
112 $this->output( "done.\n" );
116 $maintClass = "CreateAndPromote";
117 require_once( RUN_MAINTENANCE_IF_MAIN
);