Index: modules/update/update.admin.inc =================================================================== RCS file: modules/update/update.admin.inc diff -N modules/update/update.admin.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/update/update.admin.inc 7 Jul 2009 08:59:32 -0000 @@ -0,0 +1,296 @@ + 'submit', + '#type' => 'submit', + '#value' => t('Continue'), + '#weight' => 100, + ); + + if (!isset($form_state['values'])) { + //First step. + _update_available_updates_form($form); + return $form; + } + + if (is_array($form_state['values']) && array_filter($form_state['values']['extensions_to_update'])) { + //we've already submitted once and there are updates to deal with. + //if there is a form error, we show the list of possible FileTransfer backends. + _update_confirmation_form($form, $form_state); + return $form; + } +} + + +function _update_finished_report(&$form, $results) { + drupal_set_title(t("Update / Installation report")); + + if ($results['success']) { + drupal_set_message(t("Update was completed successfully! Your site has been taken out of maintinance mode.")); + } + else { + drupal_set_message(t("Update failed! See the log below for more information. Your site is still in maintinance mode"), 'error'); + } + + $form['log'] = array( + '#type' => 'item', + '#title' => t('Test results'), + '#markup' => theme('item_list', $results['messages']), + ); +} + +function _update_available_updates_form(&$form) { + + if ($available = update_get_available(TRUE)) { + module_load_include('inc', 'update', 'update.compare'); + $extension_data = update_calculate_project_data($available); + foreach ($extension_data as $name => $project) { + //Filter out extensions which are dev versions, updated or core + if (($project['install_type'] == 'dev') || ($project['status'] == UPDATE_CURRENT) || ($project['project_type'] == 'core')) { + unset($extension_data[$name]); + } + } + } + else { + unset($form); + $form = array(); + $form['message'] = array( + '#type' => 'item', + '#markup' => t('There was a problem getting update information. Please try again later.'), + ); + return; + } + + $options = array(); + //sort in reverse order by name... because of uasort, + //this is required because later entries come first when same. + krsort($extension_data); + uasort($extension_data, create_function('$a, $b', 'return ($a["status"] == UPDATE_NOT_SECURE && $b["status"] != UPDATE_NOT_SECURE) ? -1 : 1;')); + + foreach ($extension_data as $name => $project) { + $options[$name]['title'] = l($project['title'], $project['link']); + if ($project['status'] == UPDATE_NOT_SECURE) { + $options[$name]['title'] .= ' ' . t('(Security update)'); + } + $options[$name]['type'] = check_plain($project['project_type']); + $options[$name]['#attributes'] = array('class' => _update_status_project_status_class($project['status'])); + $options[$name]['installed_version'] = $project['existing_version']; + $options[$name]['recommended_version'] = l($project['recommended'], $project['releases'][$project['recommended']]['release_link'], array('attributes' => array('title' => t('Release notes for @project_name', array('@project_name' => $project['title']))))); + } + + if (!$options) { + $form = array(); + $form['message'] = array( + '#type' => 'item', + '#markup' => t('All of your extensions are up to date.'), + ); + return $form; + } + + $form['extensions_to_update'] = array( + '#type' => 'tableselect', + '#title' => 'choose', + '#options' => $options, + '#header' => array('title' => t('Name'), 'type' => t('Type'), 'installed_version' => t('Installed version'), 'recommended_version' => t('Recommended version')), + ); +} + + +function _update_confirmation_form(&$form, &$form_state) { + + $form['submit']['#name'] = 'process_updates'; + if (variable_get('site_offline', FALSE) == FALSE) { + $form['submit']['#value'] = t('Put site into maintinance mode and install updates'); + } + else { + $form['submit']['#value'] = t('Install updates'); + } + + $form['extensions_to_update'] = array( + '#type' => 'value', + '#value' => array_filter(array_values($form_state['values']['extensions_to_update'])), + ); + + $form['information'] = array(); + $form['information']['#weight'] = -100; + $form['information']['backup_header'] = array( + '#prefix' => '

', + '#markup' => t('Step 1: Backup your site'), + '#suffix' => '

', + ); + + $form['information']['backup_message'] = array( + '#prefix' => '

', + '#markup' => t('We do not currently have a web based backup tool. Learn more about how to take a backup.', array('@backup_url' => url('http://drupal.org/node/22281'))), + '#suffix' => '

', + ); + + $form['information']['main_header'] = array( + '#prefix' => '

', + '#markup' => t('Step 2: Provide your server connection details'), + '#suffix' => '

', + ); + + //Get all the available ways to transfer files + $available_backends = module_invoke_all('filetransfer_backends'); + if (count($available_backends) == 0) { + unset($form['submit']); + //@TODO: Clean up this error handling + drupal_set_message(t('Unable to continue, not available methods fo file transfer')); + } + + uasort($available_backends, 'drupal_sort_weight'); + //Get the currently selected option or previously saved option + $update_filetransfer_preferred = variable_get('update_filetransfer_preferred', NULL); + + if (isset($form_state['values']['connection_settings']['update_filetransfer_preferred'])) { + //We prefer the one last submitted. + $update_filetransfer_preferred = $form_state['values']['connection_settings']['update_filetransfer_preferred']; + } + + //If unset, take the first one available + $update_filetransfer_preferred = empty($update_filetransfer_preferred) ? key($available_backends) : $update_filetransfer_preferred; + + $form['connection_settings']['update_filetransfer_preferred'] = array( + '#type' => 'select', + '#title' => t('Connection method'), + '#default_value' => $update_filetransfer_preferred, + ); + + //Build a hidden fieldset for each one + foreach ($available_backends as $name => $backend) { + $form['connection_settings']['update_filetransfer_preferred']['#options'][$name] = $backend['title']; + $form['connection_settings'][$name] = array( + '#type' => 'fieldset', + '#attributes' =>array('class' => "filetransfer-$name", 'style' => 'display:none'), //we add this so we can show/hide later + '#title' => t('@backend connection settings', array('@backend' => $backend['title'])), + ); + $current_settings = update_get_filetransfer_settings($name); + $form['connection_settings'][$name] += system_get_filetransfer_settings_form($name, $current_settings); + } + + //If the user has already picked one before, show it. + if ($update_filetransfer_preferred) { + $form['connection_settings'][$update_filetransfer_preferred]['#attributes']['style'] = ''; + } + else { + $form['connection_settings']['update_filetransfer_preferred']['#type'] = 'radios'; + } + + $form['connection_settings']['#tree'] = TRUE; + drupal_add_js(drupal_get_path('module', 'update') . '/update.js', 'file'); +} + + +function update_update_form_validate($form, &$form_state) { + //form_set_error('connection_settings', "poo"); + if (isset($form_state['values']['connection_settings'])) { + _update_validate_filetransfer_settings($form, &$form_state); + } +} + +function _update_validate_filetransfer_settings($form, &$form_state) { + $filetransfer_backend_to_test = $form_state['values']['connection_settings']['update_filetransfer_preferred']; + $connection_settings = $form_state['values']['connection_settings'][$filetransfer_backend_to_test]; + $available_backends = module_invoke_all('filetransfer_backends'); + $filetransfer_class = $available_backends[$filetransfer_backend_to_test]['class']; + $filetransfer = call_user_func_array("{$filetransfer_class}::factory", array(DRUPAL_ROOT, $connection_settings)); + try { + $filetransfer->connect(); + } catch (Exception $e) { + form_set_error('connection_settings', $e->getMessage()); + } +} + +function update_update_form_submit($form, &$form_state) { + //We always want to rebuild unless we are done. + $form_state['rebuild'] = TRUE; + + if ($form_state['clicked_button']['#name'] == 'process_updates') { + //save the connection settings to the DB. + $filetransfer_backend_to_use = $form_state['values']['connection_settings']['update_filetransfer_preferred']; + $connection_settings = $form_state['values']['connection_settings'][$filetransfer_backend_to_use]; + + //This is a hack, perhaps we should add an attribute to non-stored fields? + //For SSH keys we wouldn't want to use a password field for instance. + $connection_settings_to_save = array(); + foreach ($connection_settings as $key => $value) { + if ($form['connection_settings'][$filetransfer_backend_to_use][$key]['#type'] != 'password') { + $connection_settings_to_save[$key] = $value; + } + } + //Set this one as the preferred update method. + variable_set('update_filetransfer_preferred', $filetransfer_backend_to_use); + //Save the connection settings minus the password + update_set_filetransfer_settings($filetransfer_backend_to_use, $connection_settings_to_save); + + $operations = array(); + foreach ($form_state['values']['extensions_to_update'] as $extension) { + + //put this in the begining (download everything first) + $operations[] = array('update_get_extension', array($extension)); + //put these on the end + $operations[] = array( + 'update_copy_extension', + array( + $extension, + update_get_default_filetransfer($connection_settings) + ), + ); + $operations[] = array('update_install_extension', array($extension)); + } + + $batch = array( + 'title' => t('Installing updates'), + 'init_message' => t('Preparing update operation'), + 'operations' => $operations, + 'finished' => 'update_batch_finished', + ); + batch_set($batch); + // When not in a form submit handler (which are the 'natural' places + // to set up a batch processing), you additionally have to manually + // trigger redirection to the processing / progress bar page, using: + batch_process(); + //theme('update_report', ) + } +} + + +/** + * Returns a class + */ +function _update_status_project_status_class($status) { + switch ($status) { + case UPDATE_CURRENT: + $class = 'ok'; + break; + case UPDATE_UNKNOWN: + case UPDATE_NOT_FETCHED: + $class = 'unknown'; + break; + case UPDATE_NOT_SECURE: + case UPDATE_REVOKED: + case UPDATE_NOT_SUPPORTED: + $class = 'error'; + break; + case UPDATE_NOT_CHECKED: + case UPDATE_NOT_CURRENT: + default: + $class = 'warning'; + break; + } + return $class; +} \ No newline at end of file Index: modules/update/update.info =================================================================== RCS file: /cvs/drupal/drupal/modules/update/update.info,v retrieving revision 1.5 diff -u -r1.5 update.info --- modules/update/update.info 11 Oct 2008 02:33:12 -0000 1.5 +++ modules/update/update.info 7 Jul 2009 08:59:32 -0000 @@ -7,6 +7,7 @@ files[] = update.module files[] = update.compare.inc files[] = update.fetch.inc -files[] = update.report.inc +files[] = update.admin.inc files[] = update.settings.inc files[] = update.install +files[] = update.js Index: modules/update/update.js =================================================================== RCS file: modules/update/update.js diff -N modules/update/update.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/update/update.js 7 Jul 2009 08:59:32 -0000 @@ -0,0 +1,37 @@ +(function ($) { + +Drupal.behaviors.updateFileTransferForm = { + attach: function(context) { + Drupal.updateFileTransferForm = new Drupal.updateFileTransferForm($('#update-update-form'), Drupal.settings.updateFileTransferForm); + } +} +Drupal.updateFileTransferForm = function(form, data) { + var self = this; + this.form = form; + this.formSettings = data; + + this.getFiletransferSelectBox().bind('change', function() { + self.changeFileTransferType(); + }); + + this.changeFileTransferType(); + +}; + +Drupal.updateFileTransferForm.prototype.getFiletransferSelectBox = function() { + return $("#edit-connection-settings-update-filetransfer-preferred", this.form); +} + +Drupal.updateFileTransferForm.prototype.changeFileTransferType = function(){ + + // take note of "self", there's a reason its there + var self = this; + + var currently_selected = this.getFiletransferSelectBox().val(); + //show one form or the other. + $('fieldset', self.form).hide(); + $('.filetransfer-' + currently_selected, self.form).fadeIn(200); +} + + +})(jQuery); \ No newline at end of file Index: modules/update/update.module =================================================================== RCS file: /cvs/drupal/drupal/modules/update/update.module,v retrieving revision 1.37 diff -u -r1.37 update.module --- modules/update/update.module 8 Jun 2009 05:00:11 -0000 1.37 +++ modules/update/update.module 7 Jul 2009 08:59:33 -0000 @@ -62,6 +62,16 @@ define('UPDATE_MAX_FETCH_ATTEMPTS', 2); /** + * Used when determining how to act on a given extension when removing / installing + */ +define('UPDATE_EXTENSION_TYPE_MODULE', 1); + +/** + * Used when determining how to act on a given extension when removing / installing + */ +define('UPDATE_EXTENSION_TYPE_THEME', 2); + +/** * Implement hook_help(). */ function update_help($path, $arg) { @@ -69,8 +79,7 @@ case 'admin/reports/updates': global $base_url; $output = '

' . 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.') . '

