Remove perf tracking code that was moved to WikimediaEvents in Ib300af5c
[lhc/web/wiklou.git] / includes / shell / Shell.php
1 <?php
2 /**
3 * Class used for executing shell commands
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Shell;
24
25 /**
26 * Executes shell commands
27 *
28 * @since 1.30
29 *
30 * Use call chaining with this class for expressiveness:
31 * $result = Shell::command( 'some command' )
32 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
33 * ->limits( [ 'time' => 300 ] )
34 * ->execute();
35 *
36 * ... = $result->getExitCode();
37 * ... = $result->getStdout();
38 */
39 class Shell {
40
41 /**
42 * Returns a new instance of this class
43 *
44 * @param string|string[] $command If string, a properly shell-escaped command line,
45 * or an array of unescaped arguments, in which case each value will be escaped
46 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
47 * @return Command
48 */
49 public static function command( $command ) {
50 $args = func_get_args();
51 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
52 // If only one argument has been passed, and that argument is an array,
53 // treat it as a list of arguments
54 $args = reset( $args );
55 }
56 $command = new Command();
57 return $command->params( $args );
58 }
59
60 /**
61 * Check if this class is effectively disabled via php.ini config
62 *
63 * @return bool
64 */
65 public static function isDisabled() {
66 static $disabled = null;
67
68 if ( is_null( $disabled ) ) {
69 if ( !function_exists( 'proc_open' ) ) {
70 wfDebug( "proc_open() is disabled\n" );
71 $disabled = true;
72 } else {
73 $disabled = false;
74 }
75 }
76
77 return $disabled;
78 }
79
80 /**
81 * Version of escapeshellarg() that works better on Windows.
82 *
83 * Originally, this fixed the incorrect use of single quotes on Windows
84 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
85 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
86 *
87 * @param string $args,... strings to escape and glue together, or a single array of
88 * strings parameter
89 * @return string
90 */
91 public static function escape( /* ... */ ) {
92 $args = func_get_args();
93 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
94 // If only one argument has been passed, and that argument is an array,
95 // treat it as a list of arguments
96 $args = reset( $args );
97 }
98
99 $first = true;
100 $retVal = '';
101 foreach ( $args as $arg ) {
102 if ( !$first ) {
103 $retVal .= ' ';
104 } else {
105 $first = false;
106 }
107
108 if ( wfIsWindows() ) {
109 // Escaping for an MSVC-style command line parser and CMD.EXE
110 // @codingStandardsIgnoreStart For long URLs
111 // Refs:
112 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
113 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
114 // * T15518
115 // * CR r63214
116 // Double the backslashes before any double quotes. Escape the double quotes.
117 // @codingStandardsIgnoreEnd
118 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
119 $arg = '';
120 $iteration = 0;
121 foreach ( $tokens as $token ) {
122 if ( $iteration % 2 == 1 ) {
123 // Delimiter, a double quote preceded by zero or more slashes
124 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
125 } elseif ( $iteration % 4 == 2 ) {
126 // ^ in $token will be outside quotes, need to be escaped
127 $arg .= str_replace( '^', '^^', $token );
128 } else { // $iteration % 4 == 0
129 // ^ in $token will appear inside double quotes, so leave as is
130 $arg .= $token;
131 }
132 $iteration++;
133 }
134 // Double the backslashes before the end of the string, because
135 // we will soon add a quote
136 $m = [];
137 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
138 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
139 }
140
141 // Add surrounding quotes
142 $retVal .= '"' . $arg . '"';
143 } else {
144 $retVal .= escapeshellarg( $arg );
145 }
146 }
147 return $retVal;
148 }
149 }