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