Add refresh feature to Preload (P)Config adapters
- Add private methods to manipulat the App config variable
This commit is contained in:
parent
4b9c52aad0
commit
dcd1f18611
2 changed files with 150 additions and 59 deletions
|
@ -4,7 +4,9 @@ namespace Friendica\Core\Config;
|
||||||
|
|
||||||
use dba;
|
use dba;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Friendica\App;
|
||||||
use Friendica\BaseObject;
|
use Friendica\BaseObject;
|
||||||
|
use Friendica\Database\DBM;
|
||||||
|
|
||||||
require_once 'include/dba.php';
|
require_once 'include/dba.php';
|
||||||
|
|
||||||
|
@ -30,19 +32,9 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$a = self::getApp();
|
|
||||||
|
|
||||||
$configs = dba::select('config', ['cat', 'v', 'k']);
|
$configs = dba::select('config', ['cat', 'v', 'k']);
|
||||||
while ($config = dba::fetch($configs)) {
|
while ($config = dba::fetch($configs)) {
|
||||||
$cat = $config['cat'];
|
$this->setPreloadedValue($config['cat'], $config['k'], $config['v']);
|
||||||
$k = $config['k'];
|
|
||||||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
|
||||||
|
|
||||||
if ($cat === 'config') {
|
|
||||||
$a->config[$k] = $value;
|
|
||||||
} else {
|
|
||||||
$a->config[$cat][$k] = $value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
dba::close($configs);
|
dba::close($configs);
|
||||||
|
|
||||||
|
@ -51,41 +43,32 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
||||||
|
|
||||||
public function get($cat, $k, $default_value = null, $refresh = false)
|
public function get($cat, $k, $default_value = null, $refresh = false)
|
||||||
{
|
{
|
||||||
$a = self::getApp();
|
if ($refresh) {
|
||||||
|
$config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
|
||||||
$return = $default_value;
|
if (DBM::is_result($config)) {
|
||||||
|
$this->setPreloadedValue($cat, $k, $config['v']);
|
||||||
if ($cat === 'config') {
|
} else {
|
||||||
if (isset($a->config[$k])) {
|
$this->deletePreloadedValue($cat, $k);
|
||||||
$return = $a->config[$k];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (isset($a->config[$cat][$k])) {
|
|
||||||
$return = $a->config[$cat][$k];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$return = $this->getPreloadedValue($cat, $k, $default_value);
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set($cat, $k, $value)
|
public function set($cat, $k, $value)
|
||||||
{
|
{
|
||||||
$a = self::getApp();
|
|
||||||
|
|
||||||
// We store our setting values as strings.
|
// We store our setting values as strings.
|
||||||
// So we have to do the conversion here so that the compare below works.
|
// So we have to do the conversion here so that the compare below works.
|
||||||
// The exception are array values.
|
// The exception are array values.
|
||||||
$compare_value = !is_array($value) ? (string)$value : $value;
|
$compare_value = !is_array($value) ? (string)$value : $value;
|
||||||
|
|
||||||
if ($this->get($cat, $k) === $compare_value) {
|
if ($this->getPreloadedValue($cat, $k) === $compare_value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($cat === 'config') {
|
$this->setPreloadedValue($cat, $k, $value);
|
||||||
$a->config[$k] = $value;
|
|
||||||
} else {
|
|
||||||
$a->config[$cat][$k] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// manage array value
|
// manage array value
|
||||||
$dbvalue = is_array($value) ? serialize($value) : $value;
|
$dbvalue = is_array($value) ? serialize($value) : $value;
|
||||||
|
@ -99,6 +82,70 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($cat, $k)
|
public function delete($cat, $k)
|
||||||
|
{
|
||||||
|
$this->deletePreloadedValue($cat, $k);
|
||||||
|
|
||||||
|
$result = dba::delete('config', ['cat' => $cat, 'k' => $k]);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a preloaded value from the App config cache
|
||||||
|
*
|
||||||
|
* @param string $cat
|
||||||
|
* @param string $k
|
||||||
|
* @param mixed $default
|
||||||
|
*/
|
||||||
|
private function getPreloadedValue($cat, $k, $default = null)
|
||||||
|
{
|
||||||
|
$a = self::getApp();
|
||||||
|
|
||||||
|
$return = $default;
|
||||||
|
|
||||||
|
if ($cat === 'config') {
|
||||||
|
if (isset($a->config[$k])) {
|
||||||
|
$return = $a->config[$k];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (isset($a->config[$cat][$k])) {
|
||||||
|
$return = $a->config[$cat][$k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a preloaded value in the App config cache
|
||||||
|
*
|
||||||
|
* Accepts raw output from the config table
|
||||||
|
*
|
||||||
|
* @param string $cat
|
||||||
|
* @param string $k
|
||||||
|
* @param mixed $v
|
||||||
|
*/
|
||||||
|
private function setPreloadedValue($cat, $k, $v)
|
||||||
|
{
|
||||||
|
$a = self::getApp();
|
||||||
|
|
||||||
|
// Only arrays are serialized in database, so we have to unserialize sparingly
|
||||||
|
$value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
|
||||||
|
|
||||||
|
if ($cat === 'config') {
|
||||||
|
$a->config[$k] = $value;
|
||||||
|
} else {
|
||||||
|
$a->config[$cat][$k] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a preloaded value from the App config cache
|
||||||
|
*
|
||||||
|
* @param string $cat
|
||||||
|
* @param string $k
|
||||||
|
*/
|
||||||
|
private function deletePreloadedValue($cat, $k)
|
||||||
{
|
{
|
||||||
$a = self::getApp();
|
$a = self::getApp();
|
||||||
|
|
||||||
|
@ -111,9 +158,5 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
|
||||||
unset($a->config[$cat][$k]);
|
unset($a->config[$cat][$k]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = dba::delete('config', ['cat' => $cat, 'k' => $k]);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,9 @@ namespace Friendica\Core\Config;
|
||||||
|
|
||||||
use dba;
|
use dba;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Friendica\App;
|
||||||
use Friendica\BaseObject;
|
use Friendica\BaseObject;
|
||||||
|
use Friendica\Database\DBM;
|
||||||
|
|
||||||
require_once 'include/dba.php';
|
require_once 'include/dba.php';
|
||||||
|
|
||||||
|
@ -30,15 +32,9 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$a = self::getApp();
|
|
||||||
|
|
||||||
$pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
|
$pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
|
||||||
while ($pconfig = dba::fetch($pconfigs)) {
|
while ($pconfig = dba::fetch($pconfigs)) {
|
||||||
$cat = $pconfig['cat'];
|
$this->setPreloadedValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
|
||||||
$k = $pconfig['k'];
|
|
||||||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
|
|
||||||
|
|
||||||
$a->config[$uid][$cat][$k] = $value;
|
|
||||||
}
|
}
|
||||||
dba::close($pconfigs);
|
dba::close($pconfigs);
|
||||||
|
|
||||||
|
@ -47,37 +43,37 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
|
||||||
|
|
||||||
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
|
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
|
||||||
{
|
{
|
||||||
$a = self::getApp();
|
if ($refresh) {
|
||||||
|
$config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
|
||||||
$return = $default_value;
|
if (DBM::is_result($config)) {
|
||||||
|
$this->setPreloadedValue($uid, $cat, $k, $config['v']);
|
||||||
if (isset($a->config[$uid][$cat][$k])) {
|
} else {
|
||||||
$return = $a->config[$uid][$cat][$k];
|
$this->deletePreloadedValue($uid, $cat, $k);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$return = $this->getPreloadedValue($uid, $cat, $k, $default_value);
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set($uid, $cat, $k, $value)
|
public function set($uid, $cat, $k, $value)
|
||||||
{
|
{
|
||||||
$a = self::getApp();
|
|
||||||
|
|
||||||
// We store our setting values as strings.
|
// We store our setting values as strings.
|
||||||
// So we have to do the conversion here so that the compare below works.
|
// So we have to do the conversion here so that the compare below works.
|
||||||
// The exception are array values.
|
// The exception are array values.
|
||||||
$compare_value = !is_array($value) ? (string)$value : $value;
|
$compare_value = !is_array($value) ? (string)$value : $value;
|
||||||
|
|
||||||
if ($this->get($uid, $cat, $k) === $compare_value) {
|
if ($this->getPreloadedValue($uid, $cat, $k) === $compare_value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$a->config[$uid][$cat][$k] = $value;
|
$this->setPreloadedValue($uid, $cat, $k, $value);
|
||||||
|
|
||||||
// manage array value
|
// manage array value
|
||||||
$dbvalue = is_array($value) ? serialize($value) : $value;
|
$dbvalue = is_array($value) ? serialize($value) : $value;
|
||||||
|
|
||||||
$result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
|
$result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
|
||||||
|
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
|
throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
|
||||||
}
|
}
|
||||||
|
@ -87,16 +83,68 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
|
||||||
|
|
||||||
public function delete($uid, $cat, $k)
|
public function delete($uid, $cat, $k)
|
||||||
{
|
{
|
||||||
$a = self::getApp();
|
$this->deletePreloadedValue($uid, $cat, $k);
|
||||||
|
|
||||||
if (!isset($a->config[$uid][$cat][$k])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
unset($a->config[$uid][$cat][$k]);
|
|
||||||
|
|
||||||
$result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
|
$result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a preloaded value from the App user config cache
|
||||||
|
*
|
||||||
|
* @param int $uid
|
||||||
|
* @param string $cat
|
||||||
|
* @param string $k
|
||||||
|
* @param mixed $default
|
||||||
|
*/
|
||||||
|
private function getPreloadedValue($uid, $cat, $k, $default = null)
|
||||||
|
{
|
||||||
|
$a = self::getApp();
|
||||||
|
|
||||||
|
$return = $default;
|
||||||
|
|
||||||
|
if (isset($a->config[$uid][$cat][$k])) {
|
||||||
|
$return = $a->config[$uid][$cat][$k];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a preloaded value in the App user config cache
|
||||||
|
*
|
||||||
|
* Accepts raw output from the pconfig table
|
||||||
|
*
|
||||||
|
* @param int $uid
|
||||||
|
* @param string $cat
|
||||||
|
* @param string $k
|
||||||
|
* @param mixed $v
|
||||||
|
*/
|
||||||
|
private function setPreloadedValue($uid, $cat, $k, $v)
|
||||||
|
{
|
||||||
|
$a = self::getApp();
|
||||||
|
|
||||||
|
// Only arrays are serialized in database, so we have to unserialize sparingly
|
||||||
|
$value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
|
||||||
|
|
||||||
|
$a->config[$uid][$cat][$k] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a preloaded value from the App user config cache
|
||||||
|
*
|
||||||
|
* @param int $uid
|
||||||
|
* @param string $cat
|
||||||
|
* @param string $k
|
||||||
|
*/
|
||||||
|
private function deletePreloadedValue($uid, $cat, $k)
|
||||||
|
{
|
||||||
|
$a = self::getApp();
|
||||||
|
|
||||||
|
if (isset($a->config[$uid][$cat][$k])) {
|
||||||
|
unset($a->config[$uid][$cat][$k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue