2 namespace Friendica\Core\Config;
5 use Friendica\BaseObject;
6 use Friendica\Database\DBM;
8 require_once 'include/dba.php';
11 * JustInTime User Configuration Adapter
13 * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
15 * @author Hypolite Petovan <mrpetovan@gmail.com>
17 class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
21 public function load($uid, $cat)
25 $pconfigs = dba::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
26 if (DBM::is_result($pconfigs)) {
27 while ($pconfig = dba::fetch($pconfigs)) {
30 self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']);
32 $this->in_db[$uid][$cat][$k] = true;
34 } else if ($cat != 'config') {
36 $a->config[$uid][$cat] = "!<unset>!";
38 dba::close($pconfigs);
41 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
46 // Looking if the whole family isn't set
47 if (isset($a->config[$uid][$cat])) {
48 if ($a->config[$uid][$cat] === '!<unset>!') {
49 return $default_value;
53 if (isset($a->config[$uid][$cat][$k])) {
54 if ($a->config[$uid][$cat][$k] === '!<unset>!') {
55 return $default_value;
57 return $a->config[$uid][$cat][$k];
61 $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
62 if (DBM::is_result($pconfig)) {
63 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
65 self::getApp()->setPConfigValue($uid, $cat, $k, $val);
67 $this->in_db[$uid][$cat][$k] = true;
71 self::getApp()->setPConfigValue($uid, $cat, $k, '!<unset>!');
73 $this->in_db[$uid][$cat][$k] = false;
75 return $default_value;
79 public function set($uid, $cat, $k, $value)
81 // We store our setting values in a string variable.
82 // So we have to do the conversion here so that the compare below works.
83 // The exception are array values.
84 $dbvalue = (!is_array($value) ? (string)$value : $value);
86 $stored = $this->get($uid, $cat, $k, null, true);
88 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
92 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
95 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
97 $result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
100 $this->in_db[$uid][$cat][$k] = true;
107 public function delete($uid, $cat, $k)
109 self::getApp()->deletePConfigValue($uid, $cat, $k);
111 if (!empty($this->in_db[$uid][$cat][$k])) {
112 unset($this->in_db[$uid][$cat][$k]);
115 $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);