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