]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Config.php
Merge branch '1.0.x' into testing
[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 Memcached_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     const settingsKey = 'config:settings';
47
48     static function loadSettings()
49     {
50         $settings = self::_getSettings();
51         if (!empty($settings)) {
52             self::_applySettings($settings);
53         }
54     }
55
56     static function _getSettings()
57     {
58         $c = self::memcache();
59
60         if (!empty($c)) {
61             $settings = $c->get(Cache::key(self::settingsKey));
62             if ($settings !== false) {
63                 return $settings;
64             }
65         }
66
67         $settings = array();
68
69         $config = new Config();
70
71         $config->find();
72
73         while ($config->fetch()) {
74             $settings[] = array($config->section, $config->setting, $config->value);
75         }
76
77         $config->free();
78
79         if (!empty($c)) {
80             $c->set(Cache::key(self::settingsKey), $settings);
81         }
82
83         return $settings;
84     }
85
86     static function _applySettings($settings)
87     {
88         global $config;
89
90         foreach ($settings as $s) {
91             list($section, $setting, $value) = $s;
92             $config[$section][$setting] = $value;
93         }
94     }
95
96     function insert()
97     {
98         $result = parent::insert();
99         if ($result) {
100             Config::_blowSettingsCache();
101         }
102         return $result;
103     }
104
105     function delete()
106     {
107         $result = parent::delete();
108         if ($result) {
109             Config::_blowSettingsCache();
110         }
111         return $result;
112     }
113
114     function update($orig=null)
115     {
116         $result = parent::update($orig);
117         if ($result) {
118             Config::_blowSettingsCache();
119         }
120         return $result;
121     }
122
123     function pkeyGet($kv)
124     {
125         return Memcached_DataObject::pkeyGet('Config', $kv);
126     }
127
128     static function save($section, $setting, $value)
129     {
130         $result = null;
131
132         $config = Config::pkeyGet(array('section' => $section,
133                                         'setting' => $setting));
134
135         if (!empty($config)) {
136             $orig = clone($config);
137             $config->value = $value;
138             $result = $config->update($orig);
139         } else {
140             $config = new Config();
141
142             $config->section = $section;
143             $config->setting = $setting;
144             $config->value   = $value;
145
146             $result = $config->insert();
147         }
148
149         return $result;
150     }
151
152     function _blowSettingsCache()
153     {
154         $c = self::memcache();
155
156         if (!empty($c)) {
157             $c->delete(Cache::key(self::settingsKey));
158         }
159     }
160 }