Backup of db before drupal security update
[yaffs-website] / web / core / modules / update / update.report.inc
1 <?php
2
3 /**
4  * @file
5  * Code required only when rendering the available updates report.
6  */
7
8 use Drupal\Component\Utility\Unicode;
9 use Drupal\Core\Template\Attribute;
10 use Drupal\Core\Url;
11
12 /**
13  * Prepares variables for project status report templates.
14  *
15  * Default template: update-report.html.twig.
16  *
17  * @param array $variables
18  *   An associative array containing:
19  *   - data: An array of data about each project's status.
20  */
21 function template_preprocess_update_report(&$variables) {
22   $data = $variables['data'];
23
24   $last = \Drupal::state()->get('update.last_check') ?: 0;
25
26   $variables['last_checked'] = [
27     '#theme' => 'update_last_check',
28     '#last' => $last,
29     // Attach the library to a variable that gets printed always.
30     '#attached' => [
31       'library' => [
32         'update/drupal.update.admin',
33       ],
34     ]
35   ];
36
37   // For no project update data, populate no data message.
38   if (empty($data)) {
39     $variables['no_updates_message'] = _update_no_data();
40   }
41
42   $rows = [];
43
44   foreach ($data as $project) {
45     $project_status = [
46       '#theme' => 'update_project_status',
47       '#project' => $project,
48     ];
49
50     // Build project rows.
51     if (!isset($rows[$project['project_type']])) {
52       $rows[$project['project_type']] = [
53         '#type' => 'table',
54         '#attributes' => ['class' => ['update']],
55       ];
56     }
57     $row_key = !empty($project['title']) ? Unicode::strtolower($project['title']) : Unicode::strtolower($project['name']);
58
59     // Add the project status row and details.
60     $rows[$project['project_type']][$row_key]['status'] = $project_status;
61
62     // Add project status class attribute to the table row.
63     switch ($project['status']) {
64       case UPDATE_CURRENT:
65         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-success']];
66         break;
67       case UPDATE_UNKNOWN:
68       case UPDATE_FETCH_PENDING:
69       case UPDATE_NOT_FETCHED:
70       case UPDATE_NOT_SECURE:
71       case UPDATE_REVOKED:
72       case UPDATE_NOT_SUPPORTED:
73         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-error']];
74         break;
75       case UPDATE_NOT_CHECKED:
76       case UPDATE_NOT_CURRENT:
77       default:
78         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-warning']];
79         break;
80     }
81   }
82
83   $project_types = [
84     'core' => t('Drupal core'),
85     'module' => t('Modules'),
86     'theme' => t('Themes'),
87     'module-disabled' => t('Uninstalled modules'),
88     'theme-disabled' => t('Uninstalled themes'),
89   ];
90
91   $variables['project_types'] = [];
92   foreach ($project_types as $type_name => $type_label) {
93     if (!empty($rows[$type_name])) {
94       ksort($rows[$type_name]);
95       $variables['project_types'][] = [
96         'label' => $type_label,
97         'table' => $rows[$type_name],
98       ];
99     }
100   }
101 }
102
103 /**
104  * Prepares variables for update project status templates.
105  *
106  * Default template: update-project-status.html.twig.
107  *
108  * @param array $variables
109  *   An associative array containing:
110  *   - project: An array of information about the project.
111  */
112 function template_preprocess_update_project_status(&$variables) {
113   // Storing by reference because we are sorting the project values.
114   $project = &$variables['project'];
115
116   // Set the project title and URL.
117   $variables['title'] = (isset($project['title'])) ? $project['title'] : $project['name'];
118   $variables['url'] = (isset($project['link'])) ? Url::fromUri($project['link'])->toString() : NULL;
119
120   $variables['install_type'] = $project['install_type'];
121   if ($project['install_type'] == 'dev' && !empty($project['datestamp'])) {
122     $variables['datestamp'] = format_date($project['datestamp'], 'custom', 'Y-M-d');
123   }
124
125   $variables['existing_version'] = $project['existing_version'];
126
127   $versions_inner = [];
128   $security_class = [];
129   $version_class = [];
130   if (isset($project['recommended'])) {
131     if ($project['status'] != UPDATE_CURRENT || $project['existing_version'] !== $project['recommended']) {
132
133       // First, figure out what to recommend.
134       // If there's only 1 security update and it has the same version we're
135       // recommending, give it the same CSS class as if it was recommended,
136       // but don't print out a separate "Recommended" line for this project.
137       if (!empty($project['security updates'])
138         && count($project['security updates']) == 1
139         && $project['security updates'][0]['version'] === $project['recommended']
140       ) {
141         $security_class[] = 'project-update__version--recommended';
142         $security_class[] = 'project-update__version---strong';
143       }
144       else {
145         $version_class[] = 'project-update__version--recommended';
146         // Apply an extra class if we're displaying both a recommended
147         // version and anything else for an extra visual hint.
148         if ($project['recommended'] !== $project['latest_version']
149           || !empty($project['also'])
150           || ($project['install_type'] == 'dev'
151             && isset($project['dev_version'])
152             && $project['latest_version'] !== $project['dev_version']
153             && $project['recommended'] !== $project['dev_version'])
154           || (isset($project['security updates'][0])
155             && $project['recommended'] !== $project['security updates'][0])
156         ) {
157           $version_class[] = 'project-update__version--recommended-strong';
158         }
159         $versions_inner[] = [
160           '#theme' => 'update_version',
161           '#version' => $project['releases'][$project['recommended']],
162           '#title' => t('Recommended version:'),
163           '#attributes' => ['class' => $version_class],
164         ];
165       }
166
167       // Now, print any security updates.
168       if (!empty($project['security updates'])) {
169         $security_class[] = 'version-security';
170         foreach ($project['security updates'] as $security_update) {
171           $versions_inner[] = [
172             '#theme' => 'update_version',
173             '#version' => $security_update,
174             '#title' => t('Security update:'),
175             '#attributes' => ['class' => $security_class],
176           ];
177         }
178       }
179     }
180
181     if ($project['recommended'] !== $project['latest_version']) {
182       $versions_inner[] = [
183         '#theme' => 'update_version',
184         '#version' => $project['releases'][$project['latest_version']],
185         '#title' => t('Latest version:'),
186         '#attributes' => ['class' => ['version-latest']],
187       ];
188     }
189     if ($project['install_type'] == 'dev'
190       && $project['status'] != UPDATE_CURRENT
191       && isset($project['dev_version'])
192       && $project['recommended'] !== $project['dev_version']) {
193       $versions_inner[] = [
194         '#theme' => 'update_version',
195         '#version' => $project['releases'][$project['dev_version']],
196         '#title' => t('Development version:'),
197         '#attributes' => ['class' => ['version-latest']],
198       ];
199     }
200   }
201
202   if (isset($project['also'])) {
203     foreach ($project['also'] as $also) {
204       $versions_inner[] = [
205         '#theme' => 'update_version',
206         '#version' => $project['releases'][$also],
207         '#title' => t('Also available:'),
208         '#attributes' => ['class' => ['version-also-available']],
209       ];
210     }
211   }
212
213   if (!empty($versions_inner)) {
214     $variables['versions'] = $versions_inner;
215   }
216
217   if (!empty($project['disabled'])) {
218     sort($project['disabled']);
219     $variables['disabled'] = $project['disabled'];
220   }
221
222   sort($project['includes']);
223   $variables['includes'] = $project['includes'];
224
225   $variables['extras'] = [];
226   if (!empty($project['extra'])) {
227     foreach ($project['extra'] as $value) {
228       $extra_item = [];
229       $extra_item['attributes'] = new Attribute();
230       $extra_item['label'] = $value['label'];
231       $extra_item['data'] = [
232         '#prefix' => '<em>',
233         '#markup' => $value['data'],
234         '#suffix' => '</em>'
235       ];
236       $variables['extras'][] = $extra_item;
237     }
238   }
239
240   // Set the project status details.
241   $status_label = NULL;
242   switch ($project['status']) {
243     case UPDATE_NOT_SECURE:
244       $status_label = t('Security update required!');
245       break;
246     case UPDATE_REVOKED:
247       $status_label = t('Revoked!');
248       break;
249     case UPDATE_NOT_SUPPORTED:
250       $status_label = t('Not supported!');
251       break;
252     case UPDATE_NOT_CURRENT:
253       $status_label = t('Update available');
254       break;
255     case UPDATE_CURRENT:
256       $status_label = t('Up to date');
257       break;
258   }
259   $variables['status']['label'] = $status_label;
260   $variables['status']['attributes'] = new Attribute();
261   $variables['status']['reason'] = (isset($project['reason'])) ? $project['reason'] : NULL;
262
263   switch ($project['status']) {
264     case UPDATE_CURRENT:
265       $uri = 'core/misc/icons/73b355/check.svg';
266       $text = t('Ok');
267       break;
268     case UPDATE_UNKNOWN:
269     case UPDATE_FETCH_PENDING:
270     case UPDATE_NOT_FETCHED:
271       $uri = 'core/misc/icons/e29700/warning.svg';
272       $text = t('Warning');
273       break;
274     case UPDATE_NOT_SECURE:
275     case UPDATE_REVOKED:
276     case UPDATE_NOT_SUPPORTED:
277       $uri = 'core/misc/icons/e32700/error.svg';
278       $text = t('Error');
279       break;
280     case UPDATE_NOT_CHECKED:
281     case UPDATE_NOT_CURRENT:
282     default:
283       $uri = 'core/misc/icons/e29700/warning.svg';
284       $text = t('Warning');
285       break;
286   }
287
288   $variables['status']['icon'] = [
289     '#theme' => 'image',
290     '#width' => 18,
291     '#height' => 18,
292     '#uri' => $uri,
293     '#alt' => $text,
294     '#title' => $text,
295   ];
296 }