Don't use `phpcs:ignoreFile` to selectively ignore sniffs
[lhc/web/wiklou.git] / includes / utils / ExecutableFinder.php
1 <?php
2 /**
3 * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org>
4 *
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.
9 *
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.
14 *
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.
18 *
19 */
20
21 use MediaWiki\Shell\Shell;
22
23 /**
24 * Utility class to find executables in likely places
25 *
26 * @since 1.31
27 */
28 class ExecutableFinder {
29
30 /**
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?)
34 *
35 * @return array
36 */
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' ) )
42 ) );
43 }
44
45 /**
46 * Search a path for any of the given executable names. Returns the
47 * executable name if found. Also checks the version string returned
48 * by each executable.
49 *
50 * Used only by environment checks.
51 *
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
57 *
58 * If $versionInfo is not false, only executables with a version
59 * matching $versionInfo[1] will be returned.
60 * @return bool|string
61 */
62 protected static function findExecutable( $path, $name, $versionInfo = false ) {
63 $command = $path . DIRECTORY_SEPARATOR . $name;
64
65 Wikimedia\suppressWarnings();
66 $file_exists = is_executable( $command );
67 Wikimedia\restoreWarnings();
68
69 if ( $file_exists ) {
70 if ( !$versionInfo ) {
71 return $command;
72 }
73
74 $output = Shell::command( $command, $versionInfo[0] )
75 ->includeStderr()->execute()->getStdout();
76 if ( strstr( $output, $versionInfo[1] ) !== false ) {
77 return $command;
78 }
79 }
80
81 return false;
82 }
83
84 /**
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
91 *
92 * If $versionInfo is not false, only executables with a version
93 * matching $versionInfo[1] will be returned.
94 * @return bool|string
95 */
96 public static function findInDefaultPaths( $names, $versionInfo = false ) {
97 $paths = self::getPossibleBinPaths();
98 foreach ( (array)$names as $name ) {
99 foreach ( $paths as $path ) {
100 $exe = self::findExecutable( $path, $name, $versionInfo );
101 if ( $exe !== false ) {
102 return $exe;
103 }
104 }
105 }
106
107 return false;
108 }
109
110 }