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)) {
33 $return[$key] = $pconfig['v'];
35 $this->in_db[$uid][$cat][$key] = true;
37 } else if ($cat != 'config') {
39 $return = "!<unset>!";
41 DBA::close($pconfigs);
43 return [$cat => $return];
49 public function get($uid, $cat, $key)
51 if (!$this->isConnected()) {
55 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
56 if (DBA::isResult($pconfig)) {
58 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
60 if (isset($value) && $value !== '') {
61 $this->in_db[$uid][$cat][$key] = true;
66 $this->in_db[$uid][$cat][$key] = false;
73 public function set($uid, $cat, $key, $value)
75 if (!$this->isConnected()) {
79 // We store our setting values in a string variable.
80 // So we have to do the conversion here so that the compare below works.
81 // The exception are array values.
82 $dbvalue = (!is_array($value) ? (string)$value : $value);
84 $stored = $this->get($uid, $cat, $key);
86 if (!isset($this->in_db[$uid])) {
87 $this->in_db[$uid] = [];
89 if (!isset($this->in_db[$uid][$cat])) {
90 $this->in_db[$uid][$cat] = [];
92 if (!isset($this->in_db[$uid][$cat][$key])) {
93 $this->in_db[$uid][$cat][$key] = false;
96 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
100 // manage array value
101 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
103 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
105 $this->in_db[$uid][$cat][$key] = $result;
113 public function delete($uid, $cat, $key)
115 if (!$this->isConnected()) {
119 if (!empty($this->in_db[$uid][$cat][$key])) {
120 unset($this->in_db[$uid][$cat][$key]);
123 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
131 public function isLoaded($uid, $cat, $key)
133 if (!$this->isConnected()) {
137 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];