Index: trunk/phase3/tests/phpunit/includes/GlobalFunctions/GlobalTest.php |
— | — | @@ -905,6 +905,32 @@ |
906 | 906 | return $a; |
907 | 907 | } |
908 | 908 | |
| 909 | + /** |
| 910 | + * @dataProvider provideWfShellMaintenanceCmdList |
| 911 | + */ |
| 912 | + function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) { |
| 913 | + $actual = wfShellMaintenanceCmd( $script, $parameters, $options ); |
| 914 | + $this->assertEquals( $expected, $actual, $description ); |
| 915 | + } |
| 916 | + |
| 917 | + function provideWfShellMaintenanceCmdList() { |
| 918 | + global $wgPhpCli; |
| 919 | + return array( |
| 920 | + array( 'eval.php', array( '--help', '--test' ), array(), |
| 921 | + "\"$wgPhpCli\" \"eval.php\" \"--help\" \"--test\"", |
| 922 | + "Called eval.php --help --test" ), |
| 923 | + array( 'eval.php', array( '--help', '--test space' ), array('php' => 'php5'), |
| 924 | + "\"php5\" \"eval.php\" \"--help\" \"--test space\"", |
| 925 | + "Called eval.php --help --test with php option" ), |
| 926 | + array( 'eval.php', array( '--help', '--test', 'X' ), array('wrapper' => 'MWScript.php'), |
| 927 | + "\"$wgPhpCli\" \"MWScript.php\" \"eval.php\" \"--help\" \"--test\" \"X\"", |
| 928 | + "Called eval.php --help --test with wrapper option" ), |
| 929 | + array( 'eval.php', array( '--help', '--test', 'y' ), array('php' => 'php5', 'wrapper' => 'MWScript.php'), |
| 930 | + "\"php5\" \"MWScript.php\" \"eval.php\" \"--help\" \"--test\" \"y\"", |
| 931 | + "Called eval.php --help --test with wrapper and php option" ), |
| 932 | + ); |
| 933 | + } |
| 934 | + |
909 | 935 | /* TODO: many more! */ |
910 | 936 | } |
911 | 937 | |
Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -2805,6 +2805,31 @@ |
2806 | 2806 | } |
2807 | 2807 | |
2808 | 2808 | /** |
| 2809 | + * Generate a shell-escaped command line string to run a maintenance script. |
| 2810 | + * Note that $parameters should be a flat array and an option with an argument |
| 2811 | + * should consist of two consecutive items in the array (do not use "--option value"). |
| 2812 | + * @param $script string MediaWiki maintenance script path |
| 2813 | + * @param $parameters Array Arguments and options to the script |
| 2814 | + * @param $options Array Associative array of options: |
| 2815 | + * 'php': The path to the php executable |
| 2816 | + * 'wrapper': Path to a PHP wrapper to handle the maintenance script |
| 2817 | + * @return Array |
| 2818 | + */ |
| 2819 | +function wfShellMaintenanceCmd( $script, array $parameters = array(), array $options = array() ) { |
| 2820 | + global $wgPhpCli; |
| 2821 | + // Give site config file a chance to run the script in a wrapper. |
| 2822 | + // The caller may likely want to call wfBasename() on $script. |
| 2823 | + wfRunHooks( 'wfShellMaintenanceCmd', array( &$script, &$parameters, &$options ) ); |
| 2824 | + $cmd = isset( $options['php'] ) ? array( $options['php'] ) : array( $wgPhpCli ); |
| 2825 | + if ( isset( $options['wrapper'] ) ) { |
| 2826 | + $cmd[] = $options['wrapper']; |
| 2827 | + } |
| 2828 | + $cmd[] = $script; |
| 2829 | + // Escape each parameter for shell |
| 2830 | + return implode( " ", array_map( 'wfEscapeShellArg', array_merge( $cmd, $parameters ) ) ); |
| 2831 | +} |
| 2832 | + |
| 2833 | +/** |
2809 | 2834 | * This function works like "use VERSION" in Perl, the program will die with a |
2810 | 2835 | * backtrace if the current version of PHP is less than the version provided |
2811 | 2836 | * |