MediaWiki REL1_31
MediaWiki\Shell\Command Class Reference

Class used for executing shell commands. More...

Inheritance diagram for MediaWiki\Shell\Command:
Collaboration diagram for MediaWiki\Shell\Command:

Public Member Functions

 __construct ()
 Constructor.
 
 __destruct ()
 Destructor.
 
 cgroup ( $cgroup)
 Sets cgroup for this command.
 
 environment (array $env)
 Sets environment variables which should be added to the executed command environment.
 
 execute ()
 Executes command.
 
 includeStderr ( $yesno=true)
 Controls whether stderr should be included in stdout, including errors from limit.sh.
 
 input ( $inputString)
 Sends the provided input to the command.
 
 limits (array $limits)
 Sets execution limits.
 
 logStderr ( $yesno=true)
 When enabled, text sent to stderr will be logged with a level of 'error'.
 
 params ()
 Adds parameters to the command.
 
 profileMethod ( $method)
 Sets calling function for profiler.
 
 restrict ( $restrictions)
 Set restrictions for this request, overwriting any previously set restrictions.
 
 unsafeParams ()
 Adds unsafe parameters to the command.
 
 whitelistPaths (array $paths)
 If called, only the files/directories that are whitelisted will be available to the shell command.
 

Protected Member Functions

 buildFinalCommand ( $command)
 String together all the options and build the final command to execute.
 
 hasRestriction ( $restriction)
 Bitfield helper on whether a specific restriction is enabled.
 

Protected Attributes

string $command = ''
 
int $restrictions = 0
 bitfield with restrictions
 

Private Attributes

string false $cgroup = false
 
bool $doIncludeStderr = false
 
bool $doLogStderr = false
 
string[] $env = []
 
bool $everExecuted = false
 
string null $inputString
 
array $limits
 
string $method
 

Detailed Description

Class used for executing shell commands.

Since
1.30

Definition at line 35 of file Command.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Shell\Command::__construct ( )

Constructor.

Don't call directly, instead use Shell::command()

Exceptions
ShellDisabledError

Definition at line 86 of file Command.php.

References MediaWiki\Shell\Shell\isDisabled().

◆ __destruct()

MediaWiki\Shell\Command::__destruct ( )

Destructor.

Makes sure programmer didn't forget to execute the command after all

Definition at line 97 of file Command.php.

References MediaWiki\Shell\Command\$command, $context, and MediaWiki\Shell\Command\$method.

Member Function Documentation

◆ buildFinalCommand()

MediaWiki\Shell\Command::buildFinalCommand ( $command)
protected

String together all the options and build the final command to execute.

Parameters
string$commandAlready-escaped command to run
Returns
array [ command, whether to use log pipe ]

Reimplemented in MediaWiki\Shell\FirejailCommand.

Definition at line 305 of file Command.php.

References MediaWiki\Shell\Command\$command, $time, MediaWiki\Shell\Command\cgroup(), MediaWiki\Shell\Command\limits(), and wfIsWindows().

Referenced by MediaWiki\Shell\Command\execute().

◆ cgroup()

MediaWiki\Shell\Command::cgroup ( $cgroup)

Sets cgroup for this command.

Parameters
string | false$cgroupAbsolute file path to the cgroup, or false to not use a cgroup
Returns
$this

Definition at line 238 of file Command.php.

References MediaWiki\Shell\Command\$cgroup, and MediaWiki\Shell\Command\cgroup().

Referenced by MediaWiki\Shell\Command\buildFinalCommand(), and MediaWiki\Shell\Command\cgroup().

◆ environment()

MediaWiki\Shell\Command::environment ( array $env)

Sets environment variables which should be added to the executed command environment.

Parameters
string[]$envarray of variable name => value
Returns
$this

Definition at line 177 of file Command.php.

References MediaWiki\Shell\Command\$env.

◆ execute()

MediaWiki\Shell\Command::execute ( )

Executes command.

Afterwards, getExitCode() and getOutput() can be used to access execution results.

Returns
Result
Exceptions
Exception
ProcOpenError
ShellDisabledError

Definition at line 368 of file Command.php.

References MediaWiki\Shell\Command\$inputString, $line, $lines, $res, $retval, MediaWiki\Shell\Command\buildFinalCommand(), list, SHELL_MAX_ARG_STRLEN, string, wfArrayFilterByKey(), wfGetCaller(), and wfIsWindows().

◆ hasRestriction()

MediaWiki\Shell\Command::hasRestriction ( $restriction)
protected

Bitfield helper on whether a specific restriction is enabled.

Parameters
int$restriction
Returns
bool

Definition at line 279 of file Command.php.

◆ includeStderr()

MediaWiki\Shell\Command::includeStderr ( $yesno = true)

Controls whether stderr should be included in stdout, including errors from limit.sh.

