diff --git includes/filetransfer/filetransfer.inc includes/filetransfer/filetransfer.inc index bda9279..e174881 100644 --- includes/filetransfer/filetransfer.inc +++ includes/filetransfer/filetransfer.inc @@ -266,7 +266,8 @@ abstract class FileTransfer { * Gets the chroot property for this connection. It does this by moving up * the tree until it finds itself. If successful, it will return a chroot. * - * @return string chroot + * @return string + * Chroot path or FALSE */ function findChroot() { // If the file exists as is, there is no chroot. diff --git includes/filetransfer/ftp.inc includes/filetransfer/ftp.inc index 3e77264..1cf7d7d 100644 --- includes/filetransfer/ftp.inc +++ includes/filetransfer/ftp.inc @@ -5,6 +5,15 @@ * Connection class using the FTP URL wrapper. */ class FileTransferFTPWrapper extends FileTransfer { + + public function __construct($jail, $username, $password, $hostname, $port) { + $this->username = $username; + $this->password = $password; + $this->hostname = $hostname; + $this->port = $port; + parent::__construct($jail); + } + function connect() { $this->connection = 'ftp://' . urlencode($this->username) . ':' . urlencode($this->password) . '@' . $this->hostname . ':' . $this->port . '/'; if (!is_dir($this->connection)) { @@ -19,29 +28,29 @@ class FileTransferFTPWrapper extends FileTransfer { } function createDirectoryJailed($directory) { - if (!@drupal_mkdir($directory)) { + if (!@drupal_mkdir($this->connection . $directory)) { $exception = new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory)); throw $exception; } } function removeDirectoryJailed($directory) { - if (is_dir($directory)) { - $dh = opendir($directory); + if (is_dir($this->connection . $directory)) { + $dh = opendir($this->connection . $directory); while (($resource = readdir($dh)) !== FALSE) { if ($resource == '.' || $resource == '..') { continue; } $full_path = $directory . DIRECTORY_SEPARATOR . $resource; - if (is_file($full_path)) { + if (is_file($this->connection . $full_path)) { $this->removeFile($full_path); } - elseif (is_dir($full_path)) { + elseif (is_dir($this->connection . $full_path)) { $this->removeDirectory($full_path . '/'); } } closedir($dh); - if (!rmdir($directory)) { + if (!rmdir($this->connection . $directory)) { $exception = new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory)); throw $exception; } diff --git includes/plugin.inc includes/plugin.inc new file mode 100644 index 0000000..f07e166 --- /dev/null +++ includes/plugin.inc @@ -0,0 +1,309 @@ + '

', + '#markup' => t('To continue please provide your server connection details'), + '#suffix' => '

', + ); + + $form['connection_settings']['#tree'] = TRUE; + $form['connection_settings']['update_filetransfer_default'] = array( + '#type' => 'select', + '#title' => t('Connection method'), + '#default_value' => $update_filetransfer_default, + '#weight' => -10, + ); + + /** + * Here we create two submit buttons. For a JS enabled client, + * they will only ever see submit_process. + * + * However, if a client doesn't have JS enabled, they will see submit_connection + * on the first form (whden picking what filetranfer type to use, and + * submit_process on the second one (which leads to the actual operation) + * + */ + $form['submit_connection'] = array( + '#prefix' => "
", + '#name' => 'enter_connection_settings', // This is later changed in JS. + '#type' => 'submit', + '#value' => t('Enter connetion settings'), // As is this. @see update.js. + '#weight' => 100, + ); + + $form['submit_process'] = array( + '#name' => 'process_updates', // This is later changed in JS. + '#type' => 'submit', + '#value' => t('Process Updates'), // As is this. @see update.js + '#weight' => 100, + '#attributes' => array('style' => 'display:none'), + ); + + // Build a hidden fieldset for each one. + foreach ($available_backends as $name => $backend) { + $form['connection_settings']['update_filetransfer_default']['#options'][$name] = $backend['title']; + $form['connection_settings'][$name] = array( + '#type' => 'fieldset', + '#attributes' => array('class' => "filetransfer-$name filetransfer"), + '#title' => t('@backend connection settings', array('@backend' => $backend['title'])), + ); + + $current_settings = variable_get("update_filetransfer_connection_settings_" . $name, array()); + $form['connection_settings'][$name] += system_get_filetransfer_settings_form($name, $current_settings); + + // Start non-JS code. + if (isset($form_state['values']['connection_settings']['update_filetransfer_default']) && $form_state['values']['connection_settings']['update_filetransfer_default'] == $name) { + /** + * This means the form has been submitted by a non-JS user, and they have + * choosen a transfer type. + */ + + // If the user switches from JS to non-JS, Drupal (and Batch API) will barf. + // This is a known bug: http://drupal.org/node/229825. + setcookie('has_js', '', time() - 3600, '/'); + unset($_COOKIE['has_js']); + + // Change the submit button to the submit_process one. + $form['submit_process']['#attributes'] = array(); + unset($form['submit_connection']); + // + // Active the proper filetransfer settings form. + $form['connection_settings'][$name]['#attributes']['style'] = 'display:block'; + // Disable the select box. + $form['connection_settings']['update_filetransfer_default']['#disabled'] = TRUE; + + // Create a button for changing the type of connection. + $form['connection_settings']['change_connection_type'] = array( + '#name' => 'change_connection_type', + '#type' => 'submit', + '#value' => t('Change connection type'), + '#weight' => -5, + '#attributes' => array('class' => 'filetransfer-change-connection-type'), + ); + } + //End non-JS code + } + return $form; +} + +/** + * Validate function for the main update form. + * + * @see update_update_form + */ +function plugin_filetransfer_form_validate($form, &$form_state) { + if (isset($form_state['values']['connection_settings'])) { + $backend = $form_state['values']['connection_settings']['update_filetransfer_default']; + $filetransfer = update_get_filetransfer($backend, $form_state['values']['connection_settings'][$backend]); + try { + if (!$filetransfer) { + throw new Exception(t("Error, this type of connection protocol (%backend) doesn't exist.", array('%backend' => $backend))); + } + $filetransfer->connect(); + } + catch (Exception $e) { + form_set_error('connection_settings', $e->getMessage()); + } + } +} + +/** + * Submit function to handle server creds being entered. + * + * @see update_update_form + */ +function plugin_filetransfer_form_submit($form, &$form_state) { + global $base_url; + switch ($form_state['clicked_button']['#name']) { + case 'process_updates': + + // Save the connection settings to the DB. + $filetransfer_backend = $form_state['values']['connection_settings']['update_filetransfer_default']; + + // If the database is available then try to save our settings. We have to make sure it is available + // since this code could potentially (will likely) be called during the installation process, before + // the database is set up. + if (db_is_active()) { + $connection_settings = array(); + foreach ($form_state['values']['connection_settings'][$filetransfer_backend] as $key => $value) { + if (!isset($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save']) || + $form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save']) { + $connection_settings[$key] = $value; + } + } + // Set this one as the default update method. + variable_set('update_filetransfer_default', $filetransfer_backend); + // Save the connection settings minus the password. + variable_set("update_filetransfer_connection_settings_" . $filetransfer_backend, $connection_settings); + + // chmod the modules directory to 755 + $ft = update_get_filetransfer($filetransfer_backend, $form_state['values']['connection_settings'][$filetransfer_backend]); + + try { + // Create the modules and themes directories if needed. + _plugin_manager_create_and_setup_directories($ft); + } catch (Exception $e) { + drupal_set_message($e->getMessage()); + return; + } + + // Now run the batch. + plugin_manager_run_batch($ft); + } + break; + + case 'enter_connection_settings': + $form_state['rebuild'] = TRUE; + break; + + case 'change_connection_type': + $form_state['rebuild'] = TRUE; + unset($form_state['values']['connection_settings']['update_filetransfer_default']); + break; + } +} + +/** + * Creates the installation directories for all of the types of + * projects that can be operated on. + * + * @param FileTransfer $filetransfer + */ +function _plugin_manager_create_and_setup_directories($filetransfer) { + $updater_classes = update_get_updaters(); + + foreach ($updater_classes as $class) { + $parent_dir = ''; + + $install_location = call_user_func("{$class}::getInstallDirectory"); + // Make the parent dir writable if need be and create the dir. + if (!is_dir($install_location)) { + $parent_dir = dirname($install_location); + if (!is_writable($parent_dir)) { + @chmod($parent_dir, 0755); + /** + * It is expected that this will fail if the conf_dir() + * is owned by the FTP user. If the FTP user == web server, + * it will succeed. + */ + } + try { + $filetransfer->createDirectory($install_location); + } catch (FileTransferException $e) { + $message = $e->getMessage(); + $thrown_message = t('Unable to create %directory due to the following: %reason', array('%directory' => $install_location, '%reason' => $message)); + throw new Exception($throw_message); + } + // Put the parent directory back + @chmod($parent_dir, 0555); + } + } +} + +/** + * Blantantly (and shamefully) (and regrettably) ripped from update.php. + * + * Interim measure, this code is not very clear. + * @param array $messages + * @return string Report similar to update.php + */ +function get_plugin_report($messages) { + $output = ""; + // Output a list of queries executed + if (!empty($messages)) { + $output .= '
'; + $output .= '

The following actions were executed

'; + foreach ($messages as $module => $updates) { + $output .= '

' . $module . '

'; + foreach ($updates as $number => $queries) { + if ($number != '#abort') { + $output .= '

Update #' . $number . '

'; + $output .= ''; + } + } + $output .= '
'; + } + return $output; +} + + +/** + * Runs the batch operation specified in $_SESSION['plugin_op'] + * + * @global string $base_url + * @param FileTransfer $filetransfer + */ +function plugin_manager_run_batch($filetransfer) { + global $base_url; + drupal_set_title('Installing updates'); + $batch = $_SESSION['plugin_op']; + unset($_SESSION['plugin_op']); + foreach ($batch['operations'] as $key => $args) { + // Add the filetransfer class to the existing batch which was defined elsewhere. + // This is obviously not pretty, but don't know a better method. + $function = $args[0]; + if ($function == 'update_batch_copy_project') { + $batch['operations'][$key][1][] = $filetransfer; + } + } + + batch_set($batch); + batch_process($base_url . '/plugin.php', $base_url . '/plugin.php?batch=1'); +} diff --git modules/system/system.module modules/system/system.module index d126a79..1d001f4 100644 --- modules/system/system.module +++ modules/system/system.module @@ -1399,27 +1399,30 @@ function system_filetransfer_backend_form_ssh() { * Helper function because SSH and FTP backends share the same elements */ function _system_filetransfer_backend_form_common() { + $form = array(); + $form['hostname'] = array ( '#type' => 'textfield', '#title' => t('Host'), '#default_value' => 'localhost', ); - - $form['port'] = array ( + + $form['port'] = array( '#type' => 'textfield', '#title' => t('Port'), '#default_value' => NULL, ); - $form['username'] = array ( + $form['username'] = array( '#type' => 'textfield', '#title' => t('Username'), ); - $form['password'] = array ( + $form['password'] = array( '#type' => 'password', '#title' => t('Password'), '#description' => t('This is not saved in the database and is only used to test the connection'), + '#filetransfer_save' => FALSE, ); return $form; diff --git modules/update/update.admin.inc modules/update/update.admin.inc new file mode 100644 index 0000000..e7111a8 --- /dev/null +++ modules/update/update.admin.inc @@ -0,0 +1,628 @@ + 'submit', + '#name' => 'process_updates', + '#value' => t('Install these updates'), + '#weight' => 100, + ); + } + return $form; +} + +/** + * The first page of the update form. + * This displays available updates in the form of a table. + */ +function _update_update_form_update_table() { + drupal_add_css('misc/ui/ui.all.css'); + drupal_add_css('misc/ui/ui.dialog.css'); + drupal_add_js('misc/ui/ui.core.js', array('weight' => JS_LIBRARY + 5)); + drupal_add_js('misc/ui/ui.dialog.js', array('weight' => JS_LIBRARY + 6)); + $form['#attached']['js'][] = DRUPAL_ROOT . '/misc/plugin.js'; + + $form['#theme'] = 'update_available_updates_form'; + $form['projects'] = array(); + // First step. + $options = array(); + $available = update_get_available(TRUE); + if ($available) { + + module_load_include('inc', 'update', 'update.compare'); + $project_data = update_calculate_project_data($available); + foreach ($project_data as $name => $project) { + // Filter out projects which are up2date already. + if ($project['status'] == UPDATE_CURRENT) { + continue; + } + + $type = ""; // The type of update (security, recommended, manual, etc). + $options[$name] = array(); // An array of projects to create the table with. + + + $options[$name]['title'] = l($project['title'], $project['link']); + if ($project['project_type'] == 'theme') { + $options[$name]['title'] .= ' ' . t('(Theme)'); + } + + $options[$name]['installed_version'] = $project['existing_version']; + $recommended_version = $project['releases'][$project['recommended']]; + + // Mark projects which are dev versions with no stable release or core as manual installs + if (($project['install_type'] == 'dev' && $recommended_version['version_extra'] == 'dev') || ($project['project_type'] == 'core')) { + $options[$name]['#manual_update'] = TRUE; + } + + $options[$name]['recommended_version'] = $recommended_version['version'] . ' ' . l(t('(Release notes)'), $recommended_version['release_link'], array('attributes' => array('title' => t('Release notes for @project_name', array('@project_name' => $project['title']))))); + + if ($recommended_version['version_major'] != $project['existing_major']) { + $options[$name]['recommended_version'] .= '
' . t('This update is a major version update which means that it may not be backwards compatible with your currently running version. It is recommended that you read the release notes and proceed at your own risk.') . '
'; + } + + if (empty($type)) { + switch ($project['status']) { + case UPDATE_NOT_SECURE: + case UPDATE_REVOKED: + $options[$name]['title'] .= ' ' . t('(Security Update)'); + $options[$name]['#weight'] = -2; + $type = 'security'; + break; + case UPDATE_NOT_SUPPORTED: + $type = 'unsupported'; + $options[$name]['title'] .= ' ' . t('(Unsupported)'); + $options[$name]['#weight'] = -1; + break; + case UPDATE_UNKNOWN: + case UPDATE_NOT_FETCHED: + case UPDATE_NOT_CHECKED: + case UPDATE_NOT_CURRENT: + $type = 'recommended'; + break; + default: + // Continues out of the switch and then out of the foreach. + continue 2; + } + } + + $options[$name]['#attributes'] = array('class' => array('update-' . $type)); + } + } + else { + $form['message'] = array( + '#markup' => t('There was a problem getting update information. Please try again later.'), + ); + return $form; + } + + $form['#attached']['css'][] = drupal_get_path('module', 'update') . '/update.css'; + + $headers = array('title' => array('data' => t('Name'), 'class' => array('update-project-name')), 'installed_version' => t('Installed version'), 'recommended_version' => t('Recommended version')); + // This is the kill switch for being able to perform updates + if (update_plugin_manager_access()) { + $manual_updates = array(); + foreach ($options as $key => $option) { + if (!empty($option['#manual_update'])) { + unset($option['#manual_update']); + $manual_updates[] = $option; + unset($options[$key]); + } + } + if (count($options)) { + $form['projects'] = array( + '#type' => 'tableselect', + '#options' => $options, + '#header' => $headers, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Install these updates'), + '#weight' => 10, + ); + } + + if ($manual_updates) { + $form['manual_updates'] = array(); + $form['manual_updates']['#prefix'] = '

' . t('Add-ons requiring manual updates') . '

'; + $form['manual_updates']['#prefix'] .= '

' . t('Updates of development releases or Drupal core are not supported at this time'); + $form['manual_updates']['#type'] = "markup"; + $form['manual_updates']['#markup'] = theme('table', array('headers' => $headers, 'manual_updates' => $manual_updates)); + $form['manual_updates']['#weight'] = 20; + } + elseif (!count($options)) { + $form['message'] = array( + '#markup' => t('All of your projects are up to date.'), + ); + return $form; + } + } + else { + $form['projects'] = array(); + $form['projects']['#type'] = "markup"; + $form['projects']['#markup'] = theme('table', array('headers' => $headers, 'options' => $options)); + } + + return $form; +} + +function _update_update_form_display_steps() { + $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']['maint_header'] = array( + '#prefix' => '

', + '#markup' => t('Step 2: Enter maintenance mode'), + '#suffix' => '

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

', + '#markup' => t('It is strongly recommended that you put your site into maintenance mode while performing an update.'), + '#suffix' => '

', + ); + + $form['information']['site_offline'] = array( + '#title' => t('Perform updates with site in maintenance mode'), + '#type' => 'checkbox', + '#default_value' => TRUE, + ); + + return $form; +} + +/** + * Submit function for the main update form. + * + * @see update_update_form() + */ +function update_update_form_submit($form, &$form_state) { + global $base_url; + switch ($form_state['clicked_button']['#name']) { + case 'process_updates': + $operations = array(); + foreach ($form_state['storage']['projects'] as $project) { + // @TODO: Present some type of warning when updating multi-site used modules. + // Put this in the begining (download everything first) + $operations[] = array('update_batch_get_project', array($project)); + $latest_version = _update_get_recommended_version($project); + // This is the .tar.gz from d.o. + $url = $latest_version['download_link']; + + $operations[] = array( + 'update_batch_copy_project', + array( + $project, + $url, + ), + ); + } + + $batch = array( + 'title' => t('Installing updates'), + 'init_message' => t('Preparing update operation'), + 'operations' => $operations, + 'finished' => 'update_batch_finished', + 'file' => drupal_get_path('module', 'update') . '/update.admin.inc', + ); + + $_SESSION['plugin_op'] = $batch; + + if ($form_state['values']['site_offline'] == TRUE) { + // Put site in offline mode. + variable_set('site_offline', TRUE); + } + drupal_goto($base_url . '/plugin.php'); + return; + + break; + + default: + $form_state['rebuild'] = TRUE; + // This is the first page, and store the list of selected projects + $form_state['storage']['projects'] = array_keys(array_filter($form_state['values']['projects'])); + break; + } +} + + +function update_install_form(&$form_state) { + $form = array(); + + $form['project_url'] = array( + '#type' => 'textfield', + '#title' => t('URL'), + '#description' => t('Paste the url to a Drupal module or theme archive (.tar.gz) here to install it. (e.g http://ftp.drupal.org/files/projects/projectname.tar.gz)'), + ); + + $form['information'] = array( + '#prefix' => '', + '#markup' => 'Or', + '#suffix' => '', + ); + + $form['project_upload'] = array( + '#type' => 'file', + '#title' => t('Upload a module or theme'), + '#description' => t('Upload a Drupal module or theme (in .tar.gz format) to install it.'), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Install'), + ); + + return $form; +} + +function update_install_form_validate($form, &$form_state) { + if (!($form_state['values']['project_url'] XOR !empty($_FILES['files']['name']['project_upload']))) { + form_set_error('project_url', t('Unable to continue, please provide a url or upload a module / theme')); + return; + //$data = drupal_http_request($form_state['values']['url']); + } +} + +function update_install_form_submit($form, &$form_state) { + global $base_url; + + if ($form_state['values']['project_url']) { + $field = 'project_url'; + $local_cache = update_get_file($form_state['values']['project_url']); + if (!$local_cache) { + form_set_error($field, t('Unable to retreive Drupal project from %url', array('%url' => $form_state['values']['project_url']))); + return; + } + } + elseif ($_FILES['files']['name']['project_upload']) { + $field = 'project_upload'; + // @todo: add some validators here. + $finfo = file_save_upload($field, array(), NULL, FILE_EXISTS_REPLACE); + // @todo: find out if the module is already instealled, if so, throw an error. + $local_cache = $finfo->uri; + } + + $archive_tar = new Archive_Tar(drupal_realpath($local_cache)); + + $files = $archive_tar->listContent(); + if (!$files) { + form_set_error($field, t('Provided URL is not a .tar.gz archive', array('%url' => $form_state['values']['url']))); + return; + } + + $project = drupal_substr($files[0]['filename'], 0, -1); // Unfortunately, we can only use the directory name for this. :( + + $project_location = DRUPAL_ROOT . '/' . file_directory_path('temporary') . '/update-extraction/' . $project; + update_untar(drupal_realpath($local_cache)); + + $updater = Updater::factory($project_location); + $project_title = Updater::getProjectTitle($project_location); + + if (!$project) { + form_set_error($field, t('Unable to determine %project name', array('%project' => $project_title))); + } + + if ($updater->isInstalled()) { + form_set_error($field, t('%project is already installed', array('%project' => $project_title))); + return; + } + + $operations = array(); + $operations[] = array( + 'update_batch_copy_project', + array( + $project, + $local_cache, + ), + ); + + $batch = array( + 'title' => t('Installing %project', array('%project' => $project_title)), + 'init_message' => t('Preparing update operation'), + 'operations' => $operations, + 'finished' => 'update_batch_finished', + 'file' => drupal_get_path('module', 'update') . '/update.admin.inc', + ); + + $_SESSION['plugin_op'] = $batch; + drupal_goto($base_url . '/plugin.php'); + return; + +} + +/** + * Batch operations related to installing / updating projects + */ + +/** + * Batch operation: download a project and put it in a temporary cache. + * + * @param string $project name of the project being installed + * @param array &$context BatchAPI storage + * + * @return void; + */ +function update_batch_get_project($project, &$context) { + if (!isset($context['results']['log'])) { + $context['results']['log'] = array(); + } + if (!isset($context['results']['log'][$project])) { + $context['results']['log'][$project] = array(); + } + if (!isset($context['sandbox']['started'])) { + $context['sandbox']['started'] = TRUE; + $context['message'] = t('Downloading %project', array('%project' => $project)); + $context['finished'] = 0; + return; + } + $latest_version = _update_get_recommended_version($project); + if ($local_cache = update_get_file($latest_version['download_link'])) { + watchdog('update', 'Downloaded %project to %local_cache', array('%project' => $project, '%local_cache' => $local_cache)); + } + else { + $context['success'] = FALSE; + $content['results'][$project][] = t('Failed to download %project', array('%project' => $project)); + } +} + + +/** + * Batch operation: copy a project to it's proper place. + * For updates, will locate the current project and replace it. + * For new installs, will download and try to determine the type from the info file + * and then place it variable_get(update_default_{$type}_location) i.e. update_default_module_location(). + * + * @todo Fix the $project param (refactor) + * @param string $project Either name of the project being installed or a + * @param string $url Location of a tarball to install if recommended version of $project not required + * @param string $filetransfer FileTransfer class + * @param array &$context BatchAPI storage + * + * @return void + */ +function update_batch_copy_project($project, $url, $filetransfer, &$context) { + + // Initialize some variables + if (!isset($context['results']['log'])) { + $context['results']['log'] = array(); + } + + if (!isset($context['results']['tasks'])) { + $context['results']['tasks'] = array(); + } + + /** + * Unfortuantely, because the batch API uses a session and a connection + * pointer will be lost between requests, when doing an update of multiple + * modules, we need to unset the connection pointer to re-init a connect. + */ + unset($filetransfer->connection); + + if (!isset($context['results']['log'][$project])) { + $context['results']['log'][$project] = array(); + } + + if (!empty($context['results']['log'][$project]['#abort'])) { + $context['#finished'] = 1; + return; + } + + $local_cache = update_get_file($url); + + // This extracts the file into the standard place. + try { + update_untar($local_cache); + } + catch (Exception $e) { + _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE); + $context['results']['log'][$project]['#abort'] = TRUE; + return; + } + + $project_source_dir = DRUPAL_ROOT . '/' . file_directory_path('temporary') . '/update-extraction/' . $project; + $updater = Updater::factory($project_source_dir); + + try { + if ($updater->isInstalled()) { + // This is an update. + $tasks = $updater->update($filetransfer); + } + else { + $tasks = $updater->install($filetransfer); + } + } + catch (UpdaterError $e) { + _update_batch_create_message($context['results']['log'][$project], t("Error installing / updating"), FALSE); + _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE); + $context['results']['log'][$project]['#abort'] = TRUE; + return; + } + + _update_batch_create_message($context['results']['log'][$project], t('Installed %project_name successfully', array('%project_name' => $project))); + $context['results']['tasks'] += $tasks; + + $context['finished'] = 1; +} + +/** + * Batch callback for when the batch is finished. + */ +function update_batch_finished($success, $results) { + foreach ($results['log'] as $module => $messages) { + if (!empty($messages['#abort'])) { + $success = FALSE; + } + } + $_SESSION['update_batch_results']['success'] = $success; + $_SESSION['update_batch_results']['messages'] = $results['log']; + $_SESSION['update_batch_results']['tasks'] = $results['tasks']; +} + +/** + * Helper function to create a structure of log messages similar to update.php + * + * @todo: Improve the update.php structure, or just fix this problem, or decide + * that plugin.php should never run updates. + * + * @param array $project_results + * @param string $message + * @param bool $success + */ +function _update_batch_create_message(&$project_results, $message, $success = TRUE) { + $next_number = count($project_results) + 1; + $project_results[$next_number] = array(array('query' => $message, 'success' => $success)); +} + +/** + * Theme main updates page. + * + * @param $variables + * form: The form. + * + * @ingroup themeable + */ +function theme_update_available_updates_form($variables) { + extract($variables); + $last = variable_get('update_last_check', 0); + $output = '
' . ($last ? t('Last checked: @time ago', array('@time' => format_interval(REQUEST_TIME - $last))) : t('Last checked: never')); + $output .= ' (' . l(t('Check manually'), 'admin/reports/updates/check') . ')'; + $output .= "
\n"; + $output .= drupal_render_children($form); + return $output; +} + +/** + * Untar a file, using the Archive_Tar class. + * + * @param string $file the filename you wish to extract + * + * @return void + * @throws Exception on failure. + */ +function update_untar($file) { + $extraction_dir = DRUPAL_ROOT . '/' . file_directory_path('temporary') . '/update-extraction'; + if (!file_exists($extraction_dir)) { + mkdir($extraction_dir); + } + $archive_tar = new Archive_Tar(drupal_realpath($file)); + if (!$archive_tar->extract($extraction_dir)) { + throw new Exception(t('Unable to extact %file', array('%file' => $file))); + } +} + +/** + * Get a filetransfer class. + * + * @param $method + * The filetransfer method to get the class for. + * @param $overrides + * A set of overrides over the defaults. + */ +function update_get_filetransfer($method, $overrides = array()) { + // Fire up the connection class + $settings = variable_get("update_filetransfer_connection_settings_" . $method, array()); + $settings = array_merge($settings, $overrides); + $available_backends = module_invoke_all('filetransfer_backends'); + $filetransfer = call_user_func_array(array($available_backends[$method]['class'], 'factory'), array(DRUPAL_ROOT, $settings)); + return $filetransfer; +} + +/** + * Coppies a file from $url to the temporary directory for updates. + * + * If the file has already been downloaded, returns the the local path. + * + * @param $url + * The URL of the file on the server. + * + * @return string + * Path to local file. + */ +function update_get_file($url) { + $parsed_url = parse_url($url); + $remote_schemes = array('http', 'https', 'ftp', 'ftps', 'smb', 'nfs'); + if (!in_array($parsed_url['scheme'], $remote_schemes)) { + // This is a local file, just return the path. + return drupal_realpath($url); + } + + // Check the cache and download the file if needed. + $local = 'temporary://update-cache/' . basename($parsed_url['path']); + + if (!file_exists(DRUPAL_ROOT . '/' . file_directory_path('temporary') . '/update-cache/')) { + mkdir(DRUPAL_ROOT . '/' . file_directory_path('temporary') . '/update-cache/'); + } + + if (!file_exists($local)) { + return system_retrieve_file($url, $local); + } + else { + return $local; + } +} + + +/** + * Gets the latest recommended release of a project. + * + * This function will prioritize updates on the same branch as the current version. + * + * @param string $name Name of the project + * @return array An array of information about the latest recommended + * release of the project + */ +function _update_get_recommended_version($name) { + if ($available = update_get_available(FALSE)) { + module_load_include('inc', 'update', 'update.compare'); + $project_data = update_calculate_project_data($available); + $project = $project_data[$name]; + return $project['releases'][$project['recommended']]; + } +} +/** + * Returns a list of classes which implement the DrupalUpdater interface + * + * @return array + */ +function update_get_updaters() { + $updaters = drupal_static(__FUNCTION__); + if (!$updaters) { + $updaters = array(); + foreach (get_declared_classes() as $class) { + if (in_array('DrupalProjectUpdater', class_implements($class))) { + $updaters[] = $class; + } + } + } + return $updaters; +} \ No newline at end of file diff --git modules/update/update.css modules/update/update.css index 5a3e1e5..bc9c4cd 100644 --- modules/update/update.css +++ modules/update/update.css @@ -108,3 +108,38 @@ table.update, .update .check-manually { padding-left: 1em; /* LTR */ } + + +table tbody tr.update-security, +table tbody tr.update-unsupported { + background: #fcc; +} + +table tbody tr.update-manual input{ + display:none; +} + +th.update-project-name { + width: 50%; +} + +.connection-settings-update-filetransfer-default-wrapper { + float:left; +} + +#edit-submit-connection { + clear: both; +} + +.filetransfer { + display:none; + clear:both; +} + +#edit-connection-settings-change-connection-type { + margin: 2.6em 0.5em 0em 1em; +} + +.update-major-version-warning { + color:#ff0000; +} \ No newline at end of file diff --git modules/update/update.fetch.inc modules/update/update.fetch.inc index 77ea549..1813da8 100644 --- modules/update/update.fetch.inc +++ modules/update/update.fetch.inc @@ -46,7 +46,7 @@ function _update_refresh() { // to clear out the stale data at this point. _update_cache_clear('update_available_releases'); - $max_fetch_attempts = variable_get('update_max_fetch_attempts', UPDATE_MAX_FETCH_ATTEMPTS); + $max_fetch_attempts = variable_get('update_max_fetch_attempts', UPDATE_MAX_FETCH_ATTEMPTS); foreach ($projects as $key => $project) { $url = _update_build_fetch_url($project, $site_key); diff --git modules/update/update.info modules/update/update.info index 76be843..deb6094 100644 --- modules/update/update.info +++ modules/update/update.info @@ -6,6 +6,9 @@ package = Core core = 7.x files[] = update.compare.inc files[] = update.fetch.inc +files[] = update.admin.inc +files[] = updaters.inc +files[] = update.js files[] = update.install files[] = update.module files[] = update.report.inc diff --git modules/update/update.js modules/update/update.js new file mode 100644 index 0000000..86af5c6 --- /dev/null +++ modules/update/update.js @@ -0,0 +1,38 @@ +// $Id$ +(function ($) { + +Drupal.behaviors.updateFileTransferForm = { + attach: function(context) { + $('#edit-connection-settings-update-filetransfer-default').change(function() { + $('.filetransfer').hide().filter('.filetransfer-' + $(this).val()).show(); + }); + $('.filetransfer').hide().filter('.filetransfer-' + $('#edit-connection-settings-update-filetransfer-default').val()).show(); + + // Hide any major version warnings, make a jquery UI dialog when checked. + + $('.update-major-version-warning').each(function (elem) { + that = $(this); + $(this).hide(); + $(":checkbox", $(this).parent().parent()).bind('change', function(e) { + if (this.checked) { + that.dialog("open"); + var buttons = {} + buttons[Drupal.t("Ok")] = function() { $(this).dialog("close"); }; + buttons[Drupal.t("Cancel")] = function() { alert('cancel'); $(this).dialog("close"); }; + that.dialog({'buttons': buttons}); + } + }); + }); + + // Removes the float on the select box (used for non-JS interface) + if($('.connection-settings-update-filetransfer-default-wrapper').length > 0) { + console.log($('.connection-settings-update-filetransfer-default-wrapper')); + $('.connection-settings-update-filetransfer-default-wrapper').css('float', 'none'); + } + // Hides the submit button for non-js users + $('#edit-submit-connection').hide(); + $('#edit-submit-process').show(); + } +} + +})(jQuery); diff --git modules/update/update.module modules/update/update.module index 36a4558..4c8f504 100644 --- modules/update/update.module +++ modules/update/update.module @@ -67,10 +67,22 @@ define('UPDATE_MAX_FETCH_ATTEMPTS', 2); function update_help($path, $arg) { switch ($path) { case 'admin/reports/updates': + case 'admin/reports/full': 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/reports/updates/install': + global $base_url; + $help = <<modules and themes at +http://drupal.org. +EOF; + $output = '

' . t($help, array('@module_url' => 'http://drupal.org/project/modules', '@theme_url' => 'http://drupal.org/project/themes')) . '

'; + return $output; case 'admin/appearance': case 'admin/config/modules': @@ -127,18 +139,48 @@ 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', + 'title' => 'Updates and Add-ons', + 'description' => 'Keep your installed modules and themes up to date and install new ones.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('update_update_form'), 'access arguments' => array('administer site configuration'), 'weight' => 10, - 'file' => 'update.report.inc', + 'type' => MENU_NORMAL_ITEM, + 'file' => 'update.admin.inc', ); - $items['admin/reports/updates/list'] = array( - 'title' => 'List', + + $items['admin/reports/updates/updater'] = array( + 'title' => 'Update', + 'description' => 'Update your installed modules and themes', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('update_update_form'), 'access arguments' => array('administer site configuration'), + 'weight' => 10, 'type' => MENU_DEFAULT_LOCAL_TASK, + 'file' => 'update.admin.inc', + ); + + $items['admin/reports/updates/full'] = array( + 'title' => 'Full update report', + 'description' => 'A comprehensive list of all modules installed on your system and their status.', + 'page callback' => 'update_status', + 'access arguments' => array('administer site configuration'), + 'weight' => 30, + 'type' => MENU_LOCAL_TASK, + 'file' => 'update.report.inc', + ); + + $items['admin/update'] = array( + 'title' => 'Updating your site', + 'description' => 'Second step of site update / new project install', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('update_update_form'), + 'access arguments' => array('administer site configuration'), + 'weight' => 10, + 'type' => MENU_CALLBACK, + 'file' => 'update.admin.inc', ); + $items['admin/reports/updates/settings'] = array( 'title' => 'Settings', 'page callback' => 'drupal_get_form', @@ -146,7 +188,9 @@ function update_menu() { 'access arguments' => array('administer site configuration'), 'file' => 'update.settings.inc', 'type' => MENU_LOCAL_TASK, + 'weight' => 50, ); + $items['admin/reports/updates/check'] = array( 'title' => 'Manual update check', 'page callback' => 'update_manual_status', @@ -155,16 +199,42 @@ function update_menu() { 'file' => 'update.fetch.inc', ); + $items['admin/config/modules/install'] = array( + 'title' => 'Install', + 'description' => 'Install new modules and themes', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('update_install_form'), + 'access callback' => 'update_plugin_manager_access', + 'access arguments' => array(), + 'weight' => 20, + 'type' => MENU_LOCAL_TASK, + 'file' => 'update.admin.inc', + ); + + $items['admin/appearance/install'] = array( + 'title' => 'Install', + 'description' => 'Install', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('update_install_form'), + 'access callback' => 'update_plugin_manager_access', + 'access arguments' => array(), + 'weight' => 20, + 'type' => MENU_LOCAL_TASK, + 'file' => 'update.admin.inc', + ); + return $items; } + /** - * Implement the hook_theme() registry. + * Implements hook_theme(). */ function update_theme() { return array( - 'update_settings' => array( + 'update_available_updates_form' => array( 'arguments' => array('form' => NULL), + 'file' => 'update.admin.inc', ), 'update_report' => array( 'arguments' => array('data' => NULL), @@ -648,6 +718,12 @@ function update_flush_caches() { return array(); } + +function update_plugin_manager_access() { + return variable_get('plugin_manager_enabled', TRUE); +} + + /** * @} End of "defgroup update_status_cache". */ diff --git modules/update/update.report.inc modules/update/update.report.inc index 5a3723a..6cbd6e0 100644 --- modules/update/update.report.inc +++ modules/update/update.report.inc @@ -30,7 +30,7 @@ function theme_update_report($variables) { $last = variable_get('update_last_check', 0); $output = '
' . ($last ? t('Last checked: @timestamp (@time ago)', array('@time' => format_interval(REQUEST_TIME - $last), '@timestamp' => format_date($last))) : t('Last checked: never')); - $output .= ' (' . l(t('Check manually'), 'admin/reports/updates/check') . ')'; + $output .= ' (' . l(t('Check manually'), 'admin/reports/updates/check', array('query' => drupal_get_destination())) . ')'; $output .= "
\n"; if (!is_array($data)) { diff --git modules/update/update.test modules/update/update.test index 314d3c7..3bb0be8 100644 --- modules/update/update.test +++ modules/update/update.test @@ -45,7 +45,6 @@ class UpdateTestHelper extends DrupalWebTestCase { */ protected function standardTests() { $this->assertRaw('

' . t('Drupal core') . '

'); - $this->assertRaw(l(t('Check manually'), 'admin/reports/updates/check'), t('Link to check available updates manually appears.')); $this->assertRaw(l(t('Drupal'), 'http://example.com/project/drupal'), t('Link to the Drupal project appears.')); $this->assertNoText(t('No available releases found')); } @@ -74,7 +73,7 @@ class UpdateCoreTestCase extends UpdateTestHelper { function testNoUpdatesAvailable() { $this->setSystemInfo7_0(); $this->refreshUpdateStatus(array('drupal' => '0')); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); $this->standardTests(); $this->assertText(t('Up to date')); $this->assertNoText(t('Update available')); @@ -87,7 +86,7 @@ class UpdateCoreTestCase extends UpdateTestHelper { function testNormalUpdateAvailable() { $this->setSystemInfo7_0(); $this->refreshUpdateStatus(array('drupal' => '1')); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); $this->standardTests(); $this->assertNoText(t('Up to date')); $this->assertText(t('Update available')); @@ -103,7 +102,7 @@ class UpdateCoreTestCase extends UpdateTestHelper { function testSecurityUpdateAvailable() { $this->setSystemInfo7_0(); $this->refreshUpdateStatus(array('drupal' => '2-sec')); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); $this->standardTests(); $this->assertNoText(t('Up to date')); $this->assertNoText(t('Update available')); @@ -130,7 +129,7 @@ class UpdateCoreTestCase extends UpdateTestHelper { ); variable_set('update_test_system_info', $system_info); $this->refreshUpdateStatus(array('drupal' => 'dev')); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); $this->assertNoText(t('2001-Sep-')); $this->assertText(t('Up to date')); $this->assertNoText(t('Update available')); @@ -185,7 +184,7 @@ class UpdateTestContribCase extends UpdateTestHelper { 'aaa_update_test' => '1_0', ) ); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); $this->standardTests(); $this->assertText(t('Up to date')); $this->assertRaw('

' . t('Modules') . '

'); @@ -240,7 +239,7 @@ class UpdateTestContribCase extends UpdateTestHelper { ); variable_set('update_test_system_info', $system_info); $this->refreshUpdateStatus(array('drupal' => '0', '#all' => '1_0')); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); $this->standardTests(); // We're expecting the report to say all projects are up to date. $this->assertText(t('Up to date')); @@ -303,7 +302,7 @@ class UpdateTestContribCase extends UpdateTestHelper { 'update_test_basetheme' => '1_1-sec', ); $this->refreshUpdateStatus($xml_mapping); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); $this->assertText(t('Security update required!')); $this->assertRaw(l(t('Update test base theme'), 'http://example.com/project/update_test_basetheme'), t('Link to the Update test base theme project appears.')); } @@ -349,7 +348,7 @@ class UpdateTestContribCase extends UpdateTestHelper { foreach (array(TRUE, FALSE) as $check_disabled) { variable_set('update_check_disabled', $check_disabled); $this->refreshUpdateStatus($xml_mapping); - $this->drupalGet('admin/reports/updates'); + $this->drupalGet('admin/reports/updates/full'); // In neither case should we see the "Themes" heading for enabled themes. $this->assertNoText(t('Themes')); if ($check_disabled) { diff --git modules/update/updaters.inc modules/update/updaters.inc new file mode 100644 index 0000000..bfa1a9c --- /dev/null +++ modules/update/updaters.inc @@ -0,0 +1,431 @@ +source = $source; + $this->name = self::getProjectName($source); + $this->title = self::getProjectTitle($source); + } + + /** + * Returns an Updater of the approiate type depending on the source provided. + * If a directory is provided which contains a module, will return a ModuleUpdater. + * + * @param string $source + * Directory of a Drupal project + * + * @return object Updater + */ + static function factory($source) { + if (is_dir($source)) { + $updater = self::getUpdaterFromDirectory($source); + } + else { + throw new UpdaterError('Unable to determine the type of the source directory'); + } + return new $updater($source); + } + + /** + * Determines which Updater class can operate on the given directory. + * + * @param string $directory + * Extracted Drupal project. + * @return string + * The class name which can work with this project type. + */ + static function getUpdaterFromDirectory($directory) { + // Gets a list of possible implementing classes + $updaters = update_get_updaters(); + foreach ($updaters as $updater) { + if (call_user_func("{$updater}::canUpdateDirectory", $directory)) { + return $updater; + } + } + throw new UpdaterError("Cannot determine the type of project"); + } + + /** + * Figure out what the most important (or only) info file is in a directory. + * + * Since there is no enforcement of which info file is the project's "main" info + * file, this will get one with the same name as the directory, or the first + * one it finds. Not ideal, but needs a larger solution. + * + * @param string $directory + * Directory to search in. + * @return string + * Path to the info file. + */ + public static function findInfoFile($directory) { + $info_files = file_scan_directory($directory, '/.*\.info/'); + if (!$info_files) { + return FALSE; + } + foreach ($info_files as $info_file) { + if (drupal_substr($info_file->filename, 0, -5) == basename($directory)) { + // Info file Has the same name as the directory, return it. + return $info_file->uri; + } + } + // Otherwise, return the first one. + $info_file = array_shift($info_files); + return $info_file->uri; + } + + /** + * Gets the name of the project directory (basename). + * + * @todo: It would be nice, if projects contained an info file which could + * provide their canonical name. + * + * @param string $directory + * @return string + */ + public static function getProjectName($directory) { + return basename($directory); + } + + + /** + * Returns the project name from a Drupal info file. + * + * @param string $directory + * Directory to search for the info file. + * + * @return string + */ + public static function getProjectTitle($directory) { + $info_file = self::findInfoFile($directory); + $info = drupal_parse_info_file($info_file); + if (!$info) { + //@TODO: Add the variables, t(), etc + throw new UpdaterError("Unable to parse info file"); + } + return $info['name']; + } + + /** + * Stores default parameters for the Updater. + * + * @param array $overrides + * An array of overrides for the default parameters. + * + * @return array + * An array of configuration parameters for an update or install operation. + */ + private function getInstallArgs($overrides = array()) { + $args = array( + 'make_backup' => FALSE, + 'install_dir' => $this->getInstallDirectory(), + 'backup_dir' => $this->getBackupDir(), + ); + return array_merge($args, $overrides); + } + + /** + * Updates a Drupal project, returns a list of next actions. + * + * @param FileTransfer $ft + * Object which is a child of FileTransfer. Used for moving files + * to the server. + * @param array $overrides + * An array of settings to override defaults + * @see self::getInstallArgs + * + * @return array + * An array of links which the user may need to complete the update + */ + public function update(&$ft, $overrides = array()) { + try { + // Establish arguments with possible overrides. + $args = $this->getInstallArgs($overrides); + + // Take a Backup. + if ($args['make_backup']) { + $this->makeBackup($args['install_dir'], $args['backup_dir']); + } + + if (!$this->name) { + //This is bad, don't want delte the install dir. + throw new UpdaterError("Fatal error in update, cowardly refusing to wipe out the install directory"); + } + + /** + * Note: If the project is installed in sites/all, it will not be deleted. + * It will be installed in sites/default as that will override the sites/all + * reference and not break other sites which may be using it. + */ + if (is_dir($args['install_dir'] . '/' . $this->name)) { + // Remove the existing installed file. + $ft->removeDirectory($args['install_dir'] . '/' . $this->name); + } + + // Copy the directory in place. + $ft->copyDirectory($this->source, $args['install_dir']); + // Run the updates. + // @TODO: decide if we want to implement this. + $this->postUpdate(); + // For now, just return a list of links of things to do. + return $this->postUpdateTasks(); + } + catch (FileTransferException $e) { + throw new UpdaterError(t("File Transfer failed, reason: !reason", array('!reason' => t($e->getMessage(), $e->arguments)))); + } + } + + /** + * Installs a Drupal project, returns a list of next actions. + * + * @param FileTransfer $ft + * Object which is a child of FileTransfer. + * @param array $overrides + * An array of settings to override defaults. + * @see self::getInstallArgs + * + * @return array + * An array of links which the user may need to complete the install. + */ + public function install(&$ft, $overrides = array()) { + try { + // Establish arguments with possible overrides. + $args = $this->getInstallArgs($overrides); + // Copy the directory in place. + $ft->copyDirectory($this->source, $args['install_dir']); + // Potentially enable something? + // @TODO: decide if we want to implement this. + $this->postInstall(); + // For now, just return a list of links of things to do. + return $this->postInstallTasks(); + } + catch (FileTransferException $e) { + throw new UpdateError(t("File Transfer failed, reason: !reason", array('!reason' => t($e->getMessage(), $this->arguments)))); + } + } + + function makeBackup(&$ft, $from, $to) { + //@TODO: Not implemented + } + + function getBackupDir() { + return file_directory_path('temporary'); + } + + /** + * Needs to be overridden by children to work + * Actions to take after the update is complete. + */ + function postUpdate() { + + } + + /** + * Needs to be overridden by children to work + * Actions to take after the install is complete. + */ + function postInstall() { + + } + + /** + * Returns an array of links to pages that should be visited post operation. + * + * @return array Links which provide actions to take after the install is finished. + */ + function postInstallTasks() { + return array(); + } + + /** + * Returns an array of links to pages that should be visited post operation. + * + * @return array Links which provide actions to take after the update is finished. + */ + function postUpdateTasks() { + return array(); + } +} + +class ModuleUpdater extends Updater implements DrupalProjectUpdater{ + + static function getInstallDirectory() { + return DRUPAL_ROOT . '/' . conf_path() . '/modules'; + } + + function isInstalled() { + return (bool) drupal_get_path('module', $this->name); + } + + static function canUpdateDirectory($directory) { + if (file_scan_directory($directory, '/.*\.module/')) { + return TRUE; + } + return FALSE; + } + + static function canUpdate($project_name) { + return (bool) drupal_get_path('module', $project_name); + } + + /** + * @todo: Not implemented + * @return + */ + function getSchemaUpdates() { + require_once './includes/install.inc'; + require_once './includes/update.inc'; + + if (_update_get_project_type($project) != 'module') { + return array(); + } + module_load_include('install', $project); + + if (!$updates = drupal_get_schema_versions($project)) { + return array(); + } + $updates_to_run = array(); + $modules_with_updates = update_get_update_list(); + if ($updates = $modules_with_updates[$project]) { + if ($updates['start']) { + return $updates['pending']; + } + } + return array(); + } + + function postInstallTasks() { + + return array( + l(t('Enable newly added modules in !project', array('!project' => $this->title)), 'admin/config/modules'), + ); + } + + function postUpdateTasks() { + // @todo: If there are schema updates. + return array( + l(t('Run database updates for !project', array('!project' => $this->title)), 'update.php'), + ); + } + +} + +class ThemeUpdater extends Updater implements DrupalProjectUpdater { + public $installDirectory; + + static function getInstallDirectory() { + return DRUPAL_ROOT . '/' . conf_path() . '/themes'; + } + + function isInstalled() { + return (bool) drupal_get_path('theme', $this->name); + } + + static function canUpdateDirectory($directory) { + // This is a lousy test, but don't know how else to confirm it is a theme. + if (file_scan_directory($directory, '/.*\.module/')) { + return FALSE; + } + return TRUE; + } + + static function canUpdate($project_name) { + return (bool) drupal_get_path('theme', $project_name); + } + + function postInstall() { + // Update the system table. + system_get_theme_data(); + + // Active the theme + db_update('system') + ->fields(array('status' => 1)) + ->condition('type', 'theme') + ->condition('name', $this->name) + ->execute(); + } + + function postInstallTasks() { + return array( + l(t('Set the !project theme as default', array('!project' => $this->title)), 'admin/appearance'), + ); + } +} + +/** + * @TODO: implement this. + */ +class ProfileUpdater extends Updater { + function installProject() { + + } +} + +class UpdaterError extends Exception { + +} \ No newline at end of file diff --git plugin.php plugin.php new file mode 100644 index 0000000..c09352f --- /dev/null +++ plugin.php @@ -0,0 +1,138 @@ +uid != 1) { + print theme('update_page', array('content' => $output, 'show_messages' => !$progress_page)); + exit; +} + +// We prepare a minimal bootstrap. +drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES); + +// This must go after drupal_bootstrap(), which unsets globals! +global $conf; + +// Load barebones libraries. +require_once DRUPAL_ROOT . '/includes/common.inc'; +require_once DRUPAL_ROOT . '/includes/batch.inc'; +require_once DRUPAL_ROOT . '/includes/form.inc'; +require_once DRUPAL_ROOT . '/includes/file.inc'; +require_once DRUPAL_ROOT . '/includes/path.inc'; + +// Load module basics (needed for hook invokes). +require_once DRUPAL_ROOT . '/includes/module.inc'; +require_once DRUPAL_ROOT . '/includes/session.inc'; +require_once DRUPAL_ROOT . '/includes/entity.inc'; + +$module_list['system']['filename'] = 'modules/system/system.module'; +$module_list['update']['filename'] = 'modules/update/update.module'; +$module_list['user']['filename'] = 'modules/user/user.module'; +module_list(TRUE, FALSE, FALSE, $module_list); +drupal_load('module', 'system'); +drupal_load('module', 'update'); +drupal_load('module', 'user'); + +// Includes needed specifically for the upgrade / install process. +require_once DRUPAL_ROOT . '/includes/plugin.inc'; +require_once DRUPAL_ROOT . '/modules/update/update.admin.inc'; + +drupal_path_initialize(); +drupal_language_initialize(); + +drupal_set_title(t('Updating your site')); +drupal_maintenance_theme(); + +/** + * This case occurrs when the operation has been run, and a report needs to be shown. + * Currently uses the update.php report, although should be changed. + * + * @todo: pretty up this report. + */ +if (isset($_SESSION['update_batch_results']) && $results = $_SESSION['update_batch_results']) { + + //Clear the session out; + unset($_SESSION['update_batch_results']); + + //Add the update.php functions and css + include_once './includes/update.inc'; + drupal_add_css('modules/system/maintenance.css'); + + if ($results['success']) { + variable_set('site_offline', FALSE); + drupal_set_message(t("Update was completed successfully! Your site has been taken out of maintenance mode.")); + } + else { + drupal_set_message(t("Update failed! See the log below for more information. Your site is still in maintenance mode"), 'error'); + } + + $output = ""; + $output .= get_plugin_report($results['messages']); + drupal_set_title(t('Update complete')); + + // These links are returned by the Updaters as "next tasks" to complete. + $links = array(); + if (is_array($results['tasks'])) { + $links += $results['tasks']; + } + + $links = array_merge($links, array( + l(t('Administration pages'), 'admin'), + l(t('Front page'), ''), + )); + + $output .= theme('item_list', array('items' => $links)); + + print theme('update_page', array('content' => $output)); + return; +} + +/** + * If a batch is running, let it run. + */ +if (isset($_GET['batch'])) { + $output = _batch_page(); +} +else { + if (empty($_SESSION['plugin_op'])) { + $output = t("It appears you have reached this page in error."); + print theme('update_page', array('content' => $output)); + return; + } + elseif (!$batch = batch_get()) { + // We have a batch to process, show the filetransfer form. + $output = drupal_render(drupal_get_form('plugin_filetransfer_form')); + } +} + +if (!empty($output)) { + // We defer the display of messages until all updates are done. + $progress_page = ($batch = batch_get()) && isset($batch['running']); + print theme('update_page', array('content' => $output, 'show_messages' => !$progress_page)); +}