Index: branches/maintenance-work/maintenance/Maintenance.php |
— | — | @@ -1,5 +1,4 @@ |
2 | 2 | <?php |
3 | | - |
4 | 3 | /** |
5 | 4 | * Abstract maintenance class for quickly writing and churning out |
6 | 5 | * maintenance scripts with minimal effort. All that _must_ be defined |
— | — | @@ -14,6 +13,9 @@ |
15 | 14 | |
16 | 15 | // This is the desired params |
17 | 16 | protected $mParams = array(); |
| 17 | + |
| 18 | + // Array of desired args |
| 19 | + protected $mArgList = array(); |
18 | 20 | |
19 | 21 | // This is the list of options that were actually passed |
20 | 22 | protected $mOptions = array(); |
— | — | @@ -55,6 +57,15 @@ |
56 | 58 | protected function addParam( $name, $description, $required = false ) { |
57 | 59 | $this->mParams[ $name ] = array( 'desc' => $description, 'require' => $required ); |
58 | 60 | } |
| 61 | + |
| 62 | + /** |
| 63 | + * Add some args that are needed. Used in formatting help |
| 64 | + */ |
| 65 | + protected function addArgs( $args ) { |
| 66 | + foreach( $args as $arg ) { |
| 67 | + $this->mArgList[] = $arg; |
| 68 | + } |
| 69 | + } |
59 | 70 | |
60 | 71 | /** |
61 | 72 | * Return input from stdin. |
— | — | @@ -97,7 +108,8 @@ |
98 | 109 | |
99 | 110 | /** |
100 | 111 | * Does the script need DB access? Specifically, we mean admin access to |
101 | | - * the DB. Override this and return true, if needed |
| 112 | + * the DB. Override this and return true, |
| 113 | + * if needed |
102 | 114 | * @return boolean |
103 | 115 | */ |
104 | 116 | protected function needsDB() { |
— | — | @@ -265,16 +277,18 @@ |
266 | 278 | * Maybe show the help. |
267 | 279 | */ |
268 | 280 | private function maybeHelp() { |
269 | | - if( isset( $this->mOptions['help'] ) ) { |
| 281 | + if( isset( $this->mOptions['help'] ) || in_array( 'help', $this->mArgs ) ) { |
270 | 282 | $this->mQuiet = false; |
271 | 283 | if( $this->mDescription ) { |
272 | 284 | $this->output( $this->mDescription . "\n" ); |
273 | 285 | } |
274 | 286 | $this->output( "\nUsage: php " . $this->mSelf . " [--" . |
275 | | - implode( array_keys( $this->mParams ), "|--" ) . "]\n" ); |
276 | | - foreach( $params as $par => $desc ) { |
277 | | - $this->output( "\t$par : $desc\n" ); |
| 287 | + implode( array_keys( $this->mParams ), "|--" ) . "] <" . |
| 288 | + implode( $this->mArgList, "> <" ) . ">\n" ); |
| 289 | + foreach( $this->mParams as $par => $info ) { |
| 290 | + $this->output( "\t$par : " . $info['desc'] . "\n" ); |
278 | 291 | } |
| 292 | + die(1); |
279 | 293 | } |
280 | 294 | } |
281 | 295 | |
Index: branches/maintenance-work/maintenance/createAndPromote.php |
— | — | @@ -8,61 +8,50 @@ |
9 | 9 | * @author Rob Church <robchur@gmail.com> |
10 | 10 | */ |
11 | 11 | |
12 | | -$options = array( 'help', 'bureaucrat' ); |
13 | | -require_once( 'commandLine.inc' ); |
| 12 | +require_once( "Maintenance.php" ); |
14 | 13 | |
15 | | -if( isset( $options['help'] ) ) { |
16 | | - showHelp(); |
17 | | - exit( 1 ); |
18 | | -} |
| 14 | +class CreateAndPromote extends Maintenance { |
19 | 15 | |
20 | | -if( count( $args ) < 2 ) { |
21 | | - echo( "Please provide a username and password for the new account.\n" ); |
22 | | - die( 1 ); |
23 | | -} |
| 16 | + public function __construct() { |
| 17 | + parent::__construct(); |
| 18 | + $this->mDescription = "Create a new user account with administrator rights"; |
| 19 | + $this->addParam( "bureaucrat", "Grant the account bureaucrat rights" ); |
| 20 | + $this->addArgs( array( "username", "password" ) ); |
| 21 | + } |
24 | 22 | |
25 | | -$username = $args[0]; |
26 | | -$password = $args[1]; |
27 | | - |
28 | | -echo( wfWikiID() . ": Creating and promoting User:{$username}..." ); |
29 | | - |
30 | | -# Validate username and check it doesn't exist |
31 | | -$user = User::newFromName( $username ); |
32 | | -if( !is_object( $user ) ) { |
33 | | - echo( "invalid username.\n" ); |
34 | | - die( 1 ); |
35 | | -} elseif( 0 != $user->idForName() ) { |
36 | | - echo( "account exists.\n" ); |
37 | | - die( 1 ); |
| 23 | + public function execute() { |
| 24 | + if( count( $this->mArgs ) < 2 ) { |
| 25 | + $this->error( "Please provide a username and password for the new account.\n", true ); |
| 26 | + } |
| 27 | + $username = $this->mArgs[0]; |
| 28 | + $password = $this->mArgs[1]; |
| 29 | + |
| 30 | + $this->output( wfWikiID() . ": Creating and promoting User:{$username}..." ); |
| 31 | + |
| 32 | + $user = User::newFromName( $username ); |
| 33 | + if( !is_object( $user ) ) { |
| 34 | + $this->error( "invalid username.\n", true ); |
| 35 | + } elseif( 0 != $user->idForName() ) { |
| 36 | + $this->error( "account exists.\n", true ); |
| 37 | + } |
| 38 | + |
| 39 | + # Insert the account into the database |
| 40 | + $user->addToDatabase(); |
| 41 | + $user->setPassword( $password ); |
| 42 | + $user->saveSettings(); |
| 43 | + |
| 44 | + # Promote user |
| 45 | + $user->addGroup( 'sysop' ); |
| 46 | + if( isset( $this->mOptions['bureaucrat'] ) ) |
| 47 | + $user->addGroup( 'bureaucrat' ); |
| 48 | + |
| 49 | + # Increment site_stats.ss_users |
| 50 | + $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 ); |
| 51 | + $ssu->doUpdate(); |
| 52 | + |
| 53 | + $this->output( "done.\n" ); |
| 54 | + } |
38 | 55 | } |
39 | 56 | |
40 | | -# Insert the account into the database |
41 | | -$user->addToDatabase(); |
42 | | -$user->setPassword( $password ); |
43 | | -$user->saveSettings(); |
44 | | - |
45 | | -# Promote user |
46 | | -$user->addGroup( 'sysop' ); |
47 | | -if( isset( $option['bureaucrat'] ) ) |
48 | | - $user->addGroup( 'bureaucrat' ); |
49 | | - |
50 | | -# Increment site_stats.ss_users |
51 | | -$ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 ); |
52 | | -$ssu->doUpdate(); |
53 | | - |
54 | | -echo( "done.\n" ); |
55 | | - |
56 | | -function showHelp() { |
57 | | - echo( <<<EOT |
58 | | -Create a new user account with administrator rights |
59 | | - |
60 | | -USAGE: php createAndPromote.php [--bureaucrat|--help] <username> <password> |
61 | | - |
62 | | - --bureaucrat |
63 | | - Grant the account bureaucrat rights |
64 | | - --help |
65 | | - Show this help information |
66 | | - |
67 | | -EOT |
68 | | - ); |
69 | | -} |
\ No newline at end of file |
| 57 | +$maintClass = "CreateAndPromote"; |
| 58 | +require_once( "doMaintenance.php" ); |