ReleaseInfo.php

  1. 8.0.x lib/Drush/UpdateService/ReleaseInfo.php
  2. 7.x lib/Drush/UpdateService/ReleaseInfo.php
  3. master lib/Drush/UpdateService/ReleaseInfo.php

Drush release info engine for update.drupal.org and compatible services.

This engine does connect directly to the update service. It doesn't depend on a bootstrapped site.

Namespace

Drush\UpdateService

Classes

Namesort ascending Description
ReleaseInfo Release info engine class.

File

lib/Drush/UpdateService/ReleaseInfo.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush release info engine for update.drupal.org and compatible services.
  5. *
  6. * This engine does connect directly to the update service. It doesn't depend
  7. * on a bootstrapped site.
  8. */
  9. namespace Drush\UpdateService;
  10. use Drush\Log\LogLevel;
  11. /**
  12. * Release info engine class.
  13. */
  14. class ReleaseInfo {
  15. const DEFAULT_URL = 'https://updates.drupal.org/release-history';
  16. // Cache release xml files for 24h by default.
  17. const CACHE_LIFETIME = 86400;
  18. private $cache;
  19. private $engine_config;
  20. /**
  21. * Constructor.
  22. */
  23. public function __construct($type, $engine, $config) {
  24. $this->engine_type = $type;
  25. $this->engine = $engine;
  26. if (is_null($config)) {
  27. $config = array();
  28. }
  29. $config += array(
  30. 'cache-duration' => drush_get_option('cache-duration-releasexml', self::CACHE_LIFETIME),
  31. );
  32. $this->engine_config = $config;
  33. $this->cache = array();
  34. }
  35. /**
  36. * Returns configured cache duration.
  37. */
  38. public function getCacheDuration() {
  39. return $this->engine_config['cache-duration'];
  40. }
  41. /**
  42. * Returns a project's release info from the update service.
  43. *
  44. * @param array $request
  45. * A request array.
  46. *
  47. * @param bool $refresh
  48. * Whether to discard cached object.
  49. *
  50. * @return \Drush\UpdateService\Project
  51. */
  52. public function get($request, $refresh = FALSE) {
  53. if ($refresh || !isset($this->cache[$request['name']])) {
  54. $project_release_info = Project::getInstance($request, $this->getCacheDuration());
  55. if ($project_release_info && !$project_release_info->isValid()) {
  56. $project_release_info = FALSE;
  57. }
  58. $this->cache[$request['name']] = $project_release_info;
  59. }
  60. return $this->cache[$request['name']];
  61. }
  62. /**
  63. * Delete cached update service file of a project.
  64. *
  65. * @param array $request
  66. * A request array.
  67. */
  68. public function clearCached(array $request) {
  69. if (isset($this->cache[$request['name']])) {
  70. unset($this->cache[$request['name']]);
  71. }
  72. $url = Project::buildFetchUrl($request);
  73. $cache_file = drush_download_file_name($url);
  74. if (file_exists($cache_file)) {
  75. unlink($cache_file);
  76. }
  77. }
  78. /**
  79. * Select the most appropriate release for a project, based on a strategy.
  80. *
  81. * @param Array &$request
  82. * A request array.
  83. * The array will be expanded with the project type.
  84. * @param String $restrict_to
  85. * One of:
  86. * 'dev': Forces choosing a -dev release.
  87. * 'version': Forces choosing a point release.
  88. * '': No restriction.
  89. * Default is ''.
  90. * @param String $select
  91. * Strategy for selecting a release, should be one of:
  92. * - auto: Try to select the latest release, if none found allow the user
  93. * to choose.
  94. * - always: Force the user to choose a release.
  95. * - never: Try to select the latest release, if none found then fail.
  96. * - ignore: Ignore and return NULL.
  97. * If no supported release is found, allow to ask the user to choose one.
  98. * @param Boolean $all
  99. * In case $select = TRUE this indicates that all available releases will be
  100. * offered the user to choose.
  101. *
  102. * @return array
  103. * The selected release.
  104. */
  105. public function selectReleaseBasedOnStrategy($request, $restrict_to = '', $select = 'never', $all = FALSE, $version = NULL) {
  106. if (!in_array($select, array('auto', 'never', 'always', 'ignore'))) {
  107. return drush_set_error('DRUSH_PM_UNKNOWN_SELECT_STRATEGY', dt("Error: select strategy must be one of: auto, never, always, ignore", array()));
  108. }
  109. $project_release_info = $this->get($request);
  110. if (!$project_release_info) {
  111. return FALSE;
  112. }
  113. if ($select != 'always') {
  114. if (isset($request['version'])) {
  115. $release = $project_release_info->getSpecificRelease($request['version']);
  116. if ($release === FALSE) {
  117. return drush_set_error('DRUSH_PM_COULD_NOT_FIND_VERSION', dt("Could not locate !project version !version.", array(
  118. '!project' => $request['name'],
  119. '!version' => $request['version'],
  120. )));
  121. }
  122. }
  123. if ($restrict_to == 'dev') {
  124. // If you specified a specific release AND --dev, that is either
  125. // redundant (okay), or contradictory (error).
  126. if (!empty($release)) {
  127. if ($release['version_extra'] != 'dev') {
  128. return drush_set_error('DRUSH_PM_COULD_NOT_FIND_VERSION', dt("You requested both --dev and !project version !version, which is not a '-dev' release.", array(
  129. '!project' => $request['name'],
  130. '!version' => $request['version'],
  131. )));
  132. }
  133. }
  134. else {
  135. $release = $project_release_info->getDevRelease();
  136. if ($release === FALSE) {
  137. return drush_set_error('DRUSH_PM_NO_DEV_RELEASE', dt('There is no development release for project !project.', array('!project' => $request['name'])));
  138. }
  139. }
  140. }
  141. // If there was no specific release requested, try to identify the most appropriate release.
  142. if (empty($release)) {
  143. $release = $project_release_info->getRecommendedOrSupportedRelease();
  144. }
  145. if ($release) {
  146. return $release;
  147. }
  148. else {
  149. $message = dt('There are no stable releases for project !project.', array('!project' => $request['name']));
  150. if ($select == 'never') {
  151. return drush_set_error('DRUSH_PM_NO_STABLE_RELEASE', $message);
  152. }
  153. drush_log($message, LogLevel::WARNING);
  154. if ($select == 'ignore') {
  155. return NULL;
  156. }
  157. }
  158. }
  159. // At this point the only chance is to ask the user to choose a release.
  160. if ($restrict_to == 'dev') {
  161. $filter = 'dev';
  162. }
  163. elseif ($all) {
  164. $filter = 'all';
  165. }
  166. else {
  167. $filter = '';
  168. }
  169. $releases = $project_release_info->filterReleases($filter, $version);
  170. $options = array();
  171. foreach($releases as $release) {
  172. $options[$release['version']] = array($release['version'], '-', gmdate('Y-M-d', $release['date']), '-', implode(', ', $release['release_status']));
  173. }
  174. $choice = drush_choice($options, dt('Choose one of the available releases for !project:', array('!project' => $request['name'])));
  175. if (!$choice) {
  176. return drush_user_abort();
  177. }
  178. return $releases[$choice];
  179. }
  180. /**
  181. * Check if a project is available in the update service.
  182. *
  183. * Optionally check for consistency by comparing given project type and
  184. * the type obtained from the update service.
  185. *
  186. * @param array $request
  187. * A request array.
  188. * @param string $type
  189. * Optional. If provided, will do a consistent check of the project type.
  190. *
  191. * @return boolean
  192. * True if the project exists and type matches.
  193. */
  194. public function checkProject($request, $type = NULL) {
  195. $project_release_info = $this->get($request);
  196. if (!$project_release_info) {
  197. return FALSE;
  198. }
  199. if ($type) {
  200. if ($project_release_info->getType() != $type) {
  201. return FALSE;
  202. }
  203. }
  204. return TRUE;
  205. }
  206. }