From c944edfb50e81a8880b02ee5496a29323021331e Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 16 Aug 2009 10:36:23 -0400 Subject: [PATCH 1/3] Add table for configuration settings Add a table for configuration settings. --- db/laconica.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/db/laconica.sql b/db/laconica.sql index 56bd4b1e38..1662ef7a8b 100644 --- a/db/laconica.sql +++ b/db/laconica.sql @@ -547,3 +547,13 @@ create table deleted_notice ( index deleted_notice_profile_id_idx (profile_id) ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; + +create table config ( + + section varchar(32) comment 'configuration section', + setting varchar(32) comment 'configuration setting', + value varchar(255) comment 'configuration value', + + constraint primary key (section, setting) + +) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; From 888ed474a161a278579debdb492acc44fbcd7790 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 21 Aug 2009 15:22:02 -0400 Subject: [PATCH 2/3] new class for configuration settings --- classes/Config.php | 22 ++++++++++++++++++++++ classes/laconica.ini | 9 +++++++++ 2 files changed, 31 insertions(+) create mode 100755 classes/Config.php diff --git a/classes/Config.php b/classes/Config.php new file mode 100755 index 0000000000..2538a14264 --- /dev/null +++ b/classes/Config.php @@ -0,0 +1,22 @@ + Date: Fri, 21 Aug 2009 16:14:32 -0400 Subject: [PATCH 3/3] load configuration options from database at runtime --- classes/Config.php | 109 ++++++++++++++++++++++++++++++++++++++++++++- lib/common.php | 48 +++++++++++--------- 2 files changed, 136 insertions(+), 21 deletions(-) diff --git a/classes/Config.php b/classes/Config.php index 2538a14264..5bec13fdc8 100755 --- a/classes/Config.php +++ b/classes/Config.php @@ -1,8 +1,29 @@ . + */ + +if (!defined('LACONICA')) { exit(1); } + /** * Table Definition for config */ -require_once 'classes/Memcached_DataObject.php'; + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; class Config extends Memcached_DataObject { @@ -19,4 +40,90 @@ class Config extends Memcached_DataObject /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE + + const settingsKey = 'config:settings'; + + static function loadSettings() + { + $settings = self::_getSettings(); + if (!empty($settings)) { + self::_applySettings($settings); + } + } + + static function _getSettings() + { + $c = self::memcache(); + + if (!empty($c)) { + $settings = $c->get(common_cache_key(self::settingsKey)); + if (!empty($settings)) { + return $settings; + } + } + + $settings = array(); + + $config = new Config(); + + $config->find(); + + while ($config->fetch()) { + $settings[] = array($config->section, $config->setting, $config->value); + } + + $config->free(); + + if (!empty($c)) { + $c->set(common_cache_key(self::settingsKey), $settings); + } + + return $settings; + } + + static function _applySettings($settings) + { + global $config; + + foreach ($settings as $s) { + list($section, $setting, $value) = $s; + $config[$section][$setting] = $value; + } + } + + function insert() + { + $result = parent::insert(); + if ($result) { + Config::_blowSettingsCache(); + } + return $result; + } + + function delete() + { + $result = parent::delete(); + if ($result) { + Config::_blowSettingsCache(); + } + return $result; + } + + function update($orig=null) + { + $result = parent::update($orig); + if ($result) { + Config::_blowSettingsCache(); + } + return $result; + } + + function _blowSettingsCache() + { + $c = self::memcache(); + + if (!empty($c)) { + $c->delete(common_cache_key(self::settingsKey)); + } + } } diff --git a/lib/common.php b/lib/common.php index a9eef13ff7..d95622ecd5 100644 --- a/lib/common.php +++ b/lib/common.php @@ -21,6 +21,8 @@ if (!defined('LACONICA')) { exit(1); } define('LACONICA_VERSION', '0.9.0dev'); +// XXX: move these to class variables + define('AVATAR_PROFILE_SIZE', 96); define('AVATAR_STREAM_SIZE', 48); define('AVATAR_MINI_SIZE', 24); @@ -369,26 +371,6 @@ if ($_db_name != 'laconica' && !array_key_exists('ini_'.$_db_name, $config['db'] $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/laconica.ini'; } -// XXX: how many of these could be auto-loaded on use? - -require_once 'Validate.php'; -require_once 'markdown.php'; - -require_once INSTALLDIR.'/lib/util.php'; -require_once INSTALLDIR.'/lib/action.php'; -require_once INSTALLDIR.'/lib/theme.php'; -require_once INSTALLDIR.'/lib/mail.php'; -require_once INSTALLDIR.'/lib/subs.php'; -require_once INSTALLDIR.'/lib/Shorturl_api.php'; -require_once INSTALLDIR.'/lib/twitter.php'; - -require_once INSTALLDIR.'/lib/clientexception.php'; -require_once INSTALLDIR.'/lib/serverexception.php'; - -// XXX: other formats here - -define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER); - function __autoload($cls) { if (file_exists(INSTALLDIR.'/classes/' . $cls . '.php')) { @@ -405,6 +387,32 @@ function __autoload($cls) } } +// XXX: how many of these could be auto-loaded on use? +// XXX: note that these files should not use config options +// at compile time since DB config options are not yet loaded. + +require_once 'Validate.php'; +require_once 'markdown.php'; + +require_once INSTALLDIR.'/lib/util.php'; +require_once INSTALLDIR.'/lib/action.php'; +require_once INSTALLDIR.'/lib/theme.php'; +require_once INSTALLDIR.'/lib/mail.php'; +require_once INSTALLDIR.'/lib/subs.php'; +require_once INSTALLDIR.'/lib/Shorturl_api.php'; +require_once INSTALLDIR.'/lib/twitter.php'; + +require_once INSTALLDIR.'/lib/clientexception.php'; +require_once INSTALLDIR.'/lib/serverexception.php'; + +// Load settings from database; note we need autoload for this + +Config::loadSettings(); + +// XXX: other formats here + +define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER); + // Give plugins a chance to initialize in a fully-prepared environment Event::handle('InitializePlugin');