MediaWiki REL1_40
ExecutableFinder.php
Go to the documentation of this file.
1<?php
22use Wikimedia\AtEase\AtEase;
23
30
38 protected static function getPossibleBinPaths() {
39 return array_unique( array_merge(
40 [ '/usr/bin', '/bin', '/usr/local/bin', '/opt/csw/bin',
41 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ],
42 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
43 ) );
44 }
45
63 protected static function findExecutable( $path, $name, $versionInfo = false ) {
64 $command = $path . DIRECTORY_SEPARATOR . $name;
65
66 AtEase::suppressWarnings();
67 $file_exists = is_executable( $command );
68 AtEase::restoreWarnings();
69
70 if ( $file_exists ) {
71 if ( !$versionInfo ) {
72 return $command;
73 }
74
75 $output = Shell::command( $command, $versionInfo[0] )
76 ->includeStderr()->execute()->getStdout();
77 if ( str_contains( $output, $versionInfo[1] ) ) {
78 return $command;
79 }
80 }
81
82 return false;
83 }
84
97 public static function findInDefaultPaths( $names, $versionInfo = false ) {
98 if ( Shell::isDisabled() ) {
99 // If we can't shell out, there's no point looking for executables
100 return false;
101 }
102
103 $paths = self::getPossibleBinPaths();
104 foreach ( (array)$names as $name ) {
105 foreach ( $paths as $path ) {
106 $exe = self::findExecutable( $path, $name, $versionInfo );
107 if ( $exe !== false ) {
108 return $exe;
109 }
110 }
111 }
112
113 return false;
114 }
115
116}
Utility class to find executables in likely places.
static getPossibleBinPaths()
Get an array of likely places we can find executables.
static findExecutable( $path, $name, $versionInfo=false)
Search a path for any of the given executable names.
static findInDefaultPaths( $names, $versionInfo=false)
Same as locateExecutable(), but checks in getPossibleBinPaths() by default.
Executes shell commands.
Definition Shell.php:46