- New Configuration (Config is now only holding the instance) - New PConfiguration (PConfig is now only holding the instance) - Config & PConfig-Adapter don't need "ConfigCache" anymore - DB-Connection is now outside App->reload() for better dependency-chainingpull/6641/head
@ -1,12 +1,19 @@ | |||
<?php | |||
namespace Friendica\Core\Config; | |||
namespace Friendica\Core\Config\Adapter; | |||
use Friendica\Database\DBA; | |||
abstract class AbstractDbaConfigAdapter | |||
{ | |||
/** @var bool */ | |||
protected $connected = true; | |||
public function __construct() | |||
{ | |||
$this->connected = DBA::connected(); | |||
} | |||
public function isConnected() | |||
{ | |||
return $this->connected; |
@ -0,0 +1,123 @@ | |||
<?php | |||
namespace Friendica\Core\Config\Adapter; | |||
use Friendica\Database\DBA; | |||
/** | |||
* JustInTime Configuration Adapter | |||
* | |||
* Default Config Adapter. Provides the best performance for pages loading few configuration variables. | |||
* | |||
* @author Hypolite Petovan <hypolite@mrpetovan.com> | |||
*/ | |||
class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter | |||
{ | |||
private $in_db; | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function load($cat = "config") | |||
{ | |||
$return = []; | |||
if (!$this->isConnected()) { | |||
return $return; | |||
} | |||
// We don't preload "system" anymore. | |||
// This reduces the number of database reads a lot. | |||
if ($cat === 'system') { | |||
return $return; | |||
} | |||
$configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]); | |||
while ($config = DBA::fetch($configs)) { | |||
$key = $config['k']; | |||
$return[$key] = $config['v']; | |||
$this->in_db[$cat][$key] = true; | |||
} | |||
DBA::close($configs); | |||
return [$cat => $config]; | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function get($cat, $key) | |||
{ | |||
if (!$this->isConnected()) { | |||
return '!<unset>!'; | |||
} | |||
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]); | |||
if (DBA::isResult($config)) { | |||
// manage array value | |||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']); | |||
$this->in_db[$cat][$key] = true; | |||
return $value; | |||
} else { | |||
$this->in_db[$cat][$key] = false; | |||
return '!<unset>!'; | |||
} | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function set($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($cat, $key); | |||
if (!isset($this->in_db[$cat])) { | |||
$this->in_db[$cat] = []; | |||
} | |||
if (!isset($this->in_db[$cat][$key])) { | |||
$this->in_db[$cat][$key] = false; | |||
} | |||
if (($stored === $dbvalue) && $this->in_db[$cat][$key]) { | |||
return true; | |||
} | |||
// manage array value | |||
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue); | |||
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true); | |||
$this->in_db[$cat][$key] = $result; | |||
return $result; | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function delete($cat, $key) | |||
{ | |||
if (!$this->isConnected()) { | |||
return false; | |||
} | |||
if (isset($this->cache[$cat][$key])) { | |||
unset($this->in_db[$cat][$key]); | |||
} | |||
$result = DBA::delete('config', ['cat' => $cat, 'k' => $key]); | |||
return $result; | |||
} | |||
} |
@ -0,0 +1,126 @@ | |||
<?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; | |||
} | |||
} |
@ -0,0 +1,104 @@ | |||
<?php | |||
namespace Friendica\Core\Config\Adapter; | |||
use Friendica\Database\DBA; | |||
/** | |||
* Preload Configuration Adapter | |||
* | |||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory. | |||
* | |||
* @author Hypolite Petovan <hypolite@mrpetovan.com> | |||
*/ | |||
class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter | |||
{ | |||
private $config_loaded = false; | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function load($cat = 'config') | |||
{ | |||
$return = []; | |||
if (!$this->isConnected()) { | |||
return $return; | |||
} | |||
if ($this->config_loaded) { | |||
return $return; | |||
} | |||
$configs = DBA::select('config', ['cat', 'v', 'k']); | |||
while ($config = DBA::fetch($configs)) { | |||
$return[$config['k']] = $config['v']; | |||
} | |||
DBA::close($configs); | |||
$this->config_loaded = true; | |||
return [$cat => $return]; | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function get($cat, $key) | |||
{ | |||
if (!$this->isConnected()) { | |||
return null; | |||
} | |||
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]); | |||
if (DBA::isResult($config)) { | |||
// manage array value | |||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']); | |||
return $value; | |||
} else { | |||
return '!<unset>!'; | |||
} | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function set($cat, $key, $value) | |||
{ | |||
if (!$this->isConnected()) { | |||
return false; | |||
} | |||
// 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; | |||
if ($this->get($cat, $key) === $compare_value) { | |||
return true; | |||
} | |||
// manage array value | |||
$dbvalue = is_array($value) ? serialize($value) : $value; | |||
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true); | |||
return $result; | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function delete($cat, $key) | |||
{ | |||
if (!$this->isConnected()) { | |||
return false; | |||
} | |||
$result = DBA::delete('config', ['cat' => $cat, 'k' => $key]); | |||
return $result; | |||
} | |||
} |
@ -0,0 +1,126 @@ | |||
<?php | |||
namespace Friendica\Core\Config\Adapter; | |||
use Friendica\Database\DBA; | |||
/** | |||
* Preload User Configuration Adapter | |||
* | |||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory. | |||
* | |||
* @author Hypolite Petovan <hypolite@mrpetovan.com> | |||
*/ | |||
class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter | |||
{ | |||
private $config_loaded = false; | |||
/** | |||
* @param int $uid The UID of the current user | |||
*/ | |||
public function __construct($uid = null) | |||
{ | |||
parent::__construct(); | |||
if (isset($uid)) { | |||
$this->load($uid, 'config'); | |||
} | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function load($uid, $cat) | |||
{ | |||
$return = []; | |||
if ($this->config_loaded) { | |||
return $return; | |||
} | |||
if (empty($uid)) { | |||
return $return; | |||
} | |||
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); | |||
while ($pconfig = DBA::fetch($pconfigs)) { | |||
$return[$pconfig['k']] = $pconfig['v']; | |||
} | |||
DBA::close($pconfigs); | |||
$this->config_loaded = true; | |||
return [$cat => $return]; | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function get($uid, $cat, $key) | |||
{ | |||
if (!$this->isConnected()) { | |||
return null; | |||
} | |||
if (!$this->config_loaded) { | |||
$this->load($uid, $cat); | |||
} | |||
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]); | |||
if (DBA::isResult($config)) { | |||
// manage array value | |||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']); | |||
return $value; | |||
} else { | |||
return '!<unset>!'; | |||
} | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function set($uid, $cat, $key, $value) | |||
{ | |||
if (!$this->isConnected()) { | |||
return false; | |||
} | |||
if (!$this->config_loaded) { | |||
$this->load($uid, $cat); | |||
} | |||
// 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; | |||
if ($this->get($uid, $cat, $key) === $compare_value) { | |||
return true; | |||
} | |||
// manage array value | |||
$dbvalue = is_array($value) ? serialize($value) : $value; | |||
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true); | |||
return $result; | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function delete($uid, $cat, $key) | |||
{ | |||
if (!$this->isConnected()) { | |||
return false; | |||
} | |||
if (!$this->config_loaded) { | |||
$this->load($uid, $cat); | |||
} | |||
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]); | |||
return $result; | |||
} | |||
} |
@ -0,0 +1,65 @@ | |||
<?php | |||
namespace Friendica\Core\Config\Cache; | |||
/** | |||
* The interface for a system-wide ConfigCache | |||
*/ | |||
interface IConfigCache | |||
{ | |||
/** | |||
* Tries to load the specified configuration array into the config array. | |||
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config. | |||
* | |||
* @param array $config | |||
* @param bool $overwrite Force value overwrite if the config key already exists | |||
*/ | |||
function load(array $config, $overwrite = false); | |||
/** | |||
* Gets a value from the config cache. | |||
* | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* | |||
* @return mixed Returns the value of the Config entry or '!<unset>!' if not set | |||
*/ | |||
function get($cat, $key = null); | |||
/** | |||
* Sets a value in the config cache. Accepts raw output from the config table | |||
* | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* @param mixed $value Value to set | |||
* | |||
* @return bool True, if the value is set | |||
*/ | |||
function set($cat, $key, $value); | |||
/** | |||
* Deletes a value from the config cache. | |||
* | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* | |||
* @return bool true, if deleted | |||
*/ | |||
function delete($cat, $key); | |||
/** | |||
* Checks if a value is set in the config cache. | |||
* | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* @return bool | |||
*/ | |||
function has($cat, $key = null); | |||
/** | |||
* Returns the whole configuration cache | |||
* | |||
* @return array | |||
*/ | |||
function getAll(); | |||
} |
@ -0,0 +1,70 @@ | |||
<?php | |||
namespace Friendica\Core\Config\Cache; | |||
/** | |||
* The interface for a user-specific config cache | |||
*/ | |||
interface IPConfigCache | |||
{ | |||
/** | |||
* Tries to load the specified configuration array into the user specific config array. | |||
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config. | |||
* | |||
* @param int $uid | |||
* @param array $config | |||
*/ | |||
function loadP($uid, array $config); | |||
/** | |||
* Retrieves a value from the user config cache | |||
* | |||
* @param int $uid User Id | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* | |||
* @return string The value of the config entry or '!<unset>!' if not set | |||
*/ | |||
function getP($uid, $cat, $key = null); | |||
/** | |||
* Sets a value in the user config cache | |||
* | |||
* Accepts raw output from the pconfig table | |||
* | |||
* @param int $uid User Id | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* @param mixed $value Value to set | |||
*/ | |||
function setP($uid, $cat, $key, $value); | |||
/** | |||
* Deletes a value from the user config cache | |||
* | |||
* @param int $uid User Id | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* | |||
* @return bool true, if deleted | |||
*/ | |||
function deleteP($uid, $cat, $key); | |||
/** | |||
* Checks if a value is set in the user config cache. | |||
* | |||
* @param int $uid User Id | |||
* @param string $cat Config category | |||
* @param string $key Config key | |||
* @return bool | |||
*/ | |||
function hasP($uid, $cat, $key = null); | |||
/** | |||
* Returns the whole configuration cache | |||
* | |||
* @return array | |||
*/ | |||
function getAll(); | |||
} |
@ -0,0 +1,154 @@ | |||
<?php | |||
namespace Friendica\Core\Config; | |||
/** | |||
* This class is responsible for all system-wide configuration values in Friendica | |||
* There are two types of storage | |||
* - The Config-Files (loaded into the FileCache @see Cache\IConfigCache ) | |||
* - The Config-DB-Table (per Config-DB-adapter @see Adapter\IConfigAdapter ) | |||
*/ | |||
class Configuration | |||
{ | |||
/** | |||
* @var Cache\IConfigCache | |||
*/ | |||
private $configCache; | |||
/** | |||
* @var Adapter\IConfigAdapter | |||
*/ | |||
private $configAdapter; | |||
/** | |||
* @param Cache\IConfigCache $configCache The configuration cache (based on the config-files) | |||
* @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend | |||
*/ | |||
public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter) | |||
{ | |||
$this->configCache = $configCache; | |||
$this->configAdapter = $configAdapter; | |||
$this->load(); | |||
} | |||
/** | |||
* Returns the Config Cache | |||
* | |||
* @return Cache\IConfigCache | |||
*/ | |||
public function getCache() | |||
{ | |||
return $this->configCache; | |||
} | |||
/** | |||
* @brief Loads all configuration values of family into a cached storage. | |||
* | |||
* All configuration values of the system are stored in the cache ( @see IConfigCache ) | |||
* | |||
* @param string $cat The category of the configuration value | |||
* | |||
* @return void | |||
*/ | |||
public function load($cat = 'config') | |||
{ | |||
// If not connected, do nothing | |||
if (!$this->configAdapter->isConnected()) { | |||
return; | |||
} | |||
// load the whole category out of the DB into the cache | |||
$this->configCache->load($this->configAdapter->load($cat), true); | |||
} | |||
/** | |||
* @brief Get a particular user's config variable given the category name | |||
* ($cat) and a $key. | |||
* | |||
* Get a particular config value from the given category ($cat) | |||
* and the $key from a cached storage either from the $this->configAdapter | |||
* (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ). | |||
* | |||
* @param string $cat The category of the configuration value | |||
* @param string $key The configuration key to query | |||
* @param mixed $default_value optional, The value to return if key is not set (default: null) | |||
* @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false) | |||
* | |||
* @return mixed Stored value or null if it does not exist | |||
*/ | |||
public function get($cat, $key, $default_value = null, $refresh = false) | |||
{ | |||
// Return the value of the cache if found and no refresh is forced | |||
if (!$refresh && $this->configCache->has($cat, $key)) { | |||
return $this->configCache->get($cat, $key); | |||
} | |||
// if we don't find the value in the cache and the adapter isn't ready, return the default value | |||
if (!$this->configAdapter->isConnected()) { | |||
return $default_value; | |||
} | |||
// load DB value to cache | |||
$dbvalue = $this->configAdapter->get($cat, $key); | |||
if ($dbvalue !== '!<unset>!') { | |||
$this->configCache->set($cat, $key, $dbvalue); | |||
return $dbvalue; | |||
} else { | |||
return $default_value; | |||
} | |||
} | |||
/** | |||
* @brief Sets a configuration value for system config | |||