From: Aaron Schulz Date: Fri, 23 Sep 2011 20:42:22 +0000 (+0000) Subject: Added wfShellMaintenanceCmd() for Het Deploy support X-Git-Tag: 1.31.0-rc.0~27456 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=122a14140ace34c869b2292ee5e7ddb18ab0dcb8;p=lhc%2Fweb%2Fwiklou.git Added wfShellMaintenanceCmd() for Het Deploy support --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 632d1b030b..ade1298009 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2804,6 +2804,31 @@ function wfInitShellLocale() { } } +/** + * Generate a shell-escaped command line string to run a maintenance 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 $script string MediaWiki maintenance script path + * @param $parameters Array Arguments and options to the script + * @param $options Array Associative array of options: + * 'php': The path to the php executable + * 'wrapper': Path to a PHP wrapper to handle the maintenance script + * @return Array + */ +function wfShellMaintenanceCmd( $script, array $parameters = array(), array $options = array() ) { + 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. + wfRunHooks( 'wfShellMaintenanceCmd', array( &$script, &$parameters, &$options ) ); + $cmd = isset( $options['php'] ) ? array( $options['php'] ) : array( $wgPhpCli ); + if ( isset( $options['wrapper'] ) ) { + $cmd[] = $options['wrapper']; + } + $cmd[] = $script; + // Escape each parameter for shell + return implode( " ", array_map( 'wfEscapeShellArg', array_merge( $cmd, $parameters ) ) ); +} + /** * This function works like "use VERSION" in Perl, the program will die with a * backtrace if the current version of PHP is less than the version provided diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php index f55ab9ea06..011993ad13 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php @@ -905,6 +905,32 @@ class GlobalTest extends MediaWikiTestCase { return $a; } + /** + * @dataProvider provideWfShellMaintenanceCmdList + */ + function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) { + $actual = wfShellMaintenanceCmd( $script, $parameters, $options ); + $this->assertEquals( $expected, $actual, $description ); + } + + function provideWfShellMaintenanceCmdList() { + global $wgPhpCli; + return array( + array( 'eval.php', array( '--help', '--test' ), array(), + "\"$wgPhpCli\" \"eval.php\" \"--help\" \"--test\"", + "Called eval.php --help --test" ), + array( 'eval.php', array( '--help', '--test space' ), array('php' => 'php5'), + "\"php5\" \"eval.php\" \"--help\" \"--test space\"", + "Called eval.php --help --test with php option" ), + array( 'eval.php', array( '--help', '--test', 'X' ), array('wrapper' => 'MWScript.php'), + "\"$wgPhpCli\" \"MWScript.php\" \"eval.php\" \"--help\" \"--test\" \"X\"", + "Called eval.php --help --test with wrapper option" ), + array( 'eval.php', array( '--help', '--test', 'y' ), array('php' => 'php5', 'wrapper' => 'MWScript.php'), + "\"php5\" \"MWScript.php\" \"eval.php\" \"--help\" \"--test\" \"y\"", + "Called eval.php --help --test with wrapper and php option" ), + ); + } + /* TODO: many more! */ }