2 namespace Friendica\Core\Config\Adapter;
4 use Friendica\Database\DBA;
7 * JustInTime User Configuration Adapter
9 * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
11 * @author Hypolite Petovan <hypolite@mrpetovan.com>
13 class JITPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
20 public function load($uid, $cat)
24 if (!$this->isConnected()) {
28 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
29 if (DBA::isResult($pconfigs)) {
30 while ($pconfig = DBA::fetch($pconfigs)) {
33 $return[$key] = $pconfig['v'];
35 $this->in_db[$uid][$cat][$key] = true;
37 } else if ($cat != 'config') {
39 $return = "!<unset>!";
41 DBA::close($pconfigs);
43 return [$cat => $return];
49 public function get($uid, $cat, $key)
51 if (!$this->isConnected()) {
55 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
56 if (DBA::isResult($pconfig)) {
58 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
60 $this->in_db[$uid][$cat][$key] = true;
64 $this->in_db[$uid][$cat][$key] = false;
72 public function set($uid, $cat, $key, $value)
74 if (!$this->isConnected()) {
78 // We store our setting values in a string variable.
79 // So we have to do the conversion here so that the compare below works.
80 // The exception are array values.
81 $dbvalue = (!is_array($value) ? (string)$value : $value);
83 $stored = $this->get($uid, $cat, $key);
85 if (!isset($this->in_db[$uid])) {
86 $this->in_db[$uid] = [];
88 if (!isset($this->in_db[$uid][$cat])) {
89 $this->in_db[$uid][$cat] = [];
91 if (!isset($this->in_db[$uid][$cat][$key])) {
92 $this->in_db[$uid][$cat][$key] = false;
95 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
100 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
102 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
104 $this->in_db[$uid][$cat][$key] = $result;
112 public function delete($uid, $cat, $key)
114 if (!$this->isConnected()) {
118 if (!empty($this->in_db[$uid][$cat][$key])) {
119 unset($this->in_db[$uid][$cat][$key]);
122 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
130 public function isLoaded($uid, $cat, $key)
132 if (!$this->isConnected()) {
136 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];