* wfShellexec() now accepts an optional parameter to receive the exit code
authorBrion Vibber <brion@users.mediawiki.org>
Fri, 26 May 2006 02:41:20 +0000 (02:41 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Fri, 26 May 2006 02:41:20 +0000 (02:41 +0000)
* Failed, but not zero-length, thumbnail renderings are now removed. Should help clean up when rsvg fails in weird ways.

RELEASE-NOTES
includes/GlobalFunctions.php
includes/Image.php

index a0fa265..af71e53 100644 (file)
@@ -339,6 +339,9 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 6086) Remove vestigial attempt to call Article::validate()
 * wfHostname() function for consistent server hostname use in debug messages
 * Send thumbnailing error messages to 'thumbnail' log group
+* wfShellexec() now accepts an optional parameter to receive the exit code
+* Failed, but not zero-length, thumbnail renderings are now removed.
+  Should help clean up when rsvg fails in weird ways.
 
 
 == Compatibility ==
index e76b41d..cbd5821 100644 (file)
@@ -1666,12 +1666,21 @@ function wfUrlProtocols() {
 }
 
 /**
- * shell_exec() with time and memory limits mirrored from the PHP configuration,
- * if supported.
- */
-function wfShellExec( $cmd )
-{
+ * Execute a shell command, with time and memory limits mirrored from the PHP
+ * configuration if supported.
+ * @param $cmd Command line, properly escaped for shell.
+ * @param &$retval optional, will receive the program's exit code.
+ *                 (non-zero is usually failure)
+ * @return collected stdout as a string (trailing newlines stripped)
+ */
+function wfShellExec( $cmd, &$retval=null ) {
        global $IP;
+       
+       if( ini_get( 'safe_mode' ) ) {
+               wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
+               $retval = 1;
+               return "Unable to run external programs in safe mode.";
+       }
 
        if ( php_uname( 's' ) == 'Linux' ) {
                $time = ini_get( 'max_execution_time' );
@@ -1692,7 +1701,12 @@ function wfShellExec( $cmd )
                $cmd = '"' . $cmd . '"';
        }
        wfDebug( "wfShellExec: $cmd\n" );
-       return shell_exec( $cmd );
+       
+       $output = array();
+       $retval = 1; // error by default?
+       $lastline = exec( $cmd, $output, $retval );
+       return implode( "\n", $output );
+       
 }
 
 /**
index fd2ce63..1ba084e 100644 (file)
@@ -1050,6 +1050,9 @@ class Image
                $this->load();
 
                $err = false;
+               $cmd = "";
+               $retval = 0;
+               
                if( $this->mime === "image/svg" ) {
                        #Right now we have only SVG
 
@@ -1066,7 +1069,7 @@ class Image
                                        $wgSVGConverters[$wgSVGConverter] );
                                wfProfileIn( 'rsvg' );
                                wfDebug( "reallyRenderThumb SVG: $cmd\n" );
-                               $err = wfShellExec( $cmd );
+                               $err = wfShellExec( $cmd, $retval );
                                wfProfileOut( 'rsvg' );
                        }
                } elseif ( $wgUseImageMagick ) {
@@ -1098,7 +1101,7 @@ class Image
                                wfEscapeShellArg($thumbPath) . " 2>&1";
                        wfDebug("reallyRenderThumb: running ImageMagick: $cmd\n");
                        wfProfileIn( 'convert' );
-                       $err = wfShellExec( $cmd );
+                       $err = wfShellExec( $cmd, $retval );
                        wfProfileOut( 'convert' );
                } elseif( $wgCustomConvertCommand ) {
                        # Use a custom convert command
@@ -1110,7 +1113,7 @@ class Image
                        $cmd = str_replace( '%h', $height, str_replace( '%w', $width, $cmd ) ); # Size
                        wfDebug( "reallyRenderThumb: Running custom convert command $cmd\n" );
                        wfProfileIn( 'convert' );
-                       $err = wfShellExec( $cmd );
+                       $err = wfShellExec( $cmd, $retval );
                        wfProfileOut( 'convert' );
                } else {
                        # Use PHP's builtin GD library functions.
@@ -1165,17 +1168,17 @@ class Image
                #
                if( file_exists( $thumbPath ) ) {
                        $thumbstat = stat( $thumbPath );
-                       if( $thumbstat['size'] == 0 ) {
+                       if( $thumbstat['size'] == 0 || $retval != 0 ) {
+                               wfDebugLog( 'thumbnail',
+                                       sprintf( 'Removing bad %d-byte thumbnail "%s"',
+                                               $thumbstat['size'], $thumbPath ) );
                                unlink( $thumbPath );
-                       } else {
-                               // All good
-                               $err = true;
                        }
                }
-               if ( $err !== true ) {
+               if ( $retval != 0 ) {
                        wfDebugLog( 'thumbnail',
-                               sprintf( 'thumbnail failed on %s: "%s" from "%s"',
-                                       wfHostname(), trim($err), $cmd ) );
+                               sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
+                                       wfHostname(), $retval, trim($err), $cmd ) );
                        return wfMsg( 'thumbnail_error', $err );
                } else {
                        return true;