MDL-25290 conversion: Converted config to use MUC

This commit is contained in:
Sam Hemelryk 2012-08-14 11:40:28 +12:00
parent 9da506c2a3
commit 007bfe8b4d
6 changed files with 76 additions and 57 deletions

View File

@ -150,6 +150,8 @@ class block_section_links extends block_base {
global $DB;
// TODO: Move these config settings to proper ones using component name
$DB->delete_records('config_plugins', array('plugin' => 'blocks/section_links'));
// Have to manually purge the cache as well
cache_helper::invalidate_by_definition('core', 'config', array(), 'blocks/section_links');
}
function has_config() {

View File

@ -34,6 +34,7 @@ $string['area'] = 'Area';
$string['caching'] = 'Caching';
$string['cacheadmin'] = 'Cache administration';
$string['cacheconfig'] = 'Configuration';
$string['cachedef_config'] = 'Config settings';
$string['cachedef_databasemeta'] = 'Database meta information';
$string['cachedef_eventinvalidation'] = 'Event invalidation';
$string['cachedef_locking'] = 'Locking';

View File

@ -75,4 +75,15 @@ $definitions = array(
'datasource' => 'question_finder',
'datasourcefile' => 'question/engine/bank.php',
),
// Used to store data from the config + config_plugins table in the database.
// The key used is the component:
// - core for all core config settings
// - plugin component for all plugin settings.
// Persistence is used because normally several settings within a script.
'config' => array(
'mode' => cache_store::MODE_APPLICATION,
'persistent' => true,
'simpledata' => true
),
);

View File

