]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadPConfigAdapter.php
Bugfixing PConfig
[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 $config;
24
25         /**
26          * @param IPConfigCache $config The config cache of this adapter
27          * @param int           $uid    The UID of the current user
28          */
29         public function __construct($config, $uid = null)
30         {
31                 $this->config = $config;
32                 if (isset($uid)) {
33                         $this->load($uid, 'config');
34                 }
35         }
36
37         public function load($uid, $family)
38         {
39                 if ($this->config_loaded) {
40                         return;
41                 }
42
43                 if (empty($uid)) {
44                         return;
45                 }
46
47                 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
48                 while ($pconfig = DBA::fetch($pconfigs)) {
49                         $this->config->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
50                 }
51                 DBA::close($pconfigs);
52
53                 $this->config_loaded = true;
54         }
55
56         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
57         {
58                 if (!$this->config_loaded) {
59                         $this->load($uid, $cat);
60                 }
61
62                 if ($refresh) {
63                         $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
64                         if (DBA::isResult($config)) {
65                                 $this->config->setP($uid, $cat, $k, $config['v']);
66                         } else {
67                                 $this->config->deleteP($uid, $cat, $k);
68                         }
69                 }
70
71                 return $this->config->getP($uid, $cat, $k, $default_value);;
72         }
73
74         public function set($uid, $cat, $k, $value)
75         {
76                 if (!$this->config_loaded) {
77                         $this->load($uid, $cat);
78                 }
79                 // We store our setting values as strings.
80                 // So we have to do the conversion here so that the compare below works.
81                 // The exception are array values.
82                 $compare_value = !is_array($value) ? (string)$value : $value;
83
84                 if ($this->config->getP($uid, $cat, $k) === $compare_value) {
85                         return true;
86                 }
87
88                 $this->config->setP($uid, $cat, $k, $value);
89
90                 // manage array value
91                 $dbvalue = is_array($value) ? serialize($value) : $value;
92
93                 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
94                 if (!$result) {
95                         throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
96                 }
97
98                 return true;
99         }
100
101         public function delete($uid, $cat, $k)
102         {
103                 if (!$this->config_loaded) {
104                         $this->load($uid, $cat);
105                 }
106
107                 $this->config->deleteP($uid, $cat, $k);
108
109                 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
110
111                 return $result;
112         }
113 }