diff options
author | Bill Pirkle <bpirkle@wikimedia.org> | 2018-08-02 15:10:31 -0500 |
---|---|---|
committer | Bill Pirkle <bpirkle@wikimedia.org> | 2018-08-06 14:37:49 -0500 |
commit | 2bd7259a2c88a2bcc30a9217770a64268e161305 (patch) | |
tree | 163252e7f24610d1c9bb631adfa8bade87b4a6ff | |
parent | a14133e55c0d4bceca030a9bdb373f3ff84c15ed (diff) | |
download | mediawikicore-2bd7259a2c88a2bcc30a9217770a64268e161305.tar.gz mediawikicore-2bd7259a2c88a2bcc30a9217770a64268e161305.zip |
Make maintenance scripts fail on unknown parameters
Passing parameters not registered via standard mechanisms
(addOption/$optionsWithArgs/$optionsWihtoutArgs) will now
cause an error, unless, the script opts out via the new
setAllowUnregisteredOptions/$allowUnregisteredOptions.
Bug: T110209
Change-Id: I21957837f10852169ca3e1eeca9bf1f4052f8c0b
-rw-r--r-- | maintenance/Maintenance.php | 35 | ||||
-rw-r--r-- | maintenance/commandLine.inc | 10 | ||||
-rwxr-xr-x | tests/phpunit/phpunit.php | 1 |
3 files changed, 42 insertions, 4 deletions
diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index d919249030ba..286bd8f1d6c3 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -85,6 +85,9 @@ abstract class Maintenance { // This is the list of arguments that were actually passed protected $mArgs = []; + // Allow arbitrary options to be passed, or only specified ones? + protected $mAllowUnregisteredOptions = false; + // Name of the script currently running protected $mSelf; @@ -211,6 +214,16 @@ abstract class Maintenance { abstract public function execute(); /** + * Checks to see if a particular option in supported. Normally this means it + * has been registered by the script via addOption. + * @param string $name The name of the option + * @return bool true if the option exists, false otherwise + */ + protected function supportsOption( $name ) { + return isset( $this->mParams[$name] ); + } + + /** * Add a parameter to the script. Will be displayed on --help * with the associated description * @@ -238,8 +251,8 @@ abstract class Maintenance { } /** - * Checks to see if a particular param exists. - * @param string $name The name of the param + * Checks to see if a particular option exists. + * @param string $name The name of the option * @return bool */ protected function hasOption( $name ) { @@ -290,6 +303,15 @@ abstract class Maintenance { } /** + * Sets whether to allow unregistered options, which are options passed to + * a script that do not match an expected parameter. + * @param bool $allow Should we allow? + */ + protected function setAllowUnregisteredOptions( $allow ) { + $this->mAllowUnregisteredOptions = $allow; + } + + /** * Set the description text. * @param string $text The text of the description */ @@ -974,6 +996,15 @@ abstract class Maintenance { $die = true; } } + if ( !$this->mAllowUnregisteredOptions ) { + # Check for unexpected options + foreach ( $this->mOptions as $opt => $val ) { + if ( !$this->supportsOption( $opt ) ) { + $this->error( "Unexpected option $opt!" ); + $die = true; + } + } + } if ( $die ) { $this->maybeHelp( true ); diff --git a/maintenance/commandLine.inc b/maintenance/commandLine.inc index 8232d529470a..bb1443f6ebec 100644 --- a/maintenance/commandLine.inc +++ b/maintenance/commandLine.inc @@ -24,7 +24,7 @@ require_once __DIR__ . '/Maintenance.php'; // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix -global $optionsWithArgs, $optionsWithoutArgs; +global $optionsWithArgs, $optionsWithoutArgs, $allowUnregisteredOptions; if ( !isset( $optionsWithArgs ) ) { $optionsWithArgs = []; @@ -32,19 +32,25 @@ if ( !isset( $optionsWithArgs ) ) { if ( !isset( $optionsWithoutArgs ) ) { $optionsWithoutArgs = []; } +if ( !isset( $allowUnregisteredOptions ) ) { + $allowUnregisteredOptions = false; +} class CommandLineInc extends Maintenance { public function __construct() { // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix - global $optionsWithArgs, $optionsWithoutArgs; + global $optionsWithArgs, $optionsWithoutArgs, $allowUnregisteredOptions; parent::__construct(); + foreach ( $optionsWithArgs as $name ) { $this->addOption( $name, '', false, true ); } foreach ( $optionsWithoutArgs as $name ) { $this->addOption( $name, '', false, false ); } + + $this->setAllowUnregisteredOptions( $allowUnregisteredOptions ); } /** diff --git a/tests/phpunit/phpunit.php b/tests/phpunit/phpunit.php index 7cf042d0b803..d83dedba4102 100755 --- a/tests/phpunit/phpunit.php +++ b/tests/phpunit/phpunit.php @@ -29,6 +29,7 @@ class PHPUnitMaintClass extends Maintenance { public function __construct() { parent::__construct(); + $this->setAllowUnregisteredOptions( true ); $this->addOption( 'with-phpunitclass', 'Class name of the PHPUnit entry point to use', |