update.install 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Update Manager module.
  5. */
  6. use Drupal\Core\Link;
  7. use Drupal\Core\Url;
  8. use Drupal\update\ProjectSecurityData;
  9. use Drupal\update\ProjectSecurityRequirement;
  10. use Drupal\update\UpdateFetcherInterface;
  11. use Drupal\update\UpdateManagerInterface;
  12. /**
  13. * Implements hook_requirements().
  14. *
  15. * @return
  16. * An array describing the status of the site regarding available updates. If
  17. * there is no update data, only one record will be returned, indicating that
  18. * the status of core can't be determined. If data is available, there will be
  19. * two records: one for core, and another for all of contrib (assuming there
  20. * are any contributed modules or themes enabled on the site). In addition to
  21. * the fields expected by hook_requirements ('value', 'severity', and
  22. * optionally 'description'), this array will contain a 'reason' attribute,
  23. * which is an integer constant to indicate why the given status is being
  24. * returned (UpdateManagerInterface::NOT_SECURE,
  25. * UpdateManagerInterface::NOT_CURRENT, or UpdateManagerInterface::UNKNOWN).
  26. * This is used for generating the appropriate email notification messages
  27. * during update_cron(), and might be useful for other modules that invoke
  28. * update_requirements() to find out if the site is up to date or not.
  29. *
  30. * @see _update_message_text()
  31. * @see _update_cron_notify()
  32. * @see \Drupal\update\UpdateManagerInterface
  33. */
  34. function update_requirements($phase) {
  35. $requirements = [];
  36. if ($phase == 'runtime') {
  37. if ($available = update_get_available(FALSE)) {
  38. module_load_include('inc', 'update', 'update.compare');
  39. $data = update_calculate_project_data($available);
  40. // First, populate the requirements for core:
  41. $requirements['update_core'] = _update_requirement_check($data['drupal'], 'core');
  42. if (!empty($available['drupal']['releases'])) {
  43. $security_data = ProjectSecurityData::createFromProjectDataAndReleases($data['drupal'], $available['drupal']['releases'])->getCoverageInfo();
  44. if ($core_coverage_requirement = ProjectSecurityRequirement::createFromProjectDataAndSecurityCoverageInfo($data['drupal'], $security_data)->getRequirement()) {
  45. $requirements['coverage_core'] = $core_coverage_requirement;
  46. }
  47. }
  48. // We don't want to check drupal a second time.
  49. unset($data['drupal']);
  50. if (!empty($data)) {
  51. // Now, sort our $data array based on each project's status. The
  52. // status constants are numbered in the right order of precedence, so
  53. // we just need to make sure the projects are sorted in ascending
  54. // order of status, and we can look at the first project we find.
  55. uasort($data, '_update_project_status_sort');
  56. $first_project = reset($data);
  57. $requirements['update_contrib'] = _update_requirement_check($first_project, 'contrib');
  58. }
  59. }
  60. else {
  61. $requirements['update_core']['title'] = t('Drupal core update status');
  62. $requirements['update_core']['value'] = t('No update data available');
  63. $requirements['update_core']['severity'] = REQUIREMENT_WARNING;
  64. $requirements['update_core']['reason'] = UpdateFetcherInterface::UNKNOWN;
  65. $requirements['update_core']['description'] = _update_no_data();
  66. }
  67. }
  68. return $requirements;
  69. }
  70. /**
  71. * Implements hook_install().
  72. */
  73. function update_install() {
  74. $queue = \Drupal::queue('update_fetch_tasks', TRUE);
  75. $queue->createQueue();
  76. }
  77. /**
  78. * Implements hook_uninstall().
  79. */
  80. function update_uninstall() {
  81. \Drupal::state()->delete('update.last_check');
  82. \Drupal::state()->delete('update.last_email_notification');
  83. $queue = \Drupal::queue('update_fetch_tasks');
  84. $queue->deleteQueue();
  85. }
  86. /**
  87. * Fills in the requirements array.
  88. *
  89. * This is shared for both core and contrib to generate the right elements in
  90. * the array for hook_requirements().
  91. *
  92. * @param $project
  93. * Array of information about the project we're testing as returned by
  94. * update_calculate_project_data().
  95. * @param $type
  96. * What kind of project this is ('core' or 'contrib').
  97. *
  98. * @return
  99. * An array to be included in the nested $requirements array.
  100. *
  101. * @see hook_requirements()
  102. * @see update_requirements()
  103. * @see update_calculate_project_data()
  104. */
  105. function _update_requirement_check($project, $type) {
  106. $requirement = [];
  107. if ($type == 'core') {
  108. $requirement['title'] = t('Drupal core update status');
  109. }
  110. else {
  111. $requirement['title'] = t('Module and theme update status');
  112. }
  113. $status = $project['status'];
  114. if ($status != UpdateManagerInterface::CURRENT) {
  115. $requirement['reason'] = $status;
  116. $requirement['severity'] = REQUIREMENT_ERROR;
  117. // When updates are available, append the available updates link to the
  118. // message from _update_message_text(), and format the two translated
  119. // strings together in a single paragraph.
  120. $requirement['description'][] = ['#markup' => _update_message_text($type, $status)];
  121. if (!in_array($status, [UpdateFetcherInterface::UNKNOWN, UpdateFetcherInterface::NOT_CHECKED, UpdateFetcherInterface::NOT_FETCHED, UpdateFetcherInterface::FETCH_PENDING])) {
  122. if (_update_manager_access()) {
  123. $requirement['description'][] = ['#prefix' => ' ', '#markup' => t('See the <a href=":available_updates">available updates</a> page for more information and to install your missing updates.', [':available_updates' => Url::fromRoute('update.report_update')->toString()])];
  124. }
  125. else {
  126. $requirement['description'][] = ['#prefix' => ' ', '#markup' => t('See the <a href=":available_updates">available updates</a> page for more information.', [':available_updates' => Url::fromRoute('update.status')->toString()])];
  127. }
  128. }
  129. }
  130. switch ($status) {
  131. case UpdateManagerInterface::NOT_SECURE:
  132. $requirement_label = t('Not secure!');
  133. break;
  134. case UpdateManagerInterface::REVOKED:
  135. $requirement_label = t('Revoked!');
  136. break;
  137. case UpdateManagerInterface::NOT_SUPPORTED:
  138. $requirement_label = t('Unsupported release');
  139. break;
  140. case UpdateManagerInterface::NOT_CURRENT:
  141. $requirement_label = t('Out of date');
  142. $requirement['severity'] = REQUIREMENT_WARNING;
  143. break;
  144. case UpdateFetcherInterface::UNKNOWN:
  145. case UpdateFetcherInterface::NOT_CHECKED:
  146. case UpdateFetcherInterface::NOT_FETCHED:
  147. case UpdateFetcherInterface::FETCH_PENDING:
  148. $requirement_label = isset($project['reason']) ? $project['reason'] : t('Can not determine status');
  149. $requirement['severity'] = REQUIREMENT_WARNING;
  150. break;
  151. default:
  152. $requirement_label = t('Up to date');
  153. }
  154. if ($status != UpdateManagerInterface::CURRENT && $type == 'core' && isset($project['recommended'])) {
  155. $requirement_label .= ' ' . t('(version @version available)', ['@version' => $project['recommended']]);
  156. }
  157. $requirement['value'] = Link::fromTextAndUrl($requirement_label, Url::fromRoute(_update_manager_access() ? 'update.report_update' : 'update.status'))->toString();
  158. return $requirement;
  159. }
  160. /**
  161. * Rebuild the router to ensure admin/reports/updates/check has CSRF protection.
  162. */
  163. function update_update_8001() {
  164. // Empty update forces a call to drupal_flush_all_caches() which rebuilds the
  165. // router.
  166. // Use hook_post_update_NAME() instead to clear the cache.The use
  167. // of hook_update_N to clear the cache has been deprecated see
  168. // https://www.drupal.org/node/2960601 for more details.
  169. }