'; - $output .= '

' . t('To extend the functionality or to change the look of your site, a number of contributed modules and themes are available.', array('@modules' => 'http://drupal.org/project/modules', '@themes' => 'http://drupal.org/project/themes')) . '

'; - $output .= '

' . t('Each time Drupal core or a contributed module or theme is updated, it is important that update.php is run.', array('@update-php' => url($base_url . '/update.php', array('external' => TRUE)))) . '

'; + return $output; case 'admin/build/themes': case 'admin/build/modules': @@ -125,13 +134,22 @@ */ function update_menu() { $items = array(); - + $items['admin/reports/updates'] = array( 'title' => 'Available updates', 'description' => 'Get a status report about available updates for your installed modules and themes.', - 'page callback' => 'update_status', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('update_update_form'), + 'access arguments' => array('administer site configuration'), + 'weight' => 10, + ); + $items['admin/update'] = array( + 'title' => 'Updating modules and themes', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('update_update_form'), 'access arguments' => array('administer site configuration'), 'weight' => 10, + 'type' => MENU_CALLBACK, ); $items['admin/settings/updates'] = array( 'title' => 'Updates', @@ -158,9 +176,19 @@ 'update_settings' => array( 'arguments' => array('form' => NULL), ), + 'update_confirm' => array( + 'template' => 'theme/update-confirm', + 'arguments' => array('form' => NULL), + ), 'update_report' => array( 'arguments' => array('data' => NULL), ), + 'update_report_form' => array( + 'arguments' => array('form' => NULL), + ), + 'update_status_project_status' => array( + 'arguments' => array('status' => NULL), + ), 'update_version' => array( 'arguments' => array('version' => NULL, 'tag' => NULL, 'class' => NULL), ), @@ -632,3 +660,163 @@ /** * @} End of "defgroup update_status_cache". */ + +function _update_get_latest_version($name) { + if ($available = update_get_available(FALSE)) { + module_load_include('inc', 'update', 'update.compare'); + $extension_data = update_calculate_project_data($available); + $project = $extension_data[$name]; + return $project['releases'][$project['latest_version']]; + } +} + + +function update_get_file($url) { + // Get each of the specified files. + $parsed_url = parse_url($url); + $local = file_directory_temp() . '/update-cache/' . basename($parsed_url['path']); + if (!file_exists(file_directory_temp() . '/update-cache/')) { + mkdir(file_directory_temp() . '/update-cache/'); + } + // Check the cache and download the file if needed. + if (!file_exists($local)) { + // $result->data is the actual contents of the downloaded file. This saves + // it into a local file, whose path is stored in $local. $local is stored + // relative to the Drupal installation. + return system_retrieve_file($url, $local); + } + else { + return $local; + } +} + +function update_untar($file) { + $extraction_dir = file_directory_temp() . '/update-extraction'; + if (!file_exists($extraction_dir)) { + mkdir($extraction_dir); + } + $archive_tar = new Archive_Tar($file); + return $archive_tar->extract($extraction_dir); +} + +/** + * Helper function to get location of an extension + */ +function _update_get_extension_location($name) { + if ($_tmp = _update_get_extension_type_and_location($name)) { + return $_tmp['location']; + } +} + +/** + * Helper function, returns a an associative array of an extensions type and location + * returns false on failure. (Should really throw an exception) + */ +function _update_get_extension_type_and_location($name) { + if ($dir = drupal_get_path('module', $name)) { + $type = UPDATE_EXTENSION_TYPE_MODULE; + } + elseif ($dir = drupal_get_path('theme', $name)) { + $type = UPDATE_EXTENSION_TYPE_THEME; + } + else { + return FALSE; + } + return array('type' => UPDATE_EXTENSION_TYPE_THEME, 'location' => $dir); +} + +/** + * Batch operations + */ + +function update_get_extension($extension_name, &$context) { + if (!isset($context['sandbox']['starting'])) { + $context['sandbox']['starting'] = 1; + $context['message'] = t('Downloading %extension', array('%extension' => $extension_name)); + $context['finished'] = 1 / 2; + return; + } + $latest_version = _update_get_latest_version($extension_name); + if ($local_cache = update_get_file($latest_version['download_link'])) { + watchdog('update', 'Downloaded %extension to %local_cache', array('%extension' => $extension_name, '%local_cache' => $local_cache)); + } + else { + $context['success'] = FALSE; + $content['results'][] = t('Failed to download %extension', array('%extension' => $extension_name)); + } + + $context['finished'] = 1; +} + +function update_copy_extension($extension_name, $filetransfer, &$context) { + if (!isset($context['sandbox']['starting'])) { + $context['sandbox']['starting'] = 1; + $context['message'] = t('Copying %extension to server', array('%extension' => $extension_name)); + $context['finished'] = .5; + return; + } + + $latest_version = _update_get_latest_version($extension_name); + $local_cache = update_get_file($latest_version['download_link']); + + $extension_destination_dir = DRUPAL_ROOT . '/' . _update_get_extension_location($extension_name); + if (!$extension_destination_dir) { + throw new Exception(t("Unable to find %extension_name", array('%extension_name' => $extension_destination_dir))); + } + + if (update_untar($local_cache)) { + $extension_source_dir = file_directory_temp() . '/update-extraction/' . $extension_name; + } + try { + $filetransfer->removeDirectory($extension_destination_dir); + $filetransfer->copyDirectory($extension_source_dir, $extension_destination_dir); + } catch (Exception $e) { + drupal_set_message(t($e->getMessage(), $e->arguments), 'error'); + //Some better error handling is needed, but batch API doesn't seem to support any. + throw $e; + } + + $context['finished'] = 1; + +} + +function update_install_extension($extension_name, &$context) { + if (!isset($context['sandbox']['done'])) { + $context['sandbox']['done'] = 0; + } + if (!$context['results']) { + $context['results'][] = t('Installed %extension', array('%extension' => $extension_name)); + } + + $context['message'] = t('Pretending to Install %extension', array('%extension' => $extension_name)); + sleep(1); + $context['sandbox']['done'] += 1; + $context['finished'] = $context['sandbox']['done'] / 4; +} + +function update_batch_finished($success, $results) { + if ($success) { + variable_set('site_offline', FALSE); + } + $_SESSION['update_batch_results']['success'] = $success; + $_SESSION['update_batch_results']['messages'] = $results; +} + +function update_get_default_filetransfer($overrides = array()) { + //Fire up the connection class + $update_filetransfer_preferred = variable_get('update_filetransfer_preferred', NULL); + $settings = variable_get("update_filetransfer_connection_settings::{$update_filetransfer_preferred}", array()); + $settings = array_merge($settings, $overrides); + $available_backends = module_invoke_all('filetransfer_backends'); + $filetransfer = call_user_func_array("{$available_backends[$update_filetransfer_preferred]['class']}::factory", array(DRUPAL_ROOT, $settings)); + return $filetransfer; +} + +function update_get_filetransfer_settings($filetransfer_backend_name) { + return variable_get("update_filetransfer_connection_settings::{$filetransfer_backend_name}", array()); +} + +function update_set_filetransfer_settings($filetransfer_backend_name, $settings) { + variable_set("update_filetransfer_connection_settings::{$filetransfer_backend_name}", $settings); +} +