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