]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
Improve dba::selectFirst calls
[friendica.git] / src / Core / PConfig.php
1 <?php
2 /**
3  * @file src/Core/PConfig.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Database\DBM;
8 use dba;
9
10 require_once 'include/dba.php';
11
12 /**
13  * @file include/Core/PConfig.php
14  * @brief contains the class with methods for the management
15  * of the user configuration
16  */
17
18 /**
19  * @brief Management of user configuration storage
20  * Note:
21  * Please do not store booleans - convert to 0/1 integer values
22  * The PConfig::get() functions return boolean false for keys that are unset,
23  * and this could lead to subtle bugs.
24  */
25 class PConfig
26 {
27         private static $in_db;
28
29         /**
30          * @brief Loads all configuration values of a user's config family into a cached storage.
31          *
32          * All configuration values of the given user are stored in global cache
33          * which is available under the global variable $a->config[$uid].
34          *
35          * @param string $uid    The user_id
36          * @param string $family The category of the configuration value
37          *
38          * @return void
39          */
40         public static function load($uid, $family)
41         {
42                 $a = get_app();
43
44                 $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid));
45                 if (DBM::is_result($r)) {
46                         while ($rr = dba::fetch($r)) {
47                                 $k = $rr['k'];
48                                 $a->config[$uid][$family][$k] = $rr['v'];
49                                 self::$in_db[$uid][$family][$k] = true;
50                         }
51                 } else if ($family != 'config') {
52                         // Negative caching
53                         $a->config[$uid][$family] = "!<unset>!";
54                 }
55                 dba::close($r);
56         }
57
58         /**
59          * @brief Get a particular user's config variable given the category name
60          * ($family) and a key.
61          *
62          * Get a particular user's config value from the given category ($family)
63          * and the $key from a cached storage in $a->config[$uid].
64          *
65          * @param string  $uid           The user_id
66          * @param string  $family        The category of the configuration value
67          * @param string  $key           The configuration key to query
68          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
69          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
70          *
71          * @return mixed Stored value or null if it does not exist
72          */
73         public static function get($uid, $family, $key, $default_value = null, $refresh = false)
74         {
75                 $a = get_app();
76
77                 if (!$refresh) {
78                         // Looking if the whole family isn't set
79                         if (isset($a->config[$uid][$family])) {
80                                 if ($a->config[$uid][$family] === '!<unset>!') {
81                                         return $default_value;
82                                 }
83                         }
84
85                         if (isset($a->config[$uid][$family][$key])) {
86                                 if ($a->config[$uid][$family][$key] === '!<unset>!') {
87                                         return $default_value;
88                                 }
89                                 return $a->config[$uid][$family][$key];
90                         }
91                 }
92
93                 $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $family, 'k' => $key]);
94                 if (DBM::is_result($pconfig)) {
95                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
96                         $a->config[$uid][$family][$key] = $val;
97                         self::$in_db[$uid][$family][$key] = true;
98
99                         return $val;
100                 } else {
101                         $a->config[$uid][$family][$key] = '!<unset>!';
102                         self::$in_db[$uid][$family][$key] = false;
103
104                         return $default_value;
105                 }
106         }
107
108         /**
109          * @brief Sets a configuration value for a user
110          *
111          * Stores a config value ($value) in the category ($family) under the key ($key)
112          * for the user_id $uid.
113          *
114          * @note Please do not store booleans - convert to 0/1 integer values!
115          *
116          * @param string $uid    The user_id
117          * @param string $family The category of the configuration value
118          * @param string $key    The configuration key to set
119          * @param string $value  The value to store
120          *
121          * @return mixed Stored $value or false
122          */
123         public static function set($uid, $family, $key, $value)
124         {
125                 $a = get_app();
126
127                 // We store our setting values in a string variable.
128                 // So we have to do the conversion here so that the compare below works.
129                 // The exception are array values.
130                 $dbvalue = (!is_array($value) ? (string)$value : $value);
131
132                 $stored = self::get($uid, $family, $key, null, true);
133
134                 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
135                         return true;
136                 }
137
138                 $a->config[$uid][$family][$key] = $dbvalue;
139
140                 // manage array value
141                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
142
143                 $ret = dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
144
145                 if ($ret) {
146                         self::$in_db[$uid][$family][$key] = true;
147                         return $value;
148                 }
149                 return $ret;
150         }
151
152         /**
153          * @brief Deletes the given key from the users's configuration.
154          *
155          * Removes the configured value from the stored cache in $a->config[$uid]
156          * and removes it from the database.
157          *
158          * @param string $uid    The user_id
159          * @param string $family The category of the configuration value
160          * @param string $key    The configuration key to delete
161          *
162          * @return mixed
163          */
164         public static function delete($uid, $family, $key)
165         {
166                 $a = get_app();
167
168                 if (x($a->config[$uid][$family], $key)) {
169                         unset($a->config[$uid][$family][$key]);
170                         unset(self::$in_db[$uid][$family][$key]);
171                 }
172
173                 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));
174
175                 return $ret;
176         }
177 }