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" );
46 $this->addArg( "username", "Username of new user" );
47 $this->addArg( "password", "Password to set (not required if --force is used)", false );
50 public function execute() {
51 $username = $this->getArg( 0 );
52 $password = $this->getArg( 1 );
53 $force = $this->hasOption( 'force' );
56 $user = User
::newFromName( $username );
57 if ( !is_object( $user ) ) {
58 $this->error( "invalid username.", true );
61 $exists = ( 0 !== $user->idForName() );
63 if ( $exists && !$force ) {
64 $this->error( "Account exists. Perhaps you want the --force option?", true );
65 } elseif ( !$exists && !$password ) {
66 $this->error( "Argument <password> required!", false );
67 $this->maybeHelp( true );
68 } elseif ( $exists ) {
69 $inGroups = $user->getGroups();
72 $promotions = array_diff(
73 array_filter( self
::$permitRoles, array( $this, 'hasOption' ) ),
77 if ( $exists && !$password && count( $promotions ) === 0 ) {
78 $this->output( "Account exists and nothing to do.\n" );
81 } elseif ( count( $promotions ) !== 0 ) {
82 $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
84 $this->output( wfWikiID() . ": Promoting $promoText" );
86 $this->output( wfWikiID() . ": Creating and promoting $promoText" );
91 # Try to set the password
93 $user->setPassword( $password );
95 $this->output( "Password set.\n" );
96 $user->saveSettings();
98 } catch ( PasswordError
$pwe ) {
99 $this->error( $pwe->getText(), true );
104 # Insert the account into the database
105 $user->addToDatabase();
106 $user->saveSettings();
110 array_map( array( $user, 'addGroup' ), $promotions );
113 # Increment site_stats.ss_users
114 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
118 $this->output( "done.\n" );
122 $maintClass = "CreateAndPromote";
123 require_once RUN_MAINTENANCE_IF_MAIN
;