(bug 19157) createAndPromote error on bad password
[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 * @file
7 * @ingroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
10
11 $options = array( 'help', 'bureaucrat' );
12 require_once( 'commandLine.inc' );
13
14 if( isset( $options['help'] ) ) {
15 showHelp();
16 exit( 1 );
17 }
18
19 if( count( $args ) < 2 ) {
20 echo( "Please provide a username and password for the new account.\n" );
21 die( 1 );
22 }
23
24 $username = $args[0];
25 $password = $args[1];
26
27 echo( wfWikiID() . ": Creating and promoting User:{$username}..." );
28
29 # Validate username and check it doesn't exist
30 $user = User::newFromName( $username );
31 if( !is_object( $user ) ) {
32 echo( "invalid username.\n" );
33 die( 1 );
34 } elseif( 0 != $user->idForName() ) {
35 echo( "account exists.\n" );
36 die( 1 );
37 }
38
39 try {
40 $user->setPassword( $password );
41 } catch( PasswordError $pwe ) {
42 $this->error( $pwe->getText(), true );
43 }
44
45 # Insert the account into the database
46 $user->addToDatabase();
47 $user->saveSettings();
48
49 # Promote user
50 $user->addGroup( 'sysop' );
51 if( isset( $option['bureaucrat'] ) )
52 $user->addGroup( 'bureaucrat' );
53
54 # Increment site_stats.ss_users
55 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
56 $ssu->doUpdate();
57
58 echo( "done.\n" );
59
60 function showHelp() {
61 echo( <<<EOT
62 Create a new user account with administrator rights
63
64 USAGE: php createAndPromote.php [--bureaucrat|--help] <username> <password>
65
66 --bureaucrat
67 Grant the account bureaucrat rights
68 --help
69 Show this help information
70
71 EOT
72 );
73 }