1
0
Fork 0

Merge remote-tracking branch 'upstream/2019.03-RC' into worker2

This commit is contained in:
Michael 2019-02-27 06:41:14 +00:00
commit fb371e1048
178 changed files with 18279 additions and 15676 deletions

View file

@ -219,7 +219,7 @@ class Addon extends BaseObject
$stamp1 = microtime(true);
$f = file_get_contents("addon/$addon/$addon.php");
$a->saveTimestamp($stamp1, "file");
$a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
$r = preg_match("|/\*.*\*/|msU", $f, $m);

View file

@ -63,7 +63,7 @@ class Cache extends \Friendica\BaseObject
$return = self::getDriver()->getAllKeys($prefix);
self::getApp()->saveTimestamp($time, 'cache');
self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack());
return $return;
}
@ -82,7 +82,7 @@ class Cache extends \Friendica\BaseObject
$return = self::getDriver()->get($key);
self::getApp()->saveTimestamp($time, 'cache');
self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack());
return $return;
}
@ -105,7 +105,7 @@ class Cache extends \Friendica\BaseObject
$return = self::getDriver()->set($key, $value, $duration);
self::getApp()->saveTimestamp($time, 'cache_write');
self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack());
return $return;
}
@ -124,7 +124,7 @@ class Cache extends \Friendica\BaseObject
$return = self::getDriver()->delete($key);
self::getApp()->saveTimestamp($time, 'cache_write');
self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack());
return $return;
}

View file

@ -8,10 +8,6 @@
*/
namespace Friendica\Core;
use Friendica\Core\Config\ConfigCache;
use Friendica\Core\Config\IConfigAdapter;
use Friendica\Core\Config\IConfigCache;
/**
* @brief Arbitrary system configuration storage
*
@ -22,116 +18,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
* Initialize the config
*
* @param Config\IConfigCache $cache The configuration cache
* @param Config\Configuration $config
*/
public static function init(Config\IConfigCache $cache)
public static function init(Config\Configuration $config)
{
self::$cache = $cache;
}
/**
* Add the adapter for DB-backend
*
* @param Config\IConfigAdapter $adapter
*/
public static function setAdapter(Config\IConfigAdapter $adapter)
{
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);
}
}

View file

@ -1,14 +0,0 @@
<?php
namespace Friendica\Core\Config;
abstract class AbstractDbaConfigAdapter
{
/** @var bool */
protected $connected = true;
public function isConnected()
{
return $this->connected;
}
}

View file

@ -0,0 +1,83 @@
<?php
namespace Friendica\Core\Config\Adapter;
use Friendica\Database\DBA;
abstract class AbstractDbaConfigAdapter
{
/**
* The connection state of the adapter
*
* @var bool
*/
protected $connected = true;
public function __construct()
{
$this->connected = DBA::connected();
}
/**
* Checks if the adapter is currently connected
*
* @return bool
*/
public function isConnected()
{
return $this->connected;
}
/**
* Formats a DB value to a config value
* - null = The db-value isn't set
* - bool = The db-value is either '0' or '1'
* - array = The db-value is a serialized array
* - string = The db-value is a string
*
* Keep in mind that there aren't any numeric/integer config values in the database
*
* @param null|string $value
*
* @return null|array|string
*/
protected function toConfigValue($value)
{
if (!isset($value)) {
return null;
}
switch (true) {
// manage array value
case preg_match("|^a:[0-9]+:{.*}$|s", $value):
return unserialize($value);
default:
return $value;
}
}
/**
* Formats a config value to a DB value (string)
*
* @param mixed $value
*
* @return string
*/
protected function toDbValue($value)
{
// if not set, save an empty string
if (!isset($value)) {
return '';
}
switch (true) {
// manage arrays
case is_array($value):
return serialize($value);
default:
return (string)$value;
}
}
}

View file

@ -0,0 +1,73 @@
<?php
namespace Friendica\Core\Config\Adapter;
/**
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
interface IConfigAdapter
{
/**
* Loads all configuration values and returns the loaded category as an array.
*
* @param string $cat The category of the configuration values to load
*
* @return array
*/
public function load($cat = "config");
/**
* Get a particular system-wide config variable given the category name
* ($family) and a key.
*
* Note: Boolean variables are defined as 0/1 in the database
*
* @param string $cat The category of the configuration value
* @param string $key The configuration key to query
*
* @return null|mixed Stored value or null if it does not exist
*/
public function get($cat, $key);
/**
* Stores a config value ($value) in the category ($family) 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);
/**
* 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 $key The configuration key to delete
*
* @return bool Operation success
*/
public function delete($cat, $key);
/**
* Checks, if the current adapter is connected to the backend
*
* @return bool
*/
public function isConnected();
/**
* Checks, if a config key ($key) in the category ($cat) is already loaded.
*
* @param string $cat The configuration category
* @param string $key The configuration key
*
* @return bool
*/
public function isLoaded($cat, $key);
}

View file

@ -6,7 +6,7 @@
* and open the template in the editor.
*/
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Adapter;
/**
*
@ -15,12 +15,12 @@ namespace Friendica\Core\Config;
interface IPConfigAdapter
{
/**
* Loads all configuration values of a user's config family into a cached storage.
* Loads all configuration values of a user's config family and returns the loaded category as an array.
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
*
* @return void
* @return array
*/
public function load($uid, $cat);
@ -28,15 +28,15 @@ interface IPConfigAdapter
* Get a particular user's config variable given the category name
* ($family) and a key.
*
* Note: Boolean variables are defined as 0/1 in the database
*
* @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 null|mixed Stored value or null 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 +46,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 +59,27 @@ 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 Operation success
*/
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();
/**
* Checks, if a config key ($key) in the category ($cat) is already loaded for the user_id $uid.
*
* @param string $uid The user_id
* @param string $cat The configuration category
* @param string $key The configuration key
*
* @return bool
*/
public function isLoaded($uid, $cat, $key);
}

View file

@ -0,0 +1,145 @@
<?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'];
$value = $this->toConfigValue($config['v']);
// The value was in the db, so don't check it again (unless you have to)
$this->in_db[$cat][$key] = true;
// just save it in case it is set
if (isset($value)) {
$return[$key] = $value;
}
}
DBA::close($configs);
return [$cat => $return];
}
/**
* {@inheritdoc}
*
* @param bool $mark if true, mark the selection of the current cat/key pair
*/
public function get($cat, $key, $mark = true)
{
if (!$this->isConnected()) {
return null;
}
// The value got checked, so mark it to avoid checking it over and over again
if ($mark) {
$this->in_db[$cat][$key] = true;
}
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
if (DBA::isResult($config)) {
$value = $this->toConfigValue($config['v']);
// just return it in case it is set
if (isset($value)) {
return $value;
}
}
return null;
}
/**
* {@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.
$compare_value = (!is_array($value) ? (string)$value : $value);
$stored_value = $this->get($cat, $key, false);
if (!isset($this->in_db[$cat])) {
$this->in_db[$cat] = [];
}
if (!isset($this->in_db[$cat][$key])) {
$this->in_db[$cat][$key] = false;
}
if (isset($stored_value) && ($stored_value === $compare_value) && $this->in_db[$cat][$key]) {
return true;
}
$dbvalue = $this->toDbValue($value);
$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;
}
/**
* {@inheritdoc}
*/
public function isLoaded($cat, $key)
{
if (!$this->isConnected()) {
return false;
}
return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];
}
}

