MediaWiki master
ExecutableFinder.php
Go to the documentation of this file.
1<?php
8namespace MediaWiki\Utils;
9
11
18
26 protected static function getPossibleBinPaths() {
27 return array_unique( array_merge(
28 [ '/usr/bin', '/bin', '/usr/local/bin', '/opt/csw/bin',
29 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ],
30 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
31 ) );
32 }
33
51 protected static function findExecutable( $path, $name, $versionInfo = false ) {
52 $command = $path . DIRECTORY_SEPARATOR . $name;
53
54 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
55 $file_exists = @is_executable( $command );
56
57 if ( $file_exists ) {
58 if ( !$versionInfo ) {
59 return $command;
60 }
61
62 $output = Shell::command( $command, $versionInfo[0] )
63 ->includeStderr()->execute()->getStdout();
64 if ( str_contains( $output, $versionInfo[1] ) ) {
65 return $command;
66 }
67 }
68
69 return false;
70 }
71
84 public static function findInDefaultPaths( $names, $versionInfo = false ) {
85 if ( Shell::isDisabled() ) {
86 // If we can't shell out, there's no point looking for executables
87 return false;
88 }
89
91 foreach ( (array)$names as $name ) {
92 foreach ( $paths as $path ) {
93 $exe = self::findExecutable( $path, $name, $versionInfo );
94 if ( $exe !== false ) {
95 return $exe;
96 }
97 }
98 }
99
100 return false;
101 }
102
103}
104
106class_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.
Copyright (C) 2017 Kunal Mehta legoktm@debian.org