ExecutableFinder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. use MediaWiki\Shell\Shell;
  21. /**
  22. * Utility class to find executables in likely places
  23. *
  24. * @since 1.31
  25. */
  26. class ExecutableFinder {
  27. /**
  28. * Get an array of likely places we can find executables. Check a bunch
  29. * of known Unix-like defaults, as well as the PATH environment variable
  30. * (which should maybe make it work for Windows?)
  31. *
  32. * @return array
  33. */
  34. protected static function getPossibleBinPaths() {
  35. return array_unique( array_merge(
  36. [ '/usr/bin', '/bin', '/usr/local/bin', '/opt/csw/bin',
  37. '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ],
  38. explode( PATH_SEPARATOR, getenv( 'PATH' ) )
  39. ) );
  40. }
  41. /**
  42. * Search a path for any of the given executable names. Returns the
  43. * executable name if found. Also checks the version string returned
  44. * by each executable.
  45. *
  46. * Used only by environment checks.
  47. *
  48. * @param string $path Path to search
  49. * @param string $name Executable name to look for
  50. * @param array|bool $versionInfo False or array with two members:
  51. * 0 => Parameter to pass to binary for version check (e.g. --version)
  52. * 1 => String to compare the output with
  53. *
  54. * If $versionInfo is not false, only executables with a version
  55. * matching $versionInfo[1] will be returned.
  56. * @return bool|string
  57. */
  58. protected static function findExecutable( $path, $name, $versionInfo = false ) {
  59. $command = $path . DIRECTORY_SEPARATOR . $name;
  60. Wikimedia\suppressWarnings();
  61. $file_exists = is_executable( $command );
  62. Wikimedia\restoreWarnings();
  63. if ( $file_exists ) {
  64. if ( !$versionInfo ) {
  65. return $command;
  66. }
  67. $output = Shell::command( $command, $versionInfo[0] )
  68. ->includeStderr()->execute()->getStdout();
  69. if ( strstr( $output, $versionInfo[1] ) !== false ) {
  70. return $command;
  71. }
  72. }
  73. return false;
  74. }
  75. /**
  76. * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
  77. * @see locateExecutable()
  78. * @param string|string[] $names Array of possible names.
  79. * @param array|bool $versionInfo Default: false or array with two members:
  80. * 0 => Parameter to run for version check, e.g. '--version'
  81. * 1 => String to compare the output with
  82. *
  83. * If $versionInfo is not false, only executables with a version
  84. * matching $versionInfo[1] will be returned.
  85. * @return bool|string
  86. */
  87. public static function findInDefaultPaths( $names, $versionInfo = false ) {
  88. if ( Shell::isDisabled() ) {
  89. // If we can't shell out, there's no point looking for executables
  90. return false;
  91. }
  92. $paths = self::getPossibleBinPaths();
  93. foreach ( (array)$names as $name ) {
  94. foreach ( $paths as $path ) {
  95. $exe = self::findExecutable( $path, $name, $versionInfo );
  96. if ( $exe !== false ) {
  97. return $exe;
  98. }
  99. }
  100. }
  101. return false;
  102. }
  103. }