Merge "Whitelist the <wbr> element."
[mediawiki.git] / maintenance / createAndPromote.php
blob03e690475a987036ddb62230af5788caabec033a
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @author Pablo Castellano <pablo@anche.no>
26 require_once __DIR__ . '/Maintenance.php';
28 /**
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' );
52 $inGroups = array();
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" );
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" );
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();
92 } catch ( PasswordError $pwe ) {
93 $this->error( $pwe->getText(), true );
97 if ( !$exists ) {
98 # Insert the account into the database
99 $user->addToDatabase();
100 $user->saveSettings();
103 # Promote user
104 array_map( array( $user, 'addGroup' ), $promotions );
106 if ( !$exists ) {
107 # Increment site_stats.ss_users
108 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
109 $ssu->doUpdate();
112 $this->output( "done.\n" );
116 $maintClass = "CreateAndPromote";
117 require_once RUN_MAINTENANCE_IF_MAIN;