MediaWiki REL1_30
Shell.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Shell;
24
26
41class Shell {
42
51 public static function command( $command ) {
52 $args = func_get_args();
53 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
54 // If only one argument has been passed, and that argument is an array,
55 // treat it as a list of arguments
56 $args = reset( $args );
57 }
59 ->getShellCommandFactory()
60 ->create();
61
62 return $command->params( $args );
63 }
64
70 public static function isDisabled() {
71 static $disabled = null;
72
73 if ( is_null( $disabled ) ) {
74 if ( !function_exists( 'proc_open' ) ) {
75 wfDebug( "proc_open() is disabled\n" );
76 $disabled = true;
77 } else {
78 $disabled = false;
79 }
80 }
81
82 return $disabled;
83 }
84
96 public static function escape( /* ... */ ) {
97 $args = func_get_args();
98 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
99 // If only one argument has been passed, and that argument is an array,
100 // treat it as a list of arguments
101 $args = reset( $args );
102 }
103
104 $first = true;
105 $retVal = '';
106 foreach ( $args as $arg ) {
107 if ( $arg === null ) {
108 continue;
109 }
110 if ( !$first ) {
111 $retVal .= ' ';
112 } else {
113 $first = false;
114 }
115
116 if ( wfIsWindows() ) {
117 // Escaping for an MSVC-style command line parser and CMD.EXE
118 // @codingStandardsIgnoreStart For long URLs
119 // Refs:
120 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
121 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
122 // * T15518
123 // * CR r63214
124 // Double the backslashes before any double quotes. Escape the double quotes.
125 // @codingStandardsIgnoreEnd
126 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
127 $arg = '';
128 $iteration = 0;
129 foreach ( $tokens as $token ) {
130 if ( $iteration % 2 == 1 ) {
131 // Delimiter, a double quote preceded by zero or more slashes
132 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
133 } elseif ( $iteration % 4 == 2 ) {
134 // ^ in $token will be outside quotes, need to be escaped
135 $arg .= str_replace( '^', '^^', $token );
136 } else { // $iteration % 4 == 0
137 // ^ in $token will appear inside double quotes, so leave as is
138 $arg .= $token;
139 }
140 $iteration++;
141 }
142 // Double the backslashes before the end of the string, because
143 // we will soon add a quote
144 $m = [];
145 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
146 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
147 }
148
149 // Add surrounding quotes
150 $retVal .= '"' . $arg . '"';
151 } else {
152 $retVal .= escapeshellarg( $arg );
153 }
154 }
155 return $retVal;
156 }
157}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfIsWindows()
Check if the operating system is Windows.
$command
Definition cdb.php:64
if( $line===false) $args
Definition cdb.php:63
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
Executes shell commands.
Definition Shell.php:41
static escape()
Version of escapeshellarg() that works better on Windows.
Definition Shell.php:96
static command( $command)
Returns a new instance of Command class.
Definition Shell.php:51
static isDisabled()
Check if this class is effectively disabled via php.ini config.
Definition Shell.php:70
$tokens