123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class Config extends Managed_DataObject
- {
-
-
- public $__table = 'config';
- public $section;
- public $setting;
- public $value;
-
-
- public static function schemaDef()
- {
- return array(
- 'fields' => array(
- 'section' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration section'),
- 'setting' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration setting'),
- 'value' => array('type' => 'text', 'description' => 'configuration value'),
- ),
- 'primary key' => array('section', 'setting'),
- );
- }
- const settingsKey = 'config:settings';
- static function loadSettings()
- {
- try {
- $settings = self::_getSettings();
- if (!empty($settings)) {
- self::_applySettings($settings);
- }
- } catch (Exception $e) {
- return;
- }
- }
- static function _getSettings()
- {
- $c = self::memcache();
- if (!empty($c)) {
- $settings = $c->get(Cache::key(self::settingsKey));
- if ($settings !== false) {
- 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(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($useWhere=false)
- {
- $result = parent::delete($useWhere);
- if ($result !== false) {
- Config::_blowSettingsCache();
- }
- return $result;
- }
- function update($dataObject=false)
- {
- $result = parent::update($dataObject);
- if ($result !== false) {
- Config::_blowSettingsCache();
- }
- return $result;
- }
- static function save($section, $setting, $value)
- {
- $result = null;
- $config = Config::pkeyGet(array('section' => $section,
- 'setting' => $setting));
- if (!empty($config)) {
- $orig = clone($config);
- $config->value = $value;
- $result = $config->update($orig);
- } else {
- $config = new Config();
- $config->section = $section;
- $config->setting = $setting;
- $config->value = $value;
- $result = $config->insert();
- }
- return $result;
- }
- function _blowSettingsCache()
- {
- $c = self::memcache();
- if (!empty($c)) {
- $c->delete(Cache::key(self::settingsKey));
- }
- }
- }
|