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