From f735507bfcd32842dcfab74c83e4a35f8440ffaa Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Mon, 13 Nov 2017 20:11:53 -0800 Subject: [PATCH] Convert RandomImageGenerator to use the new execution framework Introduces a stringifier for Command, useful for debugging. Change-Id: Ifcfccaef5a609e0cf30186e39a6bd0fa971c2dbd --- includes/shell/Command.php | 11 +++++ .../includes/api/RandomImageGenerator.php | 41 +++++++++++-------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/includes/shell/Command.php b/includes/shell/Command.php index 2afc548d08..d504611f7d 100644 --- a/includes/shell/Command.php +++ b/includes/shell/Command.php @@ -553,4 +553,15 @@ class Command { return new Result( $retval, $buffers[1], $buffers[2] ); } + + /** + * Returns the final command line before environment/limiting, etc are applied. + * Use string conversion only for debugging, don't try to pass this to + * some other execution medium. + * + * @return string + */ + public function __toString() { + return "#Command: {$this->command}"; + } } diff --git a/tests/phpunit/includes/api/RandomImageGenerator.php b/tests/phpunit/includes/api/RandomImageGenerator.php index 75e73befc7..41cd039d3d 100644 --- a/tests/phpunit/includes/api/RandomImageGenerator.php +++ b/tests/phpunit/includes/api/RandomImageGenerator.php @@ -23,6 +23,8 @@ * @author Neil Kandalgaonkar */ +use MediaWiki\Shell\Shell; + /** * RandomImageGenerator: does what it says on the tin. * Can fetch a random image, or also write a number of them to disk with random filenames. @@ -310,16 +312,16 @@ class RandomImageGenerator { // for now (only works if you have exiv2 installed, a program to read // and manipulate exif). if ( $wgExiv2Command ) { - $cmd = wfEscapeShellArg( $wgExiv2Command ) - . " -M " - . wfEscapeShellArg( "set Exif.Image.Orientation " . $orientation['exifCode'] ) - . " " - . wfEscapeShellArg( $filename ); - - $retval = 0; - $err = wfShellExec( $cmd, $retval ); + $command = Shell::command( $wgExiv2Command, + '-M', + "set Exif.Image.Orientation {$orientation['exifCode']}", + $filename + )->includeStderr(); + + $result = $command->execute(); + $retval = $result->getExitCode(); if ( $retval !== 0 ) { - print "Error with $cmd: $retval, $err\n"; + print "Error with $command: $retval, {$result->getStdout()}\n"; } } } @@ -396,22 +398,25 @@ class RandomImageGenerator { */ public function writeImageWithCommandLine( $spec, $format, $filename ) { global $wgImageMagickConvertCommand; - $args = []; - $args[] = "-size " . wfEscapeShellArg( $spec['width'] . 'x' . $spec['height'] ); - $args[] = wfEscapeShellArg( "xc:" . $spec['fill'] ); + + $args = [ + $wgImageMagickConvertCommand, + '-size', + $spec['width'] . 'x' . $spec['height'], + "xc:{$spec['fill']}", + ]; foreach ( $spec['draws'] as $draw ) { $fill = $draw['fill']; $polygon = self::shapePointsToString( $draw['shape'] ); $drawCommand = "fill $fill polygon $polygon"; - $args[] = '-draw ' . wfEscapeShellArg( $drawCommand ); + $args[] = '-draw'; + $args[] = $drawCommand; } - $args[] = wfEscapeShellArg( $filename ); + $args[] = $filename; - $command = wfEscapeShellArg( $wgImageMagickConvertCommand ) . " " . implode( " ", $args ); - $retval = null; - wfShellExec( $command, $retval ); + $result = Shell::command( $args )->execute(); - return ( $retval === 0 ); + return ( $result->getExitCode() === 0 ); } /** -- 2.20.1