Merge "RCFilters: Move parameter operations to ViewModel"
[lhc/web/wiklou.git] / includes / shell / Command.php
index fd8f6a0..4fc282c 100644 (file)
@@ -135,6 +135,11 @@ class Command {
         * @return $this
         */
        public function limits( array $limits ) {
+               if ( !isset( $limits['walltime'] ) && isset( $limits['time'] ) ) {
+                       // Emulate the behavior of old wfShellExec() where walltime fell back on time
+                       // if the latter was overridden and the former wasn't
+                       $limits['walltime'] = $limits['time'];
+               }
                $this->limits = $limits + $this->limits;
 
                return $this;
@@ -199,8 +204,6 @@ class Command {
         * @throws ShellDisabledError
         */
        public function execute() {
-               global $IP;
-
                $this->everExecuted = true;
 
                $profileMethod = $this->method ?: wfGetCaller();
@@ -229,13 +232,11 @@ class Command {
                if ( is_executable( '/bin/bash' ) ) {
                        $time = intval( $this->limits['time'] );
                        $wallTime = intval( $this->limits['walltime'] );
-                       // for b/c, wall time falls back to time
-                       $wallTime = min( $time, $wallTime );
                        $mem = intval( $this->limits['memory'] );
                        $filesize = intval( $this->limits['filesize'] );
 
                        if ( $time > 0 || $mem > 0 || $filesize > 0 || $wallTime > 0 ) {
-                               $cmd = '/bin/bash ' . escapeshellarg( "$IP/includes/limit.sh" ) . ' ' .
+                               $cmd = '/bin/bash ' . escapeshellarg( __DIR__ . '/limit.sh' ) . ' ' .
                                           escapeshellarg( $cmd ) . ' ' .
                                           escapeshellarg(
                                                   "MW_INCLUDE_STDERR=" . ( $this->useStderr ? '1' : '' ) . ';' .
@@ -247,10 +248,9 @@ class Command {
                                                   "MW_USE_LOG_PIPE=yes"
                                           );
                                $useLogPipe = true;
-                       } elseif ( $this->useStderr ) {
-                               $cmd .= ' 2>&1';
                        }
-               } elseif ( $this->useStderr ) {
+               }
+               if ( !$useLogPipe && $this->useStderr ) {
                        $cmd .= ' 2>&1';
                }
                wfDebug( __METHOD__ . ": $cmd\n" );
@@ -261,13 +261,13 @@ class Command {
                // input. See T129506.
                if ( strlen( $cmd ) > SHELL_MAX_ARG_STRLEN ) {
                        throw new Exception( __METHOD__ .
-                                                                '(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
+                               '(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
                }
 
                $desc = [
                        0 => [ 'file', 'php://stdin', 'r' ],
                        1 => [ 'pipe', 'w' ],
-                       2 => [ 'file', 'php://stderr', 'w' ],
+                       2 => [ 'pipe', 'w' ],
                ];
                if ( $useLogPipe ) {
                        $desc[3] = [ 'pipe', 'w' ];
@@ -280,6 +280,7 @@ class Command {
                        throw new ProcOpenError();
                }
                $outBuffer = $logBuffer = '';
+               $errBuffer = null;
                $emptyArray = [];
                $status = false;
                $logMsg = false;
@@ -315,12 +316,20 @@ class Command {
 
                        $readyPipes = $pipes;
 
-                       // Clear last error
+                       // clear get_last_error without actually raising an error
+                       // from http://php.net/manual/en/function.error-get-last.php#113518
+                       // TODO replace with clear_last_error when requirements are bumped to PHP7
+                       set_error_handler( function () {
+                       }, 0 );
                        // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
                        @trigger_error( '' );
+                       // @codingStandardsIgnoreEnd
+                       restore_error_handler();
+
+                       // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
                        $numReadyPipes = @stream_select( $readyPipes, $emptyArray, $emptyArray, $timeout );
+                       // @codingStandardsIgnoreEnd
                        if ( $numReadyPipes === false ) {
-                               // @codingStandardsIgnoreEnd
                                $error = error_get_last();
                                if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
                                        continue;
@@ -346,6 +355,9 @@ class Command {
                                } elseif ( $fd == 1 ) {
                                        // From stdout
                                        $outBuffer .= $block;
+                               } elseif ( $fd == 2 ) {
+                                       // From stderr
+                                       $errBuffer .= $block;
                                } elseif ( $fd == 3 ) {
                                        // From log FD
                                        $logBuffer .= $block;
@@ -396,6 +408,6 @@ class Command {
                        $this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
                }
 
-               return new Result( $retval, $outBuffer );
+               return new Result( $retval, $outBuffer, $errBuffer );
        }
 }