View file

@ -0,0 +1,145 @@
<?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'];
$value = $this->toConfigValue($pconfig['v']);
// The value was in the db, so don't check it again (unless you have to)
$this->in_db[$uid][$cat][$key] = true;
if (isset($value)) {
$return[$key] = $value;
}
}
} else if ($cat != 'config') {
// Negative caching
$return = null;
}
DBA::close($pconfigs);
return [$cat => $return];
}
/**
* {@inheritdoc}
*
* @param bool $mark if true, mark the selection of the current cat/key pair
*/
public function get($uid, $cat, $key, $mark = true)
{
if (!$this->isConnected()) {
return null;
}
// The value was in the db, so don't check it again (unless you have to)
if ($mark) {
$this->in_db[$uid][$cat][$key] = true;
}
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
if (DBA::isResult($pconfig)) {
$value = $this->toConfigValue($pconfig['v']);
if (isset($value)) {
return $value;
}
}
$this->in_db[$uid][$cat][$key] = false;
return null;
}
/**
* {@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.
$compare_value = (!is_array($value) ? (string)$value : $value);
$stored_value = $this->get($uid, $cat, $key, false);
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 (isset($stored_value) && ($stored_value === $compare_value) && $this->in_db[$uid][$cat][$key]) {
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);
$this->in_db[$uid][$cat][$key] = $result;
return $result;
}
/**
* {@inheritdoc}
*/
public function delete($uid, $cat, $key)
{
if (!$this->isConnected()) {
return false;
}
if (isset($this->in_db[$uid][$cat][$key])) {
unset($this->in_db[$uid][$cat][$key]);
}
return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
}
/**
* {@inheritdoc}
*/
public function isLoaded($uid, $cat, $key)
{
if (!$this->isConnected()) {
return false;
}
return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];
}
}

View file

@ -0,0 +1,115 @@
<?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)) {
$value = $this->toConfigValue($config['v']);
if (isset($value)) {
$return[$config['cat']][$config['k']] = $value;
}
}
DBA::close($configs);
$this->config_loaded = true;
return $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)) {
$value = $this->toConfigValue($config['v']);
if (isset($value)) {
return $value;
}
}
return null;
}
/**
* {@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;
$stored_value = $this->get($cat, $key);
if (isset($stored_value) && $stored_value === $compare_value) {
return true;
}
$dbvalue = $this->toDbValue($value);
return DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
}
/**
* {@inheritdoc}
*/
public function delete($cat, $key)
{
if (!$this->isConnected()) {
return false;
}
return DBA::delete('config', ['cat' => $cat, 'k' => $key]);
}
/**
* {@inheritdoc}
*/
public function isLoaded($cat, $key)
{
if (!$this->isConnected()) {
return false;
}
return $this->config_loaded;
}
}

View file

@ -0,0 +1,142 @@
<?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
{
/**
* @var array true if config for user is loaded
*/
private $config_loaded;
/**
* @param int $uid The UID of the current user
*/
public function __construct($uid = null)
{
parent::__construct();
$this->config_loaded = [];
if (isset($uid)) {
$this->load($uid, 'config');
}
}
/**
* {@inheritdoc}
*/
public function load($uid, $cat)
{
$return = [];
if (empty($uid)) {
return $return;
}
if (!$this->isLoaded($uid, $cat, null)) {
return $return;
}
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
while ($pconfig = DBA::fetch($pconfigs)) {
$value = $this->toConfigValue($pconfig['v']);
if (isset($value)) {
$return[$pconfig['cat']][$pconfig['k']] = $value;
}
}
DBA::close($pconfigs);
$this->config_loaded[$uid] = true;
return $return;
}
/**
* {@inheritdoc}
*/
public function get($uid, $cat, $key)
{
if (!$this->isConnected()) {
return null;
}
if (!$this->isLoaded($uid, $cat, $key)) {
$this->load($uid, $cat);
}
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
if (DBA::isResult($config)) {
$value = $this->toConfigValue($config['v']);
if (isset($value)) {
return $value;
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function set($uid, $cat, $key, $value)
{
if (!$this->isConnected()) {
return false;
}
if (!$this->isLoaded($uid, $cat, $key)) {
$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;
$stored_value = $this->get($uid, $cat, $key);
if (isset($stored_value) && $stored_value === $compare_value) {
return true;
}
$dbvalue = $this->toDbValue($value);
return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
}
/**
* {@inheritdoc}
*/
public function delete($uid, $cat, $key)
{
if (!$this->isConnected()) {
return false;
}
if (!$this->isLoaded($uid, $cat, $key)) {
$this->load($uid, $cat);
}
return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
}
/**
* {@inheritdoc}
*/
public function isLoaded($uid, $cat, $key)
{
if (!$this->isConnected()) {
return false;
}
return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];
}
}

View file

@ -0,0 +1,191 @@
<?php
namespace Friendica\Core\Config\Cache;
/**
* The Friendica config cache for the application
* Initial, all *.config.php files are loaded into this cache with the
* ConfigCacheLoader ( @see ConfigCacheLoader )
*/
class ConfigCache implements IConfigCache, IPConfigCache
{
/**
* @var array
*/
private $config;
/**
* @param array $config A initial config array
*/
public function __construct(array $config = [])
{
$this->load($config);
}
/**
* {@inheritdoc}
*/
public function load(array $config, $overwrite = false)
{
$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) {
$value = $config[$category][$key];
if (isset($value)) {
if ($overwrite) {
$this->set($category, $key, $value);
} else {
$this->setDefault($category, $key, $value);
}
}
}
}
}
}
/**
* {@inheritdoc}
*/
public function get($cat, $key = null)
{
if (isset($this->config[$cat][$key])) {
return $this->config[$cat][$key];
} elseif (!isset($key) && isset($this->config[$cat])) {
return $this->config[$cat];
} else {
return null;
}
}
/**
* Sets a default value in the config cache. Ignores already existing keys.
*
* @param string $cat Config category
* @param string $k Config key
* @param mixed $v Default value to set
*/
private function setDefault($cat, $k, $v)
{
if (!isset($this->config[$cat][$k])) {
$this->set($cat, $k, $v);
}
}
/**
* {@inheritdoc}
*/
public function set($cat, $key, $value)
{
if (!isset($this->config[$cat])) {
$this->config[$cat] = [];
}
$this->config[$cat][$key] = $value;
return true;
}
/**
* {@inheritdoc}
*/
public function delete($cat, $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 {
return false;
}
}
/**
* {@inheritdoc}
*/
public function loadP($uid, array $config)
{
$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) {
$value = $config[$category][$key];
if (isset($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 (!isset($key) && isset($this->config[$uid][$cat])) {
return $this->config[$uid][$cat];
} else {
return null;
}
}
/**
* {@inheritdoc}
*/
public function setP($uid, $cat, $key, $value)
{
if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
$this->config[$uid] = [];
}
if (!isset($this->config[$uid][$cat])) {
$this->config[$uid][$cat] = [];
}
$this->config[$uid][$cat][$key] = $value;
return true;
}
/**
* {@inheritdoc}
*/
public function deleteP($uid, $cat, $key)
{
if (isset($this->config[$uid][$cat][$key])) {
unset($this->config[$uid][$cat][$key]);
if (count($this->config[$uid][$cat]) == 0) {
unset($this->config[$uid][$cat]);
if (count($this->config[$uid]) == 0) {
unset($this->config[$uid]);
}
}
return true;
} else {
return false;
}
}
/**
* Returns the whole configuration
*
* @return array The configuration
*/
public function getAll()
{
return $this->config;
}
}

View file

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

View file

@ -0,0 +1,56 @@
<?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 null|mixed Returns the value of the Config entry or null 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);
/**
* Returns the whole configuration cache
*
* @return array
*/
function getAll();
}