@ -284,6 +284,7 @@ function message_update_providers($component='moodle') {
$DB->delete_records('message_providers', array('id' => $dbprovider->id));
$DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("%_provider_{$component}_{$dbprovider->name}_%"));
$DB->delete_records_select('user_preferences', $DB->sql_like('name', '?', false), array("message_provider_{$component}_{$dbprovider->name}_%"));
cache_helper::invalidate_by_definition('core', 'config', array(), 'message');
}
return true;
@ -574,6 +575,8 @@ function message_provider_uninstall($component) {
$DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("%_provider_{$component}_%"));
$DB->delete_records_select('user_preferences', $DB->sql_like('name', '?', false), array("message_provider_{$component}_%"));
$transaction->allow_commit();
// Purge all messaging settings from the caches. They are stored by plugin so we have to clear all message settings.
cache_helper::invalidate_by_definition('core', 'config', array(), 'message');
}
/**
@ -591,4 +594,6 @@ function message_processor_uninstall($name) {
// defaults, they will be removed on the next attempt to update the preferences
$DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("{$name}_provider_%"));
$transaction->allow_commit();
// Purge all messaging settings from the caches. They are stored by plugin so we have to clear all message settings.
cache_helper::invalidate_by_definition('core', 'config', array(), array('message', "message_{$name}"));
}

View File

@ -1306,7 +1306,7 @@ function set_config($name, $value, $plugin=NULL) {
$DB->insert_record('config', $config, false);
}
}
cache_helper::invalidate_by_definition('core', 'config', array(), 'core');
} else { // plugin scope
if ($id = $DB->get_field('config_plugins', 'id', array('name'=>$name, 'plugin'=>$plugin))) {
if ($value===null) {
@ -1323,6 +1323,7 @@ function set_config($name, $value, $plugin=NULL) {
$DB->insert_record('config_plugins', $config, false);
}
}
cache_helper::invalidate_by_definition('core', 'config', array(), $plugin);
}
return true;
@ -1345,63 +1346,55 @@ function set_config($name, $value, $plugin=NULL) {
function get_config($plugin, $name = NULL) {
global $CFG, $DB;
// normalise component name
if ($plugin === 'moodle' or $plugin === 'core') {
$plugin = NULL;
}
if (!empty($name)) { // the user is asking for a specific value
if (!empty($plugin)) {
if (isset($CFG->forced_plugin_settings[$plugin]) and array_key_exists($name, $CFG->forced_plugin_settings[$plugin])) {
// setting forced in config file
return $CFG->forced_plugin_settings[$plugin][$name];
} else {
return $DB->get_field('config_plugins', 'value', array('plugin'=>$plugin, 'name'=>$name));
}
} else {
if (array_key_exists($name, $CFG->config_php_settings)) {
// setting force in config file
return $CFG->config_php_settings[$name];
} else {
return $DB->get_field('config', 'value', array('name'=>$name));
}
}
}
// the user is after a recordset
if ($plugin) {
$localcfg = $DB->get_records_menu('config_plugins', array('plugin'=>$plugin), '', 'name,value');
if (isset($CFG->forced_plugin_settings[$plugin])) {
foreach($CFG->forced_plugin_settings[$plugin] as $n=>$v) {
if (is_null($v) or is_array($v) or is_object($v)) {
// we do not want any extra mess here, just real settings that could be saved in db
unset($localcfg[$n]);
} else {
//convert to string as if it went through the DB
$localcfg[$n] = (string)$v;
}
}
}
if ($localcfg) {
return (object)$localcfg;
} else {
return new stdClass();
}
if ($plugin === 'moodle' || $plugin === 'core' || empty($plugin)) {
$forced =& $CFG->config_php_settings;
$iscore = true;
$plugin = 'core';
} else {
// this part is not really used any more, but anyway...
$localcfg = $DB->get_records_menu('config', array(), '', 'name,value');
foreach($CFG->config_php_settings as $n=>$v) {
if (is_null($v) or is_array($v) or is_object($v)) {
// we do not want any extra mess here, just real settings that could be saved in db
unset($localcfg[$n]);
} else {
//convert to string as if it went through the DB
$localcfg[$n] = (string)$v;
}
if (array_key_exists($plugin, $CFG->forced_plugin_settings)) {
$forced =& $CFG->forced_plugin_settings[$plugin];
} else {
$forced = array();
}
return (object)$localcfg;
$iscore = false;
}
if (!empty($name) && array_key_exists($name, $forced)) {
return (string)$forced[$name];
}
$cache = cache::make('core', 'config');
$result = $cache->get($plugin);
if (!$result) {
// the user is after a recordset
$result = new stdClass;
if (!$iscore) {
$result = $DB->get_records_menu('config_plugins', array('plugin'=>$plugin), '', 'name,value');
} else {
// this part is not really used any more, but anyway...
$result = $DB->get_records_menu('config', array(), '', 'name,value');;
}
$cache->set($plugin, $result);
}
if (!empty($name)) {
if (array_key_exists($name, $result)) {
return $result[$name];
}
return false;
}
foreach ($forced as $key => $value) {
if (is_null($value) or is_array($value) or is_object($value)) {
// we do not want any extra mess here, just real settings that could be saved in db
unset($result[$key]);
} else {
//convert to string as if it went through the DB
$result[$key] = (string)$value;
}
}
return (object)$result;
}
/**
@ -1418,8 +1411,10 @@ function unset_config($name, $plugin=NULL) {
if (empty($plugin)) {
unset($CFG->$name);
$DB->delete_records('config', array('name'=>$name));
cache_helper::invalidate_by_definition('core', 'config', array(), 'core');
} else {
$DB->delete_records('config_plugins', array('name'=>$name, 'plugin'=>$plugin));
cache_helper::invalidate_by_definition('core', 'config', array(), $plugin);
}
return true;
@ -1433,10 +1428,15 @@ function unset_config($name, $plugin=NULL) {
*/
function unset_all_config_for_plugin($plugin) {
global $DB;
// Delete from the obvious config_plugins first
$DB->delete_records('config_plugins', array('plugin' => $plugin));
// Next delete any suspect settings from config
$like = $DB->sql_like('name', '?', true, true, false, '|');
$params = array($DB->sql_like_escape($plugin.'_', '|') . '%');
$DB->delete_records_select('config', $like, $params);
// Finally clear both the plugin cache and the core cache (suspect settings now removed from core).
cache_helper::invalidate_by_definition('core', 'config', array(), array('core', $plugin));
return true;
}

View File

@ -713,8 +713,8 @@ function initialise_cfg() {
try {
if ($DB) {
$localcfg = $DB->get_records_menu('config', array(), '', 'name,value');
foreach ($localcfg as $name=>$value) {
$localcfg = get_config('core');
foreach ($localcfg as $name => $value) {
if (property_exists($CFG, $name)) {
// config.php settings always take precedence
continue;