Config.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * Laconica - a distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, Control Yourself, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * Table Definition for config
  24. */
  25. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  26. class Config extends Managed_DataObject
  27. {
  28. ###START_AUTOCODE
  29. /* the code below is auto generated do not remove the above tag */
  30. public $__table = 'config'; // table name
  31. public $section; // varchar(32) primary_key not_null
  32. public $setting; // varchar(32) primary_key not_null
  33. public $value; // varchar(255)
  34. /* the code above is auto generated do not remove the tag below */
  35. ###END_AUTOCODE
  36. public static function schemaDef()
  37. {
  38. return array(
  39. 'fields' => array(
  40. 'section' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration section'),
  41. 'setting' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration setting'),
  42. 'value' => array('type' => 'varchar', 'length' => 255, 'description' => 'configuration value'),
  43. ),
  44. 'primary key' => array('section', 'setting'),
  45. );
  46. }
  47. const settingsKey = 'config:settings';
  48. static function loadSettings()
  49. {
  50. try {
  51. $settings = self::_getSettings();
  52. if (!empty($settings)) {
  53. self::_applySettings($settings);
  54. }
  55. } catch (Exception $e) {
  56. return;
  57. }
  58. }
  59. static function _getSettings()
  60. {
  61. $c = self::memcache();
  62. if (!empty($c)) {
  63. $settings = $c->get(Cache::key(self::settingsKey));
  64. if ($settings !== false) {
  65. return $settings;
  66. }
  67. }
  68. $settings = array();
  69. $config = new Config();
  70. $config->find();
  71. while ($config->fetch()) {
  72. $settings[] = array($config->section, $config->setting, $config->value);
  73. }
  74. $config->free();
  75. if (!empty($c)) {
  76. $c->set(Cache::key(self::settingsKey), $settings);
  77. }
  78. return $settings;
  79. }
  80. static function _applySettings($settings)
  81. {
  82. global $config;
  83. foreach ($settings as $s) {
  84. list($section, $setting, $value) = $s;
  85. $config[$section][$setting] = $value;
  86. }
  87. }
  88. function insert()
  89. {
  90. $result = parent::insert();
  91. if ($result) {
  92. Config::_blowSettingsCache();
  93. }
  94. return $result;
  95. }
  96. function delete($useWhere=false)
  97. {
  98. $result = parent::delete($useWhere);
  99. if ($result !== false) {
  100. Config::_blowSettingsCache();
  101. }
  102. return $result;
  103. }
  104. function update($dataObject=false)
  105. {
  106. $result = parent::update($dataObject);
  107. if ($result !== false) {
  108. Config::_blowSettingsCache();
  109. }
  110. return $result;
  111. }
  112. static function save($section, $setting, $value)
  113. {
  114. $result = null;
  115. $config = Config::pkeyGet(array('section' => $section,
  116. 'setting' => $setting));
  117. if (!empty($config)) {
  118. $orig = clone($config);
  119. $config->value = $value;
  120. $result = $config->update($orig);
  121. } else {
  122. $config = new Config();
  123. $config->section = $section;
  124. $config->setting = $setting;
  125. $config->value = $value;
  126. $result = $config->insert();
  127. }
  128. return $result;
  129. }
  130. function _blowSettingsCache()
  131. {
  132. $c = self::memcache();
  133. if (!empty($c)) {
  134. $c->delete(Cache::key(self::settingsKey));
  135. }
  136. }
  137. }