diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 0b1f633..1d8a746 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -10,6 +10,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Database\Database; use Drupal\Core\DrupalKernel; use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\Logger\RfcLogLevel; @@ -758,6 +759,22 @@ function drupal_installation_attempted() { } /** + * Check whether Drupal is installed. + * + * @return bool + * TRUE if Drupal is installed, FALSE otherwise. + */ +function drupal_is_installed() { + // Check that a default database connection has been defined. + if (!Database::getConnectionInfo()) { + return FALSE; + } + + // Check whether the sessions table exists. + return Database::getConnection()->schema()->tableExists('sessions'); +} + +/** * Gets the name of the currently active installation profile. * * When this function is called during Drupal's initial installation process, diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 890f7be..86130ba 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -1096,14 +1096,8 @@ function install_verify_database_settings($site_path) { * Verify that the database is ready (no existing Drupal installation). */ function install_verify_database_ready() { - $system_schema = system_schema(); - end($system_schema); - $table = key($system_schema); - - if ($database = Database::getConnectionInfo()) { - if (Database::getConnection()->schema()->tableExists($table)) { - throw new AlreadyInstalledException(\Drupal::service('string_translation')); - } + if (drupal_is_installed()) { + throw new AlreadyInstalledException(\Drupal::service('string_translation')); } } diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 2ddc3ee..8a9000d 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -13,7 +13,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Config\BootstrapConfigStorageFactory; use Drupal\Core\Config\NullStorage; -use Drupal\Core\Database\Database; +use Drupal\Core\Database\DatabaseException; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceModifierInterface; use Drupal\Core\DependencyInjection\ServiceProviderInterface; @@ -129,6 +129,13 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { protected $prepared = FALSE; /** + * Whether the user should be redirected to the installer. + * + * @var bool|null + */ + protected $needInstallRedirect; + + /** * Holds the list of enabled modules. * * @var array @@ -628,10 +635,7 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = try { $this->initializeSettings($request); - // Redirect the user to the installation script if Drupal has not been - // installed yet (i.e., if no $databases array has been defined in the - // settings.php file) and we are not already installing. - if (!Database::getConnectionInfo() && !drupal_installation_attempted() && PHP_SAPI !== 'cli') { + if ($this->needInstallRedirect()) { $response = new RedirectResponse($request->getBasePath() . '/core/install.php'); } else { @@ -654,6 +658,33 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = } /** + * Check whether we should redirect to the installer (core/install.php). + * + * This would be the case if Drupal is not already installed, if we are not + * already in the process of installation, and this is not being called on the + * command line. + * + * @return bool + * TRUE if the user should be redirected, FALSE otherwise. + */ + protected function needInstallRedirect() { + if (!isset($this->needInstallRedirect)) { + try { + $this->needInstallRedirect = !drupal_installation_attempted() + && PHP_SAPI !== 'cli' + && !drupal_is_installed(); + } + catch (DatabaseException $e) { + // Ignore database errors: they could have many causes, and would not + // indicate that Drupal is not installed. + $this->needInstallRedirect = FALSE; + } + } + + return $this->needInstallRedirect; + } + + /** * Converts an exception into a response. * * @param \Exception $e