View file

@ -1,23 +1,31 @@
<?php
namespace Friendica\Core\Config;
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
* @param mixed $default Default value if key isn't set
*
* @return string The value of the config entry
* @return null|string The value of the config entry or null if not set
*/
function getP($uid, $cat, $key = null, $default = null);
function getP($uid, $cat, $key = null);
/**
* Sets a value in the user config cache
@ -37,8 +45,15 @@ interface IPConfigCache
* @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);
/**
* Returns the whole configuration cache
*
* @return array
*/
function getAll();
}

View file

@ -1,184 +0,0 @@
<?php
namespace Friendica\Core\Config;
/**
* The Friendica config cache for the application
* Initial, all *.config.php files are loaded into this cache with the
* ConfigCacheLoader ( @see ConfigCacheLoader )
*
* Is used for further caching operations too (depending on the ConfigAdapter )
*/
class ConfigCache implements IConfigCache, IPConfigCache
{
private $config;
/**
* @param array $config A initial config array
*/
public function __construct(array $config = [])
{
$this->loadConfigArray($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
*/
public function loadConfigArray(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);
}
}
}
}
/**
* {@inheritdoc}
*/
public function get($cat, $key = null, $default = null)
{
$return = $default;
if ($cat === 'config') {
if (isset($this->config[$key])) {
$return = $this->config[$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 $return;
}
/**
* Sets a default value in the config cache. Ignores already existing keys.
*
* @param string $cat Config category
* @param string $k Config key
* @param mixed $v Default value to set
*/
private function setDefault($cat, $k, $v)
{
if (!isset($this->config[$cat][$k])) {
$this->set($cat, $k, $v);
}
}
/**
* {@inheritdoc}
*/
public function set($cat, $key, $value)
{
// 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;
}
return true;
}
/**
* {@inheritdoc}
*/
public function delete($cat, $key)
{
if ($cat === 'config') {
if (isset($this->config[$key])) {
unset($this->config[$key]);
}
} else {
if (isset($this->config[$cat][$key])) {
unset($this->config[$cat][$key]);
if (count($this->config[$cat]) == 0) {
unset($this->config[$cat]);
}
}
}
}
/**
* {@inheritdoc}
*/
public function getP($uid, $cat, $key = null, $default = null)
{
$return = $default;
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 $return;
}
/**
* {@inheritdoc}
*/
public function setP($uid, $cat, $key, $value)
{
// 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 (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
$this->config[$uid] = [];
}
if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
$this->config[$uid][$cat] = [];
}
if ($key === null) {
$this->config[$uid][$cat] = $value;
} else {
$this->config[$uid][$cat][$key] = $value;
}
}
/**
* {@inheritdoc}
*/
public function deleteP($uid, $cat, $key)
{
if (isset($this->config[$uid][$cat][$key])) {
unset($this->config[$uid][$cat][$key]);
if (count($this->config[$uid][$cat]) == 0) {
unset($this->config[$uid][$cat]);
if (count($this->config[$uid]) == 0) {
unset($this->config[$uid]);
}
}
}
}
/**
* Returns the whole configuration
*
* @return array The configuration
*/
public function getAll()
{
return $this->config;
}
}

View file

@ -0,0 +1,152 @@
<?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)
{
// if the value isn't loaded or refresh is needed, load it to the cache
if ($this->configAdapter->isConnected() &&
(!$this->configAdapter->isLoaded($cat, $key) ||
$refresh)) {
$dbvalue = $this->configAdapter->get($cat, $key);
if (isset($dbvalue)) {
$this->configCache->set($cat, $key, $dbvalue);
return $dbvalue;
}
}
// use the config cache for return
$result = $this->configCache->get($cat, $key);
return (isset($result)) ? $result : $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;
}
}

View file

@ -1,64 +0,0 @@
<?php
namespace Friendica\Core\Config;
/**
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
interface IConfigAdapter
{
/**
* Loads all configuration values into a cached storage.
*
* @param string $cat The category of the configuration values to load
*
* @return void
*/
public function load($cat = "config");
/**
* 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)
*
* @return mixed Stored value or null if it does not exist
*/
public function get($cat, $k, $default_value = null, $refresh = false);
/**
* Stores a config value ($value) in the category ($family) under the key ($key)
* for the user_id $uid.
*
* 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 mixed $value The value to store
*
* @return bool Operation success
*/
public function set($cat, $k, $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
*
* @return mixed
*/
public function delete($cat, $k);
/**
* Checks, if the current adapter is connected to the backend
*
* @return bool
*/
public function isConnected();
}

View file

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

View file

