Don't put \n on the end of every error() call, just do it in error() itself. Still...
[lhc/web/wiklou.git] / maintenance / createAndPromote.php
1 <?php
2
3 /**
4 * Maintenance script to create an account and grant it administrator rights
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 */
24
25 require_once( "Maintenance.php" );
26
27 class CreateAndPromote extends Maintenance {
28
29 public function __construct() {
30 parent::__construct();
31 $this->mDescription = "Create a new user account with administrator rights";
32 $this->addOption( "bureaucrat", "Grant the account bureaucrat rights" );
33 $this->addArgs( array( "username", "password" ) );
34 }
35
36 public function execute() {
37 $username = $this->getArg(0);
38 $password = $this->getArg(1);
39
40 $this->output( wfWikiID() . ": Creating and promoting User:{$username}..." );
41
42 $user = User::newFromName( $username );
43 if( !is_object( $user ) ) {
44 $this->error( "invalid username.", true );
45 } elseif( 0 != $user->idForName() ) {
46 $this->error( "account exists.", true );
47 }
48
49 # Try to set the password
50 try {
51 $user->setPassword( $password );
52 } catch( PasswordError $pwe ) {
53 $this->error( $pwe->getText(), true );
54 }
55
56 # Insert the account into the database
57 $user->addToDatabase();
58 $user->saveSettings();
59
60 # Promote user
61 $user->addGroup( 'sysop' );
62 if( $this->hasOption( 'bureaucrat' ) )
63 $user->addGroup( 'bureaucrat' );
64
65 # Increment site_stats.ss_users
66 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
67 $ssu->doUpdate();
68
69 $this->output( "done.\n" );
70 }
71 }
72
73 $maintClass = "CreateAndPromote";
74 require_once( DO_MAINTENANCE );