]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Config.php
Merge ActivitySpam plugin
[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         try {
63             $settings = self::_getSettings();
64             if (!empty($settings)) {
65                 self::_applySettings($settings);
66             }
67         } catch (Exception $e) {
68             return;
69         }
70     }
71
72     static function _getSettings()
73     {
74         $c = self::memcache();
75
76         if (!empty($c)) {
77             $settings = $c->get(Cache::key(self::settingsKey));
78             if ($settings !== false) {
79                 return $settings;
80             }
81         }
82
83         $settings = array();
84
85         $config = new Config();
86
87         $config->find();
88
89         while ($config->fetch()) {
90             $settings[] = array($config->section, $config->setting, $config->value);
91         }
92
93         $config->free();
94
95         if (!empty($c)) {
96             $c->set(Cache::key(self::settingsKey), $settings);
97         }
98
99         return $settings;
100     }
101
102     static function _applySettings($settings)
103     {
104         global $config;
105
106         foreach ($settings as $s) {
107             list($section, $setting, $value) = $s;
108             $config[$section][$setting] = $value;
109         }
110     }
111
112     function insert()
113     {
114         $result = parent::insert();
115         if ($result) {
116             Config::_blowSettingsCache();
117         }
118         return $result;
119     }
120
121     function delete()
122     {
123         $result = parent::delete();
124         if ($result) {
125             Config::_blowSettingsCache();
126         }
127         return $result;
128     }
129
130     function update($orig=null)
131     {
132         $result = parent::update($orig);
133         if ($result) {
134             Config::_blowSettingsCache();
135         }
136         return $result;
137     }
138
139     function pkeyGet($kv)
140     {
141         return Memcached_DataObject::pkeyGet('Config', $kv);
142     }
143
144     static function save($section, $setting, $value)
145     {
146         $result = null;
147
148         $config = Config::pkeyGet(array('section' => $section,
149                                         'setting' => $setting));
150
151         if (!empty($config)) {
152             $orig = clone($config);
153             $config->value = $value;
154             $result = $config->update($orig);
155         } else {
156             $config = new Config();
157
158             $config->section = $section;
159             $config->setting = $setting;
160             $config->value   = $value;
161
162             $result = $config->insert();
163         }
164
165         return $result;
166     }
167
168     function _blowSettingsCache()
169     {
170         $c = self::memcache();
171
172         if (!empty($c)) {
173             $c->delete(Cache::key(self::settingsKey));
174         }
175     }
176 }