Merge "Fix test database prefix in ParserTestTopLevelSuite"
[lhc/web/wiklou.git] / includes / shell / Shell.php
index 084e10e..0ddc443 100644 (file)
@@ -22,6 +22,7 @@
 
 namespace MediaWiki\Shell;
 
+use Hooks;
 use MediaWiki\MediaWikiServices;
 
 /**
@@ -31,6 +32,7 @@ use MediaWiki\MediaWikiServices;
  *
  * Use call chaining with this class for expressiveness:
  *  $result = Shell::command( 'some command' )
+ *       ->input( 'foo' )
  *       ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
  *       ->limits( [ 'time' => 300 ] )
  *       ->execute();
@@ -41,18 +43,6 @@ use MediaWiki\MediaWikiServices;
  */
 class Shell {
 
-       /**
-        * Apply a default set of restrictions for improved
-        * security out of the box.
-        *
-        * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV
-        *
-        * @note This value will change over time to provide increased security
-        *       by default, and is not guaranteed to be backwards-compatible.
-        * @since 1.31
-        */
-       const RESTRICT_DEFAULT = 7;
-
        /**
         * Disallow any root access. Any setuid binaries
         * will be run without elevated access.
@@ -92,26 +82,51 @@ class Shell {
         */
        const NO_EXECVE = 16;
 
+       /**
+        * Deny access to LocalSettings.php (MW_CONFIG_FILE)
+        *
+        * @since 1.31
+        */
+       const NO_LOCALSETTINGS = 32;
+
+       /**
+        * Apply a default set of restrictions for improved
+        * security out of the box.
+        *
+        * @note This value will change over time to provide increased security
+        *       by default, and is not guaranteed to be backwards-compatible.
+        * @since 1.31
+        */
+       const RESTRICT_DEFAULT = self::NO_ROOT | self::SECCOMP | self::PRIVATE_DEV |
+                                                        self::NO_LOCALSETTINGS;
+
+       /**
+        * Don't apply any restrictions
+        *
+        * @since 1.31
+        */
+       const RESTRICT_NONE = 0;
+
        /**
         * Returns a new instance of Command class
         *
-        * @param string|string[] $command String or array of strings representing the command to
+        * @note You should check Shell::isDisabled() before calling this
+        * @param string|string[] ...$commands String or array of strings representing the command to
         * be executed, each value will be escaped.
         *   Example:   [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
         * @return Command
         */
-       public static function command( $command ) {
-               $args = func_get_args();
-               if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
+       public static function command( ...$commands ) {
+               if ( count( $commands ) === 1 && is_array( reset( $commands ) ) ) {
                        // If only one argument has been passed, and that argument is an array,
                        // treat it as a list of arguments
-                       $args = reset( $args );
+                       $commands = reset( $commands );
                }
                $command = MediaWikiServices::getInstance()
                        ->getShellCommandFactory()
                        ->create();
 
-               return $command->params( $args );
+               return $command->params( $commands );
        }
 
        /**
@@ -141,12 +156,11 @@ class Shell {
         * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
         * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
         *
-        * @param string $args,... strings to escape and glue together, or a single array of
-        *     strings parameter. Null values are ignored.
+        * @param string|string[] ...$args strings to escape and glue together, or a single
+        *     array of strings parameter. Null values are ignored.
         * @return string
         */
-       public static function escape( /* ... */ ) {
-               $args = func_get_args();
+       public static function escape( ...$args ) {
                if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
                        // If only one argument has been passed, and that argument is an array,
                        // treat it as a list of arguments
@@ -204,4 +218,33 @@ class Shell {
                }
                return $retVal;
        }
+
+       /**
+        * Generate a Command object to run a MediaWiki CLI script.
+        * Note that $parameters should be a flat array and an option with an argument
+        * should consist of two consecutive items in the array (do not use "--option value").
+        *
+        * @note You should check Shell::isDisabled() before calling this
+        * @param string $script MediaWiki CLI script with full path
+        * @param string[] $parameters Arguments and options to the script
+        * @param array $options Associative array of options:
+        *     'php': The path to the php executable
+        *     'wrapper': Path to a PHP wrapper to handle the maintenance script
+        * @return Command
+        */
+       public static function makeScriptCommand( $script, $parameters, $options = [] ) {
+               global $wgPhpCli;
+               // Give site config file a chance to run the script in a wrapper.
+               // The caller may likely want to call wfBasename() on $script.
+               Hooks::run( 'wfShellWikiCmd', [ &$script, &$parameters, &$options ] );
+               $cmd = isset( $options['php'] ) ? [ $options['php'] ] : [ $wgPhpCli ];
+               if ( isset( $options['wrapper'] ) ) {
+                       $cmd[] = $options['wrapper'];
+               }
+               $cmd[] = $script;
+
+               return self::command( $cmd )
+                       ->params( $parameters )
+                       ->restrict( self::RESTRICT_DEFAULT & ~self::NO_LOCALSETTINGS );
+       }
 }