]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/PreloadPConfigAdapter.php
Bugfixings in Config
[friendica.git] / src / Core / Config / Adapter / PreloadPConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config\Adapter;
4
5 use Friendica\Database\DBA;
6
7 /**
8  * Preload User Configuration Adapter
9  *
10  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
15 {
16         /**
17          * @var array true if config for user is loaded
18          */
19         private $config_loaded;
20
21         /**
22          * @param int $uid The UID of the current user
23          */
24         public function __construct($uid = null)
25         {
26                 parent::__construct();
27
28                 $this->config_loaded = [];
29
30                 if (isset($uid)) {
31                         $this->load($uid, 'config');
32                 }
33         }
34
35         /**
36          * {@inheritdoc}
37          */
38         public function load($uid, $cat)
39         {
40                 $return = [];
41
42                 if (empty($uid)) {
43                         return $return;
44                 }
45
46                 if (!$this->isLoaded($uid, $cat, null)) {
47                         return $return;
48                 }
49
50                 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
51                 while ($pconfig = DBA::fetch($pconfigs)) {
52                         $value = $pconfig['v'];
53                         if (isset($value) && $value !== '') {
54                                 $return[$pconfig['cat']][$pconfig['k']] = $value;
55                         }
56                 }
57                 DBA::close($pconfigs);
58
59                 $this->config_loaded[$uid] = true;
60
61                 return $return;
62         }
63
64         /**
65          * {@inheritdoc}
66          */
67         public function get($uid, $cat, $key)
68         {
69                 if (!$this->isConnected()) {
70                         return null;
71                 }
72
73                 if (!$this->isLoaded($uid, $cat, $key)) {
74                         $this->load($uid, $cat);
75                 }
76
77                 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
78                 if (DBA::isResult($config)) {
79                         // manage array value
80                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
81
82                         if (isset($value) && $value !== '') {
83                                 return $value;
84                         }
85                 }
86                 return null;
87         }
88
89         /**
90          * {@inheritdoc}
91          */
92         public function set($uid, $cat, $key, $value)
93         {
94                 if (!$this->isConnected()) {
95                         return false;
96                 }
97
98                 if (!$this->isLoaded($uid, $cat, $key)) {
99                         $this->load($uid, $cat);
100                 }
101                 // We store our setting values as strings.
102                 // So we have to do the conversion here so that the compare below works.
103                 // The exception are array values.
104                 $compare_value = !is_array($value) ? (string)$value : $value;
105
106                 if ($this->get($uid, $cat, $key) === $compare_value) {
107                         return true;
108                 }
109
110                 // manage array value
111                 $dbvalue = is_array($value) ? serialize($value) : $value;
112
113                 return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
114         }
115
116         /**
117          * {@inheritdoc}
118          */
119         public function delete($uid, $cat, $key)
120         {
121                 if (!$this->isConnected()) {
122                         return false;
123                 }
124
125                 if (!$this->isLoaded($uid, $cat, $key)) {
126                         $this->load($uid, $cat);
127                 }
128
129                 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
130         }
131
132         /**
133          * {@inheritdoc}
134          */
135         public function isLoaded($uid, $cat, $key)
136         {
137                 if (!$this->isConnected()) {
138                         return false;
139                 }
140
141                 return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];
142         }
143 }