]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Config.php
Merge branch '1.0.x' into activity
[quix0rs-gnu-social.git] / classes / Config.php
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
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * Table Definition for config
26  */
27
28 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
29
30 class Config extends Managed_DataObject
31 {
32     ###START_AUTOCODE
33     /* the code below is auto generated do not remove the above tag */
34
35     public $__table = 'config';                          // table name
36     public $section;                         // varchar(32)  primary_key not_null
37     public $setting;                         // varchar(32)  primary_key not_null
38     public $value;                           // varchar(255)
39
40     /* Static get */
41     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Config',$k,$v); }
42
43     /* the code above is auto generated do not remove the tag below */
44     ###END_AUTOCODE
45
46     public static function schemaDef()
47     {
48         return array(
49             'fields' => array(
50                 'section' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration section'),
51                 'setting' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration setting'),
52                 'value' => array('type' => 'varchar', 'length' => 255, 'description' => 'configuration value'),
53             ),
54             'primary key' => array('section', 'setting'),
55         );
56     }
57
58     const settingsKey = 'config:settings';
59
60     static function loadSettings()
61     {
62         $settings = self::_getSettings();
63         if (!empty($settings)) {
64             self::_applySettings($settings);
65         }
66     }
67
68     static function _getSettings()
69     {
70         $c = self::memcache();
71
72         if (!empty($c)) {
73             $settings = $c->get(Cache::key(self::settingsKey));
74             if ($settings !== false) {
75                 return $settings;
76             }
77         }
78
79         $settings = array();
80
81         $config = new Config();
82
83         $config->find();
84
85         while ($config->fetch()) {
86             $settings[] = array($config->section, $config->setting, $config->value);
87         }
88
89         $config->free();
90
91         if (!empty($c)) {
92             $c->set(Cache::key(self::settingsKey), $settings);
93         }
94
95         return $settings;
96     }
97
98     static function _applySettings($settings)
99     {
100         global $config;
101
102         foreach ($settings as $s) {
103             list($section, $setting, $value) = $s;
104             $config[$section][$setting] = $value;
105         }
106     }
107
108     function insert()
109     {
110         $result = parent::insert();
111         if ($result) {
112             Config::_blowSettingsCache();
113         }
114         return $result;
115     }
116
117     function delete()
118     {
119         $result = parent::delete();
120         if ($result) {
121             Config::_blowSettingsCache();
122         }
123         return $result;
124     }
125
126     function update($orig=null)
127     {
128         $result = parent::update($orig);
129         if ($result) {
130             Config::_blowSettingsCache();
131         }
132         return $result;
133     }
134
135     function pkeyGet($kv)
136     {
137         return Memcached_DataObject::pkeyGet('Config', $kv);
138     }
139
140     static function save($section, $setting, $value)
141     {
142         $result = null;
143
144         $config = Config::pkeyGet(array('section' => $section,
145                                         'setting' => $setting));
146
147         if (!empty($config)) {
148             $orig = clone($config);
149             $config->value = $value;
150             $result = $config->update($orig);
151         } else {
152             $config = new Config();
153
154             $config->section = $section;
155             $config->setting = $setting;
156             $config->value   = $value;
157
158             $result = $config->insert();
159         }
160
161         return $result;
162     }
163
164     function _blowSettingsCache()
165     {
166         $c = self::memcache();
167
168         if (!empty($c)) {
169             $c->delete(Cache::key(self::settingsKey));
170         }
171     }
172 }