Config.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * Table Definition for config
  22. */
  23. class Config extends Managed_DataObject
  24. {
  25. ###START_AUTOCODE
  26. /* the code below is auto generated do not remove the above tag */
  27. public $__table = 'config'; // table name
  28. public $section; // varchar(32) primary_key not_null
  29. public $setting; // varchar(32) primary_key not_null
  30. public $value; // text
  31. /* the code above is auto generated do not remove the tag below */
  32. ###END_AUTOCODE
  33. public static function schemaDef()
  34. {
  35. return array(
  36. 'fields' => array(
  37. 'section' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration section'),
  38. 'setting' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration setting'),
  39. 'value' => array('type' => 'text', 'description' => 'configuration value'),
  40. ),
  41. 'primary key' => array('section', 'setting'),
  42. );
  43. }
  44. const settingsKey = 'config:settings';
  45. static function loadSettings()
  46. {
  47. try {
  48. $settings = self::_getSettings();
  49. if (!empty($settings)) {
  50. self::_applySettings($settings);
  51. }
  52. } catch (Exception $e) {
  53. return;
  54. }
  55. }
  56. static function _getSettings()
  57. {
  58. $c = self::memcache();
  59. if (!empty($c)) {
  60. $settings = $c->get(Cache::key(self::settingsKey));
  61. if ($settings !== false) {
  62. return $settings;
  63. }
  64. }
  65. $settings = array();
  66. $config = new Config();
  67. $config->find();
  68. while ($config->fetch()) {
  69. $settings[] = array($config->section, $config->setting, $config->value);
  70. }
  71. $config->free();
  72. if (!empty($c)) {
  73. $c->set(Cache::key(self::settingsKey), $settings);
  74. }
  75. return $settings;
  76. }
  77. static function _applySettings($settings)
  78. {
  79. global $config;
  80. foreach ($settings as $s) {
  81. list($section, $setting, $value) = $s;
  82. $config[$section][$setting] = $value;
  83. }
  84. }
  85. function insert()
  86. {
  87. $result = parent::insert();
  88. if ($result) {
  89. Config::_blowSettingsCache();
  90. }
  91. return $result;
  92. }
  93. function delete($useWhere=false)
  94. {
  95. $result = parent::delete($useWhere);
  96. if ($result !== false) {
  97. Config::_blowSettingsCache();
  98. }
  99. return $result;
  100. }
  101. function update($dataObject=false)
  102. {
  103. $result = parent::update($dataObject);
  104. if ($result !== false) {
  105. Config::_blowSettingsCache();
  106. }
  107. return $result;
  108. }
  109. static function save($section, $setting, $value)
  110. {
  111. $result = null;
  112. $config = Config::pkeyGet(array('section' => $section,
  113. 'setting' => $setting));
  114. if (!empty($config)) {
  115. $orig = clone($config);
  116. $config->value = $value;
  117. $result = $config->update($orig);
  118. } else {
  119. $config = new Config();
  120. $config->section = $section;
  121. $config->setting = $setting;
  122. $config->value = $value;
  123. $result = $config->insert();
  124. }
  125. return $result;
  126. }
  127. function _blowSettingsCache()
  128. {
  129. $c = self::memcache();
  130. if (!empty($c)) {
  131. $c->delete(Cache::key(self::settingsKey));
  132. }
  133. }
  134. }