r75476 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75475‎ | r75476 | r75477 >
Date:22:17, 26 October 2010
Author:platonides
Status:ok (Comments)
Tags:
Comment:
Provide a proper implementation for passing environment variables to wfShellExec()
The quick fix of r74821 is no longer needed.
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/media/Bitmap.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -2457,9 +2457,11 @@
24582458 * @param $cmd Command line, properly escaped for shell.
24592459 * @param &$retval optional, will receive the program's exit code.
24602460 * (non-zero is usually failure)
 2461+ * @param $environ Array optional environment variables which should be
 2462+ * added to the executed command environment.
24612463 * @return collected stdout as a string (trailing newlines stripped)
24622464 */
2463 -function wfShellExec( $cmd, &$retval = null ) {
 2465+function wfShellExec( $cmd, &$retval = null, $environ = array() ) {
24642466 global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime;
24652467
24662468 static $disabled;
@@ -2487,6 +2489,25 @@
24882490
24892491 wfInitShellLocale();
24902492
 2493+ $envcmd = '';
 2494+ foreach( $environ as $k => $v ) {
 2495+ if ( wfIsWindows() ) {
 2496+ /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
 2497+ * appear in the environment variable, so we must use carat escaping as documented in
 2498+ * http://technet.microsoft.com/en-us/library/cc723564.aspx
 2499+ * Note however that the quote isn't listed there, but is needed, and the parentheses
 2500+ * are listed there but doesn't appear to need it.
 2501+ */
 2502+ $envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . ' && ';
 2503+ } else {
 2504+ /* Assume this is a POSIX shell, thus required to accept variable assignments before the command
 2505+ * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
 2506+ */
 2507+ $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
 2508+ }
 2509+ }
 2510+ $cmd = $envcmd . $cmd;
 2511+
24912512 if ( wfIsWindows() ) {
24922513 if ( version_compare( PHP_VERSION, '5.3.0', '<' ) && /* Fixed in 5.3.0 :) */
24932514 ( version_compare( PHP_VERSION, '5.2.1', '>=' ) || php_uname( 's' ) == 'Windows NT' ) )
Index: trunk/phase3/includes/media/Bitmap.php
@@ -148,9 +148,9 @@
149149 }
150150
151151 // Use one thread only, to avoid deadlock bugs on OOM
152 - $tempEnv = 'OMP_NUM_THREADS=1 ';
 152+ $env = array( 'OMP_NUM_THREADS' => 1 );
153153 if ( strval( $wgImageMagickTempDir ) !== '' ) {
154 - $tempEnv .= 'MAGICK_TMPDIR=' . wfEscapeShellArg( $wgImageMagickTempDir ) . ' ';
 154+ $env['MAGICK_TMPDIR'] = $wgImageMagickTempDir;
155155 }
156156
157157 $cmd =
@@ -171,14 +171,9 @@
172172 " {$animation_post} " .
173173 wfEscapeShellArg( $this->escapeMagickOutput( $dstPath ) ) . " 2>&1";
174174
175 - if ( !wfIsWindows() ) {
176 - // Assume we have a POSIX compliant shell which accepts variable assignments preceding the command
177 - $cmd = $tempEnv . $cmd;
178 - }
179 -
180175 wfDebug( __METHOD__.": running ImageMagick: $cmd\n" );
181176 wfProfileIn( 'convert' );
182 - $err = wfShellExec( $cmd, $retval );
 177+ $err = wfShellExec( $cmd, $retval, $env );
183178 wfProfileOut( 'convert' );
184179 } elseif( $scaler == 'custom' ) {
185180 # Use a custom convert command

Follow-up revisions

RevisionCommit summaryAuthorDate
r78565Follow-up r75476: Windows set command treats everything until the && as part ...btongminh15:00, 18 December 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r74821r71546 broke imagemagick usage in Windows, since cmd.exe takes the variable...platonides19:48, 15 October 2010

Comments

#Comment by Hashar (talk | contribs)   06:35, 27 October 2010

Thanks for providing this. The ^ escaping is a funny part :-)

Status & tagging log