Localisation updates from https://translatewiki.net.
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index 8dad5dc..185a1f4 100644 (file)
@@ -22,7 +22,9 @@
 
 // Bail on old versions of PHP, or if composer has not been run yet to install
 // dependencies. Using dirname( __FILE__ ) here because __DIR__ is PHP5.3+.
+// @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound
 require_once dirname( __FILE__ ) . '/../includes/PHPVersionCheck.php';
+// @codingStandardsIgnoreEnd
 wfEntryPointCheck( 'cli' );
 
 /**
@@ -120,6 +122,18 @@ abstract class Maintenance {
         */
        private $config;
 
+       /**
+        * Used to read the options in the order
+        * they were passed. Useful for option
+        * chaining. (Ex. dumpBackup.php)
+        *
+        * This is an array of arrays where
+        * 0 => the option and 1 => parameter value
+        *
+        * @var array
+        */
+       public $orderedOptions = array();
+
        /**
         * Default constructor. Children should call this *first* if implementing
         * their own constructors
@@ -182,15 +196,17 @@ abstract class Maintenance {
         * @param bool $required Is the param required?
         * @param bool $withArg Is an argument required with this option?
         * @param string $shortName Character to use as short name
+        * @param bool $multiOccurrence Can this option be passed multiple times?
         */
        protected function addOption( $name, $description, $required = false,
-               $withArg = false, $shortName = false
+               $withArg = false, $shortName = false, $multiOccurrence = false
        ) {
                $this->mParams[$name] = array(
                        'desc' => $description,
                        'require' => $required,
                        'withArg' => $withArg,
-                       'shortName' => $shortName
+                       'shortName' => $shortName,
+                       'multiOccurrence' => $multiOccurrence
                );
 
                if ( $shortName !== false ) {
@@ -208,7 +224,11 @@ abstract class Maintenance {
        }
 
        /**
-        * Get an option, or return the default
+        * Get an option, or return the default.
+        *
+        * If the option was added to support multiple occurrences,
+        * this will return an array.
+        *
         * @param string $name The name of the param
         * @param mixed $default Anything you want, default null
         * @return mixed
@@ -671,6 +691,7 @@ abstract class Maintenance {
 
                $options = array();
                $args = array();
+               $this->orderedOptions = array();
 
                # Parse arguments
                for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
@@ -685,17 +706,14 @@ abstract class Maintenance {
                        } elseif ( substr( $arg, 0, 2 ) == '--' ) {
                                # Long options
                                $option = substr( $arg, 2 );
-                               if ( array_key_exists( $option, $options ) ) {
-                                       $this->error( "\nERROR: $option parameter given twice\n" );
-                                       $this->maybeHelp( true );
-                               }
                                if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) {
                                        $param = next( $argv );
                                        if ( $param === false ) {
                                                $this->error( "\nERROR: $option parameter needs a value after it\n" );
                                                $this->maybeHelp( true );
                                        }
-                                       $options[$option] = $param;
+
+                                       $this->setParam( $options, $option, $param );
                                } else {
                                        $bits = explode( '=', $option, 2 );
                                        if ( count( $bits ) > 1 ) {
@@ -704,7 +722,8 @@ abstract class Maintenance {
                                        } else {
                                                $param = 1;
                                        }
-                                       $options[$option] = $param;
+
+                                       $this->setParam( $options, $option, $param );
                                }
                        } elseif ( $arg == '-' ) {
                                # Lonely "-", often used to indicate stdin or stdout.
@@ -717,19 +736,16 @@ abstract class Maintenance {
                                        if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) {
                                                $option = $this->mShortParamsMap[$option];
                                        }
-                                       if ( array_key_exists( $option, $options ) ) {
-                                               $this->error( "\nERROR: $option parameter given twice\n" );
-                                               $this->maybeHelp( true );
-                                       }
+
                                        if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) {
                                                $param = next( $argv );
                                                if ( $param === false ) {
                                                        $this->error( "\nERROR: $option parameter needs a value after it\n" );
                                                        $this->maybeHelp( true );
                                                }
-                                               $options[$option] = $param;
+                                               $this->setParam( $options, $option, $param );
                                        } else {
-                                               $options[$option] = 1;
+                                               $this->setParam( $options, $option, 1 );
                                        }
                                }
                        } else {
@@ -743,6 +759,37 @@ abstract class Maintenance {
                $this->mInputLoaded = true;
        }
 
+       /**
+        * Helper function used solely by loadParamsAndArgs
+        * to prevent code duplication
+        *
+        * This sets the param in the options array based on
+        * whether or not it can be specified multiple times.
+        *
+        * @since 1.27
+        * @param array $options
+        * @param string $option
+        * @param mixed $value
+        */
+       private function setParam( &$options, $option, $value ) {
+               $this->orderedOptions[] = array( $option, $value );
+
+               if ( isset( $this->mParams[$option] ) ) {
+                       $multi = $this->mParams[$option]['multiOccurrence'];
+                       $exists = array_key_exists( $option, $options );
+                       if ( $multi && $exists ) {
+                               $options[$option][] = $value;
+                       } elseif ( $multi ) {
+                               $options[$option] = array( $value );
+                       } elseif ( !$exists ) {
+                               $options[$option] = $value;
+                       } else {
+                               $this->error( "\nERROR: $option parameter given twice\n" );
+                               $this->maybeHelp( true );
+                       }
+               }
+       }
+
        /**
         * Run some validation checks on the params, etc
         */