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