Commit 2b9cec6d authored by Dries's avatar Dries

Issue #2176621 by sun, alexpott: Remove global .

parent 6de66844
......@@ -405,7 +405,8 @@ function drupal_valid_http_host($host) {
*/
function drupal_settings_initialize() {
// Export these settings.php variables to the global namespace.
global $base_url, $databases, $cookie_domain, $config_directories, $config;
global $base_url, $cookie_domain, $config_directories, $config;
$databases = array();
$settings = array();
$config = array();
......@@ -414,6 +415,8 @@ function drupal_settings_initialize() {
if (is_readable(DRUPAL_ROOT . '/' . $conf_path . '/settings.php')) {
require DRUPAL_ROOT . '/' . $conf_path . '/settings.php';
}
// Initialize Database.
Database::setMultipleConnectionInfo($databases);
// Initialize Settings.
new Settings($settings);
}
......@@ -1598,7 +1601,7 @@ function _drupal_bootstrap_configuration() {
// 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 (empty($GLOBALS['databases']) && !drupal_installation_attempted() && !drupal_is_cli()) {
if (!Database::getConnectionInfo() && !drupal_installation_attempted() && !drupal_is_cli()) {
include_once __DIR__ . '/install.inc';
install_goto('core/install.php');
}
......
......@@ -431,7 +431,7 @@ function install_begin_request(&$install_state) {
$task = NULL;
// Do not install over a configured settings.php.
if (!empty($GLOBALS['databases'])) {
if (Database::getConnectionInfo()) {
throw new AlreadyInstalledException($container->get('string_translation'));
}
}
......@@ -1006,9 +1006,8 @@ function install_verify_completed_task() {
* Verifies that settings.php specifies a valid database connection.
*/
function install_verify_database_settings() {
global $databases;
if (!empty($databases)) {
$database = $databases['default']['default'];
if ($database = Database::getConnectionInfo()) {
$database = $database['default'];
$settings_file = './' . conf_path(FALSE) . '/settings.php';
$errors = install_database_errors($database, $settings_file);
if (empty($errors)) {
......@@ -1022,7 +1021,6 @@ function install_verify_database_settings() {
* Checks a database connection and returns any errors.
*/
function install_database_errors($database, $settings_file) {
global $databases;
$errors = array();
// Check database type.
......@@ -1034,18 +1032,13 @@ function install_database_errors($database, $settings_file) {
else {
// Run driver specific validation
$errors += $database_types[$driver]->validateDatabaseSettings($database);
if (!empty($errors)) {
// No point to try further.
return $errors;
}
// Run tasks associated with the database type. Any errors are caught in the
// calling function.
$databases['default']['default'] = $database;
// Just changing the global doesn't get the new information processed.
// We need to close any active connections and tell the Database class to
// re-parse $databases.
if (Database::isActiveConnection()) {
Database::closeConnection();
}
Database::parseConnectionInfo();
Database::addConnectionInfo('default', 'default', $database);
try {
db_run_tasks($driver);
}
......
This diff is collapsed.
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Installer\Form;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormBase;
/**
......@@ -26,8 +27,6 @@ public function getFormId() {
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
global $databases;
$conf_path = './' . conf_path(FALSE);
$settings_file = $conf_path . '/settings.php';
......@@ -36,25 +35,33 @@ public function buildForm(array $form, array &$form_state) {
$drivers = drupal_get_database_types();
$drivers_keys = array_keys($drivers);
// If database connection settings have been prepared in settings.php already,
// then the existing values need to be taken over.
// Unless there is input for this form (for a non-interactive installation,
// input originates from the $settings array passed into install_drupal()),
// check whether database connection settings have been prepared in
// settings.php already.
// Note: The installer even executes this form if there is a valid database
// connection already, since the submit handler of this form is responsible
// for writing all $settings to settings.php (not limited to $databases).
if (isset($databases['default']['default'])) {
$default_driver = $databases['default']['default']['driver'];
$default_options = $databases['default']['default'];
if (!isset($form_state['input']['driver']) && $database = Database::getConnectionInfo()) {
$form_state['input']['driver'] = $database['default']['driver'];
$form_state['input'][$database['default']['driver']] = $database['default'];
}
// Otherwise, use the database connection settings from the form input.
// For a non-interactive installation, this is derived from the original
// $settings array passed into install_drupal().
elseif (isset($form_state['input']['driver'])) {
if (isset($form_state['input']['driver'])) {
$default_driver = $form_state['input']['driver'];
// In case of database connection info from settings.php, as well as for a
// programmed form submission (non-interactive installer), the table prefix
// information is usually normalized into an array already, but the form
// element only allows to configure one default prefix for all tables.
$prefix = &$form_state['input'][$default_driver]['prefix'];
if (isset($prefix) && is_array($prefix)) {
$prefix = $prefix['default'];
}
$default_options = $form_state['input'][$default_driver];
}
// If there is no database information at all yet, just suggest the first
// available driver as default value, so that its settings form is made
// visible via #states when JavaScript is enabled (see below).
// If there is no database information yet, suggest the first available driver
// as default value, so that its settings form is made visible via #states
// when JavaScript is enabled (see below).
else {
$default_driver = current($drivers_keys);
$default_options = array();
......
......@@ -958,7 +958,11 @@ private function changeDatabasePrefix() {
$connection_info = Database::getConnectionInfo('default');
Database::renameConnection('default', 'simpletest_original_default');
foreach ($connection_info as $target => $value) {
$connection_info[$target]['prefix'] = $value['prefix']['default'] . $this->databasePrefix;
// Replace the full table prefix definition to ensure that no table
// prefixes of the test runner leak into the test.
$connection_info[$target]['prefix'] = array(
'default' => $value['prefix']['default'] . $this->databasePrefix,
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
}
......@@ -1162,11 +1166,10 @@ private function restoreEnvironment() {
usleep(50000);
// Remove all prefixed tables.
// @todo Connection prefix info is not normalized into an array.
$original_connection_info = Database::getConnectionInfo('simpletest_original_default');
$original_prefix = is_array($original_connection_info['default']['prefix']) ? $original_connection_info['default']['prefix']['default'] : $original_connection_info['default']['prefix'];
$original_prefix = $original_connection_info['default']['prefix']['default'];
$test_connection_info = Database::getConnectionInfo('default');
$test_prefix = is_array($test_connection_info['default']['prefix']) ? $test_connection_info['default']['prefix']['default'] : $test_connection_info['default']['prefix'];
$test_prefix = $test_connection_info['default']['prefix']['default'];
if ($original_prefix != $test_prefix) {
$tables = Database::getConnection()->schema()->findTables($test_prefix . '%');
$prefix_length = strlen($test_prefix);
......@@ -1187,10 +1190,6 @@ private function restoreEnvironment() {
// Restore original database connection.
Database::removeConnection('default');
Database::renameConnection('simpletest_original_default', 'default');
// @see TestBase::changeDatabasePrefix()
global $databases;
$connection_info = Database::getConnectionInfo('default');
$databases['default']['default'] = $connection_info['default'];
// Reset all static variables.
// All destructors of statically cached objects have been invoked above;
......
......@@ -936,6 +936,7 @@ protected function setUp() {
protected function installParameters() {
$connection_info = Database::getConnectionInfo();
$driver = $connection_info['default']['driver'];
$connection_info['default']['prefix'] = $connection_info['default']['prefix']['default'];
unset($connection_info['default']['driver']);
unset($connection_info['default']['namespace']);
unset($connection_info['default']['pdo']);
......
......@@ -52,8 +52,6 @@ function setUp() {
// and closed in this test.
// @see TestBase::changeDatabasePrefix()
Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
global $databases;
$databases['default']['monitor'] = $connection_info['default'];
$this->monitor = Database::getConnection('monitor');
}
......
......@@ -131,11 +131,13 @@ function simpletest_script_help() {
sub-processes. However, you may use e.g. '/tmpfs/test.sqlite'
--dburl A URI denoting the database driver, credentials, server hostname,
and database name to use in tests. For example:
mysql://username:password@localhost/databasename#table_prefix
Only used if specified.
and database name to use in tests.
Required when running tests without a Drupal installation that
contains default database connection info in settings.php.
Examples:
mysql://username:password@localhost/databasename#table_prefix
sqlite://localhost/relative/path/db.sqlite
sqlite://localhost//absolute/path/db.sqlite
--php The absolute path to the PHP executable. Usually not needed.
......@@ -431,7 +433,7 @@ function simpletest_script_bootstrap() {
* connections are prepared only.
*/
function simpletest_script_setup_database($new = FALSE) {
global $args, $databases;
global $args;
// If there is an existing Drupal installation that contains a database
// connection info in settings.php, then $databases['default']['default'] will
......@@ -443,7 +445,6 @@ function simpletest_script_setup_database($new = FALSE) {
// connection can be set and/or overridden with the --dburl parameter.
if (!empty($args['dburl'])) {
// Remove a possibly existing default connection (from settings.php).
unset($databases['default']);
Database::removeConnection('default');
$info = parse_url($args['dburl']);
......@@ -456,25 +457,26 @@ function simpletest_script_setup_database($new = FALSE) {
'pass' => '',
'fragment' => '',
);
if ($info['path'][0] === '/') {
$info['path'] = substr($info['path'], 1);
}
if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') {
$info['path'] = DRUPAL_ROOT . '/' . $info['path'];
}
$databases['default']['default'] = array(
'driver' => $info['scheme'],
'username' => $info['user'],
'password' => $info['pass'],
'host' => $info['host'],
'database' => ltrim($info['path'], '/'),
'database' => $info['path'],
'prefix' => array(
'default' => $info['fragment'],
),
);
}
// Otherwise, ensure that database table prefix info is an array.
// @see https://drupal.org/node/2176621
elseif (isset($databases['default']['default'])) {
if (!is_array($databases['default']['default']['prefix'])) {
$databases['default']['default']['prefix'] = array(
'default' => $databases['default']['default']['prefix'],
);
}
// Otherwise, use the default database connection from settings.php.
else {
$databases['default'] = Database::getConnectionInfo('default');
}
// If there is no default database connection for tests, we cannot continue.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment