comparison core/modules/update/update.install @ 0:c75dbcec494b

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