2 namespace Friendica\Core\Config;
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
8 * JustInTime User Configuration Adapter
10 * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
18 public function load($uid, $cat)
22 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
23 if (DBA::isResult($pconfigs)) {
24 while ($pconfig = DBA::fetch($pconfigs)) {
27 self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']);
29 $this->in_db[$uid][$cat][$k] = true;
31 } else if ($cat != 'config') {
33 $a->config[$uid][$cat] = "!<unset>!";
35 DBA::close($pconfigs);
38 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
43 // Looking if the whole family isn't set
44 if (isset($a->config[$uid][$cat])) {
45 if ($a->config[$uid][$cat] === '!<unset>!') {
46 return $default_value;
50 if (isset($a->config[$uid][$cat][$k])) {
51 if ($a->config[$uid][$cat][$k] === '!<unset>!') {
52 return $default_value;
54 return $a->config[$uid][$cat][$k];
58 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
59 if (DBA::isResult($pconfig)) {
60 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
62 self::getApp()->setPConfigValue($uid, $cat, $k, $val);
64 $this->in_db[$uid][$cat][$k] = true;
68 self::getApp()->setPConfigValue($uid, $cat, $k, '!<unset>!');
70 $this->in_db[$uid][$cat][$k] = false;
72 return $default_value;
76 public function set($uid, $cat, $k, $value)
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, $k, null, true);
85 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
89 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
92 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
94 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
97 $this->in_db[$uid][$cat][$k] = true;
103 public function delete($uid, $cat, $k)
105 self::getApp()->deletePConfigValue($uid, $cat, $k);
107 if (!empty($this->in_db[$uid][$cat][$k])) {
108 unset($this->in_db[$uid][$cat][$k]);
111 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);