Commit 3afec501 authored by catch's avatar catch

Issue #2250119 by ParisLiakos, Devin Carlson: Run updates in a full environment.

parent f6312b85
......@@ -113,7 +113,6 @@ DirectoryIndex index.php index.html index.htm
# RewriteBase /
# Redirect common PHP files to their new locations.
RewriteCond %{REQUEST_URI} ^(.*)?/(update.php) [OR]
RewriteCond %{REQUEST_URI} ^(.*)?/(install.php) [OR]
RewriteCond %{REQUEST_URI} ^(.*)?/(rebuild.php)
RewriteCond %{REQUEST_URI} !core
......
......@@ -1574,6 +1574,47 @@ function template_preprocess_container(&$variables) {
$variables['attributes'] = $element['#attributes'];
}
/**
* Returns HTML for a list of maintenance tasks to perform.
*
* @param $variables
* An associative array containing:
* - items: An associative array of maintenance tasks.
* It's the caller's responsibility to ensure this array's items contain no
* dangerous HTML such as SCRIPT tags.
* - active: The key for the currently active maintenance task.
*
* @ingroup themeable
*/
function theme_task_list($variables) {
$items = $variables['items'];
$active = $variables['active'];
$done = isset($items[$active]) || $active == NULL;
$output = '<h2 class="visually-hidden">Installation tasks</h2>';
$output .= '<ol class="task-list">';
foreach ($items as $k => $item) {
if ($active == $k) {
$class = 'active';
$status = '(' . t('active') . ')';
$done = FALSE;
}
else {
$class = $done ? 'done' : '';
$status = $done ? '(' . t('done') . ')' : '';
}
$output .= '<li';
$output .= ($class ? ' class="' . $class . '"' : '') . '>';
$output .= $item;
$output .= ($status ? '<span class="visually-hidden"> ' . $status . '</span>' : '');
$output .= '</li>';
}
$output .= '</ol>';
return $output;
}
/**
* @} End of "addtogroup themeable".
*/
......
......@@ -103,46 +103,6 @@ function _drupal_maintenance_theme() {
Drupal::service('theme.registry');
}
/**
* Returns HTML for a list of maintenance tasks to perform.
*
* @param $variables
* An associative array containing:
* - items: An associative array of maintenance tasks.
* It's the caller's responsibility to ensure this array's items contain no
* dangerous HTML such as SCRIPT tags.
* - active: The key for the currently active maintenance task.
*
* @ingroup themeable
*/
function theme_task_list($variables) {
$items = $variables['items'];
$active = $variables['active'];
$done = isset($items[$active]) || $active == NULL;
$output = '<h2 class="visually-hidden">Installation tasks</h2>';
$output .= '<ol class="task-list">';
foreach ($items as $k => $item) {
if ($active == $k) {
$class = 'active';
$status = '(' . t('active') . ')';
$done = FALSE;
}
else {
$class = $done ? 'done' : '';
$status = $done ? '(' . t('done') . ')' : '';
}
$output .= '<li';
$output .= ($class ? ' class="' . $class . '"' : '') . '>';
$output .= $item;
$output .= ($status ? '<span class="visually-hidden"> ' . $status . '</span>' : '');
$output .= '</li>';
}
$output .= '</ol>';
return $output;
}
/**
* Returns HTML for a results report of an operation run by authorize.php.
*
......
......@@ -131,33 +131,13 @@ function update_system_schema_requirements() {
/**
* Checks update requirements and reports errors and (optionally) warnings.
*
* @param $skip_warnings
* (optional) If set to TRUE, requirement warnings will be ignored, and a
* report will only be issued if there are requirement errors. Defaults to
* FALSE.
*/
function update_check_requirements($skip_warnings = FALSE) {
function update_check_requirements() {
// Check requirements of all loaded modules.
$requirements = \Drupal::moduleHandler()->invokeAll('requirements', array('update'));
$requirements += update_system_schema_requirements();
$requirements += update_settings_file_requirements();
$severity = drupal_requirements_severity($requirements);
// If there are errors, always display them. If there are only warnings, skip
// them if the caller has indicated they should be skipped.
if ($severity == REQUIREMENT_ERROR || ($severity == REQUIREMENT_WARNING && !$skip_warnings)) {
$regions['sidebar_first'] = update_task_list('requirements');
$status_report = array(
'#theme' => 'status_report',
'#requirements' => $requirements,
);
$status_report['#suffix'] = 'Check the messages and <a href="' . check_url(drupal_requirements_url($severity)) . '">try again</a>.';
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
print DefaultHtmlPageRenderer::renderPage($status_report, 'Requirements problem', 'maintenance', $regions);
exit();
}
return $requirements;
}
/**
......@@ -277,114 +257,6 @@ function update_do_one($module, $number, $dependency_map, &$context) {
$context['message'] = 'Updating ' . String::checkPlain($module) . ' module';
}
/**
* Starts the database update batch process.
*
* @param $start
* An array whose keys contain the names of modules to be updated during the
* current batch process, and whose values contain the number of the first
* requested update for that module. The actual updates that are run (and the
* order they are run in) will depend on the results of passing this data
* through the update dependency system.
* @param $redirect
* Path to redirect to when the batch has finished processing.
* @param $url
* URL of the batch processing page (should only be used for separate
* scripts like update.php).
* @param $batch
* Optional parameters to pass into the batch API.
* @param $redirect_callback
* (optional) Specify a function to be called to redirect to the progressive
* processing page.
*
* @see update_resolve_dependencies()
*/
function update_batch($start, $redirect = NULL, $url = NULL, $batch = array(), $redirect_callback = NULL) {
// During the update, bring the site offline so that schema changes do not
// affect visiting users.
$maintenance_mode = \Drupal::config('system.maintenance')->get('enabled');
if (isset($maintenance_mode)) {
$_SESSION['maintenance_mode'] = $maintenance_mode;
}
if (empty($_SESSION['maintenance_mode'])) {
if (db_table_exists('state')) {
\Drupal::state()->set('system.maintenance_mode', TRUE);
}
}
// Resolve any update dependencies to determine the actual updates that will
// be run and the order they will be run in.
$updates = update_resolve_dependencies($start);
// Store the dependencies for each update function in an array which the
// batch API can pass in to the batch operation each time it is called. (We
// do not store the entire update dependency array here because it is
// potentially very large.)
$dependency_map = array();
foreach ($updates as $function => $update) {
$dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : array();
}
$operations = array();
foreach ($updates as $update) {
if ($update['allowed']) {
// Set the installed version of each module so updates will start at the
// correct place. (The updates are already sorted, so we can simply base
// this on the first one we come across in the above foreach loop.)
if (isset($start[$update['module']])) {
drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
unset($start[$update['module']]);
}
// Add this update function to the batch.
$function = $update['module'] . '_update_' . $update['number'];
$operations[] = array('update_do_one', array($update['module'], $update['number'], $dependency_map[$function]));
}
}
$batch['operations'] = $operations;
$batch += array(
'title' => 'Updating',
'init_message' => 'Starting updates',
'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.',
'finished' => 'update_finished',
'file' => 'core/includes/update.inc',
);
batch_set($batch);
return batch_process($redirect, $url, $redirect_callback);
}
/**
* Finishes the update process and stores the results for eventual display.
*
* After the updates run, all caches are flushed. The update results are
* stored into the session (for example, to be displayed on the update results
* page in update.php). Additionally, if the site was off-line, now that the
* update process is completed, the site is set back online.
*
* @param $success
* Indicate that the batch API tasks were all completed successfully.
* @param $results
* An array of all the results that were updated in update_do_one().
* @param $operations
* A list of all the operations that had not been completed by the batch API.
*
* @see update_batch()
*/
function update_finished($success, $results, $operations) {
// Clear the caches in case the data has been updated.
update_flush_all_caches();
$_SESSION['update_results'] = $results;
$_SESSION['update_success'] = $success;
$_SESSION['updates_remaining'] = $operations;
// Now that the update is done, we can put the site back online if it was
// previously in maintenance mode.
if (isset($_SESSION['maintenance_mode'])) {
\Drupal::state()->set('system.maintenance_mode', FALSE);
unset($_SESSION['maintenance_mode']);
}
}
/**
* Returns a list of all the pending database updates.
*
......
<?php
/**
* @file
* Contains \Drupal\Core\DependencyInjection\UpdateServiceProvider.
*/
namespace Drupal\Core\DependencyInjection;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
/**
* ServiceProvider class for update.php service overrides.
*
* This class is manually added by update.php via $conf['container_service_providers']
* and required to prevent various services from trying to retrieve data from
* storages that do not exist yet.
*/
class UpdateServiceProvider implements ServiceProviderInterface, ServiceModifierInterface {
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
if (!empty($GLOBALS['conf']['update_service_provider_overrides'])) {
// Disable the Lock service.
$container
->register('lock', 'Drupal\Core\Lock\NullLockBackend');
// Prevent config from being accessed via a cache wrapper by removing
// any existing definition and setting an alias to the actual storage.
$container->removeDefinition('config.storage');
$container->setAlias('config.storage', 'config.storage.active');
$container
->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');
$container
->register('router.builder', 'Drupal\Core\Routing\RouteBuilderStatic');
$container->register('theme_handler', 'Drupal\Core\Extension\ThemeHandler')
->addArgument(new Reference('config.factory'))
->addArgument(new Reference('module_handler'))
->addArgument(new Reference('state'))
->addArgument(new Reference('info_parser'))
->addArgument(new Reference('logger.channel.default'))
->addArgument(new Reference('asset.css.collection_optimizer'));
}
}
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
// Ensure that URLs generated for the home and admin pages don't have
// 'update.php' in them.
$request = Request::createFromGlobals();
$definition = $container->getDefinition('url_generator');
$definition->addMethodCall('setBasePath', array(str_replace('/core', '', $request->getBasePath()) . '/'));
// We need to set the script path to an empty string since the value
// determined by \Drupal\Core\Routing\UrlGenerator::setRequest() is invalid
// once '/core' has been removed from the base path.
$definition->addMethodCall('setScriptPath', array(''));
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Update\Form\UpdateScriptSelectionForm.
*/
namespace Drupal\Core\Update\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides the list of available database module updates.
*/
class UpdateScriptSelectionForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'update_script_selection_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$count = 0;
$incompatible_count = 0;
$form['start'] = array(
'#tree' => TRUE,
'#type' => 'details',
);
// Ensure system.module's updates appear first.
$form['start']['system'] = array();
$updates = update_get_update_list();
$starting_updates = array();
$incompatible_updates_exist = FALSE;
foreach ($updates as $module => $update) {
if (!isset($update['start'])) {
$form['start'][$module] = array(
'#type' => 'item',
'#title' => $module . ' module',
'#markup' => $update['warning'],
'#prefix' => '<div class="messages messages--warning">',
'#suffix' => '</div>',
);
$incompatible_updates_exist = TRUE;
continue;
}
if (!empty($update['pending'])) {
$starting_updates[$module] = $update['start'];
$form['start'][$module] = array(
'#type' => 'hidden',
'#value' => $update['start'],
);
$form['start'][$module . '_updates'] = array(
'#theme' => 'item_list',
'#items' => $update['pending'],
'#title' => $module . ' module',
);
}
if (isset($update['pending'])) {
$count = $count + count($update['pending']);
}
}
// Find and label any incompatible updates.
foreach (update_resolve_dependencies($starting_updates) as $data) {
if (!$data['allowed']) {
$incompatible_updates_exist = TRUE;
$incompatible_count++;
$module_update_key = $data['module'] . '_updates';
if (isset($form['start'][$module_update_key]['#items'][$data['number']])) {
$text = $data['missing_dependencies'] ? 'This update will been skipped due to the following missing dependencies: <em>' . implode(', ', $data['missing_dependencies']) . '</em>' : "This update will be skipped due to an error in the module's code.";
$form['start'][$module_update_key]['#items'][$data['number']] .= '<div class="warning">' . $text . '</div>';
}
// Move the module containing this update to the top of the list.
$form['start'] = array($module_update_key => $form['start'][$module_update_key]) + $form['start'];
}
}
// Warn the user if any updates were incompatible.
if ($incompatible_updates_exist) {
drupal_set_message('Some of the pending updates cannot be applied because their dependencies were not met.', 'warning');
}
if (empty($count)) {
drupal_set_message(t('No pending updates.'));
unset($form);
$form['links'] = array(
'#theme' => 'links',
'#links' => update_helpful_links(),
);
// No updates to run, so caches won't get flushed later. Clear them now.
update_flush_all_caches();
}
else {
$form['help'] = array(
'#markup' => '<p>The version of Drupal you are updating from has been automatically detected.</p>',
'#weight' => -5,
);
if ($incompatible_count) {
$form['start']['#title'] = format_plural(
$count,
'1 pending update (@number_applied to be applied, @number_incompatible skipped)',
'@count pending updates (@number_applied to be applied, @number_incompatible skipped)',
array('@number_applied' => $count - $incompatible_count, '@number_incompatible' => $incompatible_count)
);
}
else {
$form['start']['#title'] = format_plural($count, '1 pending update', '@count pending updates');
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Apply pending updates',
'#button_type' => 'primary',
);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
<?php
/**
* @file
* Contains Drupal\system\Access\DbUpdateAccessCheck.
*/
namespace Drupal\system\Access;
use Drupal\Core\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;
/**
* Access check for database update routes.
*/
class DbUpdateAccessCheck implements AccessInterface {
/**
* Checks access for update routes.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
*
* @return string
* A \Drupal\Core\Access\AccessInterface constant value.
*/
public function access(AccountInterface $account) {
// Allow the global variable in settings.php to override the access check.
if (Settings::get('update_free_access')) {
return static::ALLOW;
}
return $account->hasPermission('administer software updates') ? static::ALLOW : static::KILL;
}
}
This diff is collapsed.
......@@ -43,7 +43,7 @@ protected function setUp() {
parent::setUp();
require_once DRUPAL_ROOT . '/core/includes/update.inc';
$this->update_url = $GLOBALS['base_url'] . '/core/update.php';
$this->update_url = $GLOBALS['base_url'] . '/update.php';
$this->update_user = $this->drupalCreateUser(array('administer software updates'));
}
......@@ -51,7 +51,7 @@ function testInvalidUpdateHook() {
// Confirm that a module with hook_update_8000() cannot be updated.
$this->drupalLogin($this->update_user);
$this->drupalGet($this->update_url);
$this->drupalPostForm($this->update_url, array(), t('Continue'), array('external' => TRUE));
$this->clickLink(t('Continue'));
$this->assertText(t('Some of the pending updates cannot be applied because their dependencies were not met.'));
}
......
......@@ -30,30 +30,8 @@ class UpdateScriptTest extends WebTestBase {
protected function setUp() {
parent::setUp();
$this->update_url = $GLOBALS['base_url'] . '/core/update.php';
$this->update_user = $this->drupalCreateUser(array('administer software updates'));
}
/**
* Tests that updates from schema versions prior to 8000 are prevented.
*/
function testInvalidMigration() {
// Mock a D7 system table so that the schema value of the system module
// can be retrieved.
db_create_table('system', $this->getSystemSchema());
// Assert that the table exists.
$this->assertTrue(db_table_exists('system'), 'The table exists.');
// Insert a value for the system module.
db_insert('system')
->fields(array(
'name' => 'system',
'schema_version' => 7000,
))
->execute();
$system_schema = db_query('SELECT schema_version FROM {system} WHERE name = :system', array(':system' => 'system'))->fetchField();
$this->drupalGet($this->update_url, array('external' => TRUE));
$text = 'Your system schema version is ' . $system_schema . '. Updating directly from a schema version prior to 8000 is not supported. You must <a href="https://drupal.org/node/2179269">migrate your site to Drupal 8</a> first.';
$this->assertRaw($text, 'Updates from schema versions prior to 8000 are prevented.');
$this->update_url = $GLOBALS['base_url'] . '/update.php';
$this->update_user = $this->drupalCreateUser(array('administer software updates', 'access site in maintenance mode'));
}
/**
......@@ -92,7 +70,7 @@ function testRequirements() {
// If there are no requirements warnings or errors, we expect to be able to
// go through the update process uninterrupted.
$this->drupalGet($this->update_url, array('external' => TRUE));
$this->drupalPostForm(NULL, array(), t('Continue'));
$this->clickLink(t('Continue'));
$this->assertText(t('No pending updates.'), 'End of update process was reached.');
// Confirm that all caches were cleared.
$this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared when there were no requirements warnings or errors.');
......@@ -109,8 +87,8 @@ function testRequirements() {
$this->assertText('This is a requirements warning provided by the update_script_test module.');
$this->clickLink('try again');
$this->assertNoText('This is a requirements warning provided by the update_script_test module.');
$this->drupalPostForm(NULL, array(), t('Continue'));
$this->drupalPostForm(NULL, array(), 'Apply pending updates');
$this->clickLink(t('Continue'));
$this->clickLink(t('Apply pending updates'));
$this->assertText(t('The update_script_test_update_8001() update was executed successfully.'), 'End of update process was reached.');
// Confirm that all caches were cleared.
$this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared after resolving a requirements warning and applying updates.');
......@@ -120,7 +98,7 @@ function testRequirements() {
$this->assertText('This is a requirements warning provided by the update_script_test module.');
$this->clickLink('try again');
$this->assertNoText('This is a requirements warning provided by the update_script_test module.');
$this->drupalPostForm(NULL, array(), t('Continue'));
$this->clickLink(t('Continue'));
$this->assertText(t('No pending updates.'), 'End of update process was reached.');
// Confirm that all caches were cleared.
$this->assertText(t('hook_cache_flush() invoked for update_script_test.module.'), 'Caches were cleared after applying updates and re-running the script.');
......@@ -155,7 +133,8 @@ function testThemeSystem() {
function testNoUpdateFunctionality() {
// Click through update.php with 'administer software updates' permission.
$this->drupalLogin($this->update_user);
$this->drupalPostForm($this->update_url, array(), t('Continue'), array('external' => TRUE));
$this->drupalGet($this->update_url, array('external' => TRUE));
$this->clickLink(t('Continue'));
$this->assertText(t('No pending updates.'));
$this->assertNoLink('Administration pages');
$this->assertNoLinkByHref('update.php', 0);
......@@ -165,7 +144,8 @@ function testNoUpdateFunctionality() {
// Click through update.php with 'access administration pages' permission.
$admin_user = $this->drupalCreateUser(array('administer software updates', 'access administration pages'));
$this->drupalLogin($admin_user);
$this->drupalPostForm($this->update_url, array(), t('Continue'), array('external' => TRUE));
$this->drupalGet($this->update_url, array('external' => TRUE));
$this->clickLink(t('Continue'));
$this->assertText(t('No pending updates.'));
$this->assertLink('Administration pages');
$this->assertNoLinkByHref('update.php', 1);
......@@ -187,8 +167,9 @@ function testSuccessfulUpdateFunctionality() {
// Click through update.php with 'administer software updates' permission.
$this->drupalLogin($this->update_user);
$this->drupalPostForm($this->update_url, array(), t('Continue'), array('external' => TRUE));
$this->drupalPostForm(NULL, array(), t('Apply pending updates'));
$this->drupalGet($this->update_url, array('external' => TRUE));
$this->clickLink(t('Continue'));
$this->clickLink(t('Apply pending updates'));
// Verify that updates were completed successfully.
$this->assertText('Updates were attempted.');
......@@ -219,10 +200,11 @@ function testSuccessfulUpdateFunctionality() {
// Click through update.php with 'access administration pages' and
// 'access site reports' permissions.
$admin_user = $this->drupalCreateUser(array('administer software updates', 'access administration pages', 'access site reports'));
$admin_user = $this->drupalCreateUser(array('administer software updates', 'access administration pages', 'access site reports', 'access site in maintenance mode'));
$this->drupalLogin($admin_user);
$this->drupalPostForm($this->update_url, array(), t('Continue'), array('external' => TRUE));
$this->drupalPostForm(NULL, array(), t('Apply pending updates'));
$this->drupalGet($this->update_url, array('external' => TRUE));
$this->clickLink(t('Continue'));
$this->clickLink(t('Apply pending updates'));
$this->assertText('Updates were attempted.');
$this->assertLink('logged');
$this->assertLink('Administration pages');
......
......@@ -37,7 +37,7 @@ class UpdatesWith7xTest extends WebTestBase {
protected function setUp() {
parent::setUp();
require_once DRUPAL_ROOT . '/core/includes/update.inc';
$this->update_url = $GLOBALS['base_url'] . '/core/update.php';
$this->update_url = $GLOBALS['base_url'] . '/update.php';
$this->update_user = $this->drupalCreateUser(array('administer software updates'));
}
......@@ -52,7 +52,8 @@ function testWith7x() {
// Click through update.php with 'administer software updates' permission.
$this->drupalLogin($this->update_user);
$this->drupalPostForm($this->update_url, array(), t('Continue'), array('external' => TRUE));
$this->drupalGet($this->update_url, array('external' => TRUE));
$this->clickLink(t('Continue'));
$this->assertText(t('Some of the pending updates cannot be applied because their dependencies were not met.'));
}
}
<?php
/**
* @file
* Contains \Drupal\system\Theme\DbUpdateNegotiator.
*/
namespace Drupal\system\Theme;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Theme\ThemeNegotiatorInterface;
/**
* Sets the active theme for the database update pages.
*/
class DbUpdateNegotiator implements ThemeNegotiatorInterface {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a DbUpdateNegotiator.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return $route_match->getRouteName() == 'system.db_update';
}
/**
* {@inheritdoc}
*/
public function determineActiveTheme(RouteMatchInterface $route_match) {
$custom_theme = Settings::get('maintenance_theme', 'seven');
if (!$custom_theme) {
$config = $this->configFactory->get('system.theme');
$custom_theme = $config->get('default');
}
return $custom_theme;
}
}
......@@ -407,8 +407,14 @@ system.batch_page.json:
options:
_admin_route: TRUE
system.update:
path: '/core/update.php'
system.db_update:
path: '/update.php/{op}'
defaults:
_title: 'Drupal database update'