Commit bec2ed46 authored by Nigel McNie's avatar Nigel McNie
Browse files

Make sure that the config table not existing is not vomited into the logs.

This change adds the ability to prevent the SQLException constructor from logging errors, which is used in init.php when attempting to load the config data and set default config values.
parent e113fb45
......@@ -110,12 +110,12 @@ catch (Exception $e) {
throw new ConfigSanityException($errormessage);
}
try {
db_ignore_sql_exceptions(true);
load_config();
db_ignore_sql_exceptions(false);
}
catch (SQLException $e) {
if (!defined('INSTALLER')) {
throw $e;
}
db_ignore_sql_exceptions(false);
}
// Make sure wwwroot is set and available, either in the database or int the
......
......@@ -1519,4 +1519,32 @@ function increment_perf_db_writes() {
global $PERF;
$PERF->dbwrites++;
}
/**
* Gives the caller the ability to disable logging of SQL exceptions in the
* SQLException constructor.
*
* This is only used by the config loading code to prevent spurious errors
* about the config table not existing going to the logs. If you are going to
* use this function, you had better have a very good reason!
*
* @param bool $status Whether to ignore logging exceptions or not. If null,
* you can retrieve the current value of this setting
*/
function db_ignore_sql_exceptions($status=null) {
global $DB_IGNORE_SQL_EXCEPTIONS;
// Initialise it if being called for the first time
if ($DB_IGNORE_SQL_EXCEPTIONS === null) {
$DB_IGNORE_SQL_EXCEPTIONS = false;
}
// Return the value if asked for
if ($status === null) {
return $DB_IGNORE_SQL_EXCEPTIONS;
}
$DB_IGNORE_SQL_EXCEPTIONS = (bool)$status;
}
?>
......@@ -638,12 +638,16 @@ class ConfigSanityException extends ConfigException {
*/
class SQLException extends SystemException {
public function __construct($message=null, $code=0) {
global $DB_IGNORE_SQL_EXCEPTIONS;
if ($GLOBALS['_TRANSACTION_LEVEL'] > 0) {
log_debug('rolling back a transaction');
db_rollback();
}
parent::__construct($message, $code);
log_warn($this->getMessage());
if (empty($DB_IGNORE_SQL_EXCEPTIONS)) {
log_warn($this->getMessage());
}
}
}
......
......@@ -476,15 +476,7 @@ function ini_get_bool($ini_get_arg) {
function load_config() {
global $CFG;
try {
$dbconfig = get_records_array('config', '', '', '', 'field, value');
}
catch (SQLException $e) {
// TODO: better reporting if config could not be obtained? This
// normally happens when the system isn't installed
log_info($e->getMessage());
return false;
}
$dbconfig = get_records_array('config', '', '', '', 'field, value');
foreach ($dbconfig as $cfg) {
if (isset($CFG->{$cfg->field}) && $CFG->{$cfg->field} != $cfg->value) {
......@@ -523,6 +515,7 @@ function get_config($key) {
function set_config($key, $value) {
global $CFG;
db_ignore_sql_exceptions(true);
if (get_record('config', 'field', $key)) {
if (set_field('config', 'value', $value, 'field', $key)) {
$status = true;
......@@ -534,6 +527,7 @@ function set_config($key, $value) {
$config->value = $value;
$status = insert_record('config', $config);
}
db_ignore_sql_exceptions(false);
if (!empty($status)) {
$CFG->{$key} = $value;
......
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