Commit 67a19cf4 authored by webchick's avatar webchick

#688294 by Berdir, andypost, Crell, catch: Fixed Switch from db_is_active() to...

#688294 by Berdir, andypost, Crell, catch: Fixed Switch from db_is_active() to proper exception catching.
parent 85430f58
......@@ -159,7 +159,7 @@ function authorize_filetransfer_form_submit($form, &$form_state) {
// 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()) {
try {
$connection_settings = array();
foreach ($form_state['values']['connection_settings'][$filetransfer_backend] as $key => $value) {
// We do *not* want to store passwords in the database, unless the
......@@ -186,6 +186,11 @@ function authorize_filetransfer_form_submit($form, &$form_state) {
// Now run the operation.
authorize_run_operation($filetransfer);
}
catch (Exception $e) {
// If there is no database available, we don't care and just skip
// this part entirely.
}
break;
case 'enter_connection_settings':
......
......@@ -1687,7 +1687,7 @@ function drupal_is_denied($ip) {
// won't be denied. However the user asked explicitly not to use the
// database and also in this case it's quite likely that the user relies
// on higher performance solutions like a firewall.
elseif (function_exists('db_is_active')) {
elseif (class_exists('Database', FALSE)) {
$denied = (bool)db_query("SELECT 1 FROM {blocked_ips} WHERE ip = :ip", array(':ip' => $ip))->fetchField();
}
return $denied;
......
......@@ -608,7 +608,7 @@ protected function expandArguments(&$query, &$args) {
// Update the query with the new placeholders.
// preg_replace is necessary to ensure the replacement does not affect
// placeholders that start with the same exact text. For example, if the
// query contains the placeholders :foo and :foobar, and :foo has an
// query contains the placeholders :foo and :foobar, and :foo has an
// array of values, using str_replace would affect both placeholders,
// but using the following preg_replace would only affect :foo because
// it is followed by a non-word character.
......@@ -1367,8 +1367,7 @@ abstract class Database {
// If necessary, a new connection is opened.
self::$connections[$key][$target] = self::openConnection($key, $target);
}
return isset(self::$connections[$key][$target]) ? self::$connections[$key][$target] : NULL;
return self::$connections[$key][$target];
}
/**
......@@ -1501,11 +1500,11 @@ public static function addConnectionInfo($key, $target, $info) {
// If the requested database does not exist then it is an unrecoverable
// error.
if (!isset(self::$databaseInfo[$key])) {
throw new Exception('DB does not exist');
throw new DatabaseConnectionNotDefinedException('The specified database connection is not defined: ' . $key);
}
if (!$driver = self::$databaseInfo[$key][$target]['driver']) {
throw new Exception('Drupal is not set up');
throw new DatabaseDriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
}
// We cannot rely on the registry yet, because the registry requires an
......@@ -1624,6 +1623,17 @@ class FieldsOverlapException extends Exception {}
*/
class NoFieldsException extends Exception {}
/**
* Exception thrown if an undefined database connection is requested.
*/
class DatabaseConnectionNotDefinedException extends Exception {}
/**
* Exception thrown if no driver is specified for a database connection.
*/
class DatabaseDriverNotSpecifiedException extends Exception {}
/**
* A wrapper class for creating and managing database transactions.
*
......@@ -2419,20 +2429,6 @@ function db_set_active($key = 'default') {
return Database::setActiveConnection($key);
}
/**
* Determines if there is an active connection.
*
* Note that this method will return FALSE if no connection has been established
* yet, even if one could be.
*
* @return
* TRUE if there is at least one database connection established, FALSE
* otherwise.
*/
function db_is_active() {
return Database::isActiveConnection();
}
/**
* Restricts a dynamic table, column, or constraint name to safe characters.
*
......
......@@ -572,13 +572,19 @@ function list_themes($refresh = FALSE) {
$themes = array();
// Extract from the database only when it is available.
// Also check that the site is not in the middle of an install or update.
if (!defined('MAINTENANCE_MODE') && db_is_active()) {
foreach (system_list('theme') as $theme) {
if (file_exists($theme->filename)) {
$theme->info = unserialize($theme->info);
$themes[] = $theme;
if (!defined('MAINTENANCE_MODE')) {
try {
foreach (system_list('theme') as $theme) {
if (file_exists($theme->filename)) {
$theme->info = unserialize($theme->info);
$themes[] = $theme;
}
}
}
catch (Exception $e) {
// If the database is not available, rebuild the theme data.
$themes = _system_rebuild_theme_data();
}
}
else {
// Scan the installation when the database should not be read.
......@@ -2094,19 +2100,27 @@ function _template_preprocess_default_variables() {
'title_prefix' => array(),
'title_suffix' => array(),
'user' => $user,
'db_is_active' => !defined('MAINTENANCE_MODE') && db_is_active(),
'db_is_active' => !defined('MAINTENANCE_MODE'),
'is_admin' => FALSE,
'logged_in' => FALSE,
);
// Variables that depend on a database connection.
if ($variables['db_is_active']) {
// user_access() does not exist when the database is not available because
// user.module has not been included.
if (function_exists('user_access')) {
$variables['is_admin'] = user_access('access administration pages');
$variables['is_front'] = drupal_is_front_page();
$variables['logged_in'] = ($user->uid > 0);
}
else {
$variables['is_admin'] = FALSE;
// drupal_is_front_page() might throw an exception.
try {
$variables['is_front'] = drupal_is_front_page();
}
catch (Exception $e) {
// If the database is not yet available, set default values for these
// variables.
$variables['is_front'] = FALSE;
$variables['logged_in'] = FALSE;
$variables['db_is_active'] = FALSE;
}
return $variables;
......
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