MediaWiki  1.30.0
Shell.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Shell;
24 
26 
41 class 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 }
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:41
captcha-old.count
count
Definition: captcha-old.py:249
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Shell\Shell\isDisabled
static isDisabled()
Check if this class is effectively disabled via php.ini config.
Definition: Shell.php:70
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:1047
$command
$command
Definition: cdb.php:64
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:99
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition: GlobalFunctions.php:2079
$tokens
$tokens
Definition: mwdoc-filter.php:47
MediaWiki\Shell\Shell\escape
static escape()
Version of escapeshellarg() that works better on Windows.
Definition: Shell.php:96
$args
if( $line===false) $args
Definition: cdb.php:63
MediaWiki\Shell
Definition: Command.php:21
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWiki\Shell\Shell\command
static command( $command)
Returns a new instance of Command class.
Definition: Shell.php:51