2 namespace Friendica\Core\Config;
4 use Friendica\Core\PConfig;
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 implements IPConfigAdapter
18 public function load($uid, $cat)
20 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
21 if (DBA::isResult($pconfigs)) {
22 while ($pconfig = DBA::fetch($pconfigs)) {
25 PConfig::setPConfigValue($uid, $cat, $k, $pconfig['v']);
27 $this->in_db[$uid][$cat][$k] = true;
29 } else if ($cat != 'config') {
31 PConfig::setPConfigValue($uid, $cat, "!<unset>!");
33 DBA::close($pconfigs);
36 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
39 // Looking if the whole family isn't set
40 if (PConfig::getPConfigValue($uid, $cat) !== null) {
41 if (PConfig::getPConfigValue($uid, $cat) === '!<unset>!') {
42 return $default_value;
46 if (PConfig::getPConfigValue($uid, $cat, $k) !== null) {
47 if (PConfig::getPConfigValue($uid, $cat, $k) === '!<unset>!') {
48 return $default_value;
50 return PConfig::getPConfigValue($uid, $cat, $k);
54 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
55 if (DBA::isResult($pconfig)) {
56 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
58 PConfig::setPConfigValue($uid, $cat, $k, $val);
60 $this->in_db[$uid][$cat][$k] = true;
64 PConfig::setPConfigValue($uid, $cat, $k, '!<unset>!');
66 $this->in_db[$uid][$cat][$k] = false;
68 return $default_value;
72 public function set($uid, $cat, $k, $value)
74 // We store our setting values in a string variable.
75 // So we have to do the conversion here so that the compare below works.
76 // The exception are array values.
77 $dbvalue = (!is_array($value) ? (string)$value : $value);
79 $stored = $this->get($uid, $cat, $k, null, true);
81 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
85 PConfig::setPConfigValue($uid, $cat, $k, $value);
88 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
90 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
93 $this->in_db[$uid][$cat][$k] = true;
99 public function delete($uid, $cat, $k)
101 PConfig::deletePConfigValue($uid, $cat, $k);
103 if (!empty($this->in_db[$uid][$cat][$k])) {
104 unset($this->in_db[$uid][$cat][$k]);
107 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);