@ -1,172 +0,0 @@
<?php
namespace Friendica\Core\Config;
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 $cache;
private $in_db;
/**
* @var IConfigCache The config cache of this driver
*/
private $configCache;
/**
* @param IConfigCache $configCache The config cache of this driver
*/
public function __construct(IConfigCache $configCache)
{
$this->configCache = $configCache;
$this->connected = DBA::connected();
}
/**
* {@inheritdoc}
*/
public function load($cat = "config")
{
if (!$this->isConnected()) {
return;
}
// We don't preload "system" anymore.
// This reduces the number of database reads a lot.
if ($cat === 'system') {
return;
}
$configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
while ($config = DBA::fetch($configs)) {
$k = $config['k'];
$this->configCache->set($cat, $k, $config['v']);
if ($cat !== 'config') {
$this->cache[$cat][$k] = $config['v'];
$this->in_db[$cat][$k] = true;
}
}
DBA::close($configs);
}
/**
* {@inheritdoc}
*/
public function get($cat, $k, $default_value = null, $refresh = false)
{
if (!$this->isConnected()) {
return $default_value;
}
if (!$refresh) {
// Do we have the cached value? Then return it
if (isset($this->cache[$cat][$k])) {
if ($this->cache[$cat][$k] === '!<unset>!') {
return $default_value;
} else {
return $this->cache[$cat][$k];
}
}
}
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
if (DBA::isResult($config)) {
// manage array value
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
// Assign the value from the database to the cache
$this->cache[$cat][$k] = $value;
$this->in_db[$cat][$k] = true;
return $value;
} elseif ($this->configCache->get($cat, $k) !== null) {
// Assign the value (mostly) from config/local.config.php file to the cache
$this->cache[$cat][$k] = $this->configCache->get($cat, $k);
$this->in_db[$cat][$k] = false;
return $this->configCache->get($cat, $k);
} elseif ($this->configCache->get('config', $k) !== null) {
// Assign the value (mostly) from config/local.config.php file to the cache
$this->cache[$k] = $this->configCache->get('config', $k);
$this->in_db[$k] = false;
return $this->configCache->get('config', $k);
}
$this->cache[$cat][$k] = '!<unset>!';
$this->in_db[$cat][$k] = false;
return $default_value;
}
/**
* {@inheritdoc}
*/
public function set($cat, $k, $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, $k, null, true);
if (!isset($this->in_db[$cat])) {
$this->in_db[$cat] = [];
}
if (!isset($this->in_db[$cat][$k])) {
$this->in_db[$cat] = false;
}
if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
return true;
}
$this->configCache->set($cat, $k, $value);
// Assign the just added value to the cache
$this->cache[$cat][$k] = $dbvalue;
// manage array value
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
if ($result) {
$this->in_db[$cat][$k] = true;
}
return $result;
}
/**
* {@inheritdoc}
*/
public function delete($cat, $k)
{
if (!$this->isConnected()) {
return false;
}
if (isset($this->cache[$cat][$k])) {
unset($this->cache[$cat][$k]);
unset($this->in_db[$cat][$k]);
}
$result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
return $result;
}
}

View file

@ -1,136 +0,0 @@
<?php
namespace Friendica\Core\Config;
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 implements IPConfigAdapter
{
private $in_db;
/**
* The config cache of this adapter
* @var IPConfigCache
*/
private $configCache;
/**
* @param IPConfigCache $configCache The config cache of this adapter
*/
public function __construct(IPConfigCache $configCache)
{
$this->configCache = $configCache;
}
/**
* {@inheritdoc}
*/
public function load($uid, $cat)
{
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
if (DBA::isResult($pconfigs)) {
while ($pconfig = DBA::fetch($pconfigs)) {
$k = $pconfig['k'];
$this->configCache->setP($uid, $cat, $k, $pconfig['v']);
$this->in_db[$uid][$cat][$k] = true;
}
} else if ($cat != 'config') {
// Negative caching
$this->configCache->setP($uid, $cat, null, "!<unset>!");
}
DBA::close($pconfigs);
}
/**
* {@inheritdoc}
*/
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
{
if (!$refresh) {
// Looking if the whole family isn't set
if ($this->configCache->getP($uid, $cat) !== null) {
if ($this->configCache->getP($uid, $cat) === '!<unset>!') {
return $default_value;
}
}
if ($this->configCache->getP($uid, $cat, $k) !== null) {
if ($this->configCache->getP($uid, $cat, $k) === '!<unset>!') {
return $default_value;
}
return $this->configCache->getP($uid, $cat, $k);
}
}
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
if (DBA::isResult($pconfig)) {
$val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
$this->configCache->setP($uid, $cat, $k, $val);
$this->in_db[$uid][$cat][$k] = true;
return $val;
} else {
$this->configCache->setP($uid, $cat, $k, '!<unset>!');
$this->in_db[$uid][$cat][$k] = false;
return $default_value;
}
}
/**
* {@inheritdoc}
*/
public function set($uid, $cat, $k, $value)
{
// 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, $k, null, true);
if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
return true;
}
$this->configCache->setP($uid, $cat, $k, $value);
// manage array value
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
if ($result) {
$this->in_db[$uid][$cat][$k] = true;
}
return $result;
}
/**
* {@inheritdoc}
*/
public function delete($uid, $cat, $k)
{
$this->configCache->deleteP($uid, $cat, $k);
if (!empty($this->in_db[$uid][$cat][$k])) {
unset($this->in_db[$uid][$cat][$k]);
}
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
return $result;
}
}

View file

@ -0,0 +1,146 @@
<?php
namespace Friendica\Core\Config;
/**
* This class is responsible for the user-specific configuration values in Friendica
* The values are set through the Config-DB-Table (per Config-DB-adapter @see Adapter\IPConfigAdapter )
*
* The configuration cache (@see Cache\IPConfigCache ) is used for temporary caching of database calls. This will
* increase the performance.
*/
class PConfiguration
{
/**
* @var Cache\IPConfigCache
*/
private $configCache;
/**
* @var Adapter\IPConfigAdapter
*/
private $configAdapter;
/**
* @param Cache\IPConfigCache $configCache The configuration cache
* @param Adapter\IPConfigAdapter $configAdapter The configuration DB-backend
*/
public function __construct(Cache\IPConfigCache $configCache, Adapter\IPConfigAdapter $configAdapter)
{
$this->configCache = $configCache;
$this->configAdapter = $configAdapter;
}
/**
* @brief Loads all configuration values of a user's config family into a cached storage.
*
* All configuration values of the given user are stored with the $uid in
* the cache ( @see IPConfigCache )
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
*
* @return void
*/
public function load($uid, $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->loadP($uid, $this->configAdapter->load($uid, $cat));
}
/**
* @brief Get a particular user's config variable given the category name
* ($cat) and a key.
*
* Get a particular user's config value from the given category ($cat)
* and the $key with the $uid from a cached storage either from the $this->configAdapter
* (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
*
* @param string $uid The user_id
* @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($uid, $cat, $key, $default_value = null, $refresh = false)
{
// if the value isn't loaded or refresh is needed, load it to the cache
if ($this->configAdapter->isConnected() &&
(!$this->configAdapter->isLoaded($uid, $cat, $key) ||
$refresh)) {
$dbValue = $this->configAdapter->get($uid, $cat, $key);
if (isset($dbValue)) {
$this->configCache->setP($uid, $cat, $key, $dbValue);
return $dbValue;
}
}
// use the config cache for return
$result = $this->configCache->getP($uid, $cat, $key);
return (isset($result)) ? $result : $default_value;
}
/**
* @brief Sets a configuration value for a user
*
* Stores a config value ($value) in the category ($family) under the key ($key)
* for the user_id $uid.
*
* @note Please do not store booleans - convert to 0/1 integer values!
*
* @param string $uid The user_id
* @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($uid, $cat, $key, $value)
{
// set the cache first
$cached = $this->configCache->setP($uid, $cat, $key, $value);
// If there is no connected adapter, we're finished
if (!$this->configAdapter->isConnected()) {
return $cached;
}
$stored = $this->configAdapter->set($uid, $cat, $key, $value);
return $cached && $stored;
}
/**
* @brief Deletes the given key from the users's configuration.
*
* Removes the configured value from the stored cache in $this->configCache
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
* with the given $uid.
*
* @param string $uid The user_id
* @param string $cat The category of the configuration value
* @param string $key The configuration key to delete
*
* @return bool
*/
public function delete($uid, $cat, $key)
{
$cacheRemoved = $this->configCache->deleteP($uid, $cat, $key);
if (!$this->configAdapter->isConnected()) {
return $cacheRemoved;
}
$storeRemoved = $this->configAdapter->delete($uid, $cat, $key);
return $cacheRemoved || $storeRemoved;
}
}

