2 namespace Friendica\Core\Config;
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 implements IPConfigAdapter
18 * The config cache of this adapter
24 * @param IPConfigCache $config The config cache of this adapter
26 public function __construct($config)
28 $this->config = $config;
31 public function load($uid, $cat)
33 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
34 if (DBA::isResult($pconfigs)) {
35 while ($pconfig = DBA::fetch($pconfigs)) {
38 $this->config->setP($uid, $cat, $k, $pconfig['v']);
40 $this->in_db[$uid][$cat][$k] = true;
42 } else if ($cat != 'config') {
44 $this->config->setP($uid, $cat, null, "!<unset>!");
46 DBA::close($pconfigs);
49 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
52 // Looking if the whole family isn't set
53 if ($this->config->getP($uid, $cat) !== null) {
54 if ($this->config->getP($uid, $cat) === '!<unset>!') {
55 return $default_value;
59 if ($this->config->getP($uid, $cat, $k) !== null) {
60 if ($this->config->getP($uid, $cat, $k) === '!<unset>!') {
61 return $default_value;
63 return $this->config->getP($uid, $cat, $k);
67 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
68 if (DBA::isResult($pconfig)) {
69 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
71 $this->config->setP($uid, $cat, $k, $val);
73 $this->in_db[$uid][$cat][$k] = true;
77 $this->config->setP($uid, $cat, $k, '!<unset>!');
79 $this->in_db[$uid][$cat][$k] = false;
81 return $default_value;
85 public function set($uid, $cat, $k, $value)
87 // We store our setting values in a string variable.
88 // So we have to do the conversion here so that the compare below works.
89 // The exception are array values.
90 $dbvalue = (!is_array($value) ? (string)$value : $value);
92 $stored = $this->get($uid, $cat, $k, null, true);
94 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
98 $this->config->setP($uid, $cat, $k, $value);
100 // manage array value
101 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
103 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
106 $this->in_db[$uid][$cat][$k] = true;
112 public function delete($uid, $cat, $k)
114 $this->config->deleteP($uid, $cat, $k);
116 if (!empty($this->in_db[$uid][$cat][$k])) {
117 unset($this->in_db[$uid][$cat][$k]);
120 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);