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