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 $configCache The config cache of this adapter
26 public function __construct(IPConfigCache $configCache)
28 $this->configCache = $configCache;
34 public function load($uid, $cat)
36 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
37 if (DBA::isResult($pconfigs)) {
38 while ($pconfig = DBA::fetch($pconfigs)) {
41 $this->configCache->setP($uid, $cat, $k, $pconfig['v']);
43 $this->in_db[$uid][$cat][$k] = true;
45 } else if ($cat != 'config') {
47 $this->configCache->setP($uid, $cat, null, "!<unset>!");
49 DBA::close($pconfigs);
55 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
58 // Looking if the whole family isn't set
59 if ($this->configCache->getP($uid, $cat) !== null) {
60 if ($this->configCache->getP($uid, $cat) === '!<unset>!') {
61 return $default_value;
65 if ($this->configCache->getP($uid, $cat, $k) !== null) {
66 if ($this->configCache->getP($uid, $cat, $k) === '!<unset>!') {
67 return $default_value;
69 return $this->configCache->getP($uid, $cat, $k);
73 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
74 if (DBA::isResult($pconfig)) {
75 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
77 $this->configCache->setP($uid, $cat, $k, $val);
79 $this->in_db[$uid][$cat][$k] = true;
83 $this->configCache->setP($uid, $cat, $k, '!<unset>!');
85 $this->in_db[$uid][$cat][$k] = false;
87 return $default_value;
94 public function set($uid, $cat, $k, $value)
96 // We store our setting values in a string variable.
97 // So we have to do the conversion here so that the compare below works.
98 // The exception are array values.
99 $dbvalue = (!is_array($value) ? (string)$value : $value);
101 $stored = $this->get($uid, $cat, $k, null, true);
103 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
107 $this->configCache->setP($uid, $cat, $k, $value);
109 // manage array value
110 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
112 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
115 $this->in_db[$uid][$cat][$k] = true;
124 public function delete($uid, $cat, $k)
126 $this->configCache->deleteP($uid, $cat, $k);
128 if (!empty($this->in_db[$uid][$cat][$k])) {
129 unset($this->in_db[$uid][$cat][$k]);
132 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);