View file

@ -1,123 +0,0 @@
<?php
namespace Friendica\Core\Config;
use Exception;
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;
/**
* @var IConfigCache The config cache of this driver
*/
private $configCache;
/**
* @param IConfigCache $configCache The config cache of this driver
*/
public function __construct(IConfigCache $configCache)
{
$this->configCache = $configCache;
$this->connected = DBA::connected();
$this->load();
}
/**
* {@inheritdoc}
*/
public function load($family = 'config')
{
if (!$this->isConnected()) {
return;
}
if ($this->config_loaded) {
return;
}
$configs = DBA::select('config', ['cat', 'v', 'k']);
while ($config = DBA::fetch($configs)) {
$this->configCache->set($config['cat'], $config['k'], $config['v']);
}
DBA::close($configs);
$this->config_loaded = true;
}
/**
* {@inheritdoc}
*/
public function get($cat, $k, $default_value = null, $refresh = false)
{
if (!$this->isConnected()) {
return $default_value;
}
if ($refresh) {
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
if (DBA::isResult($config)) {
$this->configCache->set($cat, $k, $config['v']);
}
}
$return = $this->configCache->get($cat, $k, $default_value);
return $return;
}
/**
* {@inheritdoc}
*/
public function set($cat, $k, $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->configCache->get($cat, $k) === $compare_value) {
return true;
}
$this->configCache->set($cat, $k, $value);
// manage array value
$dbvalue = is_array($value) ? serialize($value) : $value;
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
if (!$result) {
throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
}
return true;
}
/**
* {@inheritdoc}
*/
public function delete($cat, $k)
{
if (!$this->isConnected()) {
return false;
}
$this->configCache->delete($cat, $k);
$result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
return $result;
}
}

View file

@ -1,125 +0,0 @@
<?php
namespace Friendica\Core\Config;
use Exception;
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 implements IPConfigAdapter
{
private $config_loaded = false;
/**
* The config cache of this adapter
* @var IPConfigCache
*/
private $configCache;
/**
* @param IPConfigCache $configCache The config cache of this adapter
* @param int $uid The UID of the current user
*/
public function __construct(IPConfigCache $configCache, $uid = null)
{
$this->configCache = $configCache;
if (isset($uid)) {
$this->load($uid, 'config');
}
}
/**
* {@inheritdoc}
*/
public function load($uid, $family)
{
if ($this->config_loaded) {
return;
}
if (empty($uid)) {
return;
}
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
while ($pconfig = DBA::fetch($pconfigs)) {
$this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
}
DBA::close($pconfigs);
$this->config_loaded = true;
}
/**
* {@inheritdoc}
*/
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
{
if (!$this->config_loaded) {
$this->load($uid, $cat);
}
if ($refresh) {
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
if (DBA::isResult($config)) {
$this->configCache->setP($uid, $cat, $k, $config['v']);
} else {
$this->configCache->deleteP($uid, $cat, $k);
}
}
return $this->configCache->getP($uid, $cat, $k, $default_value);;
}
/**
* {@inheritdoc}
*/
public function set($uid, $cat, $k, $value)
{
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->configCache->getP($uid, $cat, $k) === $compare_value) {
return true;
}
$this->configCache->setP($uid, $cat, $k, $value);
// 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;
}
/**
* {@inheritdoc}
*/
public function delete($uid, $cat, $k)
{
if (!$this->config_loaded) {
$this->load($uid, $cat);
}
$this->configCache->deleteP($uid, $cat, $k);
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
return $result;
}
}

View file

