web/drupal/modules/update/update.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: update.module,v 1.17.2.5 2009/06/09 11:08:32 goba 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  * Maximum number of attempts to fetch available update data from a given host.
       
    61  */
       
    62 define('UPDATE_MAX_FETCH_ATTEMPTS', 2);
       
    63 
       
    64 /**
       
    65  * Implementation of hook_help().
       
    66  */
       
    67 function update_help($path, $arg) {
       
    68   switch ($path) {
       
    69     case 'admin/reports/updates':
       
    70       $output = '<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>';
       
    71       $output .= '<p>'. t('To extend the functionality or to change the look of your site, a number of contributed <a href="@modules">modules</a> and <a href="@themes">themes</a> are available.', array('@modules' => 'http://drupal.org/project/modules', '@themes' => 'http://drupal.org/project/themes')) .'</p>';
       
    72       return $output;
       
    73     case 'admin/build/themes':
       
    74     case 'admin/build/modules':
       
    75       include_once './includes/install.inc';
       
    76       $status = update_requirements('runtime');
       
    77       foreach (array('core', 'contrib') as $report_type) {
       
    78         $type = 'update_'. $report_type;
       
    79         if (isset($status[$type]['severity'])) {
       
    80           if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
       
    81             drupal_set_message($status[$type]['description'], 'error');
       
    82           }
       
    83           elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
       
    84             drupal_set_message($status[$type]['description'], 'warning');
       
    85           }
       
    86         }
       
    87       }
       
    88       return '<p>'. t('See the <a href="@available_updates">available updates</a> page for information on installed modules and themes with new versions released.', array('@available_updates' => url('admin/reports/updates'))) .'</p>';
       
    89 
       
    90     case 'admin/reports/updates/settings':
       
    91     case 'admin/reports/status':
       
    92       // These two pages don't need additional nagging.
       
    93       break;
       
    94 
       
    95     case 'admin/help#update':
       
    96       $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>';
       
    97       $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>';
       
    98       $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/build/modules'))) .'</p>';
       
    99       $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>';
       
   100       return $output;
       
   101 
       
   102     default:
       
   103       // Otherwise, if we're on *any* admin page and there's a security
       
   104       // update missing, print an error message about it.
       
   105       if (arg(0) == 'admin' && strpos($path, '#') === FALSE
       
   106           && user_access('administer site configuration')) {
       
   107         include_once './includes/install.inc';
       
   108         $status = update_requirements('runtime');
       
   109         foreach (array('core', 'contrib') as $report_type) {
       
   110           $type = 'update_'. $report_type;
       
   111           if (isset($status[$type])
       
   112               && isset($status[$type]['reason'])
       
   113               && $status[$type]['reason'] === UPDATE_NOT_SECURE) {
       
   114             drupal_set_message($status[$type]['description'], 'error');
       
   115           }
       
   116         }
       
   117       }
       
   118 
       
   119   }
       
   120 }
       
   121 
       
   122 /**
       
   123  * Implementation of hook_menu().
       
   124  */
       
   125 function update_menu() {
       
   126   $items = array();
       
   127 
       
   128   $items['admin/reports/updates'] = array(
       
   129     'title' => 'Available updates',
       
   130     'description' => 'Get a status report about available updates for your installed modules and themes.',
       
   131     'page callback' => 'update_status',
       
   132     'access arguments' => array('administer site configuration'),
       
   133     'file' => 'update.report.inc',
       
   134     'weight' => 10,
       
   135   );
       
   136   $items['admin/reports/updates/list'] = array(
       
   137     'title' => 'List',
       
   138     'page callback' => 'update_status',
       
   139     'access arguments' => array('administer site configuration'),
       
   140     'file' => 'update.report.inc',
       
   141     'type' => MENU_DEFAULT_LOCAL_TASK,
       
   142   );
       
   143   $items['admin/reports/updates/settings'] = array(
       
   144     'title' => 'Settings',
       
   145     'page callback' => 'drupal_get_form',
       
   146     'page arguments' => array('update_settings'),
       
   147     'access arguments' => array('administer site configuration'),
       
   148     'file' => 'update.settings.inc',
       
   149     'type' => MENU_LOCAL_TASK,
       
   150   );
       
   151   $items['admin/reports/updates/check'] = array(
       
   152     'title' => 'Manual update check',
       
   153     'page callback' => 'update_manual_status',
       
   154     'access arguments' => array('administer site configuration'),
       
   155     'file' => 'update.fetch.inc',
       
   156     'type' => MENU_CALLBACK,
       
   157   );
       
   158 
       
   159   return $items;
       
   160 }
       
   161 
       
   162 /**
       
   163  * Implementation of the hook_theme() registry.
       
   164  */
       
   165 function update_theme() {
       
   166   return array(
       
   167     'update_settings' => array(
       
   168       'arguments' => array('form' => NULL),
       
   169     ),
       
   170     'update_report' => array(
       
   171       'arguments' => array('data' => NULL),
       
   172     ),
       
   173     'update_version' => array(
       
   174       'arguments' => array('version' => NULL, 'tag' => NULL, 'class' => NULL),
       
   175     ),
       
   176   );
       
   177 }
       
   178 
       
   179 /**
       
   180  * Implementation of hook_requirements().
       
   181  *
       
   182  * @return
       
   183  *   An array describing the status of the site regarding available updates.
       
   184  *   If there is no update data, only one record will be returned, indicating
       
   185  *   that the status of core can't be determined. If data is available, there
       
   186  *   will be two records: one for core, and another for all of contrib
       
   187  *   (assuming there are any contributed modules or themes enabled on the
       
   188  *   site). In addition to the fields expected by hook_requirements ('value',
       
   189  *   'severity', and optionally 'description'), this array will contain a
       
   190  *   'reason' attribute, which is an integer constant to indicate why the
       
   191  *   given status is being returned (UPDATE_NOT_SECURE, UPDATE_NOT_CURRENT, or
       
   192  *   UPDATE_UNKNOWN). This is used for generating the appropriate e-mail
       
   193  *   notification messages during update_cron(), and might be useful for other
       
   194  *   modules that invoke update_requirements() to find out if the site is up
       
   195  *   to date or not.
       
   196  *
       
   197  * @see _update_message_text()
       
   198  * @see _update_cron_notify()
       
   199  */
       
   200 function update_requirements($phase) {
       
   201   if ($phase == 'runtime') {
       
   202     if ($available = update_get_available(FALSE)) {
       
   203       module_load_include('inc', 'update', 'update.compare');
       
   204       $data = update_calculate_project_data($available);
       
   205       // First, populate the requirements for core:
       
   206       $requirements['update_core'] = _update_requirement_check($data['drupal'], 'core');
       
   207       // We don't want to check drupal a second time.
       
   208       unset($data['drupal']);
       
   209       if (!empty($data)) {
       
   210         // Now, sort our $data array based on each project's status. The
       
   211         // status constants are numbered in the right order of precedence, so
       
   212         // we just need to make sure the projects are sorted in ascending
       
   213         // order of status, and we can look at the first project we find.
       
   214         uasort($data, '_update_project_status_sort');
       
   215         $first_project = reset($data);
       
   216         $requirements['update_contrib'] = _update_requirement_check($first_project, 'contrib');
       
   217       }
       
   218     }
       
   219     else {
       
   220       $requirements['update_core']['title'] = t('Drupal core update status');
       
   221       $requirements['update_core']['value'] = t('No update data available');
       
   222       $requirements['update_core']['severity'] = REQUIREMENT_WARNING;
       
   223       $requirements['update_core']['reason'] = UPDATE_UNKNOWN;
       
   224       $requirements['update_core']['description'] = _update_no_data();
       
   225     }
       
   226     return $requirements;
       
   227   }
       
   228 }
       
   229 
       
   230 /**
       
   231  * Private helper method to fill in the requirements array.
       
   232  *
       
   233  * This is shared for both core and contrib to generate the right elements in
       
   234  * the array for hook_requirements().
       
   235  *
       
   236  * @param $project
       
   237  *  Array of information about the project we're testing as returned by
       
   238  *  update_calculate_project_data().
       
   239  * @param $type
       
   240  *  What kind of project is this ('core' or 'contrib').
       
   241  *
       
   242  * @return
       
   243  *  An array to be included in the nested $requirements array.
       
   244  *
       
   245  * @see hook_requirements()
       
   246  * @see update_requirements()
       
   247  * @see update_calculate_project_data()
       
   248  */
       
   249 function _update_requirement_check($project, $type) {
       
   250   $requirement = array();
       
   251   if ($type == 'core') {
       
   252     $requirement['title'] = t('Drupal core update status');
       
   253   }
       
   254   else {
       
   255     $requirement['title'] = t('Module and theme update status');
       
   256   }
       
   257   $status = $project['status'];
       
   258   if ($status != UPDATE_CURRENT) {
       
   259     $requirement['reason'] = $status;
       
   260     $requirement['description'] = _update_message_text($type, $status, TRUE);
       
   261     $requirement['severity'] = REQUIREMENT_ERROR;
       
   262   }
       
   263   switch ($status) {
       
   264     case UPDATE_NOT_SECURE:
       
   265       $requirement_label = t('Not secure!');
       
   266       break;
       
   267     case UPDATE_REVOKED:
       
   268       $requirement_label = t('Revoked!');
       
   269       break;
       
   270     case UPDATE_NOT_SUPPORTED:
       
   271       $requirement_label = t('Unsupported release');
       
   272       break;
       
   273     case UPDATE_NOT_CURRENT:
       
   274       $requirement_label = t('Out of date');
       
   275       $requirement['severity'] = REQUIREMENT_WARNING;
       
   276       break;
       
   277     case UPDATE_UNKNOWN:
       
   278     case UPDATE_NOT_CHECKED:
       
   279     case UPDATE_NOT_FETCHED:
       
   280       $requirement_label = isset($project['reason']) ? $project['reason'] : t('Can not determine status');
       
   281       $requirement['severity'] = REQUIREMENT_WARNING;
       
   282       break;
       
   283     default:
       
   284       $requirement_label = t('Up to date');
       
   285   }
       
   286   if ($status != UPDATE_CURRENT && $type == 'core' && isset($project['recommended'])) {
       
   287     $requirement_label .= ' '. t('(version @version available)', array('@version' => $project['recommended']));
       
   288   }
       
   289   $requirement['value'] = l($requirement_label, 'admin/reports/updates');
       
   290   return $requirement;
       
   291 }
       
   292 
       
   293 /**
       
   294  * Implementation of hook_cron().
       
   295  */
       
   296 function update_cron() {
       
   297   $frequency = variable_get('update_check_frequency', 1);
       
   298   $interval = 60 * 60 * 24 * $frequency;
       
   299   // Cron should check for updates if there is no update data cached or if the
       
   300   // configured update interval has elapsed.
       
   301   if (!_update_cache_get('update_available_releases') || ((time() - variable_get('update_last_check', 0)) > $interval)) {
       
   302     update_refresh();
       
   303     _update_cron_notify();
       
   304   }
       
   305 }
       
   306 
       
   307 /**
       
   308  * Implementation of hook_form_alter().
       
   309  *
       
   310  * Adds a submit handler to the system modules and themes forms, so that if a
       
   311  * site admin saves either form, we invalidate the cache of available updates.
       
   312  *
       
   313  * @see update_invalidate_cache()
       
   314  */
       
   315 function update_form_alter(&$form, $form_state, $form_id) {
       
   316   if ($form_id == 'system_modules' || $form_id == 'system_themes_form' ) {
       
   317     $form['#submit'][] = 'update_invalidate_cache';
       
   318   }
       
   319 }
       
   320 
       
   321 /**
       
   322  * Prints a warning message when there is no data about available updates.
       
   323  */
       
   324 function _update_no_data() {
       
   325   $destination = drupal_get_destination();
       
   326   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(
       
   327     '@run_cron' => url('admin/reports/status/run-cron', array('query' => $destination)),
       
   328     '@check_manually' => url('admin/reports/updates/check', array('query' => $destination)),
       
   329   ));
       
   330 }
       
   331 
       
   332 /**
       
   333  * Internal helper to try to get the update information from the cache
       
   334  * if possible, and to refresh the cache when necessary.
       
   335  *
       
   336  * In addition to checking the cache lifetime, this function also ensures that
       
   337  * there are no .info files for enabled modules or themes that have a newer
       
   338  * modification timestamp than the last time we checked for available update
       
   339  * data. If any .info file was modified, it almost certainly means a new
       
   340  * version of something was installed. Without fresh available update data,
       
   341  * the logic in update_calculate_project_data() will be wrong and produce
       
   342  * confusing, bogus results.
       
   343  *
       
   344  * @param $refresh
       
   345  *   Boolean to indicate if this method should refresh the cache automatically
       
   346  *   if there's no data.
       
   347  *
       
   348  * @see update_refresh()
       
   349  * @see update_get_projects()
       
   350  */
       
   351 function update_get_available($refresh = FALSE) {
       
   352   module_load_include('inc', 'update', 'update.compare');
       
   353   $available = array();
       
   354 
       
   355   // First, make sure that none of the .info files have a change time
       
   356   // newer than the last time we checked for available updates.
       
   357   $needs_refresh = FALSE;
       
   358   $last_check = variable_get('update_last_check', 0);
       
   359   $projects = update_get_projects();
       
   360   foreach ($projects as $key => $project) {
       
   361     if ($project['info']['_info_file_ctime'] > $last_check) {
       
   362       $needs_refresh = TRUE;
       
   363       break;
       
   364     }
       
   365   }
       
   366   if (!$needs_refresh && ($cache = _update_cache_get('update_available_releases')) && $cache->expire > time()) {
       
   367     $available = $cache->data;
       
   368   }
       
   369   elseif ($needs_refresh || $refresh) {
       
   370     // If we need to refresh due to a newer .info file, ignore the argument
       
   371     // and force the refresh (e.g., even for update_requirements()) to prevent
       
   372     // bogus results.
       
   373     $available = update_refresh();
       
   374   }
       
   375   return $available;
       
   376 }
       
   377 
       
   378 /**
       
   379  * Wrapper to load the include file and then refresh the release data.
       
   380  */
       
   381 function update_refresh() {
       
   382   module_load_include('inc', 'update', 'update.fetch');
       
   383   return _update_refresh();
       
   384 }
       
   385 
       
   386 /**
       
   387  * Implementation of hook_mail().
       
   388  *
       
   389  * Constructs the email notification message when the site is out of date.
       
   390  *
       
   391  * @param $key
       
   392  *   Unique key to indicate what message to build, always 'status_notify'.
       
   393  * @param $message
       
   394  *   Reference to the message array being built.
       
   395  * @param $params
       
   396  *   Array of parameters to indicate what kind of text to include in the
       
   397  *   message body. This is a keyed array of message type ('core' or 'contrib')
       
   398  *   as the keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for
       
   399  *   the values.
       
   400  *
       
   401  * @see drupal_mail()
       
   402  * @see _update_cron_notify()
       
   403  * @see _update_message_text()
       
   404  */
       
   405 function update_mail($key, &$message, $params) {
       
   406   $language = $message['language'];
       
   407   $langcode = $language->language;
       
   408   $message['subject'] .= t('New release(s) available for !site_name', array('!site_name' => variable_get('site_name', 'Drupal')), $langcode);
       
   409   foreach ($params as $msg_type => $msg_reason) {
       
   410     $message['body'][] = _update_message_text($msg_type, $msg_reason, FALSE, $language);
       
   411   }
       
   412   $message['body'][] = t('See the available updates page for more information:', array(), $langcode) ."\n". url('admin/reports/updates', array('absolute' => TRUE, 'language' => $language));
       
   413 }
       
   414 
       
   415 /**
       
   416  * Helper function to return the appropriate message text when the site is out
       
   417  * of date or missing a security update.
       
   418  *
       
   419  * These error messages are shared by both update_requirements() for the
       
   420  * site-wide status report at admin/reports/status and in the body of the
       
   421  * notification emails generated by update_cron().
       
   422  *
       
   423  * @param $msg_type
       
   424  *   String to indicate what kind of message to generate. Can be either
       
   425  *   'core' or 'contrib'.
       
   426  * @param $msg_reason
       
   427  *   Integer constant specifying why message is generated.
       
   428  * @param $report_link
       
   429  *   Boolean that controls if a link to the updates report should be added.
       
   430  * @param $language
       
   431  *   An optional language object to use.
       
   432  * @return
       
   433  *   The properly translated error message for the given key.
       
   434  */
       
   435 function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $language = NULL) {
       
   436   $langcode = isset($language) ? $language->language : NULL;
       
   437   $text = '';
       
   438   switch ($msg_reason) {
       
   439     case UPDATE_NOT_SECURE:
       
   440       if ($msg_type == 'core') {
       
   441         $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(), $langcode);
       
   442       }
       
   443       else {
       
   444         $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(), $langcode);
       
   445       }
       
   446       break;
       
   447 
       
   448     case UPDATE_REVOKED:
       
   449       if ($msg_type == 'core') {
       
   450         $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', array(), $langcode);
       
   451       }
       
   452       else {
       
   453         $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(), $langcode);
       
   454       }
       
   455       break;
       
   456 
       
   457     case UPDATE_NOT_SUPPORTED:
       
   458       if ($msg_type == 'core') {
       
   459         $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', array(), $langcode);
       
   460       }
       
   461       else {
       
   462         $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(), $langcode);
       
   463       }
       
   464       break;
       
   465 
       
   466     case UPDATE_NOT_CURRENT:
       
   467       if ($msg_type == 'core') {
       
   468         $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(), $langcode);
       
   469       }
       
   470       else {
       
   471         $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(), $langcode);
       
   472       }
       
   473       break;
       
   474 
       
   475     case UPDATE_UNKNOWN:
       
   476     case UPDATE_NOT_CHECKED:
       
   477     case UPDATE_NOT_FETCHED:
       
   478       if ($msg_type == 'core') {
       
   479         $text = t('There was a problem determining the status of available updates for your version of Drupal.', array(), $langcode);
       
   480       }
       
   481       else {
       
   482         $text = t('There was a problem determining the status of available updates for one or more of your modules or themes.', array(), $langcode);
       
   483       }
       
   484       break;
       
   485   }
       
   486 
       
   487   if ($report_link) {
       
   488     $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))), $langcode);
       
   489   }
       
   490 
       
   491   return $text;
       
   492 }
       
   493 
       
   494 /**
       
   495  * Private sort function to order projects based on their status.
       
   496  *
       
   497  * @see update_requirements()
       
   498  * @see uasort()
       
   499  */
       
   500 function _update_project_status_sort($a, $b) {
       
   501   // The status constants are numerically in the right order, so we can
       
   502   // usually subtract the two to compare in the order we want. However,
       
   503   // negative status values should be treated as if they are huge, since we
       
   504   // always want them at the bottom of the list.
       
   505   $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
       
   506   $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
       
   507   return $a_status - $b_status;
       
   508 }
       
   509 
       
   510 /**
       
   511  * @defgroup update_status_cache Private update status cache system
       
   512  * @{
       
   513  *
       
   514  * We specifically do NOT use the core cache API for saving the fetched data
       
   515  * about available updates. It is vitally important that this cache is only
       
   516  * cleared when we're populating it after successfully fetching new available
       
   517  * update data. Usage of the core cache API results in all sorts of potential
       
   518  * problems that would result in attempting to fetch available update data all
       
   519  * the time, including if a site has a "minimum cache lifetime" (which is both
       
   520  * a minimum and a maximum) defined, or if a site uses memcache or another
       
   521  * plug-able cache system that assumes volatile caches.
       
   522  *
       
   523  * Update module still uses the {cache_update} table, but instead of using
       
   524  * cache_set(), cache_get(), and cache_clear_all(), there are private helper
       
   525  * functions that implement these same basic tasks but ensure that the cache
       
   526  * is not prematurely cleared, and that the data is always stored in the
       
   527  * database, even if memcache or another cache backend is in use.
       
   528  */
       
   529 
       
   530 /**
       
   531  * Store data in the private update status cache table.
       
   532  *
       
   533  * Note: this function completely ignores the {cache_update}.headers field
       
   534  * since that is meaningless for the kinds of data we're caching.
       
   535  *
       
   536  * @param $cid
       
   537  *   The cache ID to save the data with.
       
   538  * @param $data
       
   539  *   The data to store.
       
   540  * @param $expire
       
   541  *   One of the following values:
       
   542  *   - CACHE_PERMANENT: Indicates that the item should never be removed except
       
   543  *     by explicitly using _update_cache_clear() or update_invalidate_cache().
       
   544  *   - A Unix timestamp: Indicates that the item should be kept at least until
       
   545  *     the given time, after which it will be invalidated.
       
   546  */
       
   547 function _update_cache_set($cid, $data, $expire) {
       
   548   $serialized = 0;
       
   549   if (is_object($data) || is_array($data)) {
       
   550     $data = serialize($data);
       
   551     $serialized = 1;
       
   552   }
       
   553   $created = time();
       
   554   db_query("UPDATE {cache_update} SET data = %b, created = %d, expire = %d, serialized = %d WHERE cid = '%s'", $data, $created, $expire, $serialized, $cid);
       
   555   if (!db_affected_rows()) {
       
   556     @db_query("INSERT INTO {cache_update} (cid, data, created, expire, serialized) VALUES ('%s', %b, %d, %d, %d)", $cid, $data, $created, $expire, $serialized);
       
   557   }
       
   558 }
       
   559 
       
   560 /** 
       
   561  * Retrieve data from the private update status cache table.
       
   562  *
       
   563  * @param $cid
       
   564  *   The cache ID to retrieve.
       
   565  * @return
       
   566  *   The data for the given cache ID, or NULL if the ID was not found.
       
   567  */
       
   568 function _update_cache_get($cid) {
       
   569   $cache = db_fetch_object(db_query("SELECT data, created, expire, serialized FROM {cache_update} WHERE cid = '%s'", $cid));
       
   570   if (isset($cache->data)) {
       
   571     $cache->data = db_decode_blob($cache->data);
       
   572     if ($cache->serialized) {
       
   573       $cache->data = unserialize($cache->data);
       
   574     }
       
   575   }
       
   576   return $cache;
       
   577 }
       
   578 
       
   579 /**
       
   580  * Invalidates specific cached data relating to update status.
       
   581  *
       
   582  * @param $cid
       
   583  *   Optional cache ID of the record to clear from the private update module
       
   584  *   cache. If empty, all records will be cleared from the table.
       
   585  */
       
   586 function _update_cache_clear($cid = NULL) {
       
   587   if (empty($cid)) {
       
   588     db_query("DELETE FROM {cache_update}");
       
   589   }
       
   590   else {
       
   591     db_query("DELETE FROM {cache_update} WHERE cid = '%s'", $cid);
       
   592   }
       
   593 }
       
   594 
       
   595 /**
       
   596  * Implementation of hook_flush_caches().
       
   597  *
       
   598  * Called from update.php (among others) to flush the caches.
       
   599  * Since we're running update.php, we are likely to install a new version of
       
   600  * something, in which case, we want to check for available update data again.
       
   601  * However, because we have our own caching system, we need to directly clear
       
   602  * the database table ourselves at this point and return nothing, for example,
       
   603  * on sites that use memcache where cache_clear_all() won't know how to purge
       
   604  * this data.
       
   605  *
       
   606  * However, we only want to do this from update.php, since otherwise, we'd
       
   607  * lose all the available update data on every cron run. So, we specifically
       
   608  * check if the site is in MAINTENANCE_MODE == 'update' (which indicates
       
   609  * update.php is running, not update module... alas for overloaded names).
       
   610  */
       
   611 function update_flush_caches() {
       
   612   if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
       
   613     _update_cache_clear();
       
   614   }
       
   615   return array();
       
   616 }
       
   617 
       
   618 /**
       
   619  * Invalidates all cached data relating to update status.
       
   620  */
       
   621 function update_invalidate_cache() {
       
   622   _update_cache_clear();
       
   623 }
       
   624 
       
   625 /**
       
   626  * @} End of "defgroup update_status_cache".
       
   627  */