r51729 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r51728‎ | r51729 | r51730 >
Date:05:24, 11 June 2009
Author:demon
Status:deferred
Tags:
Comment:
* Allow passing an arg list for things we want. Right now only used in help, but maybe param validation later?
* Also check for arg 'help', not just param help
* Port createAndPromote to new format. Works perfectly.
Modified paths:
  • /branches/maintenance-work/maintenance/Maintenance.php (modified) (history)
  • /branches/maintenance-work/maintenance/createAndPromote.php (modified) (history)

Diff [purge]

Index: branches/maintenance-work/maintenance/Maintenance.php
@@ -1,5 +1,4 @@
22 <?php
3 -
43 /**
54 * Abstract maintenance class for quickly writing and churning out
65 * maintenance scripts with minimal effort. All that _must_ be defined
@@ -14,6 +13,9 @@
1514
1615 // This is the desired params
1716 protected $mParams = array();
 17+
 18+ // Array of desired args
 19+ protected $mArgList = array();
1820
1921 // This is the list of options that were actually passed
2022 protected $mOptions = array();
@@ -55,6 +57,15 @@
5658 protected function addParam( $name, $description, $required = false ) {
5759 $this->mParams[ $name ] = array( 'desc' => $description, 'require' => $required );
5860 }
 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+ }
5970
6071 /**
6172 * Return input from stdin.
@@ -97,7 +108,8 @@
98109
99110 /**
100111 * 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
102114 * @return boolean
103115 */
104116 protected function needsDB() {
@@ -265,16 +277,18 @@
266278 * Maybe show the help.
267279 */
268280 private function maybeHelp() {
269 - if( isset( $this->mOptions['help'] ) ) {
 281+ if( isset( $this->mOptions['help'] ) || in_array( 'help', $this->mArgs ) ) {
270282 $this->mQuiet = false;
271283 if( $this->mDescription ) {
272284 $this->output( $this->mDescription . "\n" );
273285 }
274286 $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" );
278291 }
 292+ die(1);
279293 }
280294 }
281295
Index: branches/maintenance-work/maintenance/createAndPromote.php
@@ -8,61 +8,50 @@
99 * @author Rob Church <robchur@gmail.com>
1010 */
1111
12 -$options = array( 'help', 'bureaucrat' );
13 -require_once( 'commandLine.inc' );
 12+require_once( "Maintenance.php" );
1413
15 -if( isset( $options['help'] ) ) {
16 - showHelp();
17 - exit( 1 );
18 -}
 14+class CreateAndPromote extends Maintenance {
1915
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+ }
2422
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+ }
3855 }
3956
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" );

Status & tagging log