/[drupal]/drupal/modules/update/update.module
ViewVC logotype

Contents of /drupal/modules/update/update.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.53 - (show annotations) (download) (as text)
Sun Oct 25 19:52:47 2009 UTC (4 weeks, 2 days ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.52: +11 -4 lines
File MIME type: text/x-php
- Patch #602490 by dww: fixed help text and action links to find the update manager pages.
1 <?php
2 // $Id: update.module,v 1.52 2009/10/24 11:41:05 dries Exp $
3
4 /**
5 * @file
6 * The "Update status" module checks for available updates of Drupal core and
7 * any installed contributed modules and themes. It warns site administrators
8 * if newer releases are available via the system status report
9 * (admin/reports/status), the module and theme pages, and optionally via email.
10 */
11
12 /**
13 * URL to check for updates, if a given project doesn't define its own.
14 */
15 define('UPDATE_DEFAULT_URL', 'http://updates.drupal.org/release-history');
16
17 // These are internally used constants for this code, do not modify.
18
19 /**
20 * Project is missing security update(s).
21 */
22 define('UPDATE_NOT_SECURE', 1);
23
24 /**
25 * Current release has been unpublished and is no longer available.
26 */
27 define('UPDATE_REVOKED', 2);
28
29 /**
30 * Current release is no longer supported by the project maintainer.
31 */
32 define('UPDATE_NOT_SUPPORTED', 3);
33
34 /**
35 * Project has a new release available, but it is not a security release.
36 */
37 define('UPDATE_NOT_CURRENT', 4);
38
39 /**
40 * Project is up to date.
41 */
42 define('UPDATE_CURRENT', 5);
43
44 /**
45 * Project's status cannot be checked.
46 */
47 define('UPDATE_NOT_CHECKED', -1);
48
49 /**
50 * No available update data was found for project.
51 */
52 define('UPDATE_UNKNOWN', -2);
53
54 /**
55 * There was a failure fetching available update data for this project.
56 */
57 define('UPDATE_NOT_FETCHED', -3);
58
59 /**
60 * We need to (re)fetch available update data for this project.
61 */
62 define('UPDATE_FETCH_PENDING', -4);
63
64 /**
65 * Maximum number of attempts to fetch available update data from a given host.
66 */
67 define('UPDATE_MAX_FETCH_ATTEMPTS', 2);
68
69 /**
70 * Maximum number of seconds to try fetching available update data at a time.
71 */
72 define('UPDATE_MAX_FETCH_TIME', 5);
73
74 /**
75 * Implement hook_help().
76 */
77 function update_help($path, $arg) {
78 switch ($path) {
79 case 'admin/reports/updates':
80 return '<p>' . t('Here you can find information about available updates for your installed modules and themes. Note that each module or theme is part of a "project", which may or may not have the same name, and might include multiple modules or themes within it.') . '</p>';
81
82 case 'admin/appearance':
83 case 'admin/config/modules':
84 include_once DRUPAL_ROOT . '/includes/install.inc';
85 $status = update_requirements('runtime');
86 foreach (array('core', 'contrib') as $report_type) {
87 $type = 'update_' . $report_type;
88 if (isset($status[$type]['severity'])) {
89 if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
90 drupal_set_message($status[$type]['description'], 'error');
91 }
92 elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
93 drupal_set_message($status[$type]['description'], 'warning');
94 }
95 }
96 }
97
98 case 'admin/appearance/update':
99 case 'admin/appearance/install':
100 case 'admin/config/modules/update':
101 case 'admin/config/modules/install':
102 case 'admin/reports/updates/update':
103 case 'admin/reports/updates/install':
104 case 'admin/reports/updates/settings':
105 case 'admin/reports/status':
106 case 'admin/update/confirm':
107 // These pages don't need additional nagging.
108 break;
109
110 case 'admin/help#update':
111 $output = '<p>' . t("The Update status module periodically checks for new versions of your site's software (including contributed modules and themes), and alerts you to available updates.") . '</p>';
112 $output .= '<p>' . t('The <a href="@update-report">report of available updates</a> will alert you when new releases are available for download. You may configure options for update checking frequency and notifications at the <a href="@update-settings">Update status module settings page</a>.', array('@update-report' => url('admin/reports/updates'), '@update-settings' => url('admin/reports/updates/settings'))) . '</p>';
113 $output .= '<p>' . t('Please note that in order to provide this information, anonymous usage statistics are sent to drupal.org. If desired, you may disable the Update status module from the <a href="@modules">module administration page</a>.', array('@modules' => url('admin/config/modules'))) . '</p>';
114 $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@update">Update status module</a>.', array('@update' => 'http://drupal.org/handbook/modules/update')) . '</p>';
115 return $output;
116
117 default:
118 // Otherwise, if we're on *any* admin page and there's a security
119 // update missing, print an error message about it.
120 if (arg(0) == 'admin' && strpos($path, '#') === FALSE
121 && user_access('administer site configuration')) {
122 include_once DRUPAL_ROOT . '/includes/install.inc';
123 $status = update_requirements('runtime');
124 foreach (array('core', 'contrib') as $report_type) {
125 $type = 'update_' . $report_type;
126 if (isset($status[$type])
127 && isset($status[$type]['reason'])
128 && $status[$type]['reason'] === UPDATE_NOT_SECURE) {
129 drupal_set_message($status[$type]['description'], 'error');
130 }
131 }
132 }
133
134 }
135 }
136
137 /**
138 * Implement hook_menu().
139 */
140 function update_menu() {
141 $items = array();
142
143 $items['admin/reports/updates'] = array(
144 'title' => 'Available updates',
145 'description' => 'Get a status report about available updates for your installed modules and themes.',
146 'page callback' => 'update_status',
147 'access arguments' => array('administer site configuration'),
148 'weight' => 10,
149 'file' => 'update.report.inc',
150 );
151 $items['admin/reports/updates/list'] = array(
152 'title' => 'List',
153 'access arguments' => array('administer site configuration'),
154 'type' => MENU_DEFAULT_LOCAL_TASK,
155 );
156 $items['admin/reports/updates/settings'] = array(
157 'title' => 'Settings',
158 'page callback' => 'drupal_get_form',
159 'page arguments' => array('update_settings'),
160 'access arguments' => array('administer site configuration'),
161 'file' => 'update.settings.inc',
162 'type' => MENU_LOCAL_TASK,
163 'weight' => 50,
164 );
165 $items['admin/reports/updates/check'] = array(
166 'title' => 'Manual update check',
167 'page callback' => 'update_manual_status',
168 'access arguments' => array('administer site configuration'),
169 'type' => MENU_CALLBACK,
170 'file' => 'update.fetch.inc',
171 );
172
173 // We want action links for updating projects at a few different locations:
174 // both the module and theme administration pages, and on the available
175 // updates report itself. The menu items will be mostly identical, except the
176 // paths and titles, so we just define them in a loop. We pass in a string
177 // indicating what context we're entering the action from, so that can
178 // customize the appearance as needed.
179 $paths = array(
180 'report' => 'admin/reports/updates',
181 'module' => 'admin/config/modules',
182 'theme' => 'admin/appearance',
183 );
184 foreach ($paths as $context => $path) {
185 $items[$path . '/install'] = array(
186 'page callback' => 'drupal_get_form',
187 'page arguments' => array('update_manager_install_form', $context),
188 'access callback' => 'update_manager_access',
189 'access arguments' => array(),
190 'weight' => 25,
191 'type' => MENU_LOCAL_ACTION,
192 'file' => 'update.manager.inc',
193 );
194 $items[$path . '/update'] = array(
195 'page callback' => 'drupal_get_form',
196 'page arguments' => array('update_manager_update_form', $context),
197 'access callback' => 'update_manager_access',
198 'access arguments' => array(),
199 'weight' => 20,
200 'type' => MENU_LOCAL_ACTION,
201 'file' => 'update.manager.inc',
202 );
203 }
204 // Customize the titles of the action links depending on where they appear.
205 $items['admin/reports/updates/install']['title'] = 'Install new module or theme';
206 $items['admin/reports/updates/update']['title'] = 'Update existing modules and themes';
207 $items['admin/config/modules/install']['title'] = 'Install new module';
208 $items['admin/config/modules/update']['title'] = 'Update existing modules';
209 $items['admin/appearance/install']['title'] = 'Install new theme';
210 $items['admin/appearance/update']['title'] = 'Update existing themes';
211
212 // Menu callback used for the confirmation page after all the releases
213 // have been downloaded, asking you to backup before installing updates.
214 $items['admin/update/confirm'] = array(
215 'title' => 'Confirm update',
216 'page callback' => 'drupal_get_form',
217 'page arguments' => array('update_manager_confirm_update_form'),
218 'access callback' => 'update_manager_access',
219 'access arguments' => array(),
220 'type' => MENU_CALLBACK,
221 'file' => 'update.manager.inc',
222 );
223
224 return $items;
225 }
226
227 /**
228 * Determine if the current user can access the updater menu items.
229 *
230 * This is used as a menu system access callback. It both enforces the
231 * 'administer software updates' permission and the global killswitch for the
232 * authorize.php script.
233 *
234 * @see update_menu()
235 */
236 function update_manager_access() {
237 return variable_get('allow_authorize_operations', TRUE) && user_access('administer software updates');
238 }
239
240 /**
241 * Implement hook_theme().
242 */
243 function update_theme() {
244 return array(
245 'update_manager_update_form' => array(
246 'render element' => 'form',
247 'file' => 'update.manager.inc',
248 ),
249 'update_last_check' => array(
250 'variables' => array('last' => NULL),
251 ),
252 'update_report' => array(
253 'variables' => array('data' => NULL),
254 ),
255 'update_version' => array(
256 'variables' => array('version' => NULL, 'tag' => NULL, 'class' => array()),
257 ),
258 'update_status_label' => array(
259 'variables' => array('status' => NULL),
260 ),
261 );
262 }
263
264 /**
265 * Implement hook_requirements().
266 *
267 * @return
268 * An array describing the status of the site regarding available updates.
269 * If there is no update data, only one record will be returned, indicating
270 * that the status of core can't be determined. If data is available, there
271 * will be two records: one for core, and another for all of contrib
272 * (assuming there are any contributed modules or themes enabled on the
273 * site). In addition to the fields expected by hook_requirements ('value',
274 * 'severity', and optionally 'description'), this array will contain a
275 * 'reason' attribute, which is an integer constant to indicate why the
276 * given status is being returned (UPDATE_NOT_SECURE, UPDATE_NOT_CURRENT, or
277 * UPDATE_UNKNOWN). This is used for generating the appropriate e-mail
278 * notification messages during update_cron(), and might be useful for other
279 * modules that invoke update_requirements() to find out if the site is up
280 * to date or not.
281 *
282 * @see _update_message_text()
283 * @see _update_cron_notify()
284 */
285 function update_requirements($phase) {
286 if ($phase == 'runtime') {
287 if ($available = update_get_available(FALSE)) {
288 module_load_include('inc', 'update', 'update.compare');
289 $data = update_calculate_project_data($available);
290 // First, populate the requirements for core:
291 $requirements['update_core'] = _update_requirement_check($data['drupal'], 'core');
292 // We don't want to check drupal a second time.
293 unset($data['drupal']);
294 if (!empty($data)) {
295 // Now, sort our $data array based on each project's status. The
296 // status constants are numbered in the right order of precedence, so
297 // we just need to make sure the projects are sorted in ascending
298 // order of status, and we can look at the first project we find.
299 uasort($data, '_update_project_status_sort');
300 $first_project = reset($data);
301 $requirements['update_contrib'] = _update_requirement_check($first_project, 'contrib');
302 }
303 }
304 else {
305 $requirements['update_core']['title'] = t('Drupal core update status');
306 $requirements['update_core']['value'] = t('No update data available');
307 $requirements['update_core']['severity'] = REQUIREMENT_WARNING;
308 $requirements['update_core']['reason'] = UPDATE_UNKNOWN;
309 $requirements['update_core']['description'] = _update_no_data();
310 }
311 return $requirements;
312 }
313 }
314
315 /**
316 * Private helper method to fill in the requirements array.
317 *
318 * This is shared for both core and contrib to generate the right elements in
319 * the array for hook_requirements().
320 *
321 * @param $project
322 * Array of information about the project we're testing as returned by
323 * update_calculate_project_data().
324 * @param $type
325 * What kind of project is this ('core' or 'contrib').
326 *
327 * @return
328 * An array to be included in the nested $requirements array.
329 *
330 * @see hook_requirements()
331 * @see update_requirements()
332 * @see update_calculate_project_data()
333 */
334 function _update_requirement_check($project, $type) {
335 $requirement = array();
336 if ($type == 'core') {
337 $requirement['title'] = t('Drupal core update status');
338 }
339 else {
340 $requirement['title'] = t('Module and theme update status');
341 }
342 $status = $project['status'];
343 if ($status != UPDATE_CURRENT) {
344 $requirement['reason'] = $status;
345 $requirement['description'] = _update_message_text($type, $status, TRUE);
346 $requirement['severity'] = REQUIREMENT_ERROR;
347 }
348 switch ($status) {
349 case UPDATE_NOT_SECURE:
350 $requirement_label = t('Not secure!');
351 break;
352 case UPDATE_REVOKED:
353 $requirement_label = t('Revoked!');
354 break;
355 case UPDATE_NOT_SUPPORTED:
356 $requirement_label = t('Unsupported release');
357 break;
358 case UPDATE_NOT_CURRENT:
359 $requirement_label = t('Out of date');
360 $requirement['severity'] = REQUIREMENT_WARNING;
361 break;
362 case UPDATE_UNKNOWN:
363 case UPDATE_NOT_CHECKED:
364 case UPDATE_NOT_FETCHED:
365 $requirement_label = isset($project['reason']) ? $project['reason'] : t('Can not determine status');
366 $requirement['severity'] = REQUIREMENT_WARNING;
367 break;
368 default:
369 $requirement_label = t('Up to date');
370 }
371 if ($status != UPDATE_CURRENT && $type == 'core' && isset($project['recommended'])) {
372 $requirement_label .= ' ' . t('(version @version available)', array('@version' => $project['recommended']));
373 }
374 $requirement['value'] = l($requirement_label, update_manager_access() ? 'admin/reports/updates/update' : 'admin/reports/updates');
375 return $requirement;
376 }
377
378 /**
379 * Implement hook_cron().
380 */
381 function update_cron() {
382 $frequency = variable_get('update_check_frequency', 1);
383 $interval = 60 * 60 * 24 * $frequency;
384 if ((REQUEST_TIME - variable_get('update_last_check', 0)) > $interval) {
385 // If the configured update interval has elapsed, we want to invalidate
386 // the cached data for all projects, attempt to re-fetch, and trigger any
387 // configured notifications about the new status.
388 update_refresh();
389 _update_cron_notify();
390 }
391 else {
392 // Otherwise, see if any individual projects are now stale or still
393 // missing data, and if so, try to fetch the data.
394 update_get_available(TRUE);
395 }
396 }
397
398 /**
399 * Implement hook_form_FORM_ID_alter().
400 *
401 * Adds a submit handler to the system modules and themes forms, so that if a
402 * site admin saves either form, we invalidate the cache of available updates.
403 *
404 * @see _update_cache_clear()
405 */
406 function update_form_system_themes_form_alter(&$form, $form_state) {
407 $form['#submit'][] = 'update_cache_clear_submit';
408 }
409
410 /**
411 * Implement hook_form_FORM_ID_alter().
412 *
413 * Adds a submit handler to the system modules and themes forms, so that if a
414 * site admin saves either form, we invalidate the cache of available updates.
415 *
416 * @see _update_cache_clear()
417 */
418 function update_form_system_modules_alter(&$form, $form_state) {
419 $form['#submit'][] = 'update_cache_clear_submit';
420 }
421
422 /**
423 * Helper function for use as a form submit callback.
424 */
425 function update_cache_clear_submit($form, &$form_state) {
426 // Clear all update module caches.
427 _update_cache_clear();
428 }
429
430 /**
431 * Prints a warning message when there is no data about available updates.
432 */
433 function _update_no_data() {
434 $destination = drupal_get_destination();
435 return t('No information is available about potential new releases for currently installed modules and themes. To check for updates, you may need to <a href="@run_cron">run cron</a> or you can <a href="@check_manually">check manually</a>. Please note that checking for available updates can take a long time, so please be patient.', array(
436 '@run_cron' => url('admin/reports/status/run-cron', array('query' => $destination)),
437 '@check_manually' => url('admin/reports/updates/check', array('query' => $destination)),
438 ));
439 }
440
441 /**
442 * Internal helper to try to get the update information from the cache
443 * if possible, and to refresh the cache when necessary.
444 *
445 * In addition to checking the cache lifetime, this function also ensures that
446 * there are no .info files for enabled modules or themes that have a newer
447 * modification timestamp than the last time we checked for available update
448 * data. If any .info file was modified, it almost certainly means a new
449 * version of something was installed. Without fresh available update data,
450 * the logic in update_calculate_project_data() will be wrong and produce
451 * confusing, bogus results.
452 *
453 * @param $refresh
454 * Boolean to indicate if this method should refresh the cache automatically
455 * if there's no data.
456 *
457 * @see update_refresh()
458 * @see update_get_projects()
459 */
460 function update_get_available($refresh = FALSE) {
461 module_load_include('inc', 'update', 'update.compare');
462 $needs_refresh = FALSE;
463
464 // Grab whatever data we currently have cached in the DB.
465 $available = _update_get_cached_available_releases();
466 $num_avail = count($available);
467
468 $projects = update_get_projects();
469 foreach ($projects as $key => $project) {
470 // If there's no data at all, we clearly need to fetch some.
471 if (empty($available[$key])) {
472 update_create_fetch_task($project);
473 $needs_refresh = TRUE;
474 continue;
475 }
476
477 // See if the .info file is newer than the last time we checked for data,
478 // and if so, mark this project's data as needing to be re-fetched. Any
479 // time an admin upgrades their local installation, the .info file will
480 // be changed, so this is the only way we can be sure we're not showing
481 // bogus information right after they upgrade.
482 if ($project['info']['_info_file_ctime'] > $available[$key]['last_fetch']) {
483 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
484 }
485
486 // If we have project data but no release data, we need to fetch. This
487 // can be triggered when we fail to contact a release history server.
488 if (empty($available[$key]['releases'])) {
489 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
490 }
491
492 // If we think this project needs to fetch, actually create the task now
493 // and remember that we think we're missing some data.
494 if (!empty($available[$key]['fetch_status']) && $available[$key]['fetch_status'] == UPDATE_FETCH_PENDING) {
495 update_create_fetch_task($project);
496 $needs_refresh = TRUE;
497 }
498 }
499
500 if ($needs_refresh && $refresh) {
501 // Attempt to drain the queue of fetch tasks.
502 update_fetch_data();
503 // After processing the queue, we've (hopefully) got better data, so pull
504 // the latest from the cache again and use that directly.
505 $available = _update_get_cached_available_releases();
506 }
507
508 return $available;
509 }
510
511 /**
512 * Wrapper to load the include file and then create a new fetch task.
513 *
514 * @see _update_create_fetch_task()
515 */
516 function update_create_fetch_task($project) {
517 module_load_include('inc', 'update', 'update.fetch');
518 return _update_create_fetch_task($project);
519 }
520
521 /**
522 * Wrapper to load the include file and then refresh the release data.
523 *
524 * @see _update_refresh();
525 */
526 function update_refresh() {
527 module_load_include('inc', 'update', 'update.fetch');
528 return _update_refresh();
529 }
530
531 /**
532 * Wrapper to load the include file and then attempt to fetch update data.
533 */
534 function update_fetch_data() {
535 module_load_include('inc', 'update', 'update.fetch');
536 return _update_fetch_data();
537 }
538
539 /**
540 * Return all currently cached data about available releases for all projects.
541 *
542 * @return
543 * Array of data about available releases, keyed by project shortname.
544 */
545 function _update_get_cached_available_releases() {
546 $data = array();
547 $cache_items = _update_get_cache_multiple('available_releases');
548 foreach ($cache_items as $cid => $cache) {
549 $cache->data['last_fetch'] = $cache->created;
550 if ($cache->expire < REQUEST_TIME) {
551 $cache->data['fetch_status'] = UPDATE_FETCH_PENDING;
552 }
553 // The project shortname is embedded in the cache ID, even if there's no
554 // data for this project in the DB at all, so use that for the indexes in
555 // the array.
556 $parts = explode('::', $cid, 2);
557 $data[$parts[1]] = $cache->data;
558 }
559 return $data;
560 }
561
562 /**
563 * Implement hook_mail().
564 *
565 * Constructs the email notification message when the site is out of date.
566 *
567 * @param $key
568 * Unique key to indicate what message to build, always 'status_notify'.
569 * @param $message
570 * Reference to the message array being built.
571 * @param $params
572 * Array of parameters to indicate what kind of text to include in the
573 * message body. This is a keyed array of message type ('core' or 'contrib')
574 * as the keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for
575 * the values.
576 *
577 * @see drupal_mail()
578 * @see _update_cron_notify()
579 * @see _update_message_text()
580 */
581 function update_mail($key, &$message, $params) {
582 $language = $message['language'];
583 $langcode = $language->language;
584 $message['subject'] .= t('New release(s) available for !site_name', array('!site_name' => variable_get('site_name', 'Drupal')), array('langcode' => $langcode));
585 foreach ($params as $msg_type => $msg_reason) {
586 $message['body'][] = _update_message_text($msg_type, $msg_reason, FALSE, $language);
587 }
588 $message['body'][] = t('See the available updates page for more information:', array(), array('langcode' => $langcode)) . "\n" . url('admin/reports/updates', array('absolute' => TRUE, 'language' => $language));
589 if (update_manager_access()) {
590 $message['body'][] = t('You can automatically install your missing updates using the Update manager:', array(), array('langcode' => $langcode)) . "\n" . url('admin/reports/updates/update', array('absolute' => TRUE, 'language' => $language));
591 }
592 $settings_url = url('admin/reports/updates/settings', array('absolute' => TRUE));
593 if (variable_get('update_notification_threshold', 'all') == 'all') {
594 $message['body'][] = t('Your site is currently configured to send these emails when any updates are available. To get notified only for security updates, please visit !url.', array('!url' => $settings_url));
595 }
596 else {
597 $message['body'][] = t('Your site is currently configured to send these emails only when security updates are available. To get notified for any available updates, please visit !url.', array('!url' => $settings_url));
598 }
599 }
600
601 /**
602 * Helper function to return the appropriate message text when the site is out
603 * of date or missing a security update.
604 *
605 * These error messages are shared by both update_requirements() for the
606 * site-wide status report at admin/reports/status and in the body of the
607 * notification emails generated by update_cron().
608 *
609 * @param $msg_type
610 * String to indicate what kind of message to generate. Can be either
611 * 'core' or 'contrib'.
612 * @param $msg_reason
613 * Integer constant specifying why message is generated.
614 * @param $report_link
615 * Boolean that controls if a link to the updates report should be added.
616 * @param $language
617 * An optional language object to use.
618 * @return
619 * The properly translated error message for the given key.
620 */
621 function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $language = NULL) {
622 $langcode = isset($language) ? $language->language : NULL;
623 $text = '';
624 switch ($msg_reason) {
625 case UPDATE_NOT_SECURE:
626 if ($msg_type == 'core') {
627 $text = t('There is a security update available for your version of Drupal. To ensure the security of your server, you should update immediately!', array(), array('langcode' => $langcode));
628 }
629 else {
630 $text = t('There are security updates available for one or more of your modules or themes. To ensure the security of your server, you should update immediately!', array(), array('langcode' => $langcode));
631 }
632 break;
633
634 case UPDATE_REVOKED:
635 if ($msg_type == 'core') {
636 $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', array(), array('langcode' => $langcode));
637 }
638 else {
639 $text = t('The installed version of at least one of your modules or themes has been revoked and is no longer available for download. Upgrading or disabling is strongly recommended!', array(), array('langcode' => $langcode));
640 }
641 break;
642
643 case UPDATE_NOT_SUPPORTED:
644 if ($msg_type == 'core') {
645 $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', array(), array('langcode' => $langcode));
646 }
647 else {
648 $text = t('The installed version of at least one of your modules or themes is no longer supported. Upgrading or disabling is strongly recommended! Please see the project homepage for more details.', array(), array('langcode' => $langcode));
649 }
650 break;
651
652 case UPDATE_NOT_CURRENT:
653 if ($msg_type == 'core') {
654 $text = t('There are updates available for your version of Drupal. To ensure the proper functioning of your site, you should update as soon as possible.', array(), array('langcode' => $langcode));
655 }
656 else {
657 $text = t('There are updates available for one or more of your modules or themes. To ensure the proper functioning of your site, you should update as soon as possible.', array(), array('langcode' => $langcode));
658 }
659 break;
660
661 case UPDATE_UNKNOWN:
662 case UPDATE_NOT_CHECKED:
663 case UPDATE_NOT_FETCHED:
664 case UPDATE_FETCH_PENDING:
665 if ($msg_type == 'core') {
666 $text = t('There was a problem determining the status of available updates for your version of Drupal.', array(), array('langcode' => $langcode));
667 }
668 else {
669 $text = t('There was a problem determining the status of available updates for one or more of your modules or themes.', array(), array('langcode' => $langcode));
670 }
671 break;
672 }
673
674 if ($report_link) {
675 if (update_manager_access()) {
676 $text .= ' ' . t('See the <a href="@available_updates">available updates</a> page for more information and to install your missing updates.', array('@available_updates' => url('admin/reports/updates/update', array('language' => $language))), array('langcode' => $langcode));
677 }
678 else {
679 $text .= ' ' . t('See the <a href="@available_updates">available updates</a> page for more information.', array('@available_updates' => url('admin/reports/updates', array('language' => $language))), array('langcode' => $langcode));
680 }
681 }
682
683 return $text;
684 }
685
686 /**
687 * Private sort function to order projects based on their status.
688 *
689 * @see update_requirements()
690 * @see uasort()
691 */
692 function _update_project_status_sort($a, $b) {
693 // The status constants are numerically in the right order, so we can
694 // usually subtract the two to compare in the order we want. However,
695 // negative status values should be treated as if they are huge, since we
696 // always want them at the bottom of the list.
697 $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
698 $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
699 return $a_status - $b_status;
700 }
701
702 /**
703 * Render the HTML to display the last time we checked for update data.
704 *
705 * In addition to properly formating the given timestamp, this function also
706 * provides a "Check manually" link that refreshes the available update and
707 * redirects back to the same page.
708 *
709 * @param $variables
710 * 'last': The timestamp when the site last checked for available updates.
711 *
712 * @see theme_update_report()
713 * @see theme_update_available_updates_form()
714 *
715 * @ingroup themeable
716 */
717 function theme_update_last_check($variables) {
718 $last = $variables['last'];
719 $output = '<div class="update checked">';
720 $output .= $last ? t('Last checked: @time ago', array('@time' => format_interval(REQUEST_TIME - $last))) : t('Last checked: never');
721 $output .= ' <span class="check-manually">(' . l(t('Check manually'), 'admin/reports/updates/check', array('query' => drupal_get_destination())) . ')</span>';
722 $output .= "</div>\n";
723 return $output;
724 }
725
726 /**
727 * @defgroup update_status_cache Private update status cache system
728 * @{
729 *
730 * We specifically do NOT use the core cache API for saving the fetched data
731 * about available updates. It is vitally important that this cache is only
732 * cleared when we're populating it after successfully fetching new available
733 * update data. Usage of the core cache API results in all sorts of potential
734 * problems that would result in attempting to fetch available update data all
735 * the time, including if a site has a "minimum cache lifetime" (which is both
736 * a minimum and a maximum) defined, or if a site uses memcache or another
737 * plug-able cache system that assumes volatile caches.
738 *
739 * Update module still uses the {cache_update} table, but instead of using
740 * cache_set(), cache_get(), and cache_clear_all(), there are private helper
741 * functions that implement these same basic tasks but ensure that the cache
742 * is not prematurely cleared, and that the data is always stored in the
743 * database, even if memcache or another cache backend is in use.
744 */
745
746 /**
747 * Store data in the private update status cache table.
748 *
749 * Note: this function completely ignores the {cache_update}.headers field
750 * since that is meaningless for the kinds of data we're caching.
751 *
752 * @param $cid
753 * The cache ID to save the data with.
754 * @param $data
755 * The data to store.
756 * @param $expire
757 * One of the following values:
758 * - CACHE_PERMANENT: Indicates that the item should never be removed except
759 * by explicitly using _update_cache_clear().
760 * - A Unix timestamp: Indicates that the item should be kept at least until
761 * the given time, after which it will be invalidated.
762 */
763 function _update_cache_set($cid, $data, $expire) {
764 $fields = array(
765 'created' => REQUEST_TIME,
766 'expire' => $expire,
767 'headers' => NULL,
768 );
769 if (!is_string($data)) {
770 $fields['data'] = serialize($data);
771 $fields['serialized'] = 1;
772 }
773 else {
774 $fields['data'] = $data;
775 $fields['serialized'] = 0;
776 }
777 db_merge('cache_update')
778 ->key(array('cid' => $cid))
779 ->fields($fields)
780 ->execute();
781 }
782
783 /**
784 * Retrieve data from the private update status cache table.
785 *
786 * @param $cid
787 * The cache ID to retrieve.
788 * @return
789 * The data for the given cache ID, or NULL if the ID was not found.
790 */
791 function _update_cache_get($cid) {
792 $cache = db_query("SELECT data, created, expire, serialized FROM {cache_update} WHERE cid = :cid", array(':cid' => $cid))->fetchObject();
793 if (isset($cache->data)) {
794 if ($cache->serialized) {
795 $cache->data = unserialize($cache->data);
796 }
797 }
798 return $cache;
799 }
800
801 /**
802 * Return an array of cache items with a given cache ID prefix.
803 *
804 * @return
805 * Associative array of cache items, keyed by cache ID.
806 */
807 function _update_get_cache_multiple($cid_prefix) {
808 $data = array();
809 $result = db_select('cache_update')
810 ->fields('cache_update', array('cid', 'data', 'created', 'expire', 'serialized'))
811 ->condition('cache_update.cid', $cid_prefix . '::%', 'LIKE')
812 ->execute();
813 foreach ($result as $cache) {
814 if ($cache) {
815 if ($cache->serialized) {
816 $cache->data = unserialize($cache->data);
817 }
818 $data[$cache->cid] = $cache;
819 }
820 }
821 return $data;
822 }
823
824 /**
825 * Invalidates cached data relating to update status.
826 *
827 * @param $cid
828 * Optional cache ID of the record to clear from the private update module
829 * cache. If empty, all records will be cleared from the table.
830 * @param $wildcard
831 * If $wildcard is TRUE, cache IDs starting with $cid are deleted in
832 * addition to the exact cache ID specified by $cid.
833 */
834 function _update_cache_clear($cid = NULL, $wildcard = FALSE) {
835 if (empty($cid)) {
836 db_truncate('cache_update')->execute();
837 }
838 else {
839 $query = db_delete('cache_update');
840 if ($wildcard) {
841 $query->condition('cid', $cid . '%', 'LIKE');
842 }
843 else {
844 $query->condition('cid', $cid);
845 }
846 $query->execute();
847 }
848 }
849
850 /**
851 * Implement hook_flush_caches().
852 *
853 * Called from update.php (among others) to flush the caches.
854 * Since we're running update.php, we are likely to install a new version of
855 * something, in which case, we want to check for available update data again.
856 * However, because we have our own caching system, we need to directly clear
857 * the database table ourselves at this point and return nothing, for example,
858 * on sites that use memcache where cache_clear_all() won't know how to purge
859 * this data.
860 *
861 * However, we only want to do this from update.php, since otherwise, we'd
862 * lose all the available update data on every cron run. So, we specifically
863 * check if the site is in MAINTENANCE_MODE == 'update' (which indicates
864 * update.php is running, not update module... alas for overloaded names).
865 */
866 function update_flush_caches() {
867 if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
868 _update_cache_clear();
869 }
870 return array();
871 }
872
873 /**
874 * @} End of "defgroup update_status_cache".
875 */

  ViewVC Help
Powered by ViewVC 1.1.2