]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/PreloadPConfigAdapter.php
Merge branch 'master' into develop
[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 = $this->toConfigValue($pconfig['v']);
53                         if (isset($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                         $value = $this->toConfigValue($config['v']);
80
81                         if (isset($value)) {
82                                 return $value;
83                         }
84                 }
85                 return null;
86         }
87
88         /**
89          * {@inheritdoc}
90          */
91         public function set($uid, $cat, $key, $value)
92         {
93                 if (!$this->isConnected()) {
94                         return false;
95                 }
96
97                 if (!$this->isLoaded($uid, $cat, $key)) {
98                         $this->load($uid, $cat);
99                 }
100                 // We store our setting values as strings.
101                 // So we have to do the conversion here so that the compare below works.
102                 // The exception are array values.
103                 $compare_value = !is_array($value) ? (string)$value : $value;
104                 $stored_value = $this->get($uid, $cat, $key);
105
106                 if (isset($stored_value) && $stored_value === $compare_value) {
107                         return true;
108                 }
109
110                 $dbvalue = $this->toDbValue($value);
111
112                 return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
113         }
114
115         /**
116          * {@inheritdoc}
117          */
118         public function delete($uid, $cat, $key)
119         {
120                 if (!$this->isConnected()) {
121                         return false;
122                 }
123
124                 if (!$this->isLoaded($uid, $cat, $key)) {
125                         $this->load($uid, $cat);
126                 }
127
128                 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
129         }
130
131         /**
132          * {@inheritdoc}
133          */
134         public function isLoaded($uid, $cat, $key)
135         {
136                 if (!$this->isConnected()) {
137                         return false;
138                 }
139
140                 return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];
141         }
142 }