friendica/src/Core/Config/Adapter/JITPConfigAdapter.php

127 righe
2.7 KiB
PHP

<?php
namespace Friendica\Core\Config\Adapter;
use Friendica\Database\DBA;
/**
* JustInTime User Configuration Adapter
*
* Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
class JITPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
{
private $in_db;
/**
* {@inheritdoc}
*/
public function load($uid, $cat)
{
$return = [];
if (!$this->isConnected()) {
return $return;
}
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
if (DBA::isResult($pconfigs)) {
while ($pconfig = DBA::fetch($pconfigs)) {
$key = $pconfig['k'];
$return[$key] = $pconfig['v'];
$this->in_db[$uid][$cat][$key] = true;
}
} else if ($cat != 'config') {
// Negative caching
$return[null] = "!<unset>!";
}
DBA::close($pconfigs);
return [$cat => $return];
}
/**
* {@inheritdoc}
*/
public function get($uid, $cat, $key)
{
if (!$this->isConnected()) {
return null;
}
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
if (DBA::isResult($pconfig)) {
// manage array value
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
$this->in_db[$uid][$cat][$key] = true;
return $value;
} else {
$this->in_db[$uid][$cat][$key] = false;
return '!<unset>!';
}
}
/**
* {@inheritdoc}
*/
public function set($uid, $cat, $key, $value)
{
if (!$this->isConnected()) {
return false;
}
// We store our setting values in a string variable.
// So we have to do the conversion here so that the compare below works.
// The exception are array values.
$dbvalue = (!is_array($value) ? (string)$value : $value);
$stored = $this->get($uid, $cat, $key);
if (!isset($this->in_db[$uid])) {
$this->in_db[$uid] = [];
}
if (!isset($this->in_db[$uid][$cat])) {
$this->in_db[$uid][$cat] = [];
}
if (!isset($this->in_db[$uid][$cat][$key])) {
$this->in_db[$uid][$cat][$key] = false;
}
if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
return true;
}
// manage array value
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
$this->in_db[$uid][$cat][$key] = $result;
return $result;
}
/**
* {@inheritdoc}
*/
public function delete($uid, $cat, $key)
{
if (!$this->isConnected()) {
return false;
}
if (!empty($this->in_db[$uid][$cat][$key])) {
unset($this->in_db[$uid][$cat][$key]);
}
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
return $result;
}
}