- /**
- * Get an array of likely places we can find executables. Check a bunch
- * of known Unix-like defaults, as well as the PATH environment variable
- * (which should maybe make it work for Windows?)
- *
- * @return array
- */
- protected static function getPossibleBinPaths() {
- return array_merge(
- [ '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
- '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ],
- explode( PATH_SEPARATOR, getenv( 'PATH' ) )
- );
- }
-
- /**
- * Search a path for any of the given executable names. Returns the
- * executable name if found. Also checks the version string returned
- * by each executable.
- *
- * Used only by environment checks.
- *
- * @param string $path Path to search
- * @param array $names Array of executable names
- * @param array|bool $versionInfo False or array with two members:
- * 0 => Command to run for version check, with $1 for the full executable name
- * 1 => String to compare the output with
- *
- * If $versionInfo is not false, only executables with a version
- * matching $versionInfo[1] will be returned.
- * @return bool|string
- */
- public static function locateExecutable( $path, $names, $versionInfo = false ) {
- if ( !is_array( $names ) ) {
- $names = [ $names ];
- }
-
- foreach ( $names as $name ) {
- $command = $path . DIRECTORY_SEPARATOR . $name;
-
- MediaWiki\suppressWarnings();
- $file_exists = is_executable( $command );
- MediaWiki\restoreWarnings();
-
- if ( $file_exists ) {
- if ( !$versionInfo ) {
- return $command;
- }
-
- $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
- if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
- return $command;
- }
- }
- }
-
- return false;
- }
-
- /**
- * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
- * @see locateExecutable()
- * @param array $names Array of possible names.
- * @param array|bool $versionInfo Default: false or array with two members:
- * 0 => Command to run for version check, with $1 for the full executable name
- * 1 => String to compare the output with
- *
- * If $versionInfo is not false, only executables with a version
- * matching $versionInfo[1] will be returned.
- * @return bool|string
- */
- public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
- foreach ( self::getPossibleBinPaths() as $path ) {
- $exe = self::locateExecutable( $path, $names, $versionInfo );
- if ( $exe !== false ) {
- return $exe;
- }
- }
-
- return false;
- }
-