2 namespace Friendica\Core\Config;
4 use Friendica\BaseObject;
5 use Friendica\Database\DBA;
7 require_once 'include/dba.php';
10 * JustInTime User Configuration Adapter
12 * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
14 * @author Hypolite Petovan <mrpetovan@gmail.com>
16 class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
20 public function load($uid, $cat)
24 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
25 if (DBA::isResult($pconfigs)) {
26 while ($pconfig = DBA::fetch($pconfigs)) {
29 self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']);
31 $this->in_db[$uid][$cat][$k] = true;
33 } else if ($cat != 'config') {
35 $a->config[$uid][$cat] = "!<unset>!";
37 DBA::close($pconfigs);
40 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
45 // Looking if the whole family isn't set
46 if (isset($a->config[$uid][$cat])) {
47 if ($a->config[$uid][$cat] === '!<unset>!') {
48 return $default_value;
52 if (isset($a->config[$uid][$cat][$k])) {
53 if ($a->config[$uid][$cat][$k] === '!<unset>!') {
54 return $default_value;
56 return $a->config[$uid][$cat][$k];
60 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
61 if (DBA::isResult($pconfig)) {
62 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
64 self::getApp()->setPConfigValue($uid, $cat, $k, $val);
66 $this->in_db[$uid][$cat][$k] = true;
70 self::getApp()->setPConfigValue($uid, $cat, $k, '!<unset>!');
72 $this->in_db[$uid][$cat][$k] = false;
74 return $default_value;
78 public function set($uid, $cat, $k, $value)
80 // We store our setting values in a string variable.
81 // So we have to do the conversion here so that the compare below works.
82 // The exception are array values.
83 $dbvalue = (!is_array($value) ? (string)$value : $value);
85 $stored = $this->get($uid, $cat, $k, null, true);
87 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
91 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
94 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
96 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
99 $this->in_db[$uid][$cat][$k] = true;
105 public function delete($uid, $cat, $k)
107 self::getApp()->deletePConfigValue($uid, $cat, $k);
109 if (!empty($this->in_db[$uid][$cat][$k])) {
110 unset($this->in_db[$uid][$cat][$k]);
113 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);