Bug 35623 - createAndPromote.php: Change to allow promotion only
[lhc/web/wiklou.git] / maintenance / createAndPromote.php
1 <?php
2 /**
3 * Creates an account and grant it administrator rights.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @author Pablo Castellano <pablo@anche.no>
24 */
25
26 require_once( __DIR__ . '/Maintenance.php' );
27
28 /**
29 * Maintenance script to create an account and grant it administrator rights.
30 *
31 * @ingroup Maintenance
32 */
33 class CreateAndPromote extends Maintenance {
34
35 static $permitRoles = array( 'sysop', 'bureaucrat' );
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 } else if ( !$exists && !$password ) {
64 $this->error( "Argument <password> required!", false );
65 $this->maybeHelp( true );
66 } else if ( $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 } else if ( 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 );