Default: don't include.

Parameters
bool$yesno
Returns
$this

Definition at line 214 of file Command.php.

◆ input()

MediaWiki\Shell\Command::input ( $inputString)

Sends the provided input to the command.

When set to null (default), the command will use the standard input.

Parameters
string | null$inputString
Returns
$this

Definition at line 201 of file Command.php.

References MediaWiki\Shell\Command\$inputString, and string.

◆ limits()

MediaWiki\Shell\Command::limits ( array $limits)

Sets execution limits.

Parameters
array$limitsAssociative array of limits. Keys (all optional): filesize (for ulimit -f), memory, time, walltime.
Returns
$this

Definition at line 160 of file Command.php.

References MediaWiki\Shell\Command\$limits, and MediaWiki\Shell\Command\limits().

Referenced by MediaWiki\Shell\Command\buildFinalCommand(), and MediaWiki\Shell\Command\limits().

◆ logStderr()

MediaWiki\Shell\Command::logStderr ( $yesno = true)

When enabled, text sent to stderr will be logged with a level of 'error'.

Parameters
bool$yesno
Returns
$this

Definition at line 226 of file Command.php.

◆ params()

MediaWiki\Shell\Command::params ( )

Adds parameters to the command.

All parameters are sanitized via Shell::escape(). Null values are ignored.

Parameters
string | string[]$args,...
Returns
$this

Definition at line 117 of file Command.php.

References $args, and MediaWiki\Shell\Shell\escape().

◆ profileMethod()

MediaWiki\Shell\Command::profileMethod ( $method)

Sets calling function for profiler.

By default, the caller for execute() will be used.

Parameters
string$method
Returns
$this

Definition at line 189 of file Command.php.

References MediaWiki\Shell\Command\$method.

◆ restrict()

MediaWiki\Shell\Command::restrict ( $restrictions)

Set restrictions for this request, overwriting any previously set restrictions.

Add the "no network" restriction:

$command->restrict( Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK );
$command
Definition cdb.php:65

Allow LocalSettings.php access:

$command->restrict( Shell::RESTRICT_DEFAULT & ~Shell::NO_LOCALSETTINGS );

Disable all restrictions:

$command->restrict( Shell::RESTRICT_NONE );
Since
1.31
Parameters
int$restrictions
Returns
$this

Definition at line 266 of file Command.php.

References MediaWiki\Shell\Command\$restrictions.

◆ unsafeParams()

MediaWiki\Shell\Command::unsafeParams ( )

Adds unsafe parameters to the command.

These parameters are NOT sanitized in any way. Null values are ignored.

Parameters
string | string[]$args,...
Returns
$this

Definition at line 136 of file Command.php.

References $args, and $value.

◆ whitelistPaths()

MediaWiki\Shell\Command::whitelistPaths ( array $paths)

If called, only the files/directories that are whitelisted will be available to the shell command.

limit.sh will always be whitelisted

Parameters
string[]$paths
Returns
$this

Reimplemented in MediaWiki\Shell\FirejailCommand.

Definition at line 293 of file Command.php.

Member Data Documentation

◆ $cgroup

string false MediaWiki\Shell\Command::$cgroup = false
private

Definition at line 72 of file Command.php.

Referenced by MediaWiki\Shell\Command\cgroup().

◆ $command

string MediaWiki\Shell\Command::$command = ''
protected

◆ $doIncludeStderr

bool MediaWiki\Shell\Command::$doIncludeStderr = false
private

Definition at line 63 of file Command.php.

◆ $doLogStderr

bool MediaWiki\Shell\Command::$doLogStderr = false
private

Definition at line 66 of file Command.php.

◆ $env

string [] MediaWiki\Shell\Command::$env = []
private

Definition at line 54 of file Command.php.

Referenced by MediaWiki\Shell\Command\environment().

◆ $everExecuted

bool MediaWiki\Shell\Command::$everExecuted = false
private

Definition at line 69 of file Command.php.

◆ $inputString

string null MediaWiki\Shell\Command::$inputString
private

Definition at line 60 of file Command.php.

Referenced by MediaWiki\Shell\Command\execute(), and MediaWiki\Shell\Command\input().

◆ $limits

array MediaWiki\Shell\Command::$limits
private
Initial value:
= [
'time' => 180,
'walltime' => 180,
'memory' => 307200,
'filesize' => 102400,
]

Definition at line 42 of file Command.php.

Referenced by MediaWiki\Shell\Command\limits().

◆ $method

string MediaWiki\Shell\Command::$method
private

◆ $restrictions

int MediaWiki\Shell\Command::$restrictions = 0
protected

bitfield with restrictions

Definition at line 79 of file Command.php.

Referenced by MediaWiki\Shell\Command\restrict().


The documentation for this class was generated from the following file: