Config FollowUp

- 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-chaining
pull/6641/head
Philipp Holzer 4 years ago
parent c36a0eabdb
commit eafcf3592d
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5

@ -33,7 +33,7 @@
*/
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Factory;
use Friendica\Util\BasePath;
use Friendica\Util\ExAuth;
@ -55,8 +55,11 @@ chdir($directory);
require dirname(__DIR__) . '/vendor/autoload.php';
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config);

@ -3,13 +3,16 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Factory;
use Friendica\Util\BasePath;
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('console', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config);

@ -9,6 +9,7 @@
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Factory;
@ -34,8 +35,11 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
require dirname(__DIR__) . '/vendor/autoload.php';
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('daemon', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config);
@ -151,7 +155,7 @@ if (!$foreground) {
file_put_contents($pidfile, $pid);
// We lose the database connection upon forking
$a->loadDatabase();
Factory\DBFactory::init($configCache, $_SERVER);
}
Config::set('system', 'worker_daemon_mode', true);

@ -7,6 +7,7 @@
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Core\Update;
use Friendica\Core\Worker;
use Friendica\Factory;
@ -32,8 +33,11 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
require dirname(__DIR__) . '/vendor/autoload.php';
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('worker', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config);

@ -5,7 +5,7 @@
*/
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache;
use Friendica\Factory;
use Friendica\Util\BasePath;
@ -16,8 +16,11 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/vendor/autoload.php';
$basedir = BasePath::create(__DIR__, $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$configLoader = new Cache\ConfigCacheLoader($basedir);
$configCache = Factory\ConfigFactory::createCache($configLoader);
Factory\DBFactory::init($configCache, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('index', $config);
$profiler = Factory\ProfilerFactory::create($logger, $config);

@ -8,8 +8,9 @@ use Detection\MobileDetect;
use DOMDocument;
use DOMXPath;
use Exception;
use Friendica\Core\Config\ConfigCache;
use Friendica\Core\Config\ConfigCacheLoader;
use Friendica\Core\Config\Cache\ConfigCacheLoader;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\Config\Configuration;
use Friendica\Database\DBA;
use Friendica\Factory\ConfigFactory;
use Friendica\Network\HTTPException\InternalServerErrorException;
@ -114,7 +115,7 @@ class App
private $logger;
/**
* @var ConfigCache The cached config
* @var Configuration The config
*/
private $config;
@ -126,11 +127,11 @@ class App
/**
* Returns the current config cache of this node
*
* @return ConfigCache
* @return IConfigCache
*/
public function getConfig()
public function getConfigCache()
{
return $this->config;
return $this->config->getCache();
}
/**
@ -195,14 +196,14 @@ class App
/**
* @brief App constructor.
*
* @param ConfigCache $config The Cached Config
* @param Configuration $config The Configuration
* @param LoggerInterface $logger Logger of this application
* @param Profiler $profiler The profiler of this application
* @param bool $isBackend Whether it is used for backend or frontend (Default true=backend)
*
* @throws Exception if the Basepath is not usable
*/
public function __construct(ConfigCache $config, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
public function __construct(Configuration $config, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
{
$this->config = $config;
$this->logger = $logger;
@ -358,21 +359,11 @@ class App
*/
public function reload()
{
Core\Config::init($this->config);
Core\PConfig::init($this->config);
$this->loadDatabase();
$this->getMode()->determine($this->basePath);
$this->determineURLPath();
if ($this->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
$adapterType = $this->config->get('system', 'config_adapter');
$adapter = ConfigFactory::createConfig($adapterType, $this->config);
Core\Config::setAdapter($adapter);
$adapterP = ConfigFactory::createPConfig($adapterType, $this->config);
Core\PConfig::setAdapter($adapterP);
Core\Config::load();
}
@ -383,7 +374,7 @@ class App
Core\Hook::loadHooks();
$loader = new ConfigCacheLoader($this->basePath);
Core\Hook::callAll('load_config', $loader);
$this->config->loadConfigArray($loader->loadCoreConfig('addon'), true);
$this->config->getCache()->load($loader->loadCoreConfig('addon'), true);
}
$this->loadDefaultTimezone();
@ -453,49 +444,6 @@ class App
}
}
public function loadDatabase()
{
if (DBA::connected()) {
return;
}
$db_host = $this->config->get('database', 'hostname');
$db_user = $this->config->get('database', 'username');
$db_pass = $this->config->get('database', 'password');
$db_data = $this->config->get('database', 'database');
$charset = $this->config->get('database', 'charset');
// Use environment variables for mysql if they are set beforehand
if (!empty(getenv('MYSQL_HOST'))
&& !empty(getenv('MYSQL_USERNAME') || !empty(getenv('MYSQL_USER')))
&& getenv('MYSQL_PASSWORD') !== false
&& !empty(getenv('MYSQL_DATABASE')))
{
$db_host = getenv('MYSQL_HOST');
if (!empty(getenv('MYSQL_PORT'))) {
$db_host .= ':' . getenv('MYSQL_PORT');
}
if (!empty(getenv('MYSQL_USERNAME'))) {
$db_user = getenv('MYSQL_USERNAME');
} else {
$db_user = getenv('MYSQL_USER');
}
$db_pass = (string) getenv('MYSQL_PASSWORD');
$db_data = getenv('MYSQL_DATABASE');
}
$stamp1 = microtime(true);
if (DBA::connect($this->config, $this->profiler, $db_host, $db_user, $db_pass, $db_data, $charset)) {
// Loads DB_UPDATE_VERSION constant
Database\DBStructure::definition($this->basePath, false);
}
unset($db_host, $db_user, $db_pass, $db_data, $charset);
$this->profiler->saveTimestamp($stamp1, 'network', Core\System::callstack());
}
public function getScheme()
{
return $this->scheme;

@ -22,116 +22,76 @@ use Friendica\Core\Config\IConfigCache;
class Config
{
/**
* @var Config\IConfigAdapter|null
* @var Config\Configuration
*/
private static $adapter;
private static $config;
/**
* @var Config\IConfigCache
*/
private static $cache;
/**
* Initialize the config with only the cache
*
* @param Config\IConfigCache $cache The configuration cache
*/
public static function init(Config\IConfigCache $cache)
{
self::$cache = $cache;
}
/**
* Add the adapter for DB-backend
* Initialize the config
*
* @param Config\IConfigAdapter $adapter
* @param Config\Configuration $config
*/
public static function setAdapter(Config\IConfigAdapter $adapter)
public static function init(Config\Configuration $config)
{
self::$adapter = $adapter;
self::$config = $config;
}
/**
* @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 $family The category of the configuration value
* @param string $cat The category of the configuration value
*
* @return void
*/
public static function load($family = "config")
public static function load($cat = "config")
{
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
return;
}
self::$adapter->load($family);
self::$config->load($cat);
}
/**
* @brief Get a particular user's config variable given the category name
* ($family) and a key.
*
* Get a particular config value from the given category ($family)
* and the $key from a cached storage either from the self::$adapter
* (@see IConfigAdapter ) or from the static::$cache (@see IConfigCache ).
*
* @param string $family The category of the configuration value
* @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 static function get($family, $key, $default_value = null, $refresh = false)
public static function get($cat, $key, $default_value = null, $refresh = false)
{
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
return self::$cache->get($family, $key, $default_value);
}
return self::$adapter->get($family, $key, $default_value, $refresh);
return self::$config->get($cat, $key, $default_value, $refresh);
}
/**
* @brief Sets a configuration value for system config
*
* Stores a config value ($value) in the category ($family) under the key ($key)
* Stores a config value ($value) in the category ($cat) under the key ($key)
*
* Note: Please do not store booleans - convert to 0/1 integer values!
*
* @param string $family The category of the configuration value
* @param string $cat The category of the configuration value
* @param string $key The configuration key to set
* @param mixed $value The value to store
*
* @return bool Operation success
*/
public static function set($family, $key, $value)
public static function set($cat, $key, $value)
{
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
return self::$cache->set($family, $key, $value);
}
return self::$adapter->set($family, $key, $value);
return self::$config->set($cat, $key, $value);
}
/**
* @brief Deletes the given key from the system configuration.
*
* Removes the configured value from the stored cache in self::$config
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
*
* @param string $family The category of the configuration value
* @param string $cat The category of the configuration value
* @param string $key The configuration key to delete
*
* @return mixed
* @return bool
*/
public static function delete($family, $key)
public static function delete($cat, $key)
{
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
self::$cache->delete($family, $key);
}
return self::$adapter->delete($family, $key);
return self::$config->delete($cat, $key);
}
}

@ -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;

@ -1,6 +1,6 @@
<?php
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Adapter;
/**
*
@ -13,7 +13,7 @@ interface IConfigAdapter
*
* @param string $cat The category of the configuration values to load
*
* @return void
* @return array
*/
public function load($cat = "config");
@ -21,14 +21,12 @@ interface IConfigAdapter
* Get a particular user's config variable given the category name
* ($family) and a key.
*
* @param string $cat The category of the configuration value
* @param string $k 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)
* @param string $cat The category of the configuration value
* @param string $key The configuration key to query
*
* @return mixed Stored value or null if it does not exist
* @return mixed Stored value or "!<unset>!" if it does not exist
*/
public function get($cat, $k, $default_value = null, $refresh = false);
public function get($cat, $key);
/**
* Stores a config value ($value) in the category ($family) under the key ($key)
@ -37,23 +35,23 @@ interface IConfigAdapter
* Note: Please do not store booleans - convert to 0/1 integer values!
*
* @param string $cat The category of the configuration value
* @param string $k The configuration key to set
* @param string $key The configuration key to set
* @param mixed $value The value to store
*
* @return bool Operation success
*/
public function set($cat, $k, $value);
public function set($cat, $key, $value);
/**
* Removes the configured value from the stored cache
* and removes it from the database.
*
* @param string $cat The category of the configuration value
* @param string $k The configuration key to delete
* @param string $key The configuration key to delete
*
* @return mixed
*/
public function delete($cat, $k);
public function delete($cat, $key);
/**
* Checks, if the current adapter is connected to the backend

@ -6,7 +6,7 @@
* and open the template in the editor.
*/
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Adapter;
/**
*
@ -20,7 +20,7 @@ interface IPConfigAdapter
* @param string $uid The user_id
* @param string $cat The category of the configuration value
*
* @return void
* @return array
*/
public function load($uid, $cat);
@ -30,13 +30,11 @@ interface IPConfigAdapter
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
* @param string $k 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)
* @param string $key The configuration key to query
*
* @return mixed Stored value or null if it does not exist
* @return mixed Stored value or "!<unset>!" if it does not exist
*/
public function get($uid, $cat, $k, $default_value = null, $refresh = false);
public function get($uid, $cat, $key);
/**
* Stores a config value ($value) in the category ($family) under the key ($key)
@ -46,12 +44,12 @@ interface IPConfigAdapter
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
* @param string $k The configuration key to set
* @param string $key The configuration key to set
* @param string $value The value to store
*
* @return bool Operation success
*/
public function set($uid, $cat, $k, $value);
public function set($uid, $cat, $key, $value);
/**
* Removes the configured value from the stored cache
@ -59,9 +57,16 @@ interface IPConfigAdapter
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
* @param string $k The configuration key to delete
* @param string $key The configuration key to delete
*
* @return mixed
* @return bool
*/
public function delete($uid, $cat, $k);
public function delete($uid, $cat, $key);
/**
* Checks, if the current adapter is connected to the backend
*
* @return bool
*/
public function isConnected();
}

