3 * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org>
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.
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.
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.
21 use MediaWiki\Shell\Shell
;
24 * Utility class to find executables in likely places
28 class ExecutableFinder
{
31 * Get an array of likely places we can find executables. Check a bunch
32 * of known Unix-like defaults, as well as the PATH environment variable
33 * (which should maybe make it work for Windows?)
37 protected static function getPossibleBinPaths() {
38 return array_unique( array_merge(
39 [ '/usr/bin', '/bin', '/usr/local/bin', '/opt/csw/bin',
40 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ],
41 explode( PATH_SEPARATOR
, getenv( 'PATH' ) )
46 * Search a path for any of the given executable names. Returns the
47 * executable name if found. Also checks the version string returned
50 * Used only by environment checks.
52 * @param string $path Path to search
53 * @param string $name Executable name to look for
54 * @param array|bool $versionInfo False or array with two members:
55 * 0 => Parameter to pass to binary for version check (e.g. --version)
56 * 1 => String to compare the output with
58 * If $versionInfo is not false, only executables with a version
59 * matching $versionInfo[1] will be returned.
62 protected static function findExecutable( $path, $name, $versionInfo = false ) {
63 $command = $path . DIRECTORY_SEPARATOR
. $name;
65 Wikimedia\
suppressWarnings();
66 $file_exists = is_executable( $command );
67 Wikimedia\restoreWarnings
();
70 if ( !$versionInfo ) {
74 $output = Shell
::command( $command, $versionInfo[0] )
75 ->includeStderr()->execute()->getStdout();
76 if ( strstr( $output, $versionInfo[1] ) !== false ) {
85 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
86 * @see locateExecutable()
87 * @param string|string[] $names Array of possible names.
88 * @param array|bool $versionInfo Default: false or array with two members:
89 * 0 => Parameter to run for version check, e.g. '--version'
90 * 1 => String to compare the output with
92 * If $versionInfo is not false, only executables with a version
93 * matching $versionInfo[1] will be returned.
96 public static function findInDefaultPaths( $names, $versionInfo = false ) {
97 if ( Shell
::isDisabled() ) {
98 // If we can't shell out, there's no point looking for executables
102 $paths = self
::getPossibleBinPaths();
103 foreach ( (array)$names as $name ) {
104 foreach ( $paths as $path ) {
105 $exe = self
::findExecutable( $path, $name, $versionInfo );
106 if ( $exe !== false ) {