Add Config adapter interfaces/classes
[friendica.git/.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 PConfigAdapter
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                                 $a->config[$uid][$cat][$k] = $pconfig['v'];
30                                 $this->in_db[$uid][$cat][$k] = true;
31                         }
32                 } else if ($cat != 'config') {
33                         // Negative caching
34                         $a->config[$uid][$cat] = "!<unset>!";
35                 }
36                 dba::close($pconfigs);
37         }
38
39         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
40         {
41                 $a = self::getApp();
42
43                 if (!$refresh) {
44                         // Looking if the whole family isn't set
45                         if (isset($a->config[$uid][$cat])) {
46                                 if ($a->config[$uid][$cat] === '!<unset>!') {
47                                         return $default_value;
48                                 }
49                         }
50
51                         if (isset($a->config[$uid][$cat][$k])) {
52                                 if ($a->config[$uid][$cat][$k] === '!<unset>!') {
53                                         return $default_value;
54                                 }
55                                 return $a->config[$uid][$cat][$k];
56                         }
57                 }
58
59                 $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
60                 if (DBM::is_result($pconfig)) {
61                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
62                         $a->config[$uid][$cat][$k] = $val;
63                         $this->in_db[$uid][$cat][$k] = true;
64
65                         return $val;
66                 } else {
67                         $a->config[$uid][$cat][$k] = '!<unset>!';
68                         $this->in_db[$uid][$cat][$k] = false;
69
70                         return $default_value;
71                 }
72         }
73
74         public function set($uid, $cat, $k, $value)
75         {
76                 $a = self::getApp();
77
78                 // We store our setting values in a string variable.
79                 // So we have to do the conversion here so that the compare below works.
80                 // The exception are array values.
81                 $dbvalue = (!is_array($value) ? (string)$value : $value);
82
83                 $stored = $this->get($uid, $cat, $k, null, true);
84
85                 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
86                         return true;
87                 }
88
89                 $a->config[$uid][$cat][$k] = $dbvalue;
90
91                 // manage array value
92                 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
93
94                 $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
95
96                 if ($result) {
97                         $this->in_db[$uid][$cat][$k] = true;
98                         return $value;
99                 }
100
101                 return $result;
102         }
103
104         public function delete($uid, $cat, $k)
105         {
106                 $a = self::getApp();
107
108                 if (!empty($a->config[$uid][$cat][$k])) {
109                         unset($a->config[$uid][$cat][$k]);
110                         unset($this->in_db[$uid][$cat][$k]);
111                 }
112
113                 $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
114
115                 return $result;
116         }
117 }