]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadPConfigAdapter.php
Add dbstructure_definition hook call
[friendica.git] / src / Core / Config / PreloadPConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use dba;
6 use Exception;
7 use Friendica\App;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBM;
10
11 require_once 'include/dba.php';
12
13 /**
14  * Preload User Configuration Adapter
15  *
16  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
17  *
18  * @author Hypolite Petovan <mrpetovan@gmail.com>
19  */
20 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
21 {
22         private $config_loaded = false;
23
24         public function __construct($uid)
25         {
26                 $this->load($uid, 'config');
27         }
28
29         public function load($uid, $family)
30         {
31                 if ($this->config_loaded) {
32                         return;
33                 }
34
35                 if (empty($uid)) {
36                         return;
37                 }
38
39                 $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
40                 while ($pconfig = dba::fetch($pconfigs)) {
41                         self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
42                 }
43                 dba::close($pconfigs);
44
45                 $this->config_loaded = true;
46         }
47
48         public function get($uid, $cat, $k, $default_value = null, $refresh = false)
49         {
50                 if (!$this->config_loaded) {
51                         $this->load($uid, $cat);
52                 }
53
54                 if ($refresh) {
55                         $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
56                         if (DBM::is_result($config)) {
57                                 self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
58                         } else {
59                                 self::getApp()->deletePConfigValue($uid, $cat, $k);
60                         }
61                 }
62
63                 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
64
65                 return $return;
66         }
67
68         public function set($uid, $cat, $k, $value)
69         {
70                 if (!$this->config_loaded) {
71                         $this->load($uid, $cat);
72                 }
73                 // We store our setting values as strings.
74                 // So we have to do the conversion here so that the compare below works.
75                 // The exception are array values.
76                 $compare_value = !is_array($value) ? (string)$value : $value;
77
78                 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
79                         return true;
80                 }
81
82                 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
83
84                 // manage array value
85                 $dbvalue = is_array($value) ? serialize($value) : $value;
86
87                 $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
88                 if (!$result) {
89                         throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
90                 }
91
92                 return true;
93         }
94
95         public function delete($uid, $cat, $k)
96         {
97                 if (!$this->config_loaded) {
98                         $this->load($uid, $cat);
99                 }
100
101                 self::getApp()->deletePConfigValue($uid, $cat, $k);
102
103                 $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
104
105                 return $result;
106         }
107 }