friendica/src/Core/Config/PreloadPConfigAdapter.php

93 lines
2.4 KiB
PHP
Raw Normal View History

2018-03-03 18:09:12 +01:00
<?php
namespace Friendica\Core\Config;
use dba;
use Exception;
use Friendica\App;
2018-03-03 18:09:12 +01:00
use Friendica\BaseObject;
use Friendica\Database\DBM;
2018-03-03 18:09:12 +01:00
require_once 'include/dba.php';
/**
2018-03-07 02:04:04 +01:00
* Preload User Configuration Adapter
2018-03-03 18:09:12 +01:00
*
2018-03-07 02:04:04 +01:00
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
2018-03-03 18:09:12 +01:00
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
{
private $config_loaded = false;
public function __construct($uid)
{
$this->load($uid, 'config');
}
public function load($uid, $family)
{
if ($this->config_loaded) {
return;
}
$pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
while ($pconfig = dba::fetch($pconfigs)) {
2018-03-07 02:04:04 +01:00
self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
2018-03-03 18:09:12 +01:00
}
dba::close($pconfigs);
$this->config_loaded = true;
}
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
{
if ($refresh) {
$config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
if (DBM::is_result($config)) {
2018-03-07 02:04:04 +01:00
self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
} else {
2018-03-07 02:04:04 +01:00
self::getApp()->deletePConfigValue($uid, $cat, $k);
}
2018-03-03 18:09:12 +01:00
}
2018-03-07 02:04:04 +01:00
$return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
2018-03-03 18:09:12 +01:00
return $return;
}
public function set($uid, $cat, $k, $value)
{
// We store our setting values as strings.
// So we have to do the conversion here so that the compare below works.
// The exception are array values.
$compare_value = !is_array($value) ? (string)$value : $value;
2018-03-07 02:04:04 +01:00
if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
2018-03-03 18:09:12 +01:00
return true;
}
2018-03-07 02:04:04 +01:00
self::getApp()->setPConfigValue($uid, $cat, $k, $value);
2018-03-03 18:09:12 +01:00
// manage array value
$dbvalue = is_array($value) ? serialize($value) : $value;
$result = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
if (!$result) {
throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
}
return true;
}
public function delete($uid, $cat, $k)
{
2018-03-07 02:04:04 +01:00
self::getApp()->deletePConfigValue($uid, $cat, $k);
$result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
return $result;
}
2018-03-03 18:09:12 +01:00
}