* @param $cmd Command line, properly escaped for shell.
* @param &$retval optional, will receive the program's exit code.
* (non-zero is usually failure)
+ * @param $environ Array optional environment variables which should be
+ * added to the executed command environment.
* @return collected stdout as a string (trailing newlines stripped)
*/
-function wfShellExec( $cmd, &$retval = null ) {
+function wfShellExec( $cmd, &$retval = null, $environ = array() ) {
global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime;
static $disabled;
wfInitShellLocale();
+ $envcmd = '';
+ foreach( $environ as $k => $v ) {
+ if ( wfIsWindows() ) {
+ /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
+ * appear in the environment variable, so we must use carat escaping as documented in
+ * http://technet.microsoft.com/en-us/library/cc723564.aspx
+ * Note however that the quote isn't listed there, but is needed, and the parentheses
+ * are listed there but doesn't appear to need it.
+ */
+ $envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . ' && ';
+ } else {
+ /* Assume this is a POSIX shell, thus required to accept variable assignments before the command
+ * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
+ */
+ $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
+ }
+ }
+ $cmd = $envcmd . $cmd;
+
if ( wfIsWindows() ) {
if ( version_compare( PHP_VERSION, '5.3.0', '<' ) && /* Fixed in 5.3.0 :) */
( version_compare( PHP_VERSION, '5.2.1', '>=' ) || php_uname( 's' ) == 'Windows NT' ) )
}
// Use one thread only, to avoid deadlock bugs on OOM
- $tempEnv = 'OMP_NUM_THREADS=1 ';
+ $env = array( 'OMP_NUM_THREADS' => 1 );
if ( strval( $wgImageMagickTempDir ) !== '' ) {
- $tempEnv .= 'MAGICK_TMPDIR=' . wfEscapeShellArg( $wgImageMagickTempDir ) . ' ';
+ $env['MAGICK_TMPDIR'] = $wgImageMagickTempDir;
}
$cmd =
" {$animation_post} " .
wfEscapeShellArg( $this->escapeMagickOutput( $dstPath ) ) . " 2>&1";
- if ( !wfIsWindows() ) {
- // Assume we have a POSIX compliant shell which accepts variable assignments preceding the command
- $cmd = $tempEnv . $cmd;
- }
-
wfDebug( __METHOD__.": running ImageMagick: $cmd\n" );
wfProfileIn( 'convert' );
- $err = wfShellExec( $cmd, $retval );
+ $err = wfShellExec( $cmd, $retval, $env );
wfProfileOut( 'convert' );
} elseif( $scaler == 'custom' ) {
# Use a custom convert command