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 = $pconfig['v'];
34 if (isset($value) && $value !== '') {
35 $return[$key] = $value;
36 $this->in_db[$uid][$cat][$key] = true;
39 } else if ($cat != 'config') {
41 $return = "!<unset>!";
43 DBA::close($pconfigs);
45 return [$cat => $return];
51 public function get($uid, $cat, $key)
53 if (!$this->isConnected()) {
57 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
58 if (DBA::isResult($pconfig)) {
60 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
62 if (isset($value) && $value !== '') {
63 $this->in_db[$uid][$cat][$key] = true;
68 $this->in_db[$uid][$cat][$key] = false;
75 public function set($uid, $cat, $key, $value)
77 if (!$this->isConnected()) {
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, $key);
88 if (!isset($this->in_db[$uid])) {
89 $this->in_db[$uid] = [];
91 if (!isset($this->in_db[$uid][$cat])) {
92 $this->in_db[$uid][$cat] = [];
94 if (!isset($this->in_db[$uid][$cat][$key])) {
95 $this->in_db[$uid][$cat][$key] = false;
98 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
102 // manage array value
103 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
105 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
107 $this->in_db[$uid][$cat][$key] = $result;
115 public function delete($uid, $cat, $key)
117 if (!$this->isConnected()) {
121 if (!empty($this->in_db[$uid][$cat][$key])) {
122 unset($this->in_db[$uid][$cat][$key]);
125 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
133 public function isLoaded($uid, $cat, $key)
135 if (!$this->isConnected()) {
139 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];