Add unit test for bug 32888
[lhc/web/wiklou.git] / maintenance / createAndPromote.php
1 <?php
2 /**
3 * Maintenance script to create 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 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class CreateAndPromote extends Maintenance {
28
29 public function __construct() {
30 parent::__construct();
31 $this->mDescription = "Create a new user account";
32 $this->addOption( "sysop", "Grant the account sysop rights" );
33 $this->addOption( "bureaucrat", "Grant the account bureaucrat rights" );
34 $this->addArg( "username", "Username of new user" );
35 $this->addArg( "password", "Password to set" );
36 }
37
38 public function execute() {
39 $username = $this->getArg( 0 );
40 $password = $this->getArg( 1 );
41
42 $this->output( wfWikiID() . ": Creating and promoting User:{$username}..." );
43
44 $user = User::newFromName( $username );
45 if ( !is_object( $user ) ) {
46 $this->error( "invalid username.", true );
47 } elseif ( 0 != $user->idForName() ) {
48 $this->error( "account exists.", true );
49 }
50
51 # Try to set the password
52 try {
53 $user->setPassword( $password );
54 } catch ( PasswordError $pwe ) {
55 $this->error( $pwe->getText(), true );
56 }
57
58 # Insert the account into the database
59 $user->addToDatabase();
60 $user->saveSettings();
61
62 # Promote user
63 if ( $this->hasOption( 'sysop' ) ) {
64 $user->addGroup( 'sysop' );
65 }
66 if ( $this->hasOption( 'bureaucrat' ) ) {
67 $user->addGroup( 'bureaucrat' );
68 }
69
70 # Increment site_stats.ss_users
71 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
72 $ssu->doUpdate();
73
74 $this->output( "done.\n" );
75 }
76 }
77
78 $maintClass = "CreateAndPromote";
79 require_once( RUN_MAINTENANCE_IF_MAIN );