@ -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;
}
}

@ -1,6 +1,6 @@
<?php
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Cache;
/**
* The Friendica config cache for the application
@ -11,6 +11,9 @@ namespace Friendica\Core\Config;
*/
class ConfigCache implements IConfigCache, IPConfigCache
{
/**
* @var array
*/
private $config;
/**
@ -18,24 +21,28 @@ class ConfigCache implements IConfigCache, IPConfigCache
*/
public function __construct(array $config = [])
{
$this->loadConfigArray($config);
$this->load($config);
}
/**
* Tries to load the specified configuration array into the App->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
* {@inheritdoc}
*/
public function loadConfigArray(array $config, $overwrite = false)
public function load(array $config, $overwrite = false)
{
foreach ($config as $category => $values) {
foreach ($values as $key => $value) {
if ($overwrite) {
$this->set($category, $key, $value);
} else {
$this->setDefault($category, $key, $value);
$categories = array_keys($config);
foreach ($categories as $category) {
if (isset($config[$category]) && is_array($config[$category])) {
$keys = array_keys($config[$category]);
foreach ($keys as $key) {
if (isset($config[$category][$key])) {
if ($overwrite) {
$this->set($category, $key, $config[$category][$key]);
} else {
$this->setDefault($category, $key, $config[$category][$key]);
}
}
}
}
}
@ -44,23 +51,22 @@ class ConfigCache implements IConfigCache, IPConfigCache
/**
* {@inheritdoc}
*/
public function get($cat, $key = null, $default = null)
public function get($cat, $key = null)
{
$return = $default;
if ($cat === 'config') {
if (isset($this->config[$key])) {
$return = $this->config[$key];
}
if (isset($this->config[$cat][$key])) {
return $this->config[$cat][$key];
} else {
if (isset($this->config[$cat][$key])) {
$return = $this->config[$cat][$key];
} elseif ($key == null && isset($this->config[$cat])) {
$return = $this->config[$cat];
}
return '!<unset>!';
}
}
return $return;
/**
* {@inheritdoc}
*/
public function has($cat, $key = null)
{
return isset($this->config[$cat][$key])
&& $this->config[$cat][$key] !== '!<unset>!';
}
/**
@ -85,52 +91,62 @@ class ConfigCache implements IConfigCache, IPConfigCache
// Only arrays are serialized in database, so we have to unserialize sparingly
$value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
if ($cat === 'config') {
$this->config[$key] = $value;
} else {
if (!isset($this->config[$cat])) {
$this->config[$cat] = [];
}
$this->config[$cat][$key] = $value;
if (!isset($this->config[$cat])) {
$this->config[$cat] = [];
}
$this->config[$cat][$key] = $value;
return true;
}
/**
* {@inheritdoc}
*/
public function hasP($uid, $cat, $key = null)
{
return isset($this->config[$uid][$cat][$key])
&& $this->config[$uid][$cat][$key] !== '!<unset>!';
}
/**
* {@inheritdoc}
*/
public function delete($cat, $key)
{
if ($cat === 'config') {
if (isset($this->config[$key])) {
unset($this->config[$key]);
if (isset($this->config[$cat][$key])) {
unset($this->config[$cat][$key]);
if (count($this->config[$cat]) == 0) {
unset($this->config[$cat]);
}
return true;
} else {
if (isset($this->config[$cat][$key])) {
unset($this->config[$cat][$key]);
if (count($this->config[$cat]) == 0) {
unset($this->config[$cat]);
}
}
return false;
}
}
/**
* {@inheritdoc}
*/
public function getP($uid, $cat, $key = null, $default = null)
public function loadP($uid, array $config)
{
$return = $default;
foreach ($config as $category => $values) {
foreach ($values as $key => $value) {
$this->setP($uid, $category, $key, $value);
}
}
}
/**
* {@inheritdoc}
*/
public function getP($uid, $cat, $key = null)
{
if (isset($this->config[$uid][$cat][$key])) {
$return = $this->config[$uid][$cat][$key];
} elseif ($key === null && isset($this->config[$uid][$cat])) {
$return = $this->config[$uid][$cat];
return $this->config[$uid][$cat][$key];
} else {
return '!<unset>!';
}
return $return;
}
/**
@ -145,15 +161,13 @@ class ConfigCache implements IConfigCache, IPConfigCache
$this->config[$uid] = [];
}
if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
if (!isset($this->config[$uid][$cat])) {
$this->config[$uid][$cat] = [];
}
if ($key === null) {
$this->config[$uid][$cat] = $value;
} else {
$this->config[$uid][$cat][$key] = $value;
}
$this->config[$uid][$cat][$key] = $value;
return true;
}
/**
@ -169,6 +183,10 @@ class ConfigCache implements IConfigCache, IPConfigCache
unset($this->config[$uid]);
}
}
return true;
} else {
return false;
}
}

@ -1,6 +1,6 @@
<?php
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Cache;
use Friendica\Core\Addon;
@ -40,13 +40,13 @@ class ConfigCacheLoader
// Setting at least the basepath we know
$config->set('system', 'basepath', $this->baseDir);
$config->loadConfigArray($this->loadCoreConfig('defaults'));
$config->loadConfigArray($this->loadCoreConfig('settings'));
$config->load($this->loadCoreConfig('defaults'));
$config->load($this->loadCoreConfig('settings'));
$config->loadConfigArray($this->loadLegacyConfig('htpreconfig'), true);
$config->loadConfigArray($this->loadLegacyConfig('htconfig'), true);
$config->load($this->loadLegacyConfig('htpreconfig'), true);
$config->load($this->loadLegacyConfig('htconfig'), true);
$config->loadConfigArray($this->loadCoreConfig('local'), true);
$config->load($this->loadCoreConfig('local'), true);
}
/**

@ -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
*
* Stores a config value ($value) in the category ($cat) under the key ($key)
*
* Note: Please do not store booleans - convert to 0/1 integer values!
*
* @param string $cat The category of the configuration value
* @param string $key The configuration key to set
* @param mixed $value The value to store
*
* @return bool Operation success
*/
public function set($cat, $key, $value)
{
// set the cache first
$cached = $this->configCache->set($cat, $key, $value);
// If there is no connected adapter, we're finished
if (!$this->configAdapter->isConnected()) {
return $cached;
}
$stored = $this->configAdapter->set($cat, $key, $value);
return $cached && $stored;
}
/**
* @brief Deletes the given key from the system configuration.
*
* Removes the configured value from the stored cache in $this->configCache
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
*
* @param string $cat The category of the configuration value
* @param string $key The configuration key to delete
*
* @return bool
*/
public function delete($cat, $key)
{
$cacheRemoved = $this->configCache->delete($cat, $key);
if (!$this->configAdapter->isConnected()) {
return $cacheRemoved;
}
$storeRemoved = $this->configAdapter->delete($cat, $key);
return $cacheRemoved || $storeRemoved;
}
}

@ -1,39 +0,0 @@
<?php
namespace Friendica\Core\Config;
/**
* The interface for a system-wide ConfigCache
*/
interface IConfigCache
{
/**
* @param string $cat Config category
* @param string $key Config key
* @param mixed $default Default value if it isn't set
*
* @return mixed Returns the value of the Config entry
*/
function get($cat, $key = null, $default = 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
*/
function delete($cat, $key);
function getAll();
}

@ -1,44 +0,0 @@
<?php
namespace Friendica\Core\Config;
/**
* The interface for a user-specific config cache
*/
interface IPConfigCache
{
/**
* Retrieves a value from the user config cache
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $key Config key
* @param mixed $default Default value if key isn't set
*
* @return string The value of the config entry
*/
function getP($uid, $cat, $key = null, $default = 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
*/
function deleteP($uid, $cat, $key);
function getAll();
}

@ -1,172 +0,0 @@
<?php
namespace Friendica\Core\Config;