Use StaticArrayWriter in LCStoreStaticArray
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index 866f959..6377734 100644 (file)
@@ -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;
 
@@ -210,6 +213,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 ) {
@@ -253,7 +266,7 @@ abstract class Maintenance {
         * this will return an array.
         *
         * @param string $name The name of the param
-        * @param mixed $default Anything you want, default null
+        * @param mixed|null $default Anything you want, default null
         * @return mixed
         */
        protected function getOption( $name, $default = null ) {
@@ -289,6 +302,15 @@ abstract class Maintenance {
                unset( $this->mParams[$name] );
        }
 
+       /**
+        * 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
@@ -309,7 +331,7 @@ abstract class Maintenance {
        /**
         * Get an argument.
         * @param int $argId The integer value (from zero) for the arg
-        * @param mixed $default The default if it doesn't exist
+        * @param mixed|null $default The default if it doesn't exist
         * @return mixed
         */
        protected function getArg( $argId = 0, $default = null ) {
@@ -359,7 +381,7 @@ abstract class Maintenance {
 
        /**
         * Return input from stdin.
-        * @param int $len The number of bytes to read. If null, just return the handle.
+        * @param int|null $len The number of bytes to read. If null, just return the handle.
         *   Maintenance::STDIN_ALL returns the full length
         * @return mixed
         */
@@ -388,7 +410,7 @@ abstract class Maintenance {
         * Throw some output to the user. Scripts can call this with no fears,
         * as we handle all --quiet stuff here
         * @param string $out The text to show to the user
-        * @param mixed $channel Unique identifier for the channel. See function outputChanneled.
+        * @param mixed|null $channel Unique identifier for the channel. See function outputChanneled.
         */
        protected function output( $out, $channel = null ) {
                // This is sometimes called very early, before Setup.php is included.
@@ -464,7 +486,7 @@ abstract class Maintenance {
         * same channel are concatenated, but any intervening messages in another
         * channel start a new line.
         * @param string $msg The message without trailing newline
-        * @param string $channel Channel identifier or null for no
+        * @param string|null $channel Channel identifier or null for no
         *     channel. Channel comparison uses ===.
         */
        public function outputChanneled( $msg, $channel = null ) {
@@ -511,7 +533,7 @@ abstract class Maintenance {
                # Generic (non script dependant) options:
 
                $this->addOption( 'help', 'Display this help message', false, false, 'h' );
-               $this->addOption( 'quiet', 'Whether to supress non-error output', false, false, 'q' );
+               $this->addOption( 'quiet', 'Whether to suppress non-error output', false, false, 'q' );
                $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true );
                $this->addOption( 'wiki', 'For specifying the wiki ID', false, true );
                $this->addOption( 'globals', 'Output globals at the end of processing for debugging' );
@@ -661,7 +683,7 @@ abstract class Maintenance {
         * Run a child maintenance script. Pass all of the current arguments
         * to it.
         * @param string $maintClass A name of a child maintenance class
-        * @param string $classFile Full path of where the child is
+        * @param string|null $classFile Full path of where the child is
         * @return Maintenance
         */
        public function runChild( $maintClass, $classFile = null ) {
@@ -922,9 +944,9 @@ abstract class Maintenance {
         * $mOptions becomes an array with keys set to the option names
         * $mArgs becomes a zero-based array containing the non-option arguments
         *
-        * @param string $self The name of the script, if any
-        * @param array $opts An array of options, in form of key=>value
-        * @param array $args An array of command line arguments
+        * @param string|null $self The name of the script, if any
+        * @param array|null $opts An array of options, in form of key=>value
+        * @param array|null $args An array of command line arguments
         */
        public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
                # If we were given opts or args, set those and return early
@@ -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 );
@@ -1118,7 +1149,7 @@ abstract class Maintenance {
         * Handle some last-minute setup here.
         */
        public function finalSetup() {
-               global $wgCommandLineMode, $wgShowSQLErrors, $wgServer;
+               global $wgCommandLineMode, $wgServer, $wgShowExceptionDetails, $wgShowHostnames;
                global $wgDBadminuser, $wgDBadminpassword, $wgDBDefaultGroup;
                global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
 
@@ -1177,7 +1208,8 @@ abstract class Maintenance {
 
                $this->afterFinalSetup();
 
-               $wgShowSQLErrors = true;
+               $wgShowExceptionDetails = true;
+               $wgShowHostnames = true;
 
                Wikimedia\suppressWarnings();
                set_time_limit( 0 );