MediaWiki master
ExecutableFinder.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Utils;
10
12
19
27 protected static function getPossibleBinPaths() {
28 return array_unique( array_merge(
29 [ '/usr/bin', '/bin', '/usr/local/bin', '/opt/csw/bin',
30 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ],
31 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
32 ) );
33 }
34
52 protected static function findExecutable( $path, $name, $versionInfo = false ) {
53 $command = $path . DIRECTORY_SEPARATOR . $name;
54
55 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
56 $file_exists = @is_executable( $command );
57
58 if ( $file_exists ) {
59 if ( !$versionInfo ) {
60 return $command;
61 }
62
63 $output = Shell::command( $command, $versionInfo[0] )
64 ->includeStderr()->execute()->getStdout();
65 if ( str_contains( $output, $versionInfo[1] ) ) {
66 return $command;
67 }
68 }
69
70 return false;
71 }
72
85 public static function findInDefaultPaths( $names, $versionInfo = false ) {
86 if ( Shell::isDisabled() ) {
87 // If we can't shell out, there's no point looking for executables
88 return false;
89 }
90
92 foreach ( (array)$names as $name ) {
93 foreach ( $paths as $path ) {
94 $exe = self::findExecutable( $path, $name, $versionInfo );
95 if ( $exe !== false ) {
96 return $exe;
97 }
98 }
99 }
100
101 return false;
102 }
103
104}
105
107class_alias( ExecutableFinder::class, 'ExecutableFinder' );
isDisabled()
Check whether a message does not exist, is an empty string, or is "-".
Definition Message.php:1210
Executes shell commands.
Definition Shell.php:32
Utility class to find executables in likely places.
static findInDefaultPaths( $names, $versionInfo=false)
Same as locateExecutable(), but checks in getPossibleBinPaths() by default.
static findExecutable( $path, $name, $versionInfo=false)
Search a path for any of the given executable names.
static getPossibleBinPaths()
Get an array of likely places we can find executables.