Add ImgAuthModifyHeaders hook to img_auth.php to modify headers
[lhc/web/wiklou.git] / includes / shell / Shell.php
index 6e4fd02..742e142 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();
@@ -45,13 +47,13 @@ class Shell {
         * Apply a default set of restrictions for improved
         * security out of the box.
         *
-        * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV
+        * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV | NO_LOCALSETTINGS
         *
         * @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;
+       const RESTRICT_DEFAULT = 39;
 
        /**
         * Disallow any root access. Any setuid binaries
@@ -92,6 +94,20 @@ class Shell {
         */
        const NO_EXECVE = 16;
 
+       /**
+        * Deny access to LocalSettings.php (MW_CONFIG_FILE)
+        *
+        * @since 1.31
+        */
+       const NO_LOCALSETTINGS = 32;
+
+       /**
+        * Don't apply any restrictions
+        *
+        * @since 1.31
+        */
+       const RESTRICT_NONE = 0;
+
        /**
         * Returns a new instance of Command class
         *
@@ -142,7 +158,7 @@ class Shell {
         * 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
+        *     strings parameter. Null values are ignored.
         * @return string
         */
        public static function escape( /* ... */ ) {
@@ -156,6 +172,9 @@ class Shell {
                $first = true;
                $retVal = '';
                foreach ( $args as $arg ) {
+                       if ( $arg === null ) {
+                               continue;
+                       }
                        if ( !$first ) {
                                $retVal .= ' ';
                        } else {
@@ -201,4 +220,32 @@ 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").
+        *
+        * @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 );
+       }
 }