@ -100,10 +100,10 @@ HELP;
}
}
$db_host = $a->getConfig()->get('database', 'hostname');
$db_user = $a->getConfig()->get('database', 'username');
$db_pass = $a->getConfig()->get('database', 'password');
$db_data = $a->getConfig()->get('database', 'database');
$db_host = $a->getConfigCache()->get('database', 'hostname');
$db_user = $a->getConfigCache()->get('database', 'username');
$db_pass = $a->getConfigCache()->get('database', 'password');
$db_data = $a->getConfigCache()->get('database', 'database');
} else {
// Creating config file
$this->out("Creating config file...\n");
@ -146,7 +146,7 @@ HELP;
$installer->resetChecks();
if (!$installer->checkDB($a->getConfig(), $db_host, $db_user, $db_pass, $db_data)) {
if (!$installer->checkDB($a->getBasePath(), $a->getConfigCache(), $a->getProfiler(), $db_host, $db_user, $db_pass, $db_data)) {
$errorMessage = $this->extractErrors($installer->getChecks());
throw new RuntimeException($errorMessage);
}

View file

@ -124,9 +124,9 @@ HELP;
$cat = $this->getArgument(0);
Core\Config::load($cat);
if ($a->getConfig()->get($cat) !== null) {
if ($a->getConfigCache()->get($cat) !== null) {
$this->out("[{$cat}]");
$catVal = $a->getConfig()->get($cat);
$catVal = $a->getConfigCache()->get($cat);
foreach ($catVal as $key => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
@ -148,7 +148,7 @@ HELP;
$this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
}
$config = $a->getConfig()->getAll();
$config = $a->getConfigCache()->getAll();
foreach ($config as $cat => $section) {
if (is_array($section)) {
foreach ($section as $key => $value) {

View file

@ -31,9 +31,10 @@ Commands
toinnodb Convert all tables from MyISAM to InnoDB
Options
-h|--help|-? Show help information
-v Show more debug information.
-f|--force Force the command in case of "update" (Ignore failed updates/running updates)
-h|--help|-? Show help information
-v Show more debug information.
-f|--force Force the update command (Even if the database structure matches)
-o|--override Override running or stalling updates
HELP;
return $help;
}
@ -68,8 +69,9 @@ HELP;
$output = DBStructure::update($a->getBasePath(), true, false);
break;
case "update":
$force = $this->getOption(['f', 'force'], false);
$output = Update::run($a->getBasePath(), $force, true, false);
$force = $this->getOption(['f', 'force'], false);
$override = $this->getOption(['o', 'override'], false);
$output = Update::run($a->getBasePath(), $force, $override,true, false);
break;
case "dumpsql":
ob_start();
@ -89,5 +91,4 @@ HELP;
return 0;
}
}

View file

@ -56,7 +56,7 @@ HELP;
}
echo L10n::t('Check for pending update actions.') . "\n";
Update::run($a->getBasePath(), true, true, false);
Update::run($a->getBasePath(), true, false, true, false);
echo L10n::t('Done.') . "\n";
echo L10n::t('Execute pending post updates.') . "\n";

View file

@ -43,7 +43,7 @@ HELP;
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
}
$php_path = BaseObject::getApp()->getConfig()->get('config', 'php_path', 'php');
$php_path = BaseObject::getApp()->getConfigCache()->get('config', 'php_path', 'php');
if ($this->getOption('v')) {
$this->out('Directory: src');

View file

@ -6,11 +6,12 @@ namespace Friendica\Core;
use DOMDocument;
use Exception;
use Friendica\Core\Config\ConfigCache;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Object\Image;
use Friendica\Util\Network;
use Friendica\Util\Profiler;
use Friendica\Util\Strings;
/**
@ -357,6 +358,7 @@ class Installer
* - mb_string
* - XML
* - iconv
* - fileinfo
* - POSIX
*
* @return bool false if something required failed
@ -452,6 +454,13 @@ class Installer
);
$returnVal = $returnVal ? $status : false;
$status = $this->checkFunction('finfo_open',
L10n::t('File Information PHP module'),
L10n::t('Error: File Information PHP module required but not installed.'),
true
);
$returnVal = $returnVal ? $status : false;
return $returnVal;
}
@ -582,7 +591,9 @@ class Installer
/**
* Checking the Database connection and if it is available for the current installation
*
* @param ConfigCache $configCache The configuration cache
* @param string $basePath The basepath of this call
* @param IConfigCache $configCache The configuration cache
* @param Profiler $profiler The profiler of this app
* @param string $dbhost Hostname/IP of the Friendica Database
* @param string $dbuser Username of the Database connection credentials
* @param string $dbpass Password of the Database connection credentials
@ -591,9 +602,9 @@ class Installer
* @return bool true if the check was successful, otherwise false
* @throws Exception
*/
public function checkDB(ConfigCache $configCache, $dbhost, $dbuser, $dbpass, $dbdata)
public function checkDB($basePath, IConfigCache $configCache, Profiler $profiler, $dbhost, $dbuser, $dbpass, $dbdata)
{
if (!DBA::connect($configCache, $dbhost, $dbuser, $dbpass, $dbdata)) {
if (!DBA::connect($basePath, $configCache, $profiler, $dbhost, $dbuser, $dbpass, $dbdata)) {
$this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
return false;

View file

@ -122,12 +122,13 @@ class Lock
/**
* @brief Releases a lock if it was set by us
*
* @param string $key Name of the lock
* @param string $key Name of the lock
* @param bool $override Overrides the lock to get releases
* @return void
*/
public static function release($key)
public static function release($key, $override = false)
{
self::getDriver()->releaseLock($key);
self::getDriver()->releaseLock($key, $override);
}
/**

View file

@ -61,11 +61,15 @@ class CacheLockDriver extends AbstractLockDriver
/**
* (@inheritdoc)
*/
public function releaseLock($key)
public function releaseLock($key, $override = false)
{
$cachekey = self::getLockKey($key);
$this->cache->compareDelete($cachekey, getmypid());
if ($override) {
$this->cache->delete($cachekey);
} else {
$this->cache->compareDelete($cachekey, getmypid());
}
$this->markRelease($key);
}

View file

@ -68,9 +68,15 @@ class DatabaseLockDriver extends AbstractLockDriver
/**
* (@inheritdoc)
*/
public function releaseLock($key)
public function releaseLock($key, $override = false)
{
DBA::delete('locks', ['name' => $key, 'pid' => $this->pid]);
if ($override) {
$where = ['name' => $key];
} else {
$where = ['name' => $key, 'pid' => $this->pid];
}
DBA::delete('locks', $where);
$this->markRelease($key);

View file

@ -33,11 +33,12 @@ interface ILockDriver
/**
* Releases a lock if it was set by us
*
* @param string $key The Name of the lock
* @param string $key The Name of the lock
* @param bool $override Overrides the lock to get released
*
* @return void
*/
public function releaseLock($key);
public function releaseLock($key, $override = false);
/**
* Releases all lock that were set by us

View file

@ -50,7 +50,7 @@ class SemaphoreLockDriver extends AbstractLockDriver
/**
* (@inheritdoc)
*/
public function releaseLock($key)
public function releaseLock($key, $override = false)
{
if (empty(self::$semaphore[$key])) {
return false;

View file

@ -5,8 +5,6 @@
namespace Friendica\Core;
use Friendica\BaseObject;
use Friendica\Factory\LoggerFactory;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
@ -67,73 +65,22 @@ class Logger extends BaseObject
/**
* Sets the default logging handler for Friendica.
* @todo Can be combined with other handlers too if necessary, could be configurable.
*
* @param LoggerInterface $logger The Logger instance of this Application
*
* @throws InternalServerErrorException if the logger factory is incompatible to this logger
*/
public static function setLogger($logger)
public static function init(LoggerInterface $logger)
{
$debugging = Config::get('system', 'debugging');
$logfile = Config::get('system', 'logfile');
$loglevel = Config::get('system', 'loglevel');
if (!$debugging || !$logfile) {
return;
}
$loglevel = self::mapLegacyConfigDebugLevel((string)$loglevel);
LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
self::$logger = $logger;
$logfile = Config::get('system', 'dlogfile');
if (!$logfile) {
return;
}
$developIp = Config::get('system', 'dlogip');
self::$devLogger = LoggerFactory::createDev('develop', $developIp);
LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
}
/**
* Mapping a legacy level to the PSR-3 compliant levels
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
* Sets the default dev-logging handler for Friendica.
*
* @param string $level the level to be mapped
*
* @return string the PSR-3 compliant level
* @param LoggerInterface $logger The Logger instance of this Application
*/
private static function mapLegacyConfigDebugLevel($level)
public static function setDevLogger(LoggerInterface $logger)
{
switch ($level) {
// legacy WARNING
case "0":
return LogLevel::ERROR;
// legacy INFO
case "1":
return LogLevel::WARNING;
// legacy TRACE
case "2":
return LogLevel::NOTICE;
// legacy DEBUG
case "3":
return LogLevel::INFO;
// legacy DATA
case "4":
return LogLevel::DEBUG;
// legacy ALL
case "5":
return LogLevel::DEBUG;
// default if nothing set
default:
return $level;
}
self::$devLogger = $logger;
}
/**
@ -155,7 +102,7 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->emergency($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->GetProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
@ -179,7 +126,7 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->alert($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
@ -202,7 +149,7 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->critical($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
@ -225,7 +172,7 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->error($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
@ -249,7 +196,7 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->warning($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
@ -270,7 +217,7 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->notice($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
@ -293,7 +240,7 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->info($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
@ -314,28 +261,28 @@ class Logger extends BaseObject
$stamp1 = microtime(true);
self::$logger->debug($message, $context);
self::getApp()->saveTimestamp($stamp1, 'file');
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
}
/**
* @brief Logs the given message at the given log level
*
* @param string $msg
* @param string $level
/**
* @brief Logs the given message at the given log level
*
* @param string $msg
* @param string $level
*
* @throws \Exception
* @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
*/
public static function log($msg, $level = LogLevel::INFO)
{
*/
public static function log($msg, $level = LogLevel::INFO)
{
if (!isset(self::$logger)) {
return;
}
$stamp1 = microtime(true);
$stamp1 = microtime(true);
self::$logger->log($level, $msg);
self::getApp()->saveTimestamp($stamp1, "file");
}
self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
}
/**
* @brief An alternative logger for development.
@ -347,14 +294,14 @@ class Logger extends BaseObject
* @param string $level
* @throws \Exception
*/
public static function devLog($msg, $level = LogLevel::DEBUG)
{
public static function devLog($msg, $level = LogLevel::DEBUG)
{
if (!isset(self::$logger)) {
return;
}
$stamp1 = microtime(true);
self::$devLogger->log($level, $msg);
self::getApp()->saveTimestamp($stamp1, "file");
}
$stamp1 = microtime(true);
self::$devLogger->log($level, $msg);
self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
}
}

View file

@ -18,123 +18,76 @@ namespace Friendica\Core;
class PConfig
{
/**
* @var Config\IPConfigAdapter
* @var Config\PConfiguration
*/
private static $adapter;
/**
* @var Config\IPConfigCache
*/
private static $cache;
private static $config;
/**
* Initialize the config with only the cache
*
* @param Config\IPConfigCache $cache The configuration cache
* @param Config\PConfiguration $config The configuration cache
*/
public static function init(Config\IPConfigCache $cache)
public static function init(Config\PConfiguration $config)
{
self::$cache = $cache;
}
/**
* Add the adapter for DB-backend
*
* @param Config\IPConfigAdapter $adapter
*/
public static function setAdapter(Config\IPConfigAdapter $adapter)
{
self::$adapter = $adapter;
self::$config = $config;
}
/**
* @brief Loads all configuration values of a user's config family into a cached storage.
*
* All configuration values of the given user are stored with the $uid in
* the cache ( @see IPConfigCache )
*
* @param string $uid The user_id
* @param string $family The category of the configuration value
* @param string $uid The user_id
* @param string $cat The category of the configuration value
*
* @return void
*/
public static function load($uid, $family)
public static function load($uid, $cat)
{
if (!isset(self::$adapter)) {
return;
}
self::$adapter->load($uid, $family);
self::$config->load($uid, $cat);
}
/**
* @brief Get a particular user's config variable given the category name
* ($family) and a key.
*
* Get a particular user's config value from the given category ($family)
* and the $key with the $uid from a cached storage either from the self::$adapter
* (@see IConfigAdapter ) or from the static::$cache (@see IConfigCache ).
* ($cat) and a key.
*
* @param string $uid The user_id
* @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($uid, $family, $key, $default_value = null, $refresh = false)
public static function get($uid, $cat, $key, $default_value = null, $refresh = false)
{
if (!isset(self::$adapter)) {
return self::$cache->getP($uid, $family, $key, $default_value);
}
return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
return self::$config->get($uid, $cat, $key, $default_value, $refresh);
}
/**
* @brief Sets a configuration value for a user
*
* Stores a config value ($value) in the category ($family) under the key ($key)
* for the user_id $uid.
*
* @note Please do not store booleans - convert to 0/1 integer values!
*
* @param string $uid The user_id
* @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($uid, $family, $key, $value)
public static function set($uid, $cat, $key, $value)
{
if (!isset(self::$adapter)) {
return self::$cache->setP($uid, $family, $key, $value);
}
return self::$adapter->set($uid, $family, $key, $value);
return self::$config->set($uid, $cat, $key, $value);
}
/**
* @brief Deletes the given key from the users's configuration.
*
* Removes the configured value from the stored cache in self::$config
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
* with the given $uid.
* @param string $uid The user_id
* @param string $cat The category of the configuration value
* @param string $key The configuration key to delete
*
* @param string $uid The user_id
* @param string $family The category of the configuration value
* @param string $key The configuration key to delete
*
* @return mixed
* @return bool
*/
public static function delete($uid, $family, $key)
public static function delete($uid, $cat, $key)
{
if (!isset(self::$adapter)) {
return self::$cache->deleteP($uid, $family, $key);
}
return self::$adapter->delete($uid, $family, $key);
return self::$config->delete($uid, $cat, $key);
}
}

View file

@ -74,7 +74,7 @@ class Renderer extends BaseObject
exit();
}
$a->saveTimestamp($stamp1, "rendering");
$a->getProfiler()->saveTimestamp($stamp1, "rendering", System::callstack());
return $output;
}
@ -101,7 +101,7 @@ class Renderer extends BaseObject
exit();
}
$a->saveTimestamp($stamp1, "file");
$a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
return $template;
}

View file

@ -176,6 +176,12 @@ class System extends BaseObject
exit();
}
public static function jsonError($httpCode, $data, $content_type = 'application/json')
{
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode);
self::jsonExit($data, $content_type);
}
/**
* @brief Encodes content to json.
*
@ -234,21 +240,6 @@ class System extends BaseObject
}
}
/**
* Generates a process identifier for the logging
*
* @param string $prefix A given prefix
*
* @return string a generated process identifier
*/
public static function processID($prefix)
{
// We aren't calling any other function here.
// Doing so could easily create an endless loop
$trailer = $prefix . ':' . getmypid() . ':';
return substr($trailer . uniqid('') . mt_rand(), 0, 26);
}
/**
* Returns the current Load of the System
*

View file

@ -51,7 +51,7 @@ class Theme
$a = \get_app();
$stamp1 = microtime(true);
$theme_file = file_get_contents("view/theme/$theme/theme.php");
$a->saveTimestamp($stamp1, "file");
$a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
$result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);

View file

@ -37,9 +37,13 @@ class Update
}
if ($build < DB_UPDATE_VERSION) {
// When we cannot execute the database update via the worker, we will do it directly
if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
if ($via_worker) {
// Calling the database update directly via the worker enables us to perform database changes to the workerqueue table itself.
// This is a fallback, since normally the database update will be performed by a worker job.
// This worker job doesn't work for changes to the "workerqueue" table itself.
self::run($basePath);
} else {
Worker::add(PRIORITY_CRITICAL, 'DBUpdate');
}
}
}
@ -48,19 +52,20 @@ class Update
* Automatic database updates
*
* @param string $basePath The base path of this application
* @param bool $force Force the Update-Check even if the lock is set
* @param bool $force Force the Update-Check even if the database version doesn't match
* @param bool $override Overrides any running/stuck updates
* @param bool $verbose Run the Update-Check verbose
* @param bool $sendMail Sends a Mail to the administrator in case of success/failure
*
* @return string Empty string if the update is successful, error messages otherwise
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function run($basePath, $force = false, $verbose = false, $sendMail = true)
public static function run($basePath, $force = false, $override = false, $verbose = false, $sendMail = true)
{
// In force mode, we release the dbupdate lock first
// Necessary in case of an stuck update
if ($force) {
Lock::release('dbupdate');
if ($override) {
Lock::release('dbupdate', true);
}
$build = Config::get('system', 'build');
@ -70,12 +75,12 @@ class Update
Config::set('system', 'build', $build);
}
if ($build != DB_UPDATE_VERSION) {
if ($build != DB_UPDATE_VERSION || $force) {
require_once 'update.php';
$stored = intval($build);
$current = intval(DB_UPDATE_VERSION);
if ($stored < $current) {
if ($stored < $current || $force) {
Config::load('database');
Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - starting', Logger::DEBUG);
@ -94,7 +99,7 @@ class Update
// update the structure in one call
$retval = DBStructure::update($basePath, $verbose, true);
if ($retval) {
if (!empty($retval)) {
if ($sendMail) {
self::updateFailed(
DB_UPDATE_VERSION,
@ -126,8 +131,6 @@ class Update
Lock::release('dbupdate');
}
}
} elseif ($force) {
DBStructure::update($basePath, $verbose, true);
}
return '';

View file

@ -8,6 +8,7 @@ use Friendica\BaseObject;
use Friendica\Database\DBA;
use Friendica\Model\Process;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Logger\WorkerLogger;
use Friendica\Util\Network;
/**
@ -360,39 +361,19 @@ class Worker
{
$a = \get_app();
$mypid = getmypid();
$argc = count($argv);
// Currently deactivated, since the new logger doesn't support this
//$new_process_id = System::processID("wrk");
$new_process_id = '';
$logger = $a->getLogger();
$workerLogger = new WorkerLogger($logger, $funcname);
Logger::log("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id);
$workerLogger ->info("Process start.", ['priority' => $queue["priority"], 'id' => $queue["id"]]);
$stamp = (float)microtime(true);
// We use the callstack here to analyze the performance of executed worker entries.
// For this reason the variables have to be initialized.
if (Config::get("system", "profiler")) {
$a->performance["start"] = microtime(true);
$a->performance["database"] = 0;
$a->performance["database_write"] = 0;
$a->performance["cache"] = 0;
$a->performance["cache_write"] = 0;
$a->performance["network"] = 0;
$a->performance["file"] = 0;
$a->performance["rendering"] = 0;
$a->performance["parser"] = 0;
$a->performance["marktime"] = 0;
$a->performance["markstart"] = microtime(true);
$a->callstack = [];
}
$a->getProfiler()->reset();
// For better logging create a new process id for every worker call
// But preserve the old one for the worker
$old_process_id = $a->process_id;
$a->process_id = $new_process_id;
$a->queue = $queue;
$up_duration = microtime(true) - self::$up_start;
@ -400,13 +381,15 @@ class Worker
// Reset global data to avoid interferences
unset($_SESSION);
// Set the workerLogger as new default logger
Logger::init($workerLogger);
if ($method_call) {
call_user_func_array(sprintf('Friendica\Worker\%s::execute', $funcname), $argv);
} else {
$funcname($argv, $argc);
}
Logger::init($logger);
$a->process_id = $old_process_id;
unset($a->queue);
$duration = (microtime(true) - $stamp);
@ -425,7 +408,7 @@ class Worker
$rest = round(max(0, $up_duration - (self::$db_duration + self::$lock_duration)), 2);
$exec = round($duration, 2);
Logger::info('Performance:', ['mode' => self::$mode, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]);
$logger->info('Performance log.', ['mode' => self::$mode, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]);
self::$up_start = microtime(true);
self::$db_duration = 0;
@ -436,92 +419,23 @@ class Worker
self::$mode = 2;
if ($duration > 3600) {
Logger::log("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 1 hour (".round($duration/60, 3).")", Logger::DEBUG);
$logger->info('Longer than 1 hour.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
} elseif ($duration > 600) {
Logger::log("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 10 minutes (".round($duration/60, 3).")", Logger::DEBUG);
$logger->info('Longer than 10 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
} elseif ($duration > 300) {
Logger::log("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 5 minutes (".round($duration/60, 3).")", Logger::DEBUG);
$logger->info('Longer than 5 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
} elseif ($duration > 120) {
Logger::log("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", Logger::DEBUG);
$logger->info('Longer than 2 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
}
Logger::log("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".number_format($duration, 4)." seconds. Process PID: ".$new_process_id);
$workerLogger->info('Process done. ', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => number_format($duration, 4)]);
// Write down the performance values into the log
if (Config::get("system", "profiler")) {
$duration = microtime(true)-$a->performance["start"];
$o = '';
if (Config::get("rendertime", "callstack")) {
if (isset($a->callstack["database"])) {
$o .= "\nDatabase Read:\n";
foreach ($a->callstack["database"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func.": ".$time."\n";
}
}
}
if (isset($a->callstack["database_write"])) {
$o .= "\nDatabase Write:\n";
foreach ($a->callstack["database_write"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func.": ".$time."\n";
}
}
}
if (isset($a->callstack["dache"])) {
$o .= "\nCache Read:\n";
foreach ($a->callstack["dache"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func.": ".$time."\n";
}
}
}
if (isset($a->callstack["dache_write"])) {
$o .= "\nCache Write:\n";
foreach ($a->callstack["dache_write"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func.": ".$time."\n";
}
}
}
if (isset($a->callstack["network"])) {
$o .= "\nNetwork:\n";
foreach ($a->callstack["network"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func.": ".$time."\n";
}
}
}
}
Logger::log(
"ID ".$queue["id"].": ".$funcname.": ".sprintf(
"DB: %s/%s, Cache: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
number_format($a->performance["database"] - $a->performance["database_write"], 2),
number_format($a->performance["database_write"], 2),
number_format($a->performance["cache"], 2),
number_format($a->performance["cache_write"], 2),
number_format($a->performance["network"], 2),
number_format($a->performance["file"], 2),
number_format($duration - ($a->performance["database"]
+ $a->performance["cache"] + $a->performance["cache_write"]
+ $a->performance["network"] + $a->performance["file"]), 2),
number_format($duration, 2)
),
Logger::DEBUG
);
}
$a->getProfiler()->saveLog($a->getLogger(), "ID " . $queue["id"] . ": " . $funcname);
$cooldown = Config::get("system", "worker_cooldown", 0);
if ($cooldown > 0) {
Logger::log("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
$logger->info('Cooldown.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'cooldown' => $cooldown]);
sleep($cooldown);
}
}
@ -943,7 +857,7 @@ class Worker
}
if (!empty($waiting)) {
$priority = array_shift(array_keys($waiting));
$priority = array_keys($waiting)[0];
Logger::info('No underassigned priority found, now taking the highest priority.', ['priority' => $priority]);
return $priority;
}