}
}
+/**
+ * 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
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! */
}