2 namespace Friendica\Core\Config\Adapter;
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 extends AbstractDbaConfigAdapter implements IPConfigAdapter
20 public function load($uid, $cat)
24 if (!$this->isConnected()) {
28 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
29 if (DBA::isResult($pconfigs)) {
30 while ($pconfig = DBA::fetch($pconfigs)) {
32 $value = $this->toConfigValue($pconfig['v']);
34 // The value was in the db, so don't check it again (unless you have to)
35 $this->in_db[$uid][$cat][$key] = true;
38 $return[$key] = $value;
41 } else if ($cat != 'config') {
45 DBA::close($pconfigs);
47 return [$cat => $return];
53 * @param bool $mark if true, mark the selection of the current cat/key pair
55 public function get($uid, $cat, $key, $mark = true)
57 if (!$this->isConnected()) {
61 // The value was in the db, so don't check it again (unless you have to)
63 $this->in_db[$uid][$cat][$key] = true;
66 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
67 if (DBA::isResult($pconfig)) {
68 $value = $this->toConfigValue($pconfig['v']);
75 $this->in_db[$uid][$cat][$key] = false;
82 public function set($uid, $cat, $key, $value)
84 if (!$this->isConnected()) {
88 // We store our setting values in a string variable.
89 // So we have to do the conversion here so that the compare below works.
90 // The exception are array values.
91 $dbvalue = (!is_array($value) ? (string)$value : $value);
93 $stored = $this->get($uid, $cat, $key, false);
95 if (!isset($this->in_db[$uid])) {
96 $this->in_db[$uid] = [];
98 if (!isset($this->in_db[$uid][$cat])) {
99 $this->in_db[$uid][$cat] = [];
101 if (!isset($this->in_db[$uid][$cat][$key])) {
102 $this->in_db[$uid][$cat][$key] = false;
105 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
109 // manage array value
110 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
112 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
114 $this->in_db[$uid][$cat][$key] = $result;
122 public function delete($uid, $cat, $key)
124 if (!$this->isConnected()) {
128 if (isset($this->in_db[$uid][$cat][$key])) {
129 unset($this->in_db[$uid][$cat][$key]);
132 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
138 public function isLoaded($uid, $cat, $key)
140 if (!$this->isConnected()) {
144 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];