]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JITPConfigAdapter.php
4992068f60cbe7662791b0b41e10f10f78c6ca8b
[friendica.git] / src / Core / Config / JITPConfigAdapter.php
1 <?php
2 namespace Friendica\Core\Config;
3
4 use Friendica\Core\PConfig;
5 use Friendica\Database\DBA;
6
7 /**
8  * JustInTime User Configuration Adapter
9  *
10  * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class JITPConfigAdapter implements IPConfigAdapter
15 {
16         private $in_db;
17
18         public function load($uid, $cat)
19         {
20                 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
21                 if (DBA::isResult($pconfigs)) {
22                         while ($pconfig = DBA::fetch($pconfigs)) {
23                                 $k = $pconfig['k'];
24
25                                 PConfig::setPConfigValue($uid, $cat, $k, $pconfig['v']);
26
27                                 $this->in_db[$uid][$cat][$k] = true;
28                         }
29                 } else if ($cat != 'config') {
30                         // Negative caching
31                         PConfig::setPConfigValue($uid, $cat, "!<unset>!");
32                 }
33                 DBA::close($pconfigs);
34         }
35
36         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
37         {
38                 if (!$refresh) {
39                         // Looking if the whole family isn't set
40                         if (PConfig::getPConfigValue($uid, $cat) !== null) {
41                                 if (PConfig::getPConfigValue($uid, $cat) === '!<unset>!') {
42                                         return $default_value;
43                                 }
44                         }
45
46                         if (PConfig::getPConfigValue($uid, $cat, $k) !== null) {
47                                 if (PConfig::getPConfigValue($uid, $cat, $k) === '!<unset>!') {
48                                         return $default_value;
49                                 }
50                                 return PConfig::getPConfigValue($uid, $cat, $k);
51                         }
52                 }
53
54                 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
55                 if (DBA::isResult($pconfig)) {
56                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
57
58                         PConfig::setPConfigValue($uid, $cat, $k, $val);
59
60                         $this->in_db[$uid][$cat][$k] = true;
61
62                         return $val;
63                 } else {
64                         PConfig::setPConfigValue($uid, $cat, $k, '!<unset>!');
65
66                         $this->in_db[$uid][$cat][$k] = false;
67
68                         return $default_value;
69                 }
70         }
71
72         public function set($uid, $cat, $k, $value)
73         {
74                 // We store our setting values in a string variable.
75                 // So we have to do the conversion here so that the compare below works.
76                 // The exception are array values.
77                 $dbvalue = (!is_array($value) ? (string)$value : $value);
78
79                 $stored = $this->get($uid, $cat, $k, null, true);
80
81                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
82                         return true;
83                 }
84
85                 PConfig::setPConfigValue($uid, $cat, $k, $value);
86
87                 // manage array value
88                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
89
90                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
91
92                 if ($result) {
93                         $this->in_db[$uid][$cat][$k] = true;
94                 }
95
96                 return $result;
97         }
98
99         public function delete($uid, $cat, $k)
100         {
101                 PConfig::deletePConfigValue($uid, $cat, $k);
102
103                 if (!empty($this->in_db[$uid][$cat][$k])) {
104                         unset($this->in_db[$uid][$cat][$k]);
105                 }
106
107                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
108
109                 return $result;
110         }
111 }