diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-04-23 05:21:19 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-04-23 05:21:19 +0000 |
commit | 67a19cf48b775499364cf10f99319ef585039890 (patch) | |
tree | f5db81827363cac4210cc423f734c3e1065538be | |
parent | 85430f583c74a4fc6687d12f35ea112518d3b6e7 (diff) | |
download | brdo-67a19cf48b775499364cf10f99319ef585039890.tar.gz brdo-67a19cf48b775499364cf10f99319ef585039890.tar.bz2 |
#688294 by Berdir, andypost, Crell, catch: Fixed Switch from db_is_active() to proper exception catching.
-rw-r--r-- | includes/authorize.inc | 7 | ||||
-rw-r--r-- | includes/bootstrap.inc | 2 | ||||
-rw-r--r-- | includes/database/database.inc | 34 | ||||
-rw-r--r-- | includes/theme.inc | 38 |
4 files changed, 48 insertions, 33 deletions
diff --git a/includes/authorize.inc b/includes/authorize.inc index 39a7125e6..8ae771a5d 100644 --- a/includes/authorize.inc +++ b/includes/authorize.inc @@ -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': diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 1344a5bbd..f15cbd95e 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -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; diff --git a/includes/database/database.inc b/includes/database/database.inc index 91547a5b8..dda27d7ba 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -608,7 +608,7 @@ abstract class DatabaseConnection extends PDO { // 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 @@ abstract class Database { // 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 @@ -1625,6 +1624,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. * * Not all databases or database configurations support transactions. For @@ -2420,20 +2430,6 @@ function db_set_active($key = 'default') { } /** - * 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. * * Only keeps alphanumeric and underscores. diff --git a/includes/theme.inc b/includes/theme.inc index fb911426a..2f07c2633 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -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; |