mirror of
https://github.com/friendica/friendica
synced 2025-04-28 13:44:25 +02:00
Merge remote-tracking branch 'upstream/develop' into error-handling
This commit is contained in:
commit
516018861e
235 changed files with 10885 additions and 10716 deletions
|
@ -19,21 +19,24 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Capability;
|
||||
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
|
||||
/**
|
||||
* Cache Interface
|
||||
* Interface for caches
|
||||
*/
|
||||
interface ICache
|
||||
interface ICanCache
|
||||
{
|
||||
/**
|
||||
* Lists all cache keys
|
||||
*
|
||||
* @param string prefix optional a prefix to search
|
||||
* @param string|null prefix optional a prefix to search
|
||||
*
|
||||
* @return array Empty if it isn't supported by the cache driver
|
||||
*/
|
||||
public function getAllKeys($prefix = null);
|
||||
public function getAllKeys(?string $prefix = null): array;
|
||||
|
||||
/**
|
||||
* Fetches cached data according to the key
|
||||
|
@ -41,41 +44,50 @@ interface ICache
|
|||
* @param string $key The key to the cached data
|
||||
*
|
||||
* @return mixed Cached $value or "null" if not found
|
||||
*
|
||||
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
|
||||
*/
|
||||
public function get($key);
|
||||
public function get(string $key);
|
||||
|
||||
/**
|
||||
* Stores data in the cache identified by the key. The input $value can have multiple formats.
|
||||
*
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The value to store
|
||||
* @param integer $ttl The cache lifespan, must be one of the Cache constants
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The value to store
|
||||
* @param integer $ttl The cache lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool;
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
*
|
||||
* @param string $key The cache key
|
||||
* @param string $key The cache key
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
|
||||
*/
|
||||
public function delete($key);
|
||||
public function delete(string $key): bool;
|
||||
|
||||
/**
|
||||
* Remove outdated data from the cache
|
||||
* @param boolean $outdated just remove outdated values
|
||||
*
|
||||
* @param boolean $outdated just remove outdated values
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
|
||||
*/
|
||||
public function clear($outdated = true);
|
||||
public function clear(bool $outdated = true): bool;
|
||||
|
||||
/**
|
||||
* Returns the name of the current cache
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
public function getName(): string;
|
||||
}
|
|
@ -19,41 +19,52 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Capability;
|
||||
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
|
||||
/**
|
||||
* This interface defines methods for Memory-Caches only
|
||||
*/
|
||||
interface IMemoryCache extends ICache
|
||||
interface ICanCacheInMemory extends ICanCache
|
||||
{
|
||||
/**
|
||||
* Sets a value if it's not already stored
|
||||
*
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The old value we know from the cache
|
||||
* @param int $ttl The cache lifespan, must be one of the Cache constants
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The old value we know from the cache
|
||||
* @param int $ttl The cache lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
|
||||
*/
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool;
|
||||
|
||||
/**
|
||||
* Compares if the old value is set and sets the new value
|
||||
*
|
||||
* @param string $key The cache key
|
||||
* @param mixed $oldValue The old value we know from the cache
|
||||
* @param mixed $newValue The new value we want to set
|
||||
* @param int $ttl The cache lifespan, must be one of the Cache constants
|
||||
* @param string $key The cache key
|
||||
* @param mixed $oldValue The old value we know from the cache
|
||||
* @param mixed $newValue The new value we want to set
|
||||
* @param int $ttl The cache lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES);
|
||||
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool;
|
||||
|
||||
/**
|
||||
* Compares if the old value is set and removes it
|
||||
*
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The old value we know and want to delete
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The old value we know and want to delete
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
|
||||
*/
|
||||
public function compareDelete($key, $value);
|
||||
public function compareDelete(string $key, $value): bool;
|
||||
}
|
|
@ -1,138 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Core\BaseCache;
|
||||
|
||||
/**
|
||||
* Database Cache
|
||||
*/
|
||||
class DatabaseCache extends BaseCache implements ICache
|
||||
{
|
||||
/**
|
||||
* @var Database
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
public function __construct(string $hostname, Database $dba)
|
||||
{
|
||||
parent::__construct($hostname);
|
||||
|
||||
$this->dba = $dba;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
if (empty($prefix)) {
|
||||
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
|
||||
} else {
|
||||
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
||||
}
|
||||
|
||||
$stmt = $this->dba->select('cache', ['k'], $where);
|
||||
|
||||
$keys = [];
|
||||
while ($key = $this->dba->fetch($stmt)) {
|
||||
array_push($keys, $key['k']);
|
||||
}
|
||||
$this->dba->close($stmt);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$cache = $this->dba->selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if ($this->dba->isResult($cache)) {
|
||||
$cached = $cache['v'];
|
||||
$value = @unserialize($cached);
|
||||
|
||||
// Only return a value if the serialized value is valid.
|
||||
// We also check if the db entry is a serialized
|
||||
// boolean 'false' value (which we want to return).
|
||||
if ($cached === serialize(false) || $value !== false) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
if ($ttl > 0) {
|
||||
$fields = [
|
||||
'v' => serialize($value),
|
||||
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
|
||||
'updated' => DateTimeFormat::utcNow()
|
||||
];
|
||||
} else {
|
||||
$fields = [
|
||||
'v' => serialize($value),
|
||||
'expires' => -1,
|
||||
'updated' => DateTimeFormat::utcNow()
|
||||
];
|
||||
}
|
||||
|
||||
return $this->dba->update('cache', $fields, ['k' => $key], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return $this->dba->delete('cache', ['k' => $key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
{
|
||||
if ($outdated) {
|
||||
return $this->dba->delete('cache', ['`expires` < NOW()']);
|
||||
} else {
|
||||
return $this->dba->delete('cache', ['`k` IS NOT NULL ']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return Type::DATABASE;
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Enum;
|
||||
|
||||
/**
|
||||
* Enumeration for cache durations
|
|
@ -19,7 +19,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Enum;
|
||||
|
||||
/**
|
||||
* Enumeration for cache types
|
13
src/Core/Cache/Exception/CachePersistenceException.php
Normal file
13
src/Core/Cache/Exception/CachePersistenceException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class CachePersistenceException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
13
src/Core/Cache/Exception/InvalidCacheDriverException.php
Normal file
13
src/Core/Cache/Exception/InvalidCacheDriverException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class InvalidCacheDriverException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
125
src/Core/Cache/Factory/Cache.php
Normal file
125
src/Core/Cache/Factory/Cache.php
Normal file
|
@ -0,0 +1,125 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache\Factory;
|
||||
|
||||
use Friendica\App\BaseURL;
|
||||
use Friendica\Core\Cache\Enum;
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
|
||||
use Friendica\Core\Cache\Type;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class CacheFactory
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*
|
||||
* A basic class to generate a CacheDriver
|
||||
*/
|
||||
class Cache
|
||||
{
|
||||
/**
|
||||
* @var string The default cache if nothing set
|
||||
*/
|
||||
const DEFAULT_TYPE = Enum\Type::DATABASE;
|
||||
|
||||
/**
|
||||
* @var IManageConfigValues The IConfiguration to read parameters out of the config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Database The database connection in case that the cache is used the dba connection
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @var string The hostname, used as Prefix for Caching
|
||||
*/
|
||||
private $hostname;
|
||||
|
||||
/**
|
||||
* @var Profiler The optional profiler if the cached should be profiled
|
||||
*/
|
||||
private $profiler;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface The Friendica Logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(BaseURL $baseURL, IManageConfigValues $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
||||
{
|
||||
$this->hostname = $baseURL->getHostname();
|
||||
$this->config = $config;
|
||||
$this->dba = $dba;
|
||||
$this->profiler = $profiler;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method creates a CacheDriver for the given cache driver name
|
||||
*
|
||||
* @param string|null $type The cache type to create (default is per config)
|
||||
*
|
||||
* @return ICanCache The instance of the CacheDriver
|
||||
*
|
||||
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
|
||||
* @throws CachePersistenceException In case the underlying cache has errors during persistence
|
||||
*/
|
||||
public function create(string $type = null): ICanCache
|
||||
{
|
||||
if (empty($type)) {
|
||||
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
|
||||
}
|
||||
|
||||
switch ($type) {
|
||||
case Enum\Type::MEMCACHE:
|
||||
$cache = new Type\MemcacheCache($this->hostname, $this->config);
|
||||
break;
|
||||
case Enum\Type::MEMCACHED:
|
||||
$cache = new Type\MemcachedCache($this->hostname, $this->config, $this->logger);
|
||||
break;
|
||||
case Enum\Type::REDIS:
|
||||
$cache = new Type\RedisCache($this->hostname, $this->config);
|
||||
break;
|
||||
case Enum\Type::APCU:
|
||||
$cache = new Type\APCuCache($this->hostname);
|
||||
break;
|
||||
default:
|
||||
$cache = new Type\DatabaseCache($this->hostname, $this->dba);
|
||||
}
|
||||
|
||||
$profiling = $this->config->get('system', 'profiling', false);
|
||||
|
||||
// In case profiling is enabled, wrap the ProfilerCache around the current cache
|
||||
if (isset($profiling) && $profiling !== false) {
|
||||
return new Type\ProfilerCacheDecorator($cache, $this->profiler);
|
||||
} else {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,26 +19,30 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Enum\Type;
|
||||
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
|
||||
|
||||
/**
|
||||
* APCu Cache.
|
||||
*/
|
||||
class APCuCache extends BaseCache implements IMemoryCache
|
||||
class APCuCache extends AbstractCache implements ICanCacheInMemory
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
use CompareSetTrait;
|
||||
use CompareDeleteTrait;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @param string $hostname
|
||||
*
|
||||
* @throws InvalidCacheDriverException
|
||||
*/
|
||||
public function __construct(string $hostname)
|
||||
{
|
||||
if (!self::isAvailable()) {
|
||||
throw new Exception('APCu is not available.');
|
||||
throw new InvalidCacheDriverException('APCu is not available.');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
|
@ -47,9 +51,9 @@ class APCuCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
public function getAllKeys(?string $prefix = null): array
|
||||
{
|
||||
$ns = $this->getCacheKey($prefix);
|
||||
$ns = $this->getCacheKey($prefix ?? '');
|
||||
$ns = preg_quote($ns, '/');
|
||||
|
||||
if (class_exists('\APCIterator')) {
|
||||
|
@ -69,12 +73,11 @@ class APCuCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function get($key)
|
||||
public function get(string $key)
|
||||
{
|
||||
$return = null;
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
$cached = apcu_fetch($cachekey, $success);
|
||||
$cached = apcu_fetch($cacheKey, $success);
|
||||
if (!$success) {
|
||||
return null;
|
||||
}
|
||||
|
@ -85,30 +88,30 @@ class APCuCache extends BaseCache implements IMemoryCache
|
|||
// We also check if the db entry is a serialized
|
||||
// boolean 'false' value (which we want to return).
|
||||
if ($cached === serialize(false) || $value !== false) {
|
||||
$return = $value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $return;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
$cached = serialize($value);
|
||||
|
||||
if ($ttl > 0) {
|
||||
return apcu_store(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
$cached,
|
||||
$ttl
|
||||
);
|
||||
} else {
|
||||
return apcu_store(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
$cached
|
||||
);
|
||||
}
|
||||
|
@ -117,16 +120,16 @@ class APCuCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function delete($key)
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
return apcu_delete($cachekey);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
return apcu_delete($cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
public function clear(bool $outdated = true): bool
|
||||
{
|
||||
if ($outdated) {
|
||||
return true;
|
||||
|
@ -147,15 +150,15 @@ class APCuCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cached = serialize($value);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
$cached = serialize($value);
|
||||
|
||||
return apcu_add($cachekey, $cached);
|
||||
return apcu_add($cacheKey, $cached);
|
||||
}
|
||||
|
||||
public static function isAvailable()
|
||||
public static function isAvailable(): bool
|
||||
{
|
||||
if (!extension_loaded('apcu')) {
|
||||
return false;
|
||||
|
@ -174,7 +177,7 @@ class APCuCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return Type::APCU;
|
||||
}
|
|
@ -19,14 +19,14 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Friendica\Core\Cache\ICache;
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
|
||||
/**
|
||||
* Abstract class for common used functions
|
||||
*/
|
||||
abstract class BaseCache implements ICache
|
||||
abstract class AbstractCache implements ICanCache
|
||||
{
|
||||
/**
|
||||
* @var string The hostname
|
||||
|
@ -42,9 +42,8 @@ abstract class BaseCache implements ICache
|
|||
* Returns the prefix (to avoid namespace conflicts)
|
||||
*
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getPrefix()
|
||||
protected function getPrefix(): string
|
||||
{
|
||||
// We fetch with the hostname as key to avoid problems with other applications
|
||||
return $this->hostName;
|
||||
|
@ -52,19 +51,20 @@ abstract class BaseCache implements ICache
|
|||
|
||||
/**
|
||||
* @param string $key The original key
|
||||
*
|
||||
* @return string The cache key used for the cache
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getCacheKey($key)
|
||||
protected function getCacheKey(string $key): string
|
||||
{
|
||||
return $this->getPrefix() . ":" . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $keys A list of cached keys
|
||||
* @return array A list of original keys
|
||||
* @param string[] $keys A list of cached keys
|
||||
*
|
||||
* @return string[] A list of original keys
|
||||
*/
|
||||
protected function getOriginalKeys($keys)
|
||||
protected function getOriginalKeys(array $keys): array
|
||||
{
|
||||
if (empty($keys)) {
|
||||
return [];
|
||||
|
@ -84,12 +84,12 @@ abstract class BaseCache implements ICache
|
|||
* Filters the keys of an array with a given prefix
|
||||
* Returns the filtered keys as an new array
|
||||
*
|
||||
* @param array $keys The keys, which should get filtered
|
||||
* @param string[] $keys The keys, which should get filtered
|
||||
* @param string|null $prefix The prefix (if null, all keys will get returned)
|
||||
*
|
||||
* @return array The filtered array with just the keys
|
||||
* @return string[] The filtered array with just the keys
|
||||
*/
|
||||
protected function filterArrayKeysByPrefix(array $keys, string $prefix = null)
|
||||
protected function filterArrayKeysByPrefix(array $keys, string $prefix = null): array
|
||||
{
|
||||
if (empty($prefix)) {
|
||||
return $keys;
|
|
@ -19,24 +19,25 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Enum;
|
||||
|
||||
/**
|
||||
* Implementation of the IMemoryCache mainly for testing purpose
|
||||
*/
|
||||
class ArrayCache extends BaseCache implements IMemoryCache
|
||||
class ArrayCache extends AbstractCache implements ICanCacheInMemory
|
||||
{
|
||||
use TraitCompareDelete;
|
||||
use CompareDeleteTrait;
|
||||
|
||||
/** @var array Array with the cached data */
|
||||
protected $cachedData = array();
|
||||
protected $cachedData = [];
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
public function getAllKeys(?string $prefix = null): array
|
||||
{
|
||||
return $this->filterArrayKeysByPrefix(array_keys($this->cachedData), $prefix);
|
||||
}
|
||||
|
@ -44,7 +45,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function get($key)
|
||||
public function get(string $key)
|
||||
{
|
||||
if (isset($this->cachedData[$key])) {
|
||||
return $this->cachedData[$key];
|
||||
|
@ -55,7 +56,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function set(string $key, $value, int $ttl = Enum\Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$this->cachedData[$key] = $value;
|
||||
return true;
|
||||
|
@ -64,7 +65,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function delete($key)
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
unset($this->cachedData[$key]);
|
||||
return true;
|
||||
|
@ -73,7 +74,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
public function clear(bool $outdated = true): bool
|
||||
{
|
||||
// Array doesn't support TTL so just don't delete something
|
||||
if ($outdated) {
|
||||
|
@ -87,7 +88,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function add(string $key, $value, int $ttl = Enum\Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
if (isset($this->cachedData[$key])) {
|
||||
return false;
|
||||
|
@ -99,7 +100,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
||||
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Enum\Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
if ($this->get($key) === $oldValue) {
|
||||
return $this->set($key, $newValue);
|
||||
|
@ -111,8 +112,8 @@ class ArrayCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return Type::ARRAY;
|
||||
return Enum\Type::ARRAY;
|
||||
}
|
||||
}
|
|
@ -19,31 +19,33 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
|
||||
/**
|
||||
* Trait TraitCompareSetDelete
|
||||
*
|
||||
* This Trait is to compensate non native "exclusive" sets/deletes in caches
|
||||
* This Trait is to compensate nonnative "exclusive" sets/deletes in caches
|
||||
*/
|
||||
trait TraitCompareDelete
|
||||
trait CompareDeleteTrait
|
||||
{
|
||||
abstract public function get($key);
|
||||
abstract public function get(string $key);
|
||||
|
||||
abstract public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
abstract public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
abstract public function delete($key);
|
||||
abstract public function delete(string $key);
|
||||
|
||||
abstract public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
abstract public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* NonNative - Compares if the old value is set and removes it
|
||||
*
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The old value we know and want to delete
|
||||
* @param string $key The cache key
|
||||
* @param mixed $value The old value we know and want to delete
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function compareDelete($key, $value) {
|
||||
public function compareDelete(string $key, $value): bool
|
||||
{
|
||||
if ($this->add($key . "_lock", true)) {
|
||||
if ($this->get($key) === $value) {
|
||||
$this->delete($key);
|
|
@ -19,40 +19,41 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
|
||||
/**
|
||||
* Trait TraitCompareSetDelete
|
||||
*
|
||||
* This Trait is to compensate non native "exclusive" sets/deletes in caches
|
||||
* This Trait is to compensate nonnative "exclusive" sets/deletes in caches
|
||||
*/
|
||||
trait TraitCompareSet
|
||||
trait CompareSetTrait
|
||||
{
|
||||
abstract public function get($key);
|
||||
abstract public function get(string $key);
|
||||
|
||||
abstract public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
abstract public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
abstract public function delete($key);
|
||||
abstract public function delete(string $key);
|
||||
|
||||
abstract public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
abstract public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* NonNative - Compares if the old value is set and sets the new value
|
||||
*
|
||||
* @param string $key The cache key
|
||||
* @param mixed $oldValue The old value we know from the cache
|
||||
* @param mixed $newValue The new value we want to set
|
||||
* @param string $key The cache key
|
||||
* @param mixed $oldValue The old value we know from the cache
|
||||
* @param mixed $newValue The new value we want to set
|
||||
* @param int $ttl The cache lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES) {
|
||||
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
if ($this->add($key . "_lock", true)) {
|
||||
if ($this->get($key) === $oldValue) {
|
||||
$this->set($key, $newValue, $ttl);
|
||||
$this->delete($key . "_lock");
|
||||
return true;
|
||||
} else {
|
||||
} else {
|
||||
$this->delete($key . "_lock");
|
||||
return false;
|
||||
}
|
165
src/Core/Cache/Type/DatabaseCache.php
Normal file
165
src/Core/Cache/Type/DatabaseCache.php
Normal file
|
@ -0,0 +1,165 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Enum;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
* Database Cache
|
||||
*/
|
||||
class DatabaseCache extends AbstractCache implements ICanCache
|
||||
{
|
||||
/**
|
||||
* @var Database
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
public function __construct(string $hostname, Database $dba)
|
||||
{
|
||||
parent::__construct($hostname);
|
||||
|
||||
$this->dba = $dba;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*
|
||||
* @throws CachePersistenceException
|
||||
*/
|
||||
public function getAllKeys(?string $prefix = null): array
|
||||
{
|
||||
try {
|
||||
if (empty($prefix)) {
|
||||
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
|
||||
} else {
|
||||
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
||||
}
|
||||
|
||||
$stmt = $this->dba->select('cache', ['k'], $where);
|
||||
|
||||
$keys = [];
|
||||
while ($key = $this->dba->fetch($stmt)) {
|
||||
array_push($keys, $key['k']);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new CachePersistenceException(sprintf('Cannot fetch all keys with prefix %s', $prefix), $exception);
|
||||
} finally {
|
||||
$this->dba->close($stmt);
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
try {
|
||||
$cache = $this->dba->selectFirst('cache', ['v'], [
|
||||
'`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()
|
||||
]);
|
||||
|
||||
if ($this->dba->isResult($cache)) {
|
||||
$cached = $cache['v'];
|
||||
$value = @unserialize($cached);
|
||||
|
||||
// Only return a value if the serialized value is valid.
|
||||
// We also check if the db entry is a serialized
|
||||
// boolean 'false' value (which we want to return).
|
||||
if ($cached === serialize(false) || $value !== false) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new CachePersistenceException(sprintf('Cannot get cache entry with key %s', $key), $exception);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = Enum\Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
try {
|
||||
if ($ttl > 0) {
|
||||
$fields = [
|
||||
'v' => serialize($value),
|
||||
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
|
||||
'updated' => DateTimeFormat::utcNow()
|
||||
];
|
||||
} else {
|
||||
$fields = [
|
||||
'v' => serialize($value),
|
||||
'expires' => -1,
|
||||
'updated' => DateTimeFormat::utcNow()
|
||||
];
|
||||
}
|
||||
|
||||
return $this->dba->update('cache', $fields, ['k' => $key], true);
|
||||
} catch (\Exception $exception) {
|
||||
throw new CachePersistenceException(sprintf('Cannot set cache entry with key %s', $key), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
try {
|
||||
return $this->dba->delete('cache', ['k' => $key]);
|
||||
} catch (\Exception $exception) {
|
||||
throw new CachePersistenceException(sprintf('Cannot delete cache entry with key %s', $key), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear(bool $outdated = true): bool
|
||||
{
|
||||
try {
|
||||
if ($outdated) {
|
||||
return $this->dba->delete('cache', ['`expires` < NOW()']);
|
||||
} else {
|
||||
return $this->dba->delete('cache', ['`k` IS NOT NULL ']);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new CachePersistenceException('Cannot clear cache', $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return Enum\Type::DATABASE;
|
||||
}
|
||||
}
|
|
@ -19,21 +19,24 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Enum\Type;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Memcache;
|
||||
|
||||
/**
|
||||
* Memcache Cache
|
||||
*/
|
||||
class MemcacheCache extends BaseCache implements IMemoryCache
|
||||
class MemcacheCache extends AbstractCache implements ICanCacheInMemory
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
use TraitMemcacheCommand;
|
||||
use CompareSetTrait;
|
||||
use CompareDeleteTrait;
|
||||
use MemcacheCommandTrait;
|
||||
|
||||
/**
|
||||
* @var Memcache
|
||||
|
@ -41,30 +44,34 @@ class MemcacheCache extends BaseCache implements IMemoryCache
|
|||
private $memcache;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @param string $hostname
|
||||
* @param IManageConfigValues $config
|
||||
*
|
||||
* @throws InvalidCacheDriverException
|
||||
* @throws CachePersistenceException
|
||||
*/
|
||||
public function __construct(string $hostname, IConfig $config)
|
||||
public function __construct(string $hostname, IManageConfigValues $config)
|
||||
{
|
||||
if (!class_exists('Memcache', false)) {
|
||||
throw new Exception('Memcache class isn\'t available');
|
||||
throw new InvalidCacheDriverException('Memcache class isn\'t available');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
|
||||
$this->memcache = new Memcache();
|
||||
|
||||
$this->server = $config->get('system', 'memcache_host');;
|
||||
$this->port = $config->get('system', 'memcache_port');
|
||||
$this->server = $config->get('system', 'memcache_host');
|
||||
$this->port = $config->get('system', 'memcache_port');
|
||||
|
||||
if (!@$this->memcache->connect($this->server, $this->port)) {
|
||||
throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
|
||||
throw new CachePersistenceException('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
public function getAllKeys(?string $prefix = null): array
|
||||
{
|
||||
$keys = $this->getOriginalKeys($this->getMemcacheKeys());
|
||||
|
||||
|
@ -74,17 +81,16 @@ class MemcacheCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function get($key)
|
||||
public function get(string $key)
|
||||
{
|
||||
$return = null;
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
// We fetch with the hostname as key to avoid problems with other applications
|
||||
$cached = $this->memcache->get($cachekey);
|
||||
$cached = $this->memcache->get($cacheKey);
|
||||
|
||||
// @see http://php.net/manual/en/memcache.get.php#84275
|
||||
if (is_bool($cached) || is_double($cached) || is_long($cached)) {
|
||||
return $return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = @unserialize($cached);
|
||||
|
@ -93,30 +99,30 @@ class MemcacheCache extends BaseCache implements IMemoryCache
|
|||
// We also check if the db entry is a serialized
|
||||
// boolean 'false' value (which we want to return).
|
||||
if ($cached === serialize(false) || $value !== false) {
|
||||
$return = $value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $return;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
// We store with the hostname as key to avoid problems with other applications
|
||||
if ($ttl > 0) {
|
||||
return $this->memcache->set(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
serialize($value),
|
||||
MEMCACHE_COMPRESSED,
|
||||
time() + $ttl
|
||||
);
|
||||
} else {
|
||||
return $this->memcache->set(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
serialize($value),
|
||||
MEMCACHE_COMPRESSED
|
||||
);
|
||||
|
@ -126,16 +132,16 @@ class MemcacheCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function delete($key)
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcache->delete($cachekey);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
return $this->memcache->delete($cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
public function clear(bool $outdated = true): bool
|
||||
{
|
||||
if ($outdated) {
|
||||
return true;
|
||||
|
@ -147,16 +153,16 @@ class MemcacheCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
return $this->memcache->add($cacheKey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return Type::MEMCACHE;
|
||||
}
|
|
@ -19,9 +19,9 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Friendica\Network\HTTPException\ServiceUnavailableException;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
|
||||
/**
|
||||
* Trait for Memcache to add a custom version of the
|
||||
|
@ -29,7 +29,7 @@ use Friendica\Network\HTTPException\ServiceUnavailableException;
|
|||
*
|
||||
* Adds the possibility to directly communicate with the memcache too
|
||||
*/
|
||||
trait TraitMemcacheCommand
|
||||
trait MemcacheCommandTrait
|
||||
{
|
||||
/**
|
||||
* @var string server address
|
||||
|
@ -52,23 +52,19 @@ trait TraitMemcacheCommand
|
|||
*
|
||||
* @return array All keys of the memcache instance
|
||||
*
|
||||
* @throws ServiceUnavailableException
|
||||
* @throws CachePersistenceException
|
||||
*/
|
||||
protected function getMemcacheKeys()
|
||||
protected function getMemcacheKeys(): array
|
||||
{
|
||||
$string = $this->sendMemcacheCommand("stats items");
|
||||
$lines = explode("\r\n", $string);
|
||||
$slabs = [];
|
||||
$keys = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
|
||||
if (preg_match("/STAT items:([\d]+):number ([\d]+)/", $line, $matches) &&
|
||||
isset($matches[1]) &&
|
||||
!in_array($matches[1], $keys)) {
|
||||
|
||||
$slabs[] = $matches[1];
|
||||
$string = $this->sendMemcacheCommand("stats cachedump " . $matches[1] . " " . $matches[2]);
|
||||
isset($matches[1]) &&
|
||||
!in_array($matches[1], $keys)) {
|
||||
$string = $this->sendMemcacheCommand("stats cachedump " . $matches[1] . " " . $matches[2]);
|
||||
preg_match_all("/ITEM (.*?) /", $string, $matches);
|
||||
$keys = array_merge($keys, $matches[1]);
|
||||
}
|
||||
|
@ -88,20 +84,19 @@ trait TraitMemcacheCommand
|
|||
*
|
||||
* @return string The returned buffer result
|
||||
*
|
||||
* @throws ServiceUnavailableException In case the memcache server isn't available (anymore)
|
||||
* @throws CachePersistenceException In case the memcache server isn't available (anymore)
|
||||
*/
|
||||
protected function sendMemcacheCommand(string $command)
|
||||
protected function sendMemcacheCommand(string $command): string
|
||||
{
|
||||
$s = @fsockopen($this->server, $this->port);
|
||||
if (!$s) {
|
||||
throw new ServiceUnavailableException("Cant connect to:" . $this->server . ':' . $this->port);
|
||||
throw new CachePersistenceException("Cant connect to:" . $this->server . ':' . $this->port);
|
||||
}
|
||||
|
||||
fwrite($s, $command . "\r\n");
|
||||
$buf = '';
|
||||
|
||||
while (!feof($s)) {
|
||||
|
||||
$buf .= fgets($s, 256);
|
||||
|
||||
if (strpos($buf, "END\r\n") !== false) { // stat says end
|
|
@ -19,22 +19,25 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Enum\Type;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Memcached;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Memcached Cache
|
||||
*/
|
||||
class MemcachedCache extends BaseCache implements IMemoryCache
|
||||
class MemcachedCache extends AbstractCache implements ICanCacheInMemory
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
use TraitMemcacheCommand;
|
||||
use CompareSetTrait;
|
||||
use CompareDeleteTrait;
|
||||
use MemcacheCommandTrait;
|
||||
|
||||
/**
|
||||
* @var \Memcached
|
||||
|
@ -53,14 +56,17 @@ class MemcachedCache extends BaseCache implements IMemoryCache
|
|||
* 1 => ...
|
||||
* }
|
||||
*
|
||||
* @param array $memcached_hosts
|
||||
* @param string $hostname
|
||||
* @param IManageConfigValues $config
|
||||
* @param LoggerInterface $logger
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws InvalidCacheDriverException
|
||||
* @throws CachePersistenceException
|
||||
*/
|
||||
public function __construct(string $hostname, IConfig $config, LoggerInterface $logger)
|
||||
public function __construct(string $hostname, IManageConfigValues $config, LoggerInterface $logger)
|
||||
{
|
||||
if (!class_exists('Memcached', false)) {
|
||||
throw new Exception('Memcached class isn\'t available');
|
||||
throw new InvalidCacheDriverException('Memcached class isn\'t available');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
|
@ -78,19 +84,19 @@ class MemcachedCache extends BaseCache implements IMemoryCache
|
|||
});
|
||||
|
||||
$this->server = $memcached_hosts[0][0] ?? 'localhost';
|
||||
$this->port = $memcached_hosts[0][1] ?? 11211;
|
||||
$this->port = $memcached_hosts[0][1] ?? 11211;
|
||||
|
||||
$this->memcached->addServers($memcached_hosts);
|
||||
|
||||
if (count($this->memcached->getServerList()) == 0) {
|
||||
throw new Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
|
||||
throw new CachePersistenceException('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
public function getAllKeys(?string $prefix = null): array
|
||||
{
|
||||
$keys = $this->getOriginalKeys($this->getMemcacheKeys());
|
||||
|
||||
|
@ -100,40 +106,40 @@ class MemcachedCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function get($key)
|
||||
public function get(string $key)
|
||||
{
|
||||
$return = null;
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
// We fetch with the hostname as key to avoid problems with other applications
|
||||
$value = $this->memcached->get($cachekey);
|
||||
$value = $this->memcached->get($cacheKey);
|
||||
|
||||
if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
|
||||
$return = $value;
|
||||
return $value;
|
||||
} elseif ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND) {
|
||||
$this->logger->notice('Try to use unknown key.', ['key' => $key]);
|
||||
return null;
|
||||
} else {
|
||||
$this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]);
|
||||
throw new CachePersistenceException(sprintf('Cannot get cache entry with key %s', $key), new \MemcachedException($this->memcached->getResultMessage(), $this->memcached->getResultCode()));
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
// We store with the hostname as key to avoid problems with other applications
|
||||
if ($ttl > 0) {
|
||||
return $this->memcached->set(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
$value,
|
||||
$ttl
|
||||
);
|
||||
} else {
|
||||
return $this->memcached->set(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
@ -142,16 +148,16 @@ class MemcachedCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function delete($key)
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcached->delete($cachekey);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
return $this->memcached->delete($cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
public function clear(bool $outdated = true): bool
|
||||
{
|
||||
if ($outdated) {
|
||||
return true;
|
||||
|
@ -163,16 +169,16 @@ class MemcachedCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcached->add($cachekey, $value, $ttl);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
return $this->memcached->add($cacheKey, $value, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return Type::MEMCACHED;
|
||||
}
|
|
@ -19,20 +19,22 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Util\Profiler;
|
||||
|
||||
/**
|
||||
* This class wraps cache driver so they can get profiled - in case the profiler is enabled
|
||||
* This class wraps cache driver, so they can get profiled - in case the profiler is enabled
|
||||
*
|
||||
* It is using the decorator pattern (@see
|
||||
* It is using the decorator pattern (@see https://en.wikipedia.org/wiki/Decorator_pattern )
|
||||
*/
|
||||
class ProfilerCache implements ICache, IMemoryCache
|
||||
class ProfilerCacheDecorator implements ICanCache, ICanCacheInMemory
|
||||
{
|
||||
/**
|
||||
* @var ICache The original cache driver
|
||||
* @var ICanCache The original cache driver
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
|
@ -41,7 +43,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
*/
|
||||
private $profiler;
|
||||
|
||||
public function __construct(ICache $cache, Profiler $profiler)
|
||||
public function __construct(ICanCache $cache, Profiler $profiler)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->profiler = $profiler;
|
||||
|
@ -50,7 +52,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
public function getAllKeys(?string $prefix = null): array
|
||||
{
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
|
@ -64,7 +66,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get($key)
|
||||
public function get(string $key)
|
||||
{
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
|
@ -78,7 +80,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
|
@ -92,7 +94,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete($key)
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
|
@ -106,7 +108,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
public function clear(bool $outdated = true): bool
|
||||
{
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
|
@ -120,9 +122,9 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
if ($this->cache instanceof ICanCacheInMemory) {
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
$return = $this->cache->add($key, $value, $ttl);
|
||||
|
@ -138,9 +140,9 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
||||
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
if ($this->cache instanceof ICanCacheInMemory) {
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
$return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
|
||||
|
@ -156,9 +158,9 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function compareDelete($key, $value)
|
||||
public function compareDelete(string $key, $value): bool
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
if ($this->cache instanceof ICanCacheInMemory) {
|
||||
$this->profiler->startRecording('cache');
|
||||
|
||||
$return = $this->cache->compareDelete($key, $value);
|
||||
|
@ -174,7 +176,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function GetName()
|
||||
public function GetName(): string
|
||||
{
|
||||
return $this->cache->getName() . ' (with profiler)';
|
||||
}
|
|
@ -19,17 +19,21 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core\Cache\Type;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Enum\Type;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Redis;
|
||||
|
||||
/**
|
||||
* Redis Cache. This driver is based on Memcache driver
|
||||
*/
|
||||
class RedisCache extends BaseCache implements IMemoryCache
|
||||
class RedisCache extends AbstractCache implements ICanCacheInMemory
|
||||
{
|
||||
/**
|
||||
* @var Redis
|
||||
|
@ -37,12 +41,13 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
private $redis;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws InvalidCacheDriverException
|
||||
* @throws CachePersistenceException
|
||||
*/
|
||||
public function __construct(string $hostname, IConfig $config)
|
||||
public function __construct(string $hostname, IManageConfigValues $config)
|
||||
{
|
||||
if (!class_exists('Redis', false)) {
|
||||
throw new Exception('Redis class isn\'t available');
|
||||
throw new InvalidCacheDriverException('Redis class isn\'t available');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
|
@ -55,24 +60,24 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
$redis_db = $config->get('system', 'redis_db', 0);
|
||||
|
||||
if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
|
||||
throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
|
||||
throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
|
||||
} elseif (!@$this->redis->connect($redis_host)) {
|
||||
throw new Exception('Expected Redis server at ' . $redis_host . ' isn\'t available');
|
||||
throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ' isn\'t available');
|
||||
}
|
||||
|
||||
if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
|
||||
throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
|
||||
throw new CachePersistenceException('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
|
||||
}
|
||||
|
||||
if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
|
||||
throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
|
||||
throw new CachePersistenceException('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
public function getAllKeys(?string $prefix = null): array
|
||||
{
|
||||
if (empty($prefix)) {
|
||||
$search = '*';
|
||||
|
@ -88,13 +93,13 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function get($key)
|
||||
public function get(string $key)
|
||||
{
|
||||
$return = null;
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$return = null;
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
$cached = $this->redis->get($cachekey);
|
||||
if ($cached === false && !$this->redis->exists($cachekey)) {
|
||||
$cached = $this->redis->get($cacheKey);
|
||||
if ($cached === false && !$this->redis->exists($cacheKey)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -113,21 +118,21 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
$cached = serialize($value);
|
||||
|
||||
if ($ttl > 0) {
|
||||
return $this->redis->setex(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
$ttl,
|
||||
$cached
|
||||
);
|
||||
} else {
|
||||
return $this->redis->set(
|
||||
$cachekey,
|
||||
$cacheKey,
|
||||
$cached
|
||||
);
|
||||
}
|
||||
|
@ -136,10 +141,10 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function delete($key)
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$this->redis->del($cachekey);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
$this->redis->del($cacheKey);
|
||||
// Redis doesn't have an error state for del()
|
||||
return true;
|
||||
}
|
||||
|
@ -147,7 +152,7 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
public function clear(bool $outdated = true): bool
|
||||
{
|
||||
if ($outdated) {
|
||||
return true;
|
||||
|
@ -159,34 +164,30 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cached = serialize($value);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
$cached = serialize($value);
|
||||
|
||||
return $this->redis->setnx($cachekey, $cached);
|
||||
return $this->redis->setnx($cacheKey, $cached);
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
||||
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
$newCached = serialize($newValue);
|
||||
|
||||
$this->redis->watch($cachekey);
|
||||
$this->redis->watch($cacheKey);
|
||||
// If the old value isn't what we expected, somebody else changed the key meanwhile
|
||||
if ($this->get($key) === $oldValue) {
|
||||
if ($ttl > 0) {
|
||||
$result = $this->redis->multi()
|
||||
->setex($cachekey, $ttl, $newCached)
|
||||
->exec();
|
||||
$result = $this->redis->multi()->setex($cacheKey, $ttl, $newCached)->exec();
|
||||
} else {
|
||||
$result = $this->redis->multi()
|
||||
->set($cachekey, $newCached)
|
||||
->exec();
|
||||
$result = $this->redis->multi()->set($cacheKey, $newCached)->exec();
|
||||
}
|
||||
return $result !== false;
|
||||
}
|
||||
|
@ -197,17 +198,15 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function compareDelete($key, $value)
|
||||
public function compareDelete(string $key, $value): bool
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cacheKey = $this->getCacheKey($key);
|
||||
|
||||
$this->redis->watch($cachekey);
|
||||
$this->redis->watch($cacheKey);
|
||||
// If the old value isn't what we expected, somebody else changed the key meanwhile
|
||||
if ($this->get($key) === $value) {
|
||||
$result = $this->redis->multi()
|
||||
->del($cachekey)
|
||||
->exec();
|
||||
return $result !== false;
|
||||
$this->redis->multi()->del($cacheKey)->exec();
|
||||
return true;
|
||||
}
|
||||
$this->redis->unwatch();
|
||||
return false;
|
||||
|
@ -216,7 +215,7 @@ class RedisCache extends BaseCache implements IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return Type::REDIS;
|
||||
}
|
|
@ -19,30 +19,35 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config;
|
||||
namespace Friendica\Core\Config\Capability;
|
||||
|
||||
use Friendica\Core\Config\Exception\ConfigPersistenceException;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
|
||||
/**
|
||||
* Interface for accessing system wide configurations
|
||||
* Interface for accessing system-wide configurations
|
||||
*/
|
||||
interface IConfig
|
||||
interface IManageConfigValues
|
||||
{
|
||||
|
||||
/**
|
||||
* Loads all configuration values of family into a cached storage.
|
||||
*
|
||||
* All configuration values of the system are stored in the cache ( @param string $cat The category of the configuration value
|
||||
* All configuration values of the system are stored in the cache.
|
||||
*
|
||||
* @param string $cat The category of the configuration value
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
function load(string $cat = 'config');
|
||||
public function load(string $cat = 'config');
|
||||
|
||||
/**
|
||||
* 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 ConfigCache).
|
||||
* and the $key from a cached storage either from the database or from the cache.
|
||||
*
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to query
|
||||
|
@ -50,8 +55,11 @@ interface IConfig
|
|||
* @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
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*
|
||||
*/
|
||||
function get(string $cat, string $key, $default_value = null, bool $refresh = false);
|
||||
public function get(string $cat, string $key, $default_value = null, bool $refresh = false);
|
||||
|
||||
/**
|
||||
* Sets a configuration value for system config
|
||||
|
@ -65,26 +73,30 @@ interface IConfig
|
|||
* @param mixed $value The value to store
|
||||
*
|
||||
* @return bool Operation success
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
function set(string $cat, string $key, $value);
|
||||
public function set(string $cat, string $key, $value): bool;
|
||||
|
||||
/**
|
||||
* 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).
|
||||
* Removes the configured value from the stored cache in the 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
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*
|
||||
*/
|
||||
function delete(string $cat, string $key);
|
||||
public function delete(string $cat, string $key): bool;
|
||||
|
||||
/**
|
||||
* Returns the Config Cache
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
function getCache();
|
||||
public function getCache(): Cache;
|
||||
}
|
13
src/Core/Config/Exception/ConfigFileException.php
Normal file
13
src/Core/Config/Exception/ConfigFileException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class ConfigFileException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
13
src/Core/Config/Exception/ConfigPersistenceException.php
Normal file
13
src/Core/Config/Exception/ConfigPersistenceException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class ConfigPersistenceException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
101
src/Core/Config/Factory/Config.php
Normal file
101
src/Core/Config/Factory/Config.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Factory;
|
||||
|
||||
use Friendica\Core\Config\Capability;
|
||||
use Friendica\Core\Config\Repository;
|
||||
use Friendica\Core\Config\Type;
|
||||
use Friendica\Core\Config\Util;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* The key of the $_SERVER variable to override the config directory
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const CONFIG_DIR_ENV = 'FRIENDICA_CONFIG_DIR';
|
||||
|
||||
/**
|
||||
* The Sub directory of the config-files
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const CONFIG_DIR = 'config';
|
||||
|
||||
/**
|
||||
* The Sub directory of the static config-files
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const STATIC_DIR = 'static';
|
||||
|
||||
/**
|
||||
* @param string $basePath The basepath of FRIENDICA
|
||||
* @param array $server The $_SERVER array
|
||||
*
|
||||
* @return Util\ConfigFileLoader
|
||||
*/
|
||||
public function createConfigFileLoader(string $basePath, array $server = []): Util\ConfigFileLoader
|
||||
{
|
||||
if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
|
||||
$configDir = $server[self::CONFIG_DIR_ENV];
|
||||
} else {
|
||||
$configDir = $basePath . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
|
||||
}
|
||||
$staticDir = $basePath . DIRECTORY_SEPARATOR . self::STATIC_DIR;
|
||||
|
||||
return new Util\ConfigFileLoader($basePath, $configDir, $staticDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Util\ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig)
|
||||
* @param array $server
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function createCache(Util\ConfigFileLoader $loader, array $server = []): Cache
|
||||
{
|
||||
$configCache = new Cache();
|
||||
$loader->setupCache($configCache, $server);
|
||||
|
||||
return $configCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The config cache of this adapter
|
||||
* @param Repository\Config $configRepo The configuration repository
|
||||
*
|
||||
* @return Capability\IManageConfigValues
|
||||
*/
|
||||
public function create(Cache $configCache, Repository\Config $configRepo)
|
||||
{
|
||||
if ($configCache->get('system', 'config_adapter') === 'preload') {
|
||||
$configuration = new Type\PreloadConfig($configCache, $configRepo);
|
||||
} else {
|
||||
$configuration = new Type\JitConfig($configCache, $configRepo);
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
}
|
187
src/Core/Config/Repository/Config.php
Normal file
187
src/Core/Config/Repository/Config.php
Normal file
|
@ -0,0 +1,187 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Repository;
|
||||
|
||||
use Friendica\Core\Config\Exception\ConfigPersistenceException;
|
||||
use Friendica\Core\Config\Util\ValueConversion;
|
||||
use Friendica\Database\Database;
|
||||
|
||||
/**
|
||||
* The Config Repository, which is using the general DB-model backend for configs
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
/** @var Database */
|
||||
protected $db;
|
||||
|
||||
public function __construct(Database $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
protected static $table_name = 'config';
|
||||
|
||||
/**
|
||||
* Checks if the model is currently connected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected(): bool
|
||||
{
|
||||
return $this->db->isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all configuration values and returns the loaded category as an array.
|
||||
*
|
||||
* @param string|null $cat The category of the configuration values to load
|
||||
*
|
||||
* @return array The config array
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function load(?string $cat = null): array
|
||||
{
|
||||
$return = [];
|
||||
|
||||
try {
|
||||
if (empty($cat)) {
|
||||
$configs = $this->db->select(static::$table_name, ['cat', 'v', 'k']);
|
||||
} else {
|
||||
$configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['cat' => $cat]);
|
||||
}
|
||||
|
||||
while ($config = $this->db->fetch($configs)) {
|
||||
$key = $config['k'];
|
||||
$value = ValueConversion::toConfigValue($config['v']);
|
||||
|
||||
// just save it in case it is set
|
||||
if (isset($value)) {
|
||||
$return[$config['cat']][$key] = $value;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new ConfigPersistenceException(sprintf('Cannot load config category %s', $cat), $exception);
|
||||
} finally {
|
||||
$this->db->close($configs);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a particular, system-wide config variable out of the DB with the
|
||||
* given category name ($cat) and a key ($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 array|string|null Stored value or null if it does not exist
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function get(string $cat, string $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$config = $this->db->selectFirst(static::$table_name, ['v'], ['cat' => $cat, 'k' => $key]);
|
||||
if ($this->db->isResult($config)) {
|
||||
$value = ValueConversion::toConfigValue($config['v']);
|
||||
|
||||
// just return it in case it is set
|
||||
if (isset($value)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new ConfigPersistenceException(sprintf('Cannot get config with category %s and key %s', $cat, $key), $exception);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
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);
|
||||
|
||||
if (isset($stored_value) && ($stored_value === $compare_value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$dbValue = ValueConversion::toDbValue($value);
|
||||
|
||||
try {
|
||||
return $this->db->update(static::$table_name, ['v' => $dbValue], ['cat' => $cat, 'k' => $key], true);
|
||||
} catch (\Exception $exception) {
|
||||
throw new ConfigPersistenceException(sprintf('Cannot set config with category %s and key %s', $cat, $key), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the configured value from the database.
|
||||
*
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to delete
|
||||
*
|
||||
* @return bool Operation success
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->db->delete(static::$table_name, ['cat' => $cat, 'k' => $key]);
|
||||
} catch (\Exception $exception) {
|
||||
throw new ConfigPersistenceException(sprintf('Cannot delete config with category %s and key %s', $cat, $key), $exception);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,19 +19,19 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core;
|
||||
namespace Friendica\Core\Config\Type;
|
||||
|
||||
use Friendica\Core\Config\Cache;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Model;
|
||||
use Friendica\Core\Config\Repository\Config;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
|
||||
/**
|
||||
* 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\ConfigCache)
|
||||
* - The Config-DB-Table (per Config-DB-model @see Model\Config\Config)
|
||||
* - The Config-Files (loaded into the FileCache @see Cache)
|
||||
* - The Config-Repository (per Config-Repository @see Config )
|
||||
*/
|
||||
abstract class BaseConfig implements IConfig
|
||||
abstract class AbstractConfig implements IManageConfigValues
|
||||
{
|
||||
/**
|
||||
* @var Cache
|
||||
|
@ -39,24 +39,24 @@ abstract class BaseConfig implements IConfig
|
|||
protected $configCache;
|
||||
|
||||
/**
|
||||
* @var Model\Config\Config
|
||||
* @var Config
|
||||
*/
|
||||
protected $configModel;
|
||||
protected $configRepo;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Model\Config\Config $configModel The configuration model
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configRepo The configuration repository
|
||||
*/
|
||||
public function __construct(Cache $configCache, Model\Config\Config $configModel)
|
||||
public function __construct(Cache $configCache, Config $configRepo)
|
||||
{
|
||||
$this->configCache = $configCache;
|
||||
$this->configModel = $configModel;
|
||||
$this->configRepo = $configRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getCache()
|
||||
public function getCache(): Cache
|
||||
{
|
||||
return $this->configCache;
|
||||
}
|
|
@ -19,10 +19,10 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config;
|
||||
namespace Friendica\Core\Config\Type;
|
||||
|
||||
use Friendica\Core\BaseConfig;
|
||||
use Friendica\Model;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\Config\Repository\Config;
|
||||
|
||||
/**
|
||||
* This class implements the Just-In-Time configuration, which will cache
|
||||
|
@ -31,7 +31,7 @@ use Friendica\Model;
|
|||
* Default Configuration type.
|
||||
* Provides the best performance for pages loading few configuration variables.
|
||||
*/
|
||||
class JitConfig extends BaseConfig
|
||||
class JitConfig extends AbstractConfig
|
||||
{
|
||||
/**
|
||||
* @var array Array of already loaded db values (even if there was no value)
|
||||
|
@ -39,12 +39,12 @@ class JitConfig extends BaseConfig
|
|||
private $db_loaded;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Model\Config\Config $configModel The configuration model
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configRepo The configuration model
|
||||
*/
|
||||
public function __construct(Cache $configCache, Model\Config\Config $configModel)
|
||||
public function __construct(Cache $configCache, Config $configRepo)
|
||||
{
|
||||
parent::__construct($configCache, $configModel);
|
||||
parent::__construct($configCache, $configRepo);
|
||||
$this->db_loaded = [];
|
||||
|
||||
$this->load();
|
||||
|
@ -52,16 +52,15 @@ class JitConfig extends BaseConfig
|
|||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
*/
|
||||
public function load(string $cat = 'config')
|
||||
{
|
||||
// If not connected, do nothing
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = $this->configModel->load($cat);
|
||||
$config = $this->configRepo->load($cat);
|
||||
|
||||
if (!empty($config[$cat])) {
|
||||
foreach ($config[$cat] as $key => $value) {
|
||||
|
@ -79,15 +78,14 @@ class JitConfig extends BaseConfig
|
|||
public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
|
||||
{
|
||||
// if the value isn't loaded or refresh is needed, load it to the cache
|
||||
if ($this->configModel->isConnected() &&
|
||||
(empty($this->db_loaded[$cat][$key]) ||
|
||||
$refresh)) {
|
||||
if ($this->configRepo->isConnected() &&
|
||||
(empty($this->db_loaded[$cat][$key]) ||
|
||||
$refresh)) {
|
||||
$dbValue = $this->configRepo->get($cat, $key);
|
||||
|
||||
$dbvalue = $this->configModel->get($cat, $key);
|
||||
|
||||
if (isset($dbvalue)) {
|
||||
$this->configCache->set($cat, $key, $dbvalue, Cache::SOURCE_DB);
|
||||
unset($dbvalue);
|
||||
if (isset($dbValue)) {
|
||||
$this->configCache->set($cat, $key, $dbValue, Cache::SOURCE_DB);
|
||||
unset($dbValue);
|
||||
}
|
||||
|
||||
$this->db_loaded[$cat][$key] = true;
|
||||
|
@ -102,17 +100,17 @@ class JitConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(string $cat, string $key, $value)
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
// set the cache first
|
||||
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
|
||||
|
||||
// If there is no connected adapter, we're finished
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$stored = $this->configModel->set($cat, $key, $value);
|
||||
$stored = $this->configRepo->set($cat, $key, $value);
|
||||
|
||||
$this->db_loaded[$cat][$key] = $stored;
|
||||
|
||||
|
@ -122,7 +120,7 @@ class JitConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
$cacheRemoved = $this->configCache->delete($cat, $key);
|
||||
|
||||
|
@ -130,11 +128,11 @@ class JitConfig extends BaseConfig
|
|||
unset($this->db_loaded[$cat][$key]);
|
||||
}
|
||||
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cacheRemoved;
|
||||
}
|
||||
|
||||
$storeRemoved = $this->configModel->delete($cat, $key);
|
||||
$storeRemoved = $this->configRepo->delete($cat, $key);
|
||||
|
||||
return $cacheRemoved || $storeRemoved;
|
||||
}
|
|
@ -19,10 +19,10 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config;
|
||||
namespace Friendica\Core\Config\Type;
|
||||
|
||||
use Friendica\Core\BaseConfig;
|
||||
use Friendica\Model;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\Config\Repository\Config;
|
||||
|
||||
/**
|
||||
* This class implements the preload configuration, which will cache
|
||||
|
@ -30,18 +30,18 @@ use Friendica\Model;
|
|||
*
|
||||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
||||
*/
|
||||
class PreloadConfig extends BaseConfig
|
||||
class PreloadConfig extends AbstractConfig
|
||||
{
|
||||
/** @var bool */
|
||||
private $config_loaded;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Model\Config\Config $configModel The configuration model
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configRepo The configuration model
|
||||
*/
|
||||
public function __construct(Cache $configCache, Model\Config\Config $configModel)
|
||||
public function __construct(Cache $configCache, Config $configRepo)
|
||||
{
|
||||
parent::__construct($configCache, $configModel);
|
||||
parent::__construct($configCache, $configRepo);
|
||||
$this->config_loaded = false;
|
||||
|
||||
$this->load();
|
||||
|
@ -51,7 +51,6 @@ class PreloadConfig extends BaseConfig
|
|||
* {@inheritDoc}
|
||||
*
|
||||
* This loads all config values everytime load is called
|
||||
*
|
||||
*/
|
||||
public function load(string $cat = 'config')
|
||||
{
|
||||
|
@ -61,11 +60,11 @@ class PreloadConfig extends BaseConfig
|
|||
}
|
||||
|
||||
// If not connected, do nothing
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = $this->configModel->load();
|
||||
$config = $this->configRepo->load();
|
||||
$this->config_loaded = true;
|
||||
|
||||
// load the whole category out of the DB into the cache
|
||||
|
@ -78,8 +77,8 @@ class PreloadConfig extends BaseConfig
|
|||
public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
|
||||
{
|
||||
if ($refresh) {
|
||||
if ($this->configModel->isConnected()) {
|
||||
$config = $this->configModel->get($cat, $key);
|
||||
if ($this->configRepo->isConnected()) {
|
||||
$config = $this->configRepo->get($cat, $key);
|
||||
if (isset($config)) {
|
||||
$this->configCache->set($cat, $key, $config, Cache::SOURCE_DB);
|
||||
}
|
||||
|
@ -95,7 +94,7 @@ class PreloadConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(string $cat, string $key, $value)
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
if (!$this->config_loaded) {
|
||||
$this->load();
|
||||
|
@ -105,11 +104,11 @@ class PreloadConfig extends BaseConfig
|
|||
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
|
||||
|
||||
// If there is no connected adapter, we're finished
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$stored = $this->configModel->set($cat, $key, $value);
|
||||
$stored = $this->configRepo->set($cat, $key, $value);
|
||||
|
||||
return $cached && $stored;
|
||||
}
|
||||
|
@ -117,7 +116,7 @@ class PreloadConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
if ($this->config_loaded) {
|
||||
$this->load();
|
||||
|
@ -125,26 +124,12 @@ class PreloadConfig extends BaseConfig
|
|||
|
||||
$cacheRemoved = $this->configCache->delete($cat, $key);
|
||||
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cacheRemoved;
|
||||
}
|
||||
|
||||
$storeRemoved = $this->configModel->delete($cat, $key);
|
||||
$storeRemoved = $this->configRepo->delete($cat, $key);
|
||||
|
||||
return $cacheRemoved || $storeRemoved;
|
||||
}
|
||||
|
||||
public function testSetDouble()
|
||||
{
|
||||
$this->configModel->shouldReceive('isConnected')
|
||||
->andReturn(true);
|
||||
|
||||
// constructor loading
|
||||
$this->configModel->shouldReceive('load')
|
||||
->with('config')
|
||||
->andReturn(['config' => ['test' => 'it']])
|
||||
->once();
|
||||
|
||||
parent::testSetDouble();
|
||||
}
|
||||
}
|
364
src/Core/Config/Util/ConfigFileLoader.php
Normal file
364
src/Core/Config/Util/ConfigFileLoader.php
Normal file
|
@ -0,0 +1,364 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Util;
|
||||
|
||||
use Friendica\Core\Addon;
|
||||
use Friendica\Core\Config\Exception\ConfigFileException;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
|
||||
/**
|
||||
* The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see Cache )
|
||||
*
|
||||
* It is capable of loading the following config files:
|
||||
* - *.config.php (current)
|
||||
* - *.ini.php (deprecated)
|
||||
* - *.htconfig.php (deprecated)
|
||||
*/
|
||||
class ConfigFileLoader
|
||||
{
|
||||
/**
|
||||
* The default name of the user defined ini file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const CONFIG_INI = 'local';
|
||||
|
||||
/**
|
||||
* The default name of the user defined legacy config file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const CONFIG_HTCONFIG = 'htconfig';
|
||||
|
||||
/**
|
||||
* The sample string inside the configs, which shouldn't get loaded
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SAMPLE_END = '-sample';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $baseDir;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $configDir;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $staticDir;
|
||||
|
||||
/**
|
||||
* @param string $baseDir The base
|
||||
* @param string $configDir
|
||||
* @param string $staticDir
|
||||
*/
|
||||
public function __construct(string $baseDir, string $configDir, string $staticDir)
|
||||
{
|
||||
$this->baseDir = $baseDir;
|
||||
$this->configDir = $configDir;
|
||||
$this->staticDir = $staticDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the configuration files into an configuration cache
|
||||
*
|
||||
* First loads the default value for all the configuration keys, then the legacy configuration files, then the
|
||||
* expected local.config.php
|
||||
*
|
||||
* @param Cache $config The config cache to load to
|
||||
* @param array $server The $_SERVER array
|
||||
* @param bool $raw Setup the raw config format
|
||||
*
|
||||
* @throws ConfigFileException
|
||||
*/
|
||||
public function setupCache(Cache $config, array $server = [], bool $raw = false)
|
||||
{
|
||||
// Load static config files first, the order is important
|
||||
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
|
||||
$config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
|
||||
|
||||
// try to load the legacy config first
|
||||
$config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
|
||||
$config->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
|
||||
|
||||
// Now load every other config you find inside the 'config/' directory
|
||||
$this->loadCoreConfig($config);
|
||||
|
||||
$config->load($this->loadEnvConfig($server), Cache::SOURCE_ENV);
|
||||
|
||||
// In case of install mode, add the found basepath (because there isn't a basepath set yet
|
||||
if (!$raw && empty($config->get('system', 'basepath'))) {
|
||||
// Setting at least the basepath we know
|
||||
$config->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the static core-configuration and returns the config array.
|
||||
*
|
||||
* @param string $name The name of the configuration
|
||||
*
|
||||
* @return array The config array (empty if no config found)
|
||||
*
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
private function loadStaticConfig(string $name): array
|
||||
{
|
||||
$configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
|
||||
$iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
|
||||
|
||||
if (file_exists($configName)) {
|
||||
return $this->loadConfigFile($configName);
|
||||
} elseif (file_exists($iniName)) {
|
||||
return $this->loadINIConfigFile($iniName);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specified core-configuration into the config cache.
|
||||
*
|
||||
* @param Cache $config The Config cache
|
||||
*
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
private function loadCoreConfig(Cache $config)
|
||||
{
|
||||
// try to load legacy ini-files first
|
||||
foreach ($this->getConfigFiles(true) as $configFile) {
|
||||
$config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
|
||||
}
|
||||
|
||||
// try to load supported config at last to overwrite it
|
||||
foreach ($this->getConfigFiles() as $configFile) {
|
||||
$config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specified addon-configuration and returns the config array.
|
||||
*
|
||||
* @param string $name The name of the configuration
|
||||
*
|
||||
* @return array The config array (empty if no config found)
|
||||
*
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
public function loadAddonConfig(string $name): array
|
||||
{
|
||||
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
|
||||
Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
|
||||
$name . DIRECTORY_SEPARATOR . // openstreetmap/
|
||||
'config'. DIRECTORY_SEPARATOR . // config/
|
||||
$name . ".config.php"; // openstreetmap.config.php
|
||||
|
||||
if (file_exists($filepath)) {
|
||||
return $this->loadConfigFile($filepath);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load environment specific variables, based on the `env.config.php` mapping table
|
||||
*
|
||||
* @param array $server The $_SERVER variable
|
||||
*
|
||||
* @return array The config array (empty if no config was found)
|
||||
*
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
public function loadEnvConfig(array $server): array
|
||||
{
|
||||
$filepath = $this->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/static/
|
||||
"env.config.php"; // env.config.php
|
||||
|
||||
if (!file_exists($filepath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$envConfig = $this->loadConfigFile($filepath);
|
||||
|
||||
$return = [];
|
||||
|
||||
foreach ($envConfig as $envKey => $configStructure) {
|
||||
if (isset($server[$envKey])) {
|
||||
$return[$configStructure[0]][$configStructure[1]] = $server[$envKey];
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the config files of the config-directory
|
||||
*
|
||||
* @param bool $ini True, if scan for ini-files instead of config files
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getConfigFiles(bool $ini = false): array
|
||||
{
|
||||
$files = scandir($this->configDir);
|
||||
$found = [];
|
||||
|
||||
$filePattern = ($ini ? '*.ini.php' : '*.config.php');
|
||||
|
||||
// Don't load sample files
|
||||
$sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
|
||||
|
||||
foreach ($files as $filename) {
|
||||
if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
|
||||
$found[] = $this->configDir . '/' . $filename;
|
||||
}
|
||||
}
|
||||
|
||||
return $found;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
|
||||
*
|
||||
* @param string $name The name of the config file (default is empty, which means .htconfig.php)
|
||||
*
|
||||
* @return array The configuration array (empty if no config found)
|
||||
*
|
||||
* @deprecated since version 2018.09
|
||||
*/
|
||||
private function loadLegacyConfig(string $name = ''): array
|
||||
{
|
||||
$name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
|
||||
$fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
|
||||
|
||||
$config = [];
|
||||
if (file_exists($fullName)) {
|
||||
$a = new \stdClass();
|
||||
$a->config = [];
|
||||
include $fullName;
|
||||
|
||||
$htConfigCategories = array_keys($a->config);
|
||||
|
||||
// map the legacy configuration structure to the current structure
|
||||
foreach ($htConfigCategories as $htConfigCategory) {
|
||||
if (is_array($a->config[$htConfigCategory])) {
|
||||
$keys = array_keys($a->config[$htConfigCategory]);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
|
||||
}
|
||||
} else {
|
||||
$config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
|
||||
}
|
||||
}
|
||||
|
||||
unset($a);
|
||||
|
||||
if (isset($db_host)) {
|
||||
$config['database']['hostname'] = $db_host;
|
||||
unset($db_host);
|
||||
}
|
||||
if (isset($db_user)) {
|
||||
$config['database']['username'] = $db_user;
|
||||
unset($db_user);
|
||||
}
|
||||
if (isset($db_pass)) {
|
||||
$config['database']['password'] = $db_pass;
|
||||
unset($db_pass);
|
||||
}
|
||||
if (isset($db_data)) {
|
||||
$config['database']['database'] = $db_data;
|
||||
unset($db_data);
|
||||
}
|
||||
if (isset($config['system']['db_charset'])) {
|
||||
$config['database']['charset'] = $config['system']['db_charset'];
|
||||
}
|
||||
if (isset($pidfile)) {
|
||||
$config['system']['pidfile'] = $pidfile;
|
||||
unset($pidfile);
|
||||
}
|
||||
if (isset($default_timezone)) {
|
||||
$config['system']['default_timezone'] = $default_timezone;
|
||||
unset($default_timezone);
|
||||
}
|
||||
if (isset($lang)) {
|
||||
$config['system']['language'] = $lang;
|
||||
unset($lang);
|
||||
}
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specified legacy configuration file and returns the config array.
|
||||
*
|
||||
* @param string $filepath
|
||||
*
|
||||
* @return array The configuration array
|
||||
* @throws ConfigFileException
|
||||
* @deprecated since version 2018.12
|
||||
*/
|
||||
private function loadINIConfigFile(string $filepath): array
|
||||
{
|
||||
$contents = include($filepath);
|
||||
|
||||
$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
|
||||
|
||||
if ($config === false) {
|
||||
throw new ConfigFileException('Error parsing INI config file ' . $filepath);
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specified configuration file and returns the config array.
|
||||
*
|
||||
* The config format is PHP array and the template for configuration files is the following:
|
||||
*
|
||||
* <?php return [
|
||||
* 'section' => [
|
||||
* 'key' => 'value',
|
||||
* ],
|
||||
* ];
|
||||
*
|
||||
* @param string $filepath The filepath of the
|
||||
*
|
||||
* @return array The config array0
|
||||
*
|
||||
* @throws ConfigFileException if the config cannot get loaded.
|
||||
*/
|
||||
private function loadConfigFile(string $filepath): array
|
||||
{
|
||||
$config = include($filepath);
|
||||
|
||||
if (!is_array($config)) {
|
||||
throw new ConfigFileException('Error loading config file ' . $filepath);
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
62
src/Core/Config/Util/ValueConversion.php
Normal file
62
src/Core/Config/Util/ValueConversion.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Util;
|
||||
|
||||
/**
|
||||
* Util class to help to convert from/to (p)config values
|
||||
*/
|
||||
class ValueConversion
|
||||
{
|
||||
/**
|
||||
* 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 string|null $value
|
||||
*
|
||||
* @return null|array|string
|
||||
*/
|
||||
public static function toConfigValue(?string $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
|
||||
*/
|
||||
public static function toDbValue($value): string
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,8 +19,9 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config;
|
||||
namespace Friendica\Core\Config\ValueObject;
|
||||
|
||||
use Friendica\Core\Config\Util\ConfigFileLoader;
|
||||
use ParagonIE\HiddenString\HiddenString;
|
||||
|
||||
/**
|
||||
|
@ -45,7 +46,7 @@ class Cache
|
|||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
private $config = [];
|
||||
|
||||
/**
|
||||
* @var int[][]
|
||||
|
@ -96,16 +97,16 @@ class Cache
|
|||
/**
|
||||
* Gets a value from the config cache.
|
||||
*
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
* @param string $cat Config category
|
||||
* @param string|null $key Config key
|
||||
*
|
||||
* @return null|mixed Returns the value of the Config entry or null if not set
|
||||
*/
|
||||
public function get(string $cat, string $key = null)
|
||||
public function get(string $cat, ?string $key = null)
|
||||
{
|
||||
if (isset($this->config[$cat][$key])) {
|
||||
return $this->config[$cat][$key];
|
||||
} else if (!isset($key) && isset($this->config[$cat])) {
|
||||
} elseif (!isset($key) && isset($this->config[$cat])) {
|
||||
return $this->config[$cat];
|
||||
} else {
|
||||
return null;
|
||||
|
@ -122,7 +123,7 @@ class Cache
|
|||
*
|
||||
* @return bool True, if the value is set
|
||||
*/
|
||||
public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
|
||||
public function set(string $cat, string $key, $value, int $source = self::SOURCE_DEFAULT): bool
|
||||
{
|
||||
if (!isset($this->config[$cat])) {
|
||||
$this->config[$cat] = [];
|
||||
|
@ -155,7 +156,7 @@ class Cache
|
|||
*
|
||||
* @return bool true, if deleted
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
if (isset($this->config[$cat][$key])) {
|
||||
unset($this->config[$cat][$key]);
|
||||
|
@ -173,9 +174,9 @@ class Cache
|
|||
/**
|
||||
* Returns the whole configuration
|
||||
*
|
||||
* @return array The configuration
|
||||
* @return string[][] The configuration
|
||||
*/
|
||||
public function getAll()
|
||||
public function getAll(): array
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
@ -183,11 +184,11 @@ class Cache
|
|||
/**
|
||||
* Returns an array with missing categories/Keys
|
||||
*
|
||||
* @param array $config The array to check
|
||||
* @param string[][] $config The array to check
|
||||
*
|
||||
* @return array
|
||||
* @return string[][]
|
||||
*/
|
||||
public function keyDiff(array $config)
|
||||
public function keyDiff(array $config): array
|
||||
{
|
||||
$return = [];
|
||||
|
|
@ -23,7 +23,7 @@ namespace Friendica\Core;
|
|||
|
||||
use DOMDocument;
|
||||
use Exception;
|
||||
use Friendica\Core\Config\Cache;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DBStructure;
|
||||
use Friendica\DI;
|
||||
|
@ -278,7 +278,7 @@ class Installer
|
|||
$cmd = "$phppath -v";
|
||||
$result = trim(shell_exec($cmd));
|
||||
$passed2 = (strpos($result, "(cli)") !== false);
|
||||
list($result) = explode("\n", $result);
|
||||
[$result] = explode("\n", $result);
|
||||
$help = "";
|
||||
if (!$passed2) {
|
||||
$help .= DI::l10n()->t("PHP executable is not the php cli binary \x28could be cgi-fgci version\x29") . EOL;
|
||||
|
@ -678,8 +678,8 @@ class Installer
|
|||
/**
|
||||
* Setup the default cache for a new installation
|
||||
*
|
||||
* @param Cache $configCache The configuration cache
|
||||
* @param string $basePath The determined basepath
|
||||
* @param \Friendica\Core\Config\ValueObject\Cache $configCache The configuration cache
|
||||
* @param string $basePath The determined basepath
|
||||
*
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\Session\ISession;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Session\Capability\IHandleSessions;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\Strings;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
@ -62,7 +62,7 @@ class L10n
|
|||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(IConfig $config, Database $dba, LoggerInterface $logger, ISession $session, array $server, array $get)
|
||||
public function __construct(IManageConfigValues $config, Database $dba, LoggerInterface $logger, IHandleSessions $session, array $server, array $get)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
$this->logger = $logger;
|
||||
|
@ -85,7 +85,7 @@ class L10n
|
|||
/**
|
||||
* Sets the language session variable
|
||||
*/
|
||||
private function setSessionVariable(ISession $session)
|
||||
private function setSessionVariable(IHandleSessions $session)
|
||||
{
|
||||
if ($session->get('authenticated') && !$session->get('language')) {
|
||||
$session->set('language', $this->lang);
|
||||
|
@ -103,7 +103,7 @@ class L10n
|
|||
}
|
||||
}
|
||||
|
||||
private function setLangFromSession(ISession $session)
|
||||
private function setLangFromSession(IHandleSessions $session)
|
||||
{
|
||||
if ($session->get('language') !== $this->lang) {
|
||||
$this->loadTranslationTable($session->get('language'));
|
||||
|
|
|
@ -1,162 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\BaseLock;
|
||||
use Friendica\Core\Cache\Duration;
|
||||
use Friendica\Core\Cache\IMemoryCache;
|
||||
|
||||
class CacheLock extends BaseLock
|
||||
{
|
||||
/**
|
||||
* @var string The static prefix of all locks inside the cache
|
||||
*/
|
||||
const CACHE_PREFIX = 'lock:';
|
||||
|
||||
/**
|
||||
* @var \Friendica\Core\Cache\ICache;
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* CacheLock constructor.
|
||||
*
|
||||
* @param IMemoryCache $cache The CacheDriver for this type of lock
|
||||
*/
|
||||
public function __construct(IMemoryCache $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
||||
$cachekey = self::getLockKey($key);
|
||||
|
||||
do {
|
||||
$lock = $this->cache->get($cachekey);
|
||||
// When we do want to lock something that was already locked by us.
|
||||
if ((int)$lock == getmypid()) {
|
||||
$got_lock = true;
|
||||
}
|
||||
|
||||
// When we do want to lock something new
|
||||
if (is_null($lock)) {
|
||||
// At first initialize it with "0"
|
||||
$this->cache->add($cachekey, 0);
|
||||
// Now the value has to be "0" because otherwise the key was used by another process meanwhile
|
||||
if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$got_lock && ($timeout > 0)) {
|
||||
usleep(rand(10000, 200000));
|
||||
}
|
||||
} while (!$got_lock && ((time() - $start) < $timeout));
|
||||
|
||||
return $got_lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function release($key, $override = false)
|
||||
{
|
||||
$cachekey = self::getLockKey($key);
|
||||
|
||||
if ($override) {
|
||||
$return = $this->cache->delete($cachekey);
|
||||
} else {
|
||||
$return = $this->cache->compareDelete($cachekey, getmypid());
|
||||
}
|
||||
$this->markRelease($key);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function isLocked($key)
|
||||
{
|
||||
$cachekey = self::getLockKey($key);
|
||||
$lock = $this->cache->get($cachekey);
|
||||
return isset($lock) && ($lock !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->cache->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = '')
|
||||
{
|
||||
$locks = $this->cache->getAllKeys(self::CACHE_PREFIX . $prefix);
|
||||
|
||||
array_walk($locks, function (&$lock, $key) {
|
||||
$lock = substr($lock, strlen(self::CACHE_PREFIX));
|
||||
});
|
||||
|
||||
return $locks;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function releaseAll($override = false)
|
||||
{
|
||||
$success = parent::releaseAll($override);
|
||||
|
||||
$locks = $this->getLocks();
|
||||
|
||||
foreach ($locks as $lock) {
|
||||
if (!$this->release($lock, $override)) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key The original key
|
||||
*
|
||||
* @return string The cache key used for the cache
|
||||
*/
|
||||
private static function getLockKey($key)
|
||||
{
|
||||
return self::CACHE_PREFIX . $key;
|
||||
}
|
||||
}
|
|
@ -19,23 +19,22 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
namespace Friendica\Core\Lock\Capability;
|
||||
|
||||
use Friendica\Core\Cache\Duration;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Lock\Exception\LockPersistenceException;
|
||||
|
||||
/**
|
||||
* Lock Interface
|
||||
*/
|
||||
interface ILock
|
||||
interface ICanLock
|
||||
{
|
||||
/**
|
||||
* Checks, if a key is currently locked to a or my process
|
||||
*
|
||||
* @param string $key The name of the lock
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLocked($key);
|
||||
public function isLocked(string $key): bool;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -45,9 +44,9 @@ interface ILock
|
|||
* @param integer $timeout Seconds until we give up
|
||||
* @param integer $ttl Seconds The lock lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return boolean Was the lock successful?
|
||||
* @throws LockPersistenceException In case the underlying persistence throws errors
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES);
|
||||
public function acquire(string $key, int $timeout = 120, int $ttl = Duration::FIVE_MINUTES): bool;
|
||||
|
||||
/**
|
||||
* Releases a lock if it was set by us
|
||||
|
@ -55,32 +54,36 @@ interface ILock
|
|||
* @param string $key The Name of the lock
|
||||
* @param bool $override Overrides the lock to get released
|
||||
*
|
||||
* @return boolean Was the unlock successful?
|
||||
* @return bool Was the unlock successful?
|
||||
*
|
||||
* @throws LockPersistenceException In case the underlying persistence throws errors
|
||||
*/
|
||||
public function release($key, $override = false);
|
||||
public function release(string $key, bool $override = false): bool;
|
||||
|
||||
/**
|
||||
* Releases all lock that were set by us
|
||||
*
|
||||
* @param bool $override Override to release all locks
|
||||
*
|
||||
* @return boolean Was the unlock of all locks successful?
|
||||
* @return bool Was the unlock of all locks successful?
|
||||
*
|
||||
* @throws LockPersistenceException In case the underlying persistence throws errors
|
||||
*/
|
||||
public function releaseAll($override = false);
|
||||
public function releaseAll(bool $override = false): bool;
|
||||
|
||||
/**
|
||||
* Returns the name of the current lock
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Lists all locks
|
||||
*
|
||||
* @param string prefix optional a prefix to search
|
||||
*
|
||||
* @return array Empty if it isn't supported by the cache driver
|
||||
* @return string[] Empty if it isn't supported by the cache driver
|
||||
*
|
||||
* @throws LockPersistenceException In case the underlying persistence throws errors
|
||||
*/
|
||||
public function getLocks(string $prefix = '');
|
||||
public function getLocks(string $prefix = ''): array;
|
||||
}
|
|
@ -1,178 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\BaseLock;
|
||||
use Friendica\Core\Cache\Duration;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
* Locking driver that stores the locks in the database
|
||||
*/
|
||||
class DatabaseLock extends BaseLock
|
||||
{
|
||||
/**
|
||||
* The current ID of the process
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $pid;
|
||||
|
||||
/**
|
||||
* @var Database The database connection of Friendica
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @param null|int $pid The Id of the current process (null means determine automatically)
|
||||
*/
|
||||
public function __construct(Database $dba, $pid = null)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
$this->pid = isset($pid) ? $pid : getmypid();
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
||||
do {
|
||||
$this->dba->lock('locks');
|
||||
$lock = $this->dba->selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if ($this->dba->isResult($lock)) {
|
||||
if ($lock['locked']) {
|
||||
// We want to lock something that was already locked by us? So we got the lock.
|
||||
if ($lock['pid'] == $this->pid) {
|
||||
$got_lock = true;
|
||||
}
|
||||
}
|
||||
if (!$lock['locked']) {
|
||||
$this->dba->update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
|
||||
$got_lock = true;
|
||||
}
|
||||
} else {
|
||||
$this->dba->insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
|
||||
$this->dba->unlock();
|
||||
|
||||
if (!$got_lock && ($timeout > 0)) {
|
||||
usleep(rand(100000, 2000000));
|
||||
}
|
||||
} while (!$got_lock && ((time() - $start) < $timeout));
|
||||
|
||||
return $got_lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function release($key, $override = false)
|
||||
{
|
||||
if ($override) {
|
||||
$where = ['name' => $key];
|
||||
} else {
|
||||
$where = ['name' => $key, 'pid' => $this->pid];
|
||||
}
|
||||
|
||||
if ($this->dba->exists('locks', $where)) {
|
||||
$return = $this->dba->delete('locks', $where);
|
||||
} else {
|
||||
$return = false;
|
||||
}
|
||||
|
||||
$this->markRelease($key);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function releaseAll($override = false)
|
||||
{
|
||||
$success = parent::releaseAll($override);
|
||||
|
||||
if ($override) {
|
||||
$where = ['1 = 1'];
|
||||
} else {
|
||||
$where = ['pid' => $this->pid];
|
||||
}
|
||||
$return = $this->dba->delete('locks', $where);
|
||||
|
||||
$this->acquiredLocks = [];
|
||||
|
||||
return $return && $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function isLocked($key)
|
||||
{
|
||||
$lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if ($this->dba->isResult($lock)) {
|
||||
return $lock['locked'] !== false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return Type::DATABASE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = '')
|
||||
{
|
||||
if (empty($prefix)) {
|
||||
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
|
||||
} else {
|
||||
$where = ['`expires` >= ? AND `name` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
||||
}
|
||||
|
||||
$stmt = $this->dba->select('locks', ['name'], $where);
|
||||
|
||||
$keys = [];
|
||||
while ($key = $this->dba->fetch($stmt)) {
|
||||
array_push($keys, $key['name']);
|
||||
}
|
||||
$this->dba->close($stmt);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
}
|
|
@ -19,9 +19,9 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
namespace Friendica\Core\Lock\Enum;
|
||||
|
||||
use Friendica\Core\Cache\Type as CacheType;
|
||||
use Friendica\Core\Cache\Enum\Type as CacheType;
|
||||
|
||||
/**
|
||||
* Enumeration for lock types
|
13
src/Core/Lock/Exception/InvalidLockDriverException.php
Normal file
13
src/Core/Lock/Exception/InvalidLockDriverException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class InvalidLockDriverException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
13
src/Core/Lock/Exception/LockPersistenceException.php
Normal file
13
src/Core/Lock/Exception/LockPersistenceException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class LockPersistenceException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
147
src/Core/Lock/Factory/Lock.php
Normal file
147
src/Core/Lock/Factory/Lock.php
Normal file
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock\Factory;
|
||||
|
||||
use Friendica\Core\Cache\Factory\Cache;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Enum;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
use Friendica\Core\Lock\Type;
|
||||
use Friendica\Database\Database;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class LockFactory
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*
|
||||
* A basic class to generate a LockDriver
|
||||
*/
|
||||
class Lock
|
||||
{
|
||||
/**
|
||||
* @var string The default driver for caching
|
||||
*/
|
||||
const DEFAULT_DRIVER = 'default';
|
||||
|
||||
/**
|
||||
* @var IManageConfigValues The configuration to read parameters out of the config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Database The database connection in case that the cache is used the dba connection
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @var Cache The memory cache driver in case we use it
|
||||
*/
|
||||
private $cacheFactory;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface The Friendica Logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(Cache $cacheFactory, IManageConfigValues $config, Database $dba, LoggerInterface $logger)
|
||||
{
|
||||
$this->cacheFactory = $cacheFactory;
|
||||
$this->config = $config;
|
||||
$this->dba = $dba;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$lock_type = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER);
|
||||
|
||||
try {
|
||||
switch ($lock_type) {
|
||||
case Enum\Type::MEMCACHE:
|
||||
case Enum\Type::MEMCACHED:
|
||||
case Enum\Type::REDIS:
|
||||
case Enum\Type::APCU:
|
||||
$cache = $this->cacheFactory->create($lock_type);
|
||||
if ($cache instanceof ICanCacheInMemory) {
|
||||
return new Type\CacheLock($cache);
|
||||
} else {
|
||||
throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'database':
|
||||
return new Type\DatabaseLock($this->dba);
|
||||
break;
|
||||
|
||||
case 'semaphore':
|
||||
return new Type\SemaphoreLock();
|
||||
break;
|
||||
|
||||
default:
|
||||
return self::useAutoDriver();
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->alert('Driver \'' . $lock_type . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]);
|
||||
return self::useAutoDriver();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method tries to find the best - local - locking method for Friendica
|
||||
*
|
||||
* The following sequence will be tried:
|
||||
* 1. Semaphore Locking
|
||||
* 2. Cache Locking
|
||||
* 3. Database Locking
|
||||
*
|
||||
* @return ICanLock
|
||||
*/
|
||||
private function useAutoDriver()
|
||||
{
|
||||
// 1. Try to use Semaphores for - local - locking
|
||||
if (function_exists('sem_get')) {
|
||||
try {
|
||||
return new Type\SemaphoreLock();
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Using Semaphore driver for locking failed.', ['exception' => $exception]);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
|
||||
$cache_type = $this->config->get('system', 'cache_driver', 'database');
|
||||
if ($cache_type != Enum\Type::DATABASE) {
|
||||
try {
|
||||
$cache = $this->cacheFactory->create($cache_type);
|
||||
if ($cache instanceof ICanCacheInMemory) {
|
||||
return new Type\CacheLock($cache);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Using Cache driver for locking failed.', ['exception' => $exception]);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Use Database Locking as a Fallback
|
||||
return new Type\DatabaseLock($this->dba);
|
||||
}
|
||||
}
|
|
@ -19,14 +19,14 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core;
|
||||
namespace Friendica\Core\Lock\Type;
|
||||
|
||||
use Friendica\Core\Lock\ILock;
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
|
||||
/**
|
||||
* Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
|
||||
*/
|
||||
abstract class BaseLock implements ILock
|
||||
abstract class AbstractLock implements ICanLock
|
||||
{
|
||||
/**
|
||||
* @var array The local acquired locks
|
||||
|
@ -40,7 +40,7 @@ abstract class BaseLock implements ILock
|
|||
*
|
||||
* @return bool Returns true if the lock is set
|
||||
*/
|
||||
protected function hasAcquiredLock($key)
|
||||
protected function hasAcquiredLock(string $key): bool
|
||||
{
|
||||
return isset($this->acquireLock[$key]) && $this->acquiredLocks[$key] === true;
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ abstract class BaseLock implements ILock
|
|||
*
|
||||
* @param string $key The Name of the lock
|
||||
*/
|
||||
protected function markAcquire($key)
|
||||
protected function markAcquire(string $key)
|
||||
{
|
||||
$this->acquiredLocks[$key] = true;
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ abstract class BaseLock implements ILock
|
|||
*
|
||||
* @param string $key The Name of the lock
|
||||
*/
|
||||
protected function markRelease($key)
|
||||
protected function markRelease(string $key)
|
||||
{
|
||||
unset($this->acquiredLocks[$key]);
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ abstract class BaseLock implements ILock
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function releaseAll($override = false)
|
||||
public function releaseAll(bool $override = false): bool
|
||||
{
|
||||
$return = true;
|
||||
|
180
src/Core/Lock/Type/CacheLock.php
Normal file
180
src/Core/Lock/Type/CacheLock.php
Normal file
|
@ -0,0 +1,180 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock\Type;
|
||||
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Core\Lock\Exception\LockPersistenceException;
|
||||
|
||||
class CacheLock extends AbstractLock
|
||||
{
|
||||
/**
|
||||
* @var string The static prefix of all locks inside the cache
|
||||
*/
|
||||
const CACHE_PREFIX = 'lock:';
|
||||
|
||||
/**
|
||||
* @var ICanCache;
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* CacheLock constructor.
|
||||
*
|
||||
* @param ICanCacheInMemory $cache The CacheDriver for this type of lock
|
||||
*/
|
||||
public function __construct(ICanCacheInMemory $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire(string $key, int $timeout = 120, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
||||
$lockKey = self::getLockKey($key);
|
||||
|
||||
try {
|
||||
do {
|
||||
$lock = $this->cache->get($lockKey);
|
||||
// When we do want to lock something that was already locked by us.
|
||||
if ((int)$lock == getmypid()) {
|
||||
$got_lock = true;
|
||||
}
|
||||
|
||||
// When we do want to lock something new
|
||||
if (is_null($lock)) {
|
||||
// At first initialize it with "0"
|
||||
$this->cache->add($lockKey, 0);
|
||||
// Now the value has to be "0" because otherwise the key was used by another process meanwhile
|
||||
if ($this->cache->compareSet($lockKey, 0, getmypid(), $ttl)) {
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$got_lock && ($timeout > 0)) {
|
||||
usleep(rand(10000, 200000));
|
||||
}
|
||||
} while (!$got_lock && ((time() - $start) < $timeout));
|
||||
} catch (CachePersistenceException $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot acquire lock for key %s', $key), $exception);
|
||||
}
|
||||
|
||||
return $got_lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function release(string $key, bool $override = false): bool
|
||||
{
|
||||
$lockKey = self::getLockKey($key);
|
||||
|
||||
try {
|
||||
if ($override) {
|
||||
$return = $this->cache->delete($lockKey);
|
||||
} else {
|
||||
$return = $this->cache->compareDelete($lockKey, getmypid());
|
||||
}
|
||||
} catch (CachePersistenceException $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot release lock for key %s (override %b)', $key, $override), $exception);
|
||||
}
|
||||
$this->markRelease($key);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function isLocked(string $key): bool
|
||||
{
|
||||
$lockKey = self::getLockKey($key);
|
||||
try {
|
||||
$lock = $this->cache->get($lockKey);
|
||||
} catch (CachePersistenceException $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot check lock state for key %s', $key), $exception);
|
||||
}
|
||||
return isset($lock) && ($lock !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->cache->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = ''): array
|
||||
{
|
||||
try {
|
||||
$locks = $this->cache->getAllKeys(self::CACHE_PREFIX . $prefix);
|
||||
} catch (CachePersistenceException $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot get locks with prefix %s', $prefix), $exception);
|
||||
}
|
||||
|
||||
array_walk($locks, function (&$lock) {
|
||||
$lock = substr($lock, strlen(self::CACHE_PREFIX));
|
||||
});
|
||||
|
||||
return $locks;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function releaseAll(bool $override = false): bool
|
||||
{
|
||||
$success = parent::releaseAll($override);
|
||||
|
||||
$locks = $this->getLocks();
|
||||
|
||||
foreach ($locks as $lock) {
|
||||
if (!$this->release($lock, $override)) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key The original key
|
||||
*
|
||||
* @return string The cache key used for the cache
|
||||
*/
|
||||
private static function getLockKey(string $key): string
|
||||
{
|
||||
return self::CACHE_PREFIX . $key;
|
||||
}
|
||||
}
|
212
src/Core/Lock/Type/DatabaseLock.php
Normal file
212
src/Core/Lock/Type/DatabaseLock.php
Normal file
|
@ -0,0 +1,212 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock\Type;
|
||||
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Lock\Enum\Type;
|
||||
use Friendica\Core\Lock\Exception\LockPersistenceException;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
* Locking driver that stores the locks in the database
|
||||
*/
|
||||
class DatabaseLock extends AbstractLock
|
||||
{
|
||||
/**
|
||||
* The current ID of the process
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $pid;
|
||||
|
||||
/**
|
||||
* @var Database The database connection of Friendica
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @param int|null $pid The id of the current process (null means determine automatically)
|
||||
*/
|
||||
public function __construct(Database $dba, ?int $pid = null)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
$this->pid = $pid ?? getmypid();
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire(string $key, int $timeout = 120, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
||||
try {
|
||||
do {
|
||||
$this->dba->lock('locks');
|
||||
$lock = $this->dba->selectFirst('locks', ['locked', 'pid'], [
|
||||
'`name` = ? AND `expires` >= ?', $key,DateTimeFormat::utcNow()
|
||||
]);
|
||||
|
||||
if ($this->dba->isResult($lock)) {
|
||||
if ($lock['locked']) {
|
||||
// We want to lock something that was already locked by us? So we got the lock.
|
||||
if ($lock['pid'] == $this->pid) {
|
||||
$got_lock = true;
|
||||
}
|
||||
}
|
||||
if (!$lock['locked']) {
|
||||
$this->dba->update('locks', [
|
||||
'locked' => true,
|
||||
'pid' => $this->pid,
|
||||
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')
|
||||
], ['name' => $key]);
|
||||
$got_lock = true;
|
||||
}
|
||||
} else {
|
||||
$this->dba->insert('locks', [
|
||||
'name' => $key,
|
||||
'locked' => true,
|
||||
'pid' => $this->pid,
|
||||
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
|
||||
$this->dba->unlock();
|
||||
|
||||
if (!$got_lock && ($timeout > 0)) {
|
||||
usleep(rand(100000, 2000000));
|
||||
}
|
||||
} while (!$got_lock && ((time() - $start) < $timeout));
|
||||
} catch (\Exception $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot acquire lock for key %s', $key), $exception);
|
||||
}
|
||||
|
||||
return $got_lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function release(string $key, bool $override = false): bool
|
||||
{
|
||||
if ($override) {
|
||||
$where = ['name' => $key];
|
||||
} else {
|
||||
$where = ['name' => $key, 'pid' => $this->pid];
|
||||
}
|
||||
|
||||
try {
|
||||
if ($this->dba->exists('locks', $where)) {
|
||||
$return = $this->dba->delete('locks', $where);
|
||||
} else {
|
||||
$return = false;
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot release lock for key %s (override %b)', $key, $override), $exception);
|
||||
}
|
||||
|
||||
$this->markRelease($key);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function releaseAll(bool $override = false): bool
|
||||
{
|
||||
$success = parent::releaseAll($override);
|
||||
|
||||
if ($override) {
|
||||
$where = ['1 = 1'];
|
||||
} else {
|
||||
$where = ['pid' => $this->pid];
|
||||
}
|
||||
|
||||
try {
|
||||
$return = $this->dba->delete('locks', $where);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot release all lock (override %b)', $override), $exception);
|
||||
}
|
||||
|
||||
$this->acquiredLocks = [];
|
||||
|
||||
return $return && $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function isLocked(string $key): bool
|
||||
{
|
||||
try {
|
||||
$lock = $this->dba->selectFirst('locks', ['locked'], [
|
||||
'`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot check lock state for key %s', $key), $exception);
|
||||
}
|
||||
|
||||
if ($this->dba->isResult($lock)) {
|
||||
return $lock['locked'] !== false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return Type::DATABASE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = ''): array
|
||||
{
|
||||
try {
|
||||
if (empty($prefix)) {
|
||||
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
|
||||
} else {
|
||||
$where = ['`expires` >= ? AND `name` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
||||
}
|
||||
|
||||
$stmt = $this->dba->select('locks', ['name'], $where);
|
||||
|
||||
$keys = [];
|
||||
while ($key = $this->dba->fetch($stmt)) {
|
||||
array_push($keys, $key['name']);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new LockPersistenceException(sprintf('Cannot get lock with prefix %s', $prefix), $exception);
|
||||
} finally {
|
||||
$this->dba->close($stmt);
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
}
|
|
@ -19,19 +19,21 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
namespace Friendica\Core\Lock\Type;
|
||||
|
||||
use Friendica\Core\BaseLock;
|
||||
use Friendica\Core\Cache\Duration;
|
||||
use Friendica\Core\Cache\Enum\Duration;
|
||||
use Friendica\Core\Lock\Enum\Type;
|
||||
use Friendica\Core\Lock\Exception\InvalidLockDriverException;
|
||||
use function get_temppath;
|
||||
|
||||
class SemaphoreLock extends BaseLock
|
||||
class SemaphoreLock extends AbstractLock
|
||||
{
|
||||
private static $semaphore = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (!function_exists('sem_get')) {
|
||||
throw new \Exception('Semaphore lock not supported');
|
||||
throw new InvalidLockDriverException('Semaphore lock not supported');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,11 +58,11 @@ class SemaphoreLock extends BaseLock
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
|
||||
public function acquire(string $key, int $timeout = 120, int $ttl = Duration::FIVE_MINUTES): bool
|
||||
{
|
||||
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
|
||||
if (!empty(self::$semaphore[$key])) {
|
||||
if ((bool)sem_acquire(self::$semaphore[$key], ($timeout === 0))) {
|
||||
if (sem_acquire(self::$semaphore[$key], ($timeout === 0))) {
|
||||
$this->markAcquire($key);
|
||||
return true;
|
||||
}
|
||||
|
@ -75,7 +77,7 @@ class SemaphoreLock extends BaseLock
|
|||
* @param bool $override not necessary parameter for semaphore locks since the lock lives as long as the execution
|
||||
* of the using function
|
||||
*/
|
||||
public function release($key, $override = false)
|
||||
public function release(string $key, bool $override = false): bool
|
||||
{
|
||||
$success = false;
|
||||
|
||||
|
@ -95,7 +97,7 @@ class SemaphoreLock extends BaseLock
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function isLocked($key)
|
||||
public function isLocked(string $key): bool
|
||||
{
|
||||
return isset(self::$semaphore[$key]);
|
||||
}
|
||||
|
@ -103,7 +105,7 @@ class SemaphoreLock extends BaseLock
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return Type::SEMAPHORE;
|
||||
}
|
||||
|
@ -111,7 +113,7 @@ class SemaphoreLock extends BaseLock
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = '')
|
||||
public function getLocks(string $prefix = ''): array
|
||||
{
|
||||
// We can just return our own semaphore keys, since we don't know
|
||||
// the state of other semaphores, even if the .sem files exists
|
||||
|
@ -135,7 +137,7 @@ class SemaphoreLock extends BaseLock
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function releaseAll($override = false)
|
||||
public function releaseAll(bool $override = false): bool
|
||||
{
|
||||
// Semaphores are just alive during a run, so there is no need to release
|
||||
// You can just release your own locks
|
|
@ -22,7 +22,7 @@
|
|||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\DI;
|
||||
use Friendica\Util\Logger\WorkerLogger;
|
||||
use Friendica\Core\Logger\Type\WorkerLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
|
|
13
src/Core/Logger/Exception/LogLevelException.php
Normal file
13
src/Core/Logger/Exception/LogLevelException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Logger\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class LogLevelException extends \InvalidArgumentException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
13
src/Core/Logger/Exception/LoggerArgumentException.php
Normal file
13
src/Core/Logger/Exception/LoggerArgumentException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Logger\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class LoggerArgumentException extends \InvalidArgumentException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
13
src/Core/Logger/Exception/LoggerException.php
Normal file
13
src/Core/Logger/Exception/LoggerException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Logger\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class LoggerException extends \Exception
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
310
src/Core/Logger/Factory/Logger.php
Normal file
310
src/Core/Logger/Factory/Logger.php
Normal file
|
@ -0,0 +1,310 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Factory;
|
||||
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Core;
|
||||
use Friendica\Core\Logger\Exception\LogLevelException;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\FileSystem;
|
||||
use Friendica\Core\Logger\Util\Introspection;
|
||||
use Friendica\Core\Logger\Type\Monolog\DevelopHandler;
|
||||
use Friendica\Core\Logger\Type\Monolog\IntrospectionProcessor;
|
||||
use Friendica\Core\Logger\Type\ProfilerLogger;
|
||||
use Friendica\Core\Logger\Type\StreamLogger;
|
||||
use Friendica\Core\Logger\Type\SyslogLogger;
|
||||
use Friendica\Util\Profiler;
|
||||
use Monolog;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* A logger factory
|
||||
*/
|
||||
class Logger
|
||||
{
|
||||
const DEV_CHANNEL = 'dev';
|
||||
|
||||
/**
|
||||
* A list of classes, which shouldn't get logged
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private static $ignoreClassList = [
|
||||
Core\Logger::class,
|
||||
Profiler::class,
|
||||
'Friendica\\Core\\Logger\\Type',
|
||||
];
|
||||
|
||||
/** @var string The log-channel (app, worker, ...) */
|
||||
private $channel;
|
||||
|
||||
public function __construct(string $channel)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new PSR-3 compliant logger instances
|
||||
*
|
||||
* @param Database $database The Friendica Database instance
|
||||
* @param IManageConfigValues $config The config
|
||||
* @param Profiler $profiler The profiler of the app
|
||||
* @param FileSystem $fileSystem FileSystem utils
|
||||
* @param string|null $minLevel (optional) Override minimum Loglevel to log
|
||||
*
|
||||
* @return LoggerInterface The PSR-3 compliant logger instance
|
||||
*/
|
||||
public function create(Database $database, IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, ?string $minLevel = null): LoggerInterface
|
||||
{
|
||||
if (empty($config->get('system', 'debugging', false))) {
|
||||
$logger = new NullLogger();
|
||||
$database->setLogger($logger);
|
||||
return $logger;
|
||||
}
|
||||
|
||||
$introspection = new Introspection(self::$ignoreClassList);
|
||||
$minLevel = $minLevel ?? $config->get('system', 'loglevel');
|
||||
$loglevel = self::mapLegacyConfigDebugLevel((string)$minLevel);
|
||||
|
||||
switch ($config->get('system', 'logger_config', 'stream')) {
|
||||
case 'monolog':
|
||||
$loggerTimeZone = new \DateTimeZone('UTC');
|
||||
Monolog\Logger::setTimezone($loggerTimeZone);
|
||||
|
||||
$logger = new Monolog\Logger($this->channel);
|
||||
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
||||
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
|
||||
|
||||
$stream = $config->get('system', 'logfile');
|
||||
|
||||
// just add a stream in case it's either writable or not file
|
||||
if (!is_file($stream) || is_writable($stream)) {
|
||||
try {
|
||||
static::addStreamHandler($logger, $stream, $loglevel);
|
||||
} catch (\Throwable $e) {
|
||||
// No Logger ..
|
||||
try {
|
||||
$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
|
||||
} catch (\Throwable $e) {
|
||||
// No logger ...
|
||||
$logger = new NullLogger();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'syslog':
|
||||
try {
|
||||
$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
|
||||
} catch (LogLevelException $exception) {
|
||||
// If there's a wrong config value for loglevel, try again with standard
|
||||
$logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
|
||||
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
|
||||
} catch (\Throwable $e) {
|
||||
// No logger ...
|
||||
$logger = new NullLogger();
|
||||
}
|
||||
break;
|
||||
|
||||
case 'stream':
|
||||
default:
|
||||
$stream = $config->get('system', 'logfile');
|
||||
// just add a stream in case it's either writable or not file
|
||||
if (!is_file($stream) || is_writable($stream)) {
|
||||
try {
|
||||
$logger = new StreamLogger($this->channel, $stream, $introspection, $fileSystem, $loglevel);
|
||||
} catch (LogLevelException $exception) {
|
||||
// If there's a wrong config value for loglevel, try again with standard
|
||||
$logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
|
||||
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
|
||||
} catch (\Throwable $t) {
|
||||
// No logger ...
|
||||
$logger = new NullLogger();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
|
||||
} catch (LogLevelException $exception) {
|
||||
// If there's a wrong config value for loglevel, try again with standard
|
||||
$logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
|
||||
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
|
||||
} catch (\Throwable $e) {
|
||||
// No logger ...
|
||||
$logger = new NullLogger();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$profiling = $config->get('system', 'profiling', false);
|
||||
|
||||
// In case profiling is enabled, wrap the ProfilerLogger around the current logger
|
||||
if (isset($profiling) && $profiling !== false) {
|
||||
$logger = new ProfilerLogger($logger, $profiler);
|
||||
}
|
||||
|
||||
$database->setLogger($logger);
|
||||
return $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new PSR-3 compliant develop logger
|
||||
*
|
||||
* If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
|
||||
* you'll use this logger instance for the duration of your work.
|
||||
*
|
||||
* It should never get filled during normal usage of Friendica
|
||||
*
|
||||
* @param IManageConfigValues $config The config
|
||||
* @param Profiler $profiler The profiler of the app
|
||||
* @param FileSystem $fileSystem FileSystem utils
|
||||
*
|
||||
* @return LoggerInterface The PSR-3 compliant logger instance
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function createDev(IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem)
|
||||
{
|
||||
$debugging = $config->get('system', 'debugging');
|
||||
$stream = $config->get('system', 'dlogfile');
|
||||
$developerIp = $config->get('system', 'dlogip');
|
||||
|
||||
if ((!isset($developerIp) || !$debugging) &&
|
||||
(!is_file($stream) || is_writable($stream))) {
|
||||
return new NullLogger();
|
||||
}
|
||||
|
||||
$loggerTimeZone = new \DateTimeZone('UTC');
|
||||
Monolog\Logger::setTimezone($loggerTimeZone);
|
||||
|
||||
$introspection = new Introspection(self::$ignoreClassList);
|
||||
|
||||
switch ($config->get('system', 'logger_config', 'stream')) {
|
||||
|
||||
case 'monolog':
|
||||
$loggerTimeZone = new \DateTimeZone('UTC');
|
||||
Monolog\Logger::setTimezone($loggerTimeZone);
|
||||
|
||||
$logger = new Monolog\Logger(self::DEV_CHANNEL);
|
||||
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
||||
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
|
||||
|
||||
$logger->pushHandler(new DevelopHandler($developerIp));
|
||||
|
||||
static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
|
||||
break;
|
||||
|
||||
case 'syslog':
|
||||
$logger = new SyslogLogger(self::DEV_CHANNEL, $introspection, LogLevel::DEBUG);
|
||||
break;
|
||||
|
||||
case 'stream':
|
||||
default:
|
||||
$logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, $fileSystem, LogLevel::DEBUG);
|
||||
break;
|
||||
}
|
||||
|
||||
$profiling = $config->get('system', 'profiling', false);
|
||||
|
||||
// In case profiling is enabled, wrap the ProfilerLogger around the current logger
|
||||
if (isset($profiling) && $profiling !== false) {
|
||||
$logger = new ProfilerLogger($logger, $profiler);
|
||||
}
|
||||
|
||||
return $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param string $level the level to be mapped
|
||||
*
|
||||
* @return string the PSR-3 compliant level
|
||||
*/
|
||||
private static function mapLegacyConfigDebugLevel(string $level): string
|
||||
{
|
||||
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":
|
||||
// legacy ALL
|
||||
case "5":
|
||||
return LogLevel::DEBUG;
|
||||
// default if nothing set
|
||||
default:
|
||||
return $level;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding a handler to a given logger instance
|
||||
*
|
||||
* @param LoggerInterface $logger The logger instance
|
||||
* @param mixed $stream The stream which handles the logger output
|
||||
* @param string $level The level, for which this handler at least should handle logging
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
public static function addStreamHandler(LoggerInterface $logger, $stream, string $level = LogLevel::NOTICE)
|
||||
{
|
||||
if ($logger instanceof Monolog\Logger) {
|
||||
$loglevel = Monolog\Logger::toMonologLevel($level);
|
||||
|
||||
// fallback to notice if an invalid loglevel is set
|
||||
if (!is_int($loglevel)) {
|
||||
$loglevel = LogLevel::NOTICE;
|
||||
}
|
||||
|
||||
try {
|
||||
$fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
|
||||
|
||||
$formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
|
||||
$fileHandler->setFormatter($formatter);
|
||||
|
||||
$logger->pushHandler($fileHandler);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot create Monolog Logger.', $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
206
src/Core/Logger/Type/AbstractLogger.php
Normal file
206
src/Core/Logger/Type/AbstractLogger.php
Normal file
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Core\Logger\Util\Introspection;
|
||||
use Friendica\Util\Strings;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* This class contains all necessary dependencies and calls for Friendica
|
||||
* Every new Logger should extend this class and define, how addEntry() works
|
||||
*
|
||||
* Additional information for each Logger, who extends this class:
|
||||
* - Introspection
|
||||
* - UID for each call
|
||||
* - Channel of the current call (i.e. index, worker, daemon, ...)
|
||||
*/
|
||||
abstract class AbstractLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* The output channel of this logger
|
||||
* @var string
|
||||
*/
|
||||
protected $channel;
|
||||
|
||||
/**
|
||||
* The Introspection for the current call
|
||||
* @var Introspection
|
||||
*/
|
||||
protected $introspection;
|
||||
|
||||
/**
|
||||
* The UID of the current call
|
||||
* @var string
|
||||
*/
|
||||
protected $logUid;
|
||||
|
||||
/**
|
||||
* Adds a new entry to the log
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function addEntry($level, string $message, array $context = []);
|
||||
|
||||
/**
|
||||
* @param string $channel The output channel
|
||||
* @param Introspection $introspection The introspection of the current call
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
public function __construct(string $channel, Introspection $introspection)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
$this->introspection = $introspection;
|
||||
|
||||
try {
|
||||
$this->logUid = Strings::getRandomHex(6);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot generate log Id', $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
|
||||
*
|
||||
* @see https://www.php-fig.org/psr/psr-3/#12-message
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return string the interpolated message
|
||||
*/
|
||||
protected function psrInterpolate(string $message, array $context = []): string
|
||||
{
|
||||
$replace = [];
|
||||
foreach ($context as $key => $value) {
|
||||
// check that the value can be casted to string
|
||||
if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
|
||||
$replace['{' . $key . '}'] = $value;
|
||||
} elseif (is_array($value)) {
|
||||
$replace['{' . $key . '}'] = @json_encode($value);
|
||||
}
|
||||
}
|
||||
|
||||
return strtr($message, $replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON Encodes a complete array including objects with "__toString()" methods
|
||||
*
|
||||
* @param array $input an Input Array to encode
|
||||
*
|
||||
* @return false|string The json encoded output of the array
|
||||
*/
|
||||
protected function jsonEncodeArray(array $input)
|
||||
{
|
||||
$output = [];
|
||||
|
||||
foreach ($input as $key => $value) {
|
||||
if (is_object($value) && method_exists($value, '__toString')) {
|
||||
$output[$key] = $value->__toString();
|
||||
} else {
|
||||
$output[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return @json_encode($output);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::ALERT, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::ERROR, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::WARNING, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::NOTICE, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::INFO, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::DEBUG, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$this->addEntry($level, (string) $message, $context);
|
||||
}
|
||||
}
|
68
src/Core/Logger/Type/Monolog/DevelopHandler.php
Normal file
68
src/Core/Logger/Type/Monolog/DevelopHandler.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Type\Monolog;
|
||||
|
||||
use Monolog\Handler;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Simple handler for Friendica developers to use for deeper logging
|
||||
*
|
||||
* If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
|
||||
* you'll use Logger::develop() for the duration of your work, and you clean it up when you're done before submitting your PR.
|
||||
*/
|
||||
class DevelopHandler extends Handler\AbstractHandler
|
||||
{
|
||||
/**
|
||||
* @var string The IP of the developer who wants to debug
|
||||
*/
|
||||
private $developerIp;
|
||||
|
||||
/**
|
||||
* @param string $developerIp The IP of the developer who wants to debug
|
||||
* @param int $level The minimum logging level at which this handler will be triggered
|
||||
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
|
||||
*/
|
||||
public function __construct($developerIp, $level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
parent::__construct($level, $bubble);
|
||||
|
||||
$this->developerIp = $developerIp;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(array $record): bool
|
||||
{
|
||||
if (!$this->isHandling($record)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Just in case the remote IP is the same as the developer IP log the output
|
||||
if (!is_null($this->developerIp) && $_SERVER['REMOTE_ADDR'] != $this->developerIp) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false === $this->bubble;
|
||||
}
|
||||
}
|
62
src/Core/Logger/Type/Monolog/IntrospectionProcessor.php
Normal file
62
src/Core/Logger/Type/Monolog/IntrospectionProcessor.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Type\Monolog;
|
||||
|
||||
use Friendica\Core\Logger\Util\Introspection;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Processor\ProcessorInterface;
|
||||
|
||||
/**
|
||||
* Injects line/file//function where the log message came from
|
||||
*/
|
||||
class IntrospectionProcessor implements ProcessorInterface
|
||||
{
|
||||
private $level;
|
||||
|
||||
private $introspection;
|
||||
|
||||
/**
|
||||
* @param Introspection $introspection Holds the Introspection of the current call
|
||||
* @param string|int $level The minimum logging level at which this Processor will be triggered
|
||||
*/
|
||||
public function __construct(Introspection $introspection, $level = Logger::DEBUG)
|
||||
{
|
||||
$this->level = Logger::toMonologLevel($level);
|
||||
$introspection->addClasses(['Monolog\\']);
|
||||
$this->introspection = $introspection;
|
||||
}
|
||||
|
||||
public function __invoke(array $record): array
|
||||
{
|
||||
// return if the level is not high enough
|
||||
if ($record['level'] < $this->level) {
|
||||
return $record;
|
||||
}
|
||||
// we should have the call source now
|
||||
$record['extra'] = array_merge(
|
||||
$record['extra'],
|
||||
$this->introspection->getRecord()
|
||||
);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
145
src/Core/Logger/Type/ProfilerLogger.php
Normal file
145
src/Core/Logger/Type/ProfilerLogger.php
Normal file
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Type;
|
||||
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* This Logger adds additional profiling data in case profiling is enabled.
|
||||
* It uses a predefined logger.
|
||||
*/
|
||||
class ProfilerLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* The Logger of the current call
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* The Profiler for the current call
|
||||
* @var Profiler
|
||||
*/
|
||||
protected $profiler;
|
||||
|
||||
/**
|
||||
* ProfilerLogger constructor.
|
||||
* @param LoggerInterface $logger The Logger of the current call
|
||||
* @param Profiler $profiler The profiler of the current call
|
||||
*/
|
||||
public function __construct(LoggerInterface $logger, Profiler $profiler)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->profiler = $profiler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->emergency($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->alert($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->critical($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->error($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->warning($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->notice($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->info($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->debug($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->log($level, $message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
}
|
27
src/Core/Logger/Type/README.md
Normal file
27
src/Core/Logger/Type/README.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
## Friendica\Util\Logger
|
||||
|
||||
This namespace contains the different implementations of a Logger.
|
||||
|
||||
### Configuration guideline
|
||||
|
||||
The following settings are possible for `logger_config`:
|
||||
- `monolog`: A Logging framework with lots of additions (see [Monolog](https://github.com/Seldaek/monolog/)). There are just Friendica additions inside the Monolog directory
|
||||
- [`stream`](StreamLogger.php): A small logger for files or streams
|
||||
- [`syslog`](SyslogLogger.php): Prints the logging output into the syslog
|
||||
|
||||
[`VoidLogger`](VoidLogger.php) is a fallback logger without any function if no debugging is enabled.
|
||||
|
||||
[`ProfilerLogger`](ProfilerLogger.php) is a wrapper around an existing logger in case profiling is enabled for Friendica.
|
||||
Every log call will be saved to the `Profiler` with a timestamp.
|
||||
|
||||
### Implementation guideline
|
||||
|
||||
Each logging implementation should pe capable of printing at least the following information:
|
||||
- An unique ID for each Request/Call
|
||||
- The process ID (PID)
|
||||
- A timestamp of the logging entry
|
||||
- The critically of the log entry
|
||||
- A log message
|
||||
- A context of the log message (f.e which user)
|
||||
|
||||
If possible, a Logger should extend [`AbstractLogger`](AbstractLogger.php), because it contains additional, Friendica specific business logic for each logging call.
|
205
src/Core/Logger/Type/StreamLogger.php
Normal file
205
src/Core/Logger/Type/StreamLogger.php
Normal file
|
@ -0,0 +1,205 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerArgumentException;
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Core\Logger\Exception\LogLevelException;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\FileSystem;
|
||||
use Friendica\Core\Logger\Util\Introspection;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* A Logger instance for logging into a stream (file, stdout, stderr)
|
||||
*/
|
||||
class StreamLogger extends AbstractLogger
|
||||
{
|
||||
/**
|
||||
* The minimum loglevel at which this logger will be triggered
|
||||
* @var string
|
||||
*/
|
||||
private $logLevel;
|
||||
|
||||
/**
|
||||
* The file URL of the stream (if needed)
|
||||
* @var string
|
||||
*/
|
||||
private $url;
|
||||
|
||||
/**
|
||||
* The stream, where the current logger is writing into
|
||||
* @var resource
|
||||
*/
|
||||
private $stream;
|
||||
|
||||
/**
|
||||
* The current process ID
|
||||
* @var int
|
||||
*/
|
||||
private $pid;
|
||||
|
||||
/**
|
||||
* @var FileSystem
|
||||
*/
|
||||
private $fileSystem;
|
||||
|
||||
/**
|
||||
* Translates LogLevel log levels to integer values
|
||||
* @var array
|
||||
*/
|
||||
private $levelToInt = [
|
||||
LogLevel::EMERGENCY => 0,
|
||||
LogLevel::ALERT => 1,
|
||||
LogLevel::CRITICAL => 2,
|
||||
LogLevel::ERROR => 3,
|
||||
LogLevel::WARNING => 4,
|
||||
LogLevel::NOTICE => 5,
|
||||
LogLevel::INFO => 6,
|
||||
LogLevel::DEBUG => 7,
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
|
||||
* @param string $level The minimum loglevel at which this logger will be triggered
|
||||
*
|
||||
* @throws LoggerArgumentException
|
||||
* @throws LogLevelException
|
||||
*/
|
||||
public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, string $level = LogLevel::DEBUG)
|
||||
{
|
||||
$this->fileSystem = $fileSystem;
|
||||
|
||||
parent::__construct($channel, $introspection);
|
||||
|
||||
if (is_resource($stream)) {
|
||||
$this->stream = $stream;
|
||||
} elseif (is_string($stream)) {
|
||||
$this->url = $stream;
|
||||
} else {
|
||||
throw new LoggerArgumentException('A stream must either be a resource or a string.');
|
||||
}
|
||||
|
||||
$this->pid = getmypid();
|
||||
if (array_key_exists($level, $this->levelToInt)) {
|
||||
$this->logLevel = $this->levelToInt[$level];
|
||||
} else {
|
||||
throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
|
||||
}
|
||||
|
||||
$this->checkStream();
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if ($this->url && is_resource($this->stream)) {
|
||||
fclose($this->stream);
|
||||
}
|
||||
|
||||
$this->stream = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry to the log
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws LoggerException
|
||||
* @throws LogLevelException
|
||||
*/
|
||||
protected function addEntry($level, string $message, array $context = [])
|
||||
{
|
||||
if (!array_key_exists($level, $this->levelToInt)) {
|
||||
throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
|
||||
}
|
||||
|
||||
$logLevel = $this->levelToInt[$level];
|
||||
|
||||
if ($logLevel > $this->logLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->checkStream();
|
||||
|
||||
$formattedLog = $this->formatLog($level, $message, $context);
|
||||
fwrite($this->stream, $formattedLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a log record for the syslog output
|
||||
*
|
||||
* @param mixed $level The loglevel/priority
|
||||
* @param string $message The message
|
||||
* @param array $context The context of this call
|
||||
*
|
||||
* @return string the formatted syslog output
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
private function formatLog($level, string $message, array $context = []): string
|
||||
{
|
||||
$record = $this->introspection->getRecord();
|
||||
$record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
|
||||
|
||||
try {
|
||||
$logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot get current datetime.', $exception);
|
||||
}
|
||||
$logMessage .= $this->channel . ' ';
|
||||
$logMessage .= '[' . strtoupper($level) . ']: ';
|
||||
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
|
||||
$logMessage .= $this->jsonEncodeArray($context) . ' - ';
|
||||
$logMessage .= $this->jsonEncodeArray($record);
|
||||
$logMessage .= PHP_EOL;
|
||||
|
||||
return $logMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the current stream
|
||||
*
|
||||
* @throws LoggerException
|
||||
* @throws LoggerArgumentException
|
||||
*/
|
||||
private function checkStream()
|
||||
{
|
||||
if (is_resource($this->stream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($this->url)) {
|
||||
throw new LoggerArgumentException('Missing stream URL.');
|
||||
}
|
||||
|
||||
try {
|
||||
$this->stream = $this->fileSystem->createStream($this->url);
|
||||
} catch (\UnexpectedValueException $exception) {
|
||||
throw new LoggerException('Cannot create stream.', $exception);
|
||||
}
|
||||
}
|
||||
}
|
230
src/Core/Logger/Type/SyslogLogger.php
Normal file
230
src/Core/Logger/Type/SyslogLogger.php
Normal file
|
@ -0,0 +1,230 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Core\Logger\Exception\LogLevelException;
|
||||
use Friendica\Core\Logger\Util\Introspection;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* A Logger instance for syslogging (fast, but simple)
|
||||
* @see http://php.net/manual/en/function.syslog.php
|
||||
*/
|
||||
class SyslogLogger extends AbstractLogger
|
||||
{
|
||||
const IDENT = 'Friendica';
|
||||
|
||||
/**
|
||||
* Translates LogLevel log levels to syslog log priorities.
|
||||
* @var array
|
||||
*/
|
||||
private $logLevels = [
|
||||
LogLevel::DEBUG => LOG_DEBUG,
|
||||
LogLevel::INFO => LOG_INFO,
|
||||
LogLevel::NOTICE => LOG_NOTICE,
|
||||
LogLevel::WARNING => LOG_WARNING,
|
||||
LogLevel::ERROR => LOG_ERR,
|
||||
LogLevel::CRITICAL => LOG_CRIT,
|
||||
LogLevel::ALERT => LOG_ALERT,
|
||||
LogLevel::EMERGENCY => LOG_EMERG,
|
||||
];
|
||||
|
||||
/**
|
||||
* Translates log priorities to string outputs
|
||||
* @var array
|
||||
*/
|
||||
private $logToString = [
|
||||
LOG_DEBUG => 'DEBUG',
|
||||
LOG_INFO => 'INFO',
|
||||
LOG_NOTICE => 'NOTICE',
|
||||
LOG_WARNING => 'WARNING',
|
||||
LOG_ERR => 'ERROR',
|
||||
LOG_CRIT => 'CRITICAL',
|
||||
LOG_ALERT => 'ALERT',
|
||||
LOG_EMERG => 'EMERGENCY'
|
||||
];
|
||||
|
||||
/**
|
||||
* Indicates what logging options will be used when generating a log message
|
||||
* @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $logOpts;
|
||||
|
||||
/**
|
||||
* Used to specify what type of program is logging the message
|
||||
* @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $logFacility;
|
||||
|
||||
/**
|
||||
* The minimum loglevel at which this logger will be triggered
|
||||
* @var int
|
||||
*/
|
||||
private $logLevel;
|
||||
|
||||
/**
|
||||
* A error message of the current operation
|
||||
* @var string
|
||||
*/
|
||||
private $errorMessage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param string $level The minimum loglevel at which this logger will be triggered
|
||||
* @param int $logOpts Indicates what logging options will be used when generating a log message
|
||||
* @param int $logFacility Used to specify what type of program is logging the message
|
||||
*
|
||||
* @throws LogLevelException
|
||||
* @throws LoggerException
|
||||
*/
|
||||
public function __construct($channel, Introspection $introspection, string $level = LogLevel::NOTICE, int $logOpts = LOG_PID, int $logFacility = LOG_USER)
|
||||
{
|
||||
parent::__construct($channel, $introspection);
|
||||
$this->logOpts = $logOpts;
|
||||
$this->logFacility = $logFacility;
|
||||
$this->logLevel = $this->mapLevelToPriority($level);
|
||||
$this->introspection->addClasses([self::class]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry to the syslog
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @throws LogLevelException in case the level isn't valid
|
||||
* @throws LoggerException In case the syslog cannot be opened for writing
|
||||
*/
|
||||
protected function addEntry($level, string $message, array $context = [])
|
||||
{
|
||||
$logLevel = $this->mapLevelToPriority($level);
|
||||
|
||||
if ($logLevel > $this->logLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
$formattedLog = $this->formatLog($logLevel, $message, $context);
|
||||
$this->write($logLevel, $formattedLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the LogLevel (@see LogLevel) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters)
|
||||
*
|
||||
* @param string $level A LogLevel
|
||||
*
|
||||
* @return int The SysLog priority
|
||||
*
|
||||
* @throws LogLevelException If the loglevel isn't valid
|
||||
*/
|
||||
public function mapLevelToPriority(string $level): int
|
||||
{
|
||||
if (!array_key_exists($level, $this->logLevels)) {
|
||||
throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
|
||||
}
|
||||
|
||||
return $this->logLevels[$level];
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the Syslog
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
closelog();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a message to the syslog
|
||||
*
|
||||
* @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
|
||||
*
|
||||
* @param int $priority The Priority
|
||||
* @param string $message The message of the log
|
||||
*
|
||||
* @throws LoggerException In case the syslog cannot be opened/written
|
||||
*/
|
||||
private function write(int $priority, string $message)
|
||||
{
|
||||
set_error_handler([$this, 'customErrorHandler']);
|
||||
$opened = openlog(self::IDENT, $this->logOpts, $this->logFacility);
|
||||
restore_error_handler();
|
||||
|
||||
if (!$opened) {
|
||||
throw new LoggerException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
|
||||
}
|
||||
|
||||
$this->syslogWrapper($priority, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a log record for the syslog output
|
||||
*
|
||||
* @param int $level The loglevel/priority
|
||||
* @param string $message The message
|
||||
* @param array $context The context of this call
|
||||
*
|
||||
* @return string the formatted syslog output
|
||||
*/
|
||||
private function formatLog(int $level, string $message, array $context = []): string
|
||||
{
|
||||
$record = $this->introspection->getRecord();
|
||||
$record = array_merge($record, ['uid' => $this->logUid]);
|
||||
|
||||
$logMessage = $this->channel . ' ';
|
||||
$logMessage .= '[' . $this->logToString[$level] . ']: ';
|
||||
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
|
||||
$logMessage .= $this->jsonEncodeArray($context) . ' - ';
|
||||
$logMessage .= $this->jsonEncodeArray($record);
|
||||
|
||||
return $logMessage;
|
||||
}
|
||||
|
||||
private function customErrorHandler($code, $msg)
|
||||
{
|
||||
$this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* A syslog wrapper to make syslog functionality testable
|
||||
*
|
||||
* @param int $level The syslog priority
|
||||
* @param string $entry The message to send to the syslog function
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
protected function syslogWrapper(int $level, string $entry)
|
||||
{
|
||||
set_error_handler([$this, 'customErrorHandler']);
|
||||
$written = syslog($level, $entry);
|
||||
restore_error_handler();
|
||||
|
||||
if (!$written) {
|
||||
throw new LoggerException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
|
||||
}
|
||||
}
|
||||
}
|
235
src/Core/Logger/Type/WorkerLogger.php
Normal file
235
src/Core/Logger/Type/WorkerLogger.php
Normal file
|
@ -0,0 +1,235 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Util\Strings;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* A Logger for specific worker tasks, which adds a worker id to it.
|
||||
* Uses the decorator pattern (https://en.wikipedia.org/wiki/Decorator_pattern)
|
||||
*/
|
||||
class WorkerLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface The original Logger instance
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @var string the current worker ID
|
||||
*/
|
||||
private $workerId;
|
||||
|
||||
/**
|
||||
* @var string The called function name
|
||||
*/
|
||||
private $functionName;
|
||||
|
||||
/**
|
||||
* @param LoggerInterface $logger The logger for worker entries
|
||||
* @param string $functionName The current function name of the worker
|
||||
* @param int $idLength The length of the generated worker ID
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
public function __construct(LoggerInterface $logger, string $functionName = '', int $idLength = 7)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->functionName = $functionName;
|
||||
try {
|
||||
$this->workerId = Strings::getRandomHex($idLength);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot generate random Hex.', $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the function name for additional logging
|
||||
*
|
||||
* @param string $functionName
|
||||
*/
|
||||
public function setFunctionName(string $functionName)
|
||||
{
|
||||
$this->functionName = $functionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the worker context for each log entry
|
||||
*
|
||||
* @param array $context
|
||||
*/
|
||||
private function addContext(array &$context)
|
||||
{
|
||||
$context['worker_id'] = $this->workerId;
|
||||
$context['worker_cmd'] = $this->functionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the worker ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getWorkerId(): string
|
||||
{
|
||||
return $this->workerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* System is unusable.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->emergency($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Action must be taken immediately.
|
||||
*
|
||||
* Example: Entire website down, database unavailable, etc. This should
|
||||
* trigger the SMS alerts and wake you up.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->alert($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Critical conditions.
|
||||
*
|
||||
* Example: Application component unavailable, unexpected exception.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->critical($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime errors that do not require immediate action but should typically
|
||||
* be logged and monitored.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->error($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceptional occurrences that are not errors.
|
||||
*
|
||||
* Example: Use of deprecated APIs, poor use of an API, undesirable things
|
||||
* that are not necessarily wrong.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->warning($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normal but significant events.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->notice($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interesting events.
|
||||
*
|
||||
* Example: User logs in, SQL logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->info($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detailed debug information.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->debug($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs with an arbitrary level.
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->log($level, $message, $context);
|
||||
}
|
||||
}
|
110
src/Core/Logger/Util/Introspection.php
Normal file
110
src/Core/Logger/Util/Introspection.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Logger\Util;
|
||||
|
||||
/**
|
||||
* Get Introspection information about the current call
|
||||
*/
|
||||
class Introspection
|
||||
{
|
||||
/** @var int */
|
||||
private $skipStackFramesCount;
|
||||
|
||||
/** @var string[] */
|
||||
private $skipClassesPartials;
|
||||
|
||||
private $skipFunctions = [
|
||||
'call_user_func',
|
||||
'call_user_func_array',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param string[] $skipClassesPartials An array of classes to skip during logging
|
||||
* @param int $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
|
||||
*/
|
||||
public function __construct(array $skipClassesPartials = [], int $skipStackFramesCount = 0)
|
||||
{
|
||||
$this->skipClassesPartials = $skipClassesPartials;
|
||||
$this->skipStackFramesCount = $skipStackFramesCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new classes to get skipped
|
||||
*
|
||||
* @param array $classNames
|
||||
*/
|
||||
public function addClasses(array $classNames)
|
||||
{
|
||||
$this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the introspection record of the current call
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRecord(): array
|
||||
{
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
|
||||
$i = 1;
|
||||
|
||||
while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
|
||||
$i++;
|
||||
}
|
||||
|
||||
$i += $this->skipStackFramesCount;
|
||||
|
||||
return [
|
||||
'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
|
||||
'line' => $trace[$i - 1]['line'] ?? null,
|
||||
'function' => $trace[$i]['function'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current trace class or function has to be skipped
|
||||
*
|
||||
* @param array $trace The current trace array
|
||||
* @param int $index The index of the current hierarchy level
|
||||
*
|
||||
* @return bool True if the class or function should get skipped, otherwise false
|
||||
*/
|
||||
private function isTraceClassOrSkippedFunction(array $trace, int $index): bool
|
||||
{
|
||||
if (!isset($trace[$index])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($trace[$index]['class'])) {
|
||||
foreach ($this->skipClassesPartials as $part) {
|
||||
if (strpos($trace[$index]['class'], $part) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -19,35 +19,34 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\PConfig;
|
||||
namespace Friendica\Core\PConfig\Capability;
|
||||
|
||||
use Friendica\Core\PConfig\ValueObject;
|
||||
|
||||
/**
|
||||
* Interface for accessing user specific configurations
|
||||
*/
|
||||
interface IPConfig
|
||||
interface IManagePersonalConfigValues
|
||||
{
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param int $uid The user_id
|
||||
* @param int $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
*
|
||||
* @return array The loaded config array
|
||||
* @see Cache
|
||||
*
|
||||
*/
|
||||
function load(int $uid, string $cat = 'config');
|
||||
public function load(int $uid, string $cat = 'config'): array;
|
||||
|
||||
/**
|
||||
* 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 PConfigCache).
|
||||
* and the $key with the $uid from a cached storage either from the database
|
||||
* or from the configCache
|
||||
*
|
||||
* @param int $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
|
@ -56,8 +55,9 @@ interface IPConfig
|
|||
* @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
|
||||
*
|
||||
*/
|
||||
function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false);
|
||||
public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false);
|
||||
|
||||
/**
|
||||
* Sets a configuration value for a user
|
||||
|
@ -74,28 +74,26 @@ interface IPConfig
|
|||
*
|
||||
* @return bool Operation success
|
||||
*/
|
||||
function set(int $uid, string $cat, string $key, $value);
|
||||
public function set(int $uid, string $cat, string $key, $value): bool;
|
||||
|
||||
/**
|
||||
* Deletes the given key from the users's configuration.
|
||||
* Deletes the given key from the users 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.
|
||||
* Removes the configured value from the stored cache and removes it from the database with the given $uid.
|
||||
*
|
||||
* @param int $uid The user_id
|
||||
* @param int $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to delete
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function delete(int $uid, string $cat, string $key);
|
||||
public function delete(int $uid, string $cat, string $key): bool;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the Config Cache
|
||||
*
|
||||
* @return Cache
|
||||
* @return ValueObject\Cache
|
||||
*/
|
||||
function getCache();
|
||||
public function getCache(): ValueObject\Cache;
|
||||
}
|
13
src/Core/PConfig/Exception/PConfigPersistenceException.php
Normal file
13
src/Core/PConfig/Exception/PConfigPersistenceException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\PConfig\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class PConfigPersistenceException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
49
src/Core/PConfig/Factory/PConfig.php
Normal file
49
src/Core/PConfig/Factory/PConfig.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\PConfig\Factory;
|
||||
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
|
||||
use Friendica\Core\PConfig\Repository;
|
||||
use Friendica\Core\PConfig\Type;
|
||||
use Friendica\Core\PConfig\ValueObject;
|
||||
|
||||
class PConfig
|
||||
{
|
||||
/**
|
||||
* @param Cache $configCache The config cache
|
||||
* @param ValueObject\Cache $pConfigCache The personal config cache
|
||||
* @param Repository\PConfig $configRepo The configuration model
|
||||
*
|
||||
* @return IManagePersonalConfigValues
|
||||
*/
|
||||
public function create(Cache $configCache, ValueObject\Cache $pConfigCache, Repository\PConfig $configRepo): IManagePersonalConfigValues
|
||||
{
|
||||
if ($configCache->get('system', 'config_adapter') === 'preload') {
|
||||
$configuration = new Type\PreloadPConfig($pConfigCache, $configRepo);
|
||||
} else {
|
||||
$configuration = new Type\JitPConfig($pConfigCache, $configRepo);
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
}
|
191
src/Core/PConfig/Repository/PConfig.php
Normal file
191
src/Core/PConfig/Repository/PConfig.php
Normal file
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\PConfig\Repository;
|
||||
|
||||
use Friendica\Core\Config\Util\ValueConversion;
|
||||
use Friendica\Core\PConfig\Exception\PConfigPersistenceException;
|
||||
use Friendica\Database\Database;
|
||||
|
||||
/**
|
||||
* The Config model backend for users, which is using the general DB-model backend for user-configs
|
||||
*/
|
||||
class PConfig
|
||||
{
|
||||
protected static $table_name = 'pconfig';
|
||||
|
||||
/** @var Database */
|
||||
protected $db;
|
||||
|
||||
public function __construct(Database $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the model is currently connected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected(): bool
|
||||
{
|
||||
return $this->db->isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all configuration values and returns the loaded category as an array.
|
||||
*
|
||||
* @param int $uid The id of the user to load
|
||||
* @param string|null $cat The category of the configuration values to load
|
||||
*
|
||||
* @return string[][] The config array
|
||||
*
|
||||
* @throws PConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function load(int $uid, ?string $cat = null): array
|
||||
{
|
||||
$return = [];
|
||||
|
||||
try {
|
||||
if (empty($cat)) {
|
||||
$configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['uid' => $uid]);
|
||||
} else {
|
||||
$configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
|
||||
}
|
||||
|
||||
while ($config = $this->db->fetch($configs)) {
|
||||
$key = $config['k'];
|
||||
$value = ValueConversion::toConfigValue($config['v']);
|
||||
|
||||
// just save it in case it is set
|
||||
if (isset($value)) {
|
||||
$return[$config['cat']][$key] = $value;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new PConfigPersistenceException(sprintf('Cannot load config category %s for user %d', $cat, $uid), $exception);
|
||||
} finally {
|
||||
$this->db->close($configs);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a particular user config variable out of the DB with the
|
||||
* given category name ($cat) and a key ($key).
|
||||
*
|
||||
* Note: Boolean variables are defined as 0/1 in the database
|
||||
*
|
||||
* @param int $uid The id of the user to load
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to query
|
||||
*
|
||||
* @return array|string|null Stored value or null if it does not exist
|
||||
*
|
||||
* @throws PConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function get(int $uid, string $cat, string $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$config = $this->db->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
if ($this->db->isResult($config)) {
|
||||
$value = ValueConversion::toConfigValue($config['v']);
|
||||
|
||||
// just return it in case it is set
|
||||
if (isset($value)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new PConfigPersistenceException(sprintf('Cannot get config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a config value ($value) in the category ($cat) under the key ($key) for a
|
||||
* given user ($uid).
|
||||
*
|
||||
* Note: Please do not store booleans - convert to 0/1 integer values!
|
||||
*
|
||||
* @param int $uid The id of the user to load
|
||||
* @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
|
||||
*
|
||||
* @throws PConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function set(int $uid, string $cat, string $key, $value): bool
|
||||
{
|
||||
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);
|
||||
|
||||
if (isset($stored_value) && ($stored_value === $compare_value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
$dbValue = ValueConversion::toDbValue($value);
|
||||
return $this->db->update(static::$table_name, ['v' => $dbValue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
|
||||
} catch (\Exception $exception) {
|
||||
throw new PConfigPersistenceException(sprintf('Cannot set config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the configured value of the given user.
|
||||
*
|
||||
* @param int $uid The id of the user to load
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to delete
|
||||
*
|
||||
* @return bool Operation success
|
||||
*
|
||||
* @throws PConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function delete(int $uid, string $cat, string $key): bool
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->db->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
} catch (\Exception $exception) {
|
||||
throw new PConfigPersistenceException(sprintf('Cannot delete config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,20 +19,20 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core;
|
||||
namespace Friendica\Core\PConfig\Type;
|
||||
|
||||
use Friendica\Core\PConfig\Cache;
|
||||
use Friendica\Core\PConfig\IPConfig;
|
||||
use Friendica\Model;
|
||||
use Friendica\Core\PConfig\Repository;
|
||||
use Friendica\Core\PConfig\ValueObject\Cache;
|
||||
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
|
||||
|
||||
/**
|
||||
* This class is responsible for the user-specific configuration values in Friendica
|
||||
* The values are set through the Config-DB-Table (per Config-DB-model @see Model\Config\PConfig)
|
||||
* The values are set through the Config-DB-Table (per Config-DB-model @see Repository\PConfig)
|
||||
*
|
||||
* The configuration cache (@see Cache\PConfigCache) is used for temporary caching of database calls. This will
|
||||
* The configuration cache (@see Cache) is used for temporary caching of database calls. This will
|
||||
* increase the performance.
|
||||
*/
|
||||
abstract class BasePConfig implements IPConfig
|
||||
abstract class AbstractPConfigValues implements IManagePersonalConfigValues
|
||||
{
|
||||
/**
|
||||
* @var Cache
|
||||
|
@ -40,18 +40,18 @@ abstract class BasePConfig implements IPConfig
|
|||
protected $configCache;
|
||||
|
||||
/**
|
||||
* @var Model\Config\PConfig
|
||||
* @var Repository\PConfig
|
||||
*/
|
||||
protected $configModel;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache
|
||||
* @param Model\Config\PConfig $configModel The configuration model
|
||||
* @param Cache $configCache The configuration cache
|
||||
* @param Repository\PConfig $configRepo The configuration model
|
||||
*/
|
||||
public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
|
||||
public function __construct(Cache $configCache, Repository\PConfig $configRepo)
|
||||
{
|
||||
$this->configCache = $configCache;
|
||||
$this->configModel = $configModel;
|
||||
$this->configModel = $configRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,7 +59,7 @@ abstract class BasePConfig implements IPConfig
|
|||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function getCache()
|
||||
public function getCache(): Cache
|
||||
{
|
||||
return $this->configCache;
|
||||
}
|
|
@ -19,10 +19,10 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\PConfig;
|
||||
namespace Friendica\Core\PConfig\Type;
|
||||
|
||||
use Friendica\Core\BasePConfig;
|
||||
use Friendica\Model;
|
||||
use Friendica\Core\PConfig\Repository;
|
||||
use Friendica\Core\PConfig\ValueObject;
|
||||
|
||||
/**
|
||||
* This class implements the Just-In-Time configuration, which will cache
|
||||
|
@ -31,7 +31,7 @@ use Friendica\Model;
|
|||
* Default Configuration type.
|
||||
* Provides the best performance for pages loading few configuration variables.
|
||||
*/
|
||||
class JitPConfig extends BasePConfig
|
||||
class JitPConfig extends AbstractPConfigValues
|
||||
{
|
||||
/**
|
||||
* @var array Array of already loaded db values (even if there was no value)
|
||||
|
@ -39,12 +39,12 @@ class JitPConfig extends BasePConfig
|
|||
private $db_loaded;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache
|
||||
* @param Model\Config\PConfig $configModel The configuration model
|
||||
* @param ValueObject\Cache $configCache The configuration cache
|
||||
* @param Repository\PConfig $configRepo The configuration model
|
||||
*/
|
||||
public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
|
||||
public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
|
||||
{
|
||||
parent::__construct($configCache, $configModel);
|
||||
parent::__construct($configCache, $configRepo);
|
||||
$this->db_loaded = [];
|
||||
}
|
||||
|
||||
|
@ -52,11 +52,11 @@ class JitPConfig extends BasePConfig
|
|||
* {@inheritDoc}
|
||||
*
|
||||
*/
|
||||
public function load(int $uid, string $cat = 'config')
|
||||
public function load(int $uid, string $cat = 'config'): array
|
||||
{
|
||||
// If not connected or no uid, do nothing
|
||||
if (!$uid || !$this->configModel->isConnected()) {
|
||||
return;
|
||||
return [];
|
||||
}
|
||||
|
||||
$config = $this->configModel->load($uid, $cat);
|
||||
|
@ -84,14 +84,12 @@ class JitPConfig extends BasePConfig
|
|||
|
||||
// if the value isn't loaded or refresh is needed, load it to the cache
|
||||
if ($this->configModel->isConnected() &&
|
||||
(empty($this->db_loaded[$uid][$cat][$key]) ||
|
||||
$refresh)) {
|
||||
(empty($this->db_loaded[$uid][$cat][$key]) || $refresh)) {
|
||||
$dbValue = $this->configModel->get($uid, $cat, $key);
|
||||
|
||||
$dbvalue = $this->configModel->get($uid, $cat, $key);
|
||||
|
||||
if (isset($dbvalue)) {
|
||||
$this->configCache->set($uid, $cat, $key, $dbvalue);
|
||||
unset($dbvalue);
|
||||
if (isset($dbValue)) {
|
||||
$this->configCache->set($uid, $cat, $key, $dbValue);
|
||||
unset($dbValue);
|
||||
}
|
||||
|
||||
$this->db_loaded[$uid][$cat][$key] = true;
|
||||
|
@ -106,7 +104,7 @@ class JitPConfig extends BasePConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(int $uid, string $cat, string $key, $value)
|
||||
public function set(int $uid, string $cat, string $key, $value): bool
|
||||
{
|
||||
if (!$uid) {
|
||||
return false;
|
||||
|
@ -130,7 +128,7 @@ class JitPConfig extends BasePConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete(int $uid, string $cat, string $key)
|
||||
public function delete(int $uid, string $cat, string $key): bool
|
||||
{
|
||||
if (!$uid) {
|
||||
return false;
|
|
@ -19,10 +19,10 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\PConfig;
|
||||
namespace Friendica\Core\PConfig\Type;
|
||||
|
||||
use Friendica\Core\BasePConfig;
|
||||
use Friendica\Model;
|
||||
use Friendica\Core\PConfig\Repository;
|
||||
use Friendica\Core\PConfig\ValueObject;
|
||||
|
||||
/**
|
||||
* This class implements the preload configuration, which will cache
|
||||
|
@ -30,18 +30,18 @@ use Friendica\Model;
|
|||
*
|
||||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
||||
*/
|
||||
class PreloadPConfig extends BasePConfig
|
||||
class PreloadPConfig extends AbstractPConfigValues
|
||||
{
|
||||
/** @var array */
|
||||
private $config_loaded;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache
|
||||
* @param Model\Config\PConfig $configModel The configuration model
|
||||
* @param ValueObject\Cache $configCache The configuration cache
|
||||
* @param Repository\PConfig $configRepo The configuration model
|
||||
*/
|
||||
public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
|
||||
public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
|
||||
{
|
||||
parent::__construct($configCache, $configModel);
|
||||
parent::__construct($configCache, $configRepo);
|
||||
$this->config_loaded = [];
|
||||
}
|
||||
|
||||
|
@ -51,16 +51,16 @@ class PreloadPConfig extends BasePConfig
|
|||
* This loads all config values everytime load is called
|
||||
*
|
||||
*/
|
||||
public function load(int $uid, string $cat = 'config')
|
||||
public function load(int $uid, string $cat = 'config'): array
|
||||
{
|
||||
// Don't load the whole configuration twice or with invalid uid
|
||||
if (!$uid || !empty($this->config_loaded[$uid])) {
|
||||
return;
|
||||
return [];
|
||||
}
|
||||
|
||||
// If not connected, do nothing
|
||||
if (!$this->configModel->isConnected()) {
|
||||
return;
|
||||
return [];
|
||||
}
|
||||
|
||||
$config = $this->configModel->load($uid);
|
||||
|
@ -101,7 +101,7 @@ class PreloadPConfig extends BasePConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(int $uid, string $cat, string $key, $value)
|
||||
public function set(int $uid, string $cat, string $key, $value): bool
|
||||
{
|
||||
if (!$uid) {
|
||||
return false;
|
||||
|
@ -127,7 +127,7 @@ class PreloadPConfig extends BasePConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete(int $uid, string $cat, string $key)
|
||||
public function delete(int $uid, string $cat, string $key): bool
|
||||
{
|
||||
if (!$uid) {
|
||||
return false;
|
|
@ -19,7 +19,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\PConfig;
|
||||
namespace Friendica\Core\PConfig\ValueObject;
|
||||
|
||||
use ParagonIE\HiddenString\HiddenString;
|
||||
|
||||
|
@ -31,7 +31,7 @@ class Cache
|
|||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
private $config = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
|
@ -53,7 +53,7 @@ class Cache
|
|||
* @param int $uid
|
||||
* @param array $config
|
||||
*/
|
||||
public function load($uid, array $config)
|
||||
public function load(int $uid, array $config)
|
||||
{
|
||||
if (!is_int($uid)) {
|
||||
return;
|
||||
|
@ -63,7 +63,6 @@ class Cache
|
|||
|
||||
foreach ($categories as $category) {
|
||||
if (isset($config[$category]) && is_array($config[$category])) {
|
||||
|
||||
$keys = array_keys($config[$category]);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
|
@ -81,11 +80,11 @@ class Cache
|
|||
*
|
||||
* @param int $uid User Id
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
* @param string|null $key Config key
|
||||
*
|
||||
* @return null|string The value of the config entry or null if not set
|
||||
* @return null|mixed The value of the config entry or null if not set
|
||||
*/
|
||||
public function get($uid, string $cat, string $key = null)
|
||||
public function get(int $uid, string $cat, ?string $key = null)
|
||||
{
|
||||
if (!is_int($uid)) {
|
||||
return null;
|
||||
|
@ -112,7 +111,7 @@ class Cache
|
|||
*
|
||||
* @return bool Set successful
|
||||
*/
|
||||
public function set($uid, string $cat, string $key, $value)
|
||||
public function set(int $uid, string $cat, string $key, $value): bool
|
||||
{
|
||||
if (!is_int($uid)) {
|
||||
return false;
|
||||
|
@ -127,8 +126,8 @@ class Cache
|
|||
}
|
||||
|
||||
if ($this->hidePasswordOutput &&
|
||||
$key == 'password' &&
|
||||
!empty($value) && is_string($value)) {
|
||||
$key == 'password' &&
|
||||
!empty($value) && is_string($value)) {
|
||||
$this->config[$uid][$cat][$key] = new HiddenString((string)$value);
|
||||
} else {
|
||||
$this->config[$uid][$cat][$key] = $value;
|
||||
|
@ -147,7 +146,7 @@ class Cache
|
|||
*
|
||||
* @return bool true, if deleted
|
||||
*/
|
||||
public function delete($uid, string $cat, string $key)
|
||||
public function delete(int $uid, string $cat, string $key): bool
|
||||
{
|
||||
if (!is_int($uid)) {
|
||||
return false;
|
||||
|
@ -171,9 +170,9 @@ class Cache
|
|||
/**
|
||||
* Returns the whole configuration
|
||||
*
|
||||
* @return array The configuration
|
||||
* @return string[][] The configuration
|
||||
*/
|
||||
public function getAll()
|
||||
public function getAll(): array
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
@ -181,11 +180,11 @@ class Cache
|
|||
/**
|
||||
* Returns an array with missing categories/Keys
|
||||
*
|
||||
* @param array $config The array to check
|
||||
* @param string[][] $config The array to check
|
||||
*
|
||||
* @return array
|
||||
* @return string[][]
|
||||
*/
|
||||
public function keyDiff(array $config)
|
||||
public function keyDiff(array $config): array
|
||||
{
|
||||
$return = [];
|
||||
|
|
@ -22,7 +22,7 @@
|
|||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Model;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
@ -48,7 +48,7 @@ class Process
|
|||
private $mode;
|
||||
|
||||
/**
|
||||
* @var IConfig
|
||||
* @var IManageConfigValues
|
||||
*/
|
||||
private $config;
|
||||
|
||||
|
@ -67,7 +67,7 @@ class Process
|
|||
*/
|
||||
private $pid;
|
||||
|
||||
public function __construct(LoggerInterface $logger, App\Mode $mode, IConfig $config, Model\Process $processModel, string $basepath, int $pid)
|
||||
public function __construct(LoggerInterface $logger, App\Mode $mode, IManageConfigValues $config, Model\Process $processModel, string $basepath, int $pid)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->mode = $mode;
|
||||
|
@ -176,7 +176,7 @@ class Process
|
|||
if (count($data) != 2) {
|
||||
continue;
|
||||
}
|
||||
list($key, $val) = $data;
|
||||
[$key, $val] = $data;
|
||||
$meminfo[$key] = (int)trim(str_replace('kB', '', $val));
|
||||
$meminfo[$key] = (int)($meminfo[$key] / 1024);
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace Friendica\Core;
|
|||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Network\HTTPClientOptions;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
||||
use Friendica\Object\Search\ContactResult;
|
||||
use Friendica\Object\Search\ResultList;
|
||||
use Friendica\Util\Network;
|
||||
|
@ -228,7 +228,7 @@ class Search
|
|||
$return = Contact::searchByName($search, $mode);
|
||||
} else {
|
||||
$p = $page > 1 ? 'p=' . $page : '';
|
||||
$curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HTTPClientOptions::ACCEPT_CONTENT => ['application/json']]);
|
||||
$curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), [HttpClientOptions::ACCEPT_CONTENT => ['application/json']]);
|
||||
if ($curlResult->isSuccess()) {
|
||||
$searchResult = json_decode($curlResult->getBody(), true);
|
||||
if (!empty($searchResult['profiles'])) {
|
||||
|
|
|
@ -19,19 +19,19 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
namespace Friendica\Core\Session\Capability;
|
||||
|
||||
/**
|
||||
* Contains all global supported Session methods
|
||||
*/
|
||||
interface ISession
|
||||
interface IHandleSessions
|
||||
{
|
||||
/**
|
||||
* Start the current session
|
||||
*
|
||||
* @return self The own Session instance
|
||||
*/
|
||||
public function start();
|
||||
public function start(): IHandleSessions;
|
||||
|
||||
/**
|
||||
* Checks if the key exists in this session
|
||||
|
@ -40,7 +40,7 @@ interface ISession
|
|||
*
|
||||
* @return boolean True, if it exists
|
||||
*/
|
||||
public function exists(string $name);
|
||||
public function exists(string $name): bool;
|
||||
|
||||
/**
|
||||
* Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
|
94
src/Core/Session/Factory/Session.php
Normal file
94
src/Core/Session/Factory/Session.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Session\Factory;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Enum;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Session\Capability\IHandleSessions;
|
||||
use Friendica\Core\Session\Type;
|
||||
use Friendica\Core\Session\Handler;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Factory for creating a valid Session for this run
|
||||
*/
|
||||
class Session
|
||||
{
|
||||
/** @var string The plain, PHP internal session management */
|
||||
const HANDLER_NATIVE = 'native';
|
||||
/** @var string Using the database for session management */
|
||||
const HANDLER_DATABASE = 'database';
|
||||
/** @var string Using the cache for session management */
|
||||
const HANDLER_CACHE = 'cache';
|
||||
|
||||
const HANDLER_DEFAULT = self::HANDLER_DATABASE;
|
||||
|
||||
/**
|
||||
* @param App\Mode $mode
|
||||
* @param App\BaseURL $baseURL
|
||||
* @param IManageConfigValues $config
|
||||
* @param Database $dba
|
||||
* @param ICanCache $cache
|
||||
* @param LoggerInterface $logger
|
||||
* @param Profiler $profiler
|
||||
* @param array $server
|
||||
*
|
||||
* @return IHandleSessions
|
||||
*/
|
||||
public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, ICanCache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
|
||||
{
|
||||
$profiler->startRecording('session');
|
||||
$session = null;
|
||||
|
||||
try {
|
||||
if ($mode->isInstall() || $mode->isBackend()) {
|
||||
$session = new Type\Memory();
|
||||
} else {
|
||||
$session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
|
||||
$handler = null;
|
||||
|
||||
switch ($session_handler) {
|
||||
case self::HANDLER_DATABASE:
|
||||
$handler = new Handler\Database($dba, $logger, $server);
|
||||
break;
|
||||
case self::HANDLER_CACHE:
|
||||
// In case we're using the db as cache driver, use the native db session, not the cache
|
||||
if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) {
|
||||
$handler = new Handler\Database($dba, $logger, $server);
|
||||
} else {
|
||||
$handler = new Handler\Cache($cache, $logger);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$session = new Type\Native($baseURL, $handler);
|
||||
}
|
||||
} finally {
|
||||
$profiler->stopRecording();
|
||||
return $session;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,8 +21,10 @@
|
|||
|
||||
namespace Friendica\Core\Session\Handler;
|
||||
|
||||
use Friendica\Core\Cache\ICache;
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Core\Session;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
/**
|
||||
|
@ -30,29 +32,37 @@ use SessionHandlerInterface;
|
|||
*/
|
||||
class Cache implements SessionHandlerInterface
|
||||
{
|
||||
/** @var ICache */
|
||||
/** @var ICanCache */
|
||||
private $cache;
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
public function __construct(ICache $cache)
|
||||
public function __construct(ICanCache $cache, LoggerInterface $logger)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->cache = $cache;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function open($save_path, $session_name)
|
||||
public function open($path, $name): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function read($session_id)
|
||||
public function read($id)
|
||||
{
|
||||
if (empty($session_id)) {
|
||||
if (empty($id)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$data = $this->cache->get('session:' . $session_id);
|
||||
if (!empty($data)) {
|
||||
Session::$exists = true;
|
||||
return $data;
|
||||
try {
|
||||
$data = $this->cache->get('session:' . $id);
|
||||
if (!empty($data)) {
|
||||
Session::$exists = true;
|
||||
return $data;
|
||||
}
|
||||
} catch (CachePersistenceException $exception) {
|
||||
$this->logger->warning('Cannot read session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return '';
|
||||
}
|
||||
|
||||
return '';
|
||||
|
@ -65,36 +75,45 @@ class Cache implements SessionHandlerInterface
|
|||
* on the case. Uses the Session::expire for existing session, 5 minutes
|
||||
* for newly created session.
|
||||
*
|
||||
* @param string $session_id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $session_data Serialized session data
|
||||
* @param string $id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $data Serialized session data
|
||||
*
|
||||
* @return boolean Returns false if parameters are missing, true otherwise
|
||||
* @throws \Exception
|
||||
* @return bool Returns false if parameters are missing, true otherwise
|
||||
*/
|
||||
public function write($session_id, $session_data)
|
||||
public function write($id, $data): bool
|
||||
{
|
||||
if (!$session_id) {
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$session_data) {
|
||||
return $this->destroy($session_id);
|
||||
if (!$data) {
|
||||
return $this->destroy($id);
|
||||
}
|
||||
|
||||
return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
|
||||
try {
|
||||
return $this->cache->set('session:' . $id, $data, Session::$expire);
|
||||
} catch (CachePersistenceException $exception) {
|
||||
$this->logger->warning('Cannot write session', ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
public function destroy($id): bool
|
||||
{
|
||||
return $this->cache->delete('session:' . $id);
|
||||
try {
|
||||
return $this->cache->delete('session:' . $id);
|
||||
} catch (CachePersistenceException $exception) {
|
||||
$this->logger->warning('Cannot destroy session', ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function gc($maxlifetime)
|
||||
public function gc($max_lifetime): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -52,24 +52,29 @@ class Database implements SessionHandlerInterface
|
|||
$this->server = $server;
|
||||
}
|
||||
|
||||
public function open($save_path, $session_name)
|
||||
public function open($path, $name): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function read($session_id)
|
||||
public function read($id)
|
||||
{
|
||||
if (empty($session_id)) {
|
||||
if (empty($id)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
|
||||
if ($this->dba->isResult($session)) {
|
||||
Session::$exists = true;
|
||||
return $session['data'];
|
||||
try {
|
||||
$session = $this->dba->selectFirst('session', ['data'], ['sid' => $id]);
|
||||
if ($this->dba->isResult($session)) {
|
||||
Session::$exists = true;
|
||||
return $session['data'];
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot read session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
|
||||
$this->logger->notice('no data for session', ['session_id' => $id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
|
||||
|
||||
return '';
|
||||
}
|
||||
|
@ -81,49 +86,63 @@ class Database implements SessionHandlerInterface
|
|||
* on the case. Uses the Session::expire global for existing session, 5 minutes
|
||||
* for newly created session.
|
||||
*
|
||||
* @param string $session_id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $session_data Serialized session data
|
||||
* @param string $id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $data Serialized session data
|
||||
*
|
||||
* @return boolean Returns false if parameters are missing, true otherwise
|
||||
* @throws \Exception
|
||||
* @return bool Returns false if parameters are missing, true otherwise
|
||||
*/
|
||||
public function write($session_id, $session_data)
|
||||
public function write($id, $data): bool
|
||||
{
|
||||
if (!$session_id) {
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$session_data) {
|
||||
return $this->destroy($session_id);
|
||||
if (!$data) {
|
||||
return $this->destroy($id);
|
||||
}
|
||||
|
||||
$expire = time() + Session::$expire;
|
||||
$default_expire = time() + 300;
|
||||
|
||||
if (Session::$exists) {
|
||||
$fields = ['data' => $session_data, 'expire' => $expire];
|
||||
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
|
||||
$this->dba->update('session', $fields, $condition);
|
||||
} else {
|
||||
$fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
|
||||
$this->dba->insert('session', $fields);
|
||||
try {
|
||||
if (Session::$exists) {
|
||||
$fields = ['data' => $data, 'expire' => $expire];
|
||||
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire];
|
||||
$this->dba->update('session', $fields, $condition);
|
||||
} else {
|
||||
$fields = ['sid' => $id, 'expire' => $default_expire, 'data' => $data];
|
||||
$this->dba->insert('session', $fields);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot write session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
public function destroy($id): bool
|
||||
{
|
||||
return $this->dba->delete('session', ['sid' => $id]);
|
||||
try {
|
||||
return $this->dba->delete('session', ['sid' => $id]);
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot destroy session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function gc($maxlifetime)
|
||||
public function gc($max_lifetime): bool
|
||||
{
|
||||
return $this->dba->delete('session', ["`expire` < ?", time()]);
|
||||
try {
|
||||
return $this->dba->delete('session', ["`expire` < ?", time()]);
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot use garbage collector.'. ['exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,17 +19,19 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
namespace Friendica\Core\Session\Type;
|
||||
|
||||
use Friendica\Core\Session\Capability\IHandleSessions;
|
||||
|
||||
/**
|
||||
* Contains the base methods for $_SESSION interaction
|
||||
*/
|
||||
class AbstractSession
|
||||
class AbstractSession implements IHandleSessions
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function start()
|
||||
public function start(): IHandleSessions
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
@ -37,7 +39,7 @@ class AbstractSession
|
|||
/**
|
||||
* {@inheritDoc}}
|
||||
*/
|
||||
public function exists(string $name)
|
||||
public function exists(string $name): bool
|
||||
{
|
||||
return isset($_SESSION[$name]);
|
||||
}
|
|
@ -19,14 +19,16 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
namespace Friendica\Core\Session\Type;
|
||||
|
||||
use Friendica\Core\Session\Capability\IHandleSessions;
|
||||
|
||||
/**
|
||||
* Usable for backend processes (daemon/worker) and testing
|
||||
*
|
||||
* @todo after replacing the last direct $_SESSION call, use a internal array instead of the global variable
|
||||
*/
|
||||
class Memory extends AbstractSession implements ISession
|
||||
class Memory extends AbstractSession implements IHandleSessions
|
||||
{
|
||||
public function __construct()
|
||||
{
|
|
@ -19,16 +19,17 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
namespace Friendica\Core\Session\Type;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Session\Capability\IHandleSessions;
|
||||
use Friendica\Model\User\Cookie;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
/**
|
||||
* The native Session class which uses the PHP internal Session functions
|
||||
*/
|
||||
class Native extends AbstractSession implements ISession
|
||||
class Native extends AbstractSession implements IHandleSessions
|
||||
{
|
||||
public function __construct(App\BaseURL $baseURL, SessionHandlerInterface $handler = null)
|
||||
{
|
||||
|
@ -48,7 +49,7 @@ class Native extends AbstractSession implements ISession
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function start()
|
||||
public function start(): IHandleSessions
|
||||
{
|
||||
session_start();
|
||||
return $this;
|
78
src/Core/Storage/Capability/ICanConfigureStorage.php
Normal file
78
src/Core/Storage/Capability/ICanConfigureStorage.php
Normal file
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Capability;
|
||||
|
||||
/**
|
||||
* The interface to use for configurable storage backends
|
||||
*/
|
||||
interface ICanConfigureStorage
|
||||
{
|
||||
/**
|
||||
* Get info about storage options
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* This method return an array with information about storage options
|
||||
* from which the form presented to the user is build.
|
||||
*
|
||||
* The returned array is:
|
||||
*
|
||||
* [
|
||||
* 'option1name' => [ ..info.. ],
|
||||
* 'option2name' => [ ..info.. ],
|
||||
* ...
|
||||
* ]
|
||||
*
|
||||
* An empty array can be returned if backend doesn't have any options
|
||||
*
|
||||
* The info array for each option MUST be as follows:
|
||||
*
|
||||
* [
|
||||
* 'type', // define the field used in form, and the type of data.
|
||||
* // one of 'checkbox', 'combobox', 'custom', 'datetime',
|
||||
* // 'input', 'intcheckbox', 'password', 'radio', 'richtext'
|
||||
* // 'select', 'select_raw', 'textarea'
|
||||
*
|
||||
* 'label', // Translatable label of the field
|
||||
* 'value', // Current value
|
||||
* 'help text', // Translatable description for the field
|
||||
* extra data // Optional. Depends on 'type':
|
||||
* // select: array [ value => label ] of choices
|
||||
* // intcheckbox: value of input element
|
||||
* // select_raw: prebuild html string of < option > tags
|
||||
* ]
|
||||
*
|
||||
* See https://github.com/friendica/friendica/wiki/Quick-Template-Guide
|
||||
*/
|
||||
public function getOptions(): array;
|
||||
|
||||
/**
|
||||
* Validate and save options
|
||||
*
|
||||
* @param array $data Array [optionname => value] to be saved
|
||||
*
|
||||
* @return array Validation errors: [optionname => error message]
|
||||
*
|
||||
* Return array must be empty if no error.
|
||||
*/
|
||||
public function saveOptions(array $data): array;
|
||||
}
|
57
src/Core/Storage/Capability/ICanReadFromStorage.php
Normal file
57
src/Core/Storage/Capability/ICanReadFromStorage.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Capability;
|
||||
|
||||
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
||||
use Friendica\Core\Storage\Exception\StorageException;
|
||||
|
||||
/**
|
||||
* Interface for basic storage backends
|
||||
*/
|
||||
interface ICanReadFromStorage
|
||||
{
|
||||
/**
|
||||
* Get data from backend
|
||||
*
|
||||
* @param string $reference Data reference
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws StorageException in case there's an unexpected error
|
||||
* @throws ReferenceStorageException in case the reference doesn't exist
|
||||
*/
|
||||
public function get(string $reference): string;
|
||||
|
||||
/**
|
||||
* The name of the backend
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string;
|
||||
|
||||
/**
|
||||
* The name of the backend
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string;
|
||||
}
|
56
src/Core/Storage/Capability/ICanWriteToStorage.php
Normal file
56
src/Core/Storage/Capability/ICanWriteToStorage.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Capability;
|
||||
|
||||
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
||||
use Friendica\Core\Storage\Exception\StorageException;
|
||||
|
||||
/**
|
||||
* Interface for writable storage backends
|
||||
*
|
||||
* Used for storages with CRUD functionality, mainly used for user data (e.g. photos, attachements).
|
||||
* There's only one active writable storage possible. This type of storage is selectable by the current administrator.
|
||||
*/
|
||||
interface ICanWriteToStorage extends ICanReadFromStorage
|
||||
{
|
||||
/**
|
||||
* Put data in backend as $ref. If $ref is not defined a new reference is created.
|
||||
*
|
||||
* @param string $data Data to save
|
||||
* @param string $reference Data reference. Optional.
|
||||
*
|
||||
* @return string Saved data reference
|
||||
*
|
||||
* @throws StorageException in case there's an unexpected error
|
||||
*/
|
||||
public function put(string $data, string $reference = ""): string;
|
||||
|
||||
/**
|
||||
* Remove data from backend
|
||||
*
|
||||
* @param string $reference Data reference
|
||||
*
|
||||
* @throws StorageException in case there's an unexpected error
|
||||
* @throws ReferenceStorageException in case the reference doesn't exist
|
||||
*/
|
||||
public function delete(string $reference);
|
||||
}
|
29
src/Core/Storage/Exception/InvalidClassStorageException.php
Normal file
29
src/Core/Storage/Exception/InvalidClassStorageException.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Exception;
|
||||
|
||||
/**
|
||||
* Storage Exception in case of invalid storage class
|
||||
*/
|
||||
class InvalidClassStorageException extends StorageException
|
||||
{
|
||||
}
|
29
src/Core/Storage/Exception/ReferenceStorageException.php
Normal file
29
src/Core/Storage/Exception/ReferenceStorageException.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Exception;
|
||||
|
||||
/**
|
||||
* Storage Exception in case of invalid references
|
||||
*/
|
||||
class ReferenceStorageException extends StorageException
|
||||
{
|
||||
}
|
31
src/Core/Storage/Exception/StorageException.php
Normal file
31
src/Core/Storage/Exception/StorageException.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Storage Exception for unexpected failures
|
||||
*/
|
||||
class StorageException extends Exception
|
||||
{
|
||||
}
|
|
@ -19,12 +19,20 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core;
|
||||
namespace Friendica\Core\Storage\Repository;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Hook;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Core\Storage\Exception\InvalidClassStorageException;
|
||||
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
||||
use Friendica\Core\Storage\Exception\StorageException;
|
||||
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
|
||||
use Friendica\Core\Storage\Capability\ICanConfigureStorage;
|
||||
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Model\Storage;
|
||||
use Friendica\Core\Storage\Type;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
@ -42,40 +50,40 @@ class StorageManager
|
|||
// Default storage backends
|
||||
/** @var string[] */
|
||||
const DEFAULT_BACKENDS = [
|
||||
Storage\Filesystem::NAME,
|
||||
Storage\Database::NAME,
|
||||
Type\Filesystem::NAME,
|
||||
Type\Database::NAME,
|
||||
];
|
||||
|
||||
/** @var string[] List of valid backend classes */
|
||||
private $validBackends;
|
||||
|
||||
/**
|
||||
* @var Storage\IStorage[] A local cache for storage instances
|
||||
* @var ICanReadFromStorage[] A local cache for storage instances
|
||||
*/
|
||||
private $backendInstances = [];
|
||||
|
||||
/** @var Database */
|
||||
private $dba;
|
||||
/** @var IConfig */
|
||||
/** @var IManageConfigValues */
|
||||
private $config;
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
/** @var L10n */
|
||||
private $l10n;
|
||||
|
||||
/** @var Storage\IWritableStorage */
|
||||
/** @var ICanWriteToStorage */
|
||||
private $currentBackend;
|
||||
|
||||
/**
|
||||
* @param Database $dba
|
||||
* @param IConfig $config
|
||||
* @param LoggerInterface $logger
|
||||
* @param L10n $l10n
|
||||
* @param Database $dba
|
||||
* @param IManageConfigValues $config
|
||||
* @param LoggerInterface $logger
|
||||
* @param L10n $l10n
|
||||
*
|
||||
* @throws Storage\InvalidClassStorageException in case the active backend class is invalid
|
||||
* @throws Storage\StorageException in case of unexpected errors during the active backend class loading
|
||||
* @throws InvalidClassStorageException in case the active backend class is invalid
|
||||
* @throws StorageException in case of unexpected errors during the active backend class loading
|
||||
*/
|
||||
public function __construct(Database $dba, IConfig $config, LoggerInterface $logger, L10n $l10n)
|
||||
public function __construct(Database $dba, IManageConfigValues $config, LoggerInterface $logger, L10n $l10n)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
$this->config = $config;
|
||||
|
@ -92,9 +100,9 @@ class StorageManager
|
|||
/**
|
||||
* Return current storage backend class
|
||||
*
|
||||
* @return Storage\IWritableStorage
|
||||
* @return ICanWriteToStorage
|
||||
*/
|
||||
public function getBackend()
|
||||
public function getBackend(): ICanWriteToStorage
|
||||
{
|
||||
return $this->currentBackend;
|
||||
}
|
||||
|
@ -104,16 +112,16 @@ class StorageManager
|
|||
*
|
||||
* @param string $name Backend name
|
||||
*
|
||||
* @return Storage\IWritableStorage
|
||||
* @return ICanWriteToStorage
|
||||
*
|
||||
* @throws Storage\InvalidClassStorageException in case there's no backend class for the name
|
||||
* @throws Storage\StorageException in case of an unexpected failure during the hook call
|
||||
* @throws InvalidClassStorageException in case there's no backend class for the name
|
||||
* @throws StorageException in case of an unexpected failure during the hook call
|
||||
*/
|
||||
public function getWritableStorageByName(string $name): Storage\IWritableStorage
|
||||
public function getWritableStorageByName(string $name): ICanWriteToStorage
|
||||
{
|
||||
$storage = $this->getByName($name, $this->validBackends);
|
||||
if (!$storage instanceof Storage\IWritableStorage) {
|
||||
throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
|
||||
if (!$storage instanceof ICanWriteToStorage) {
|
||||
throw new InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
|
||||
}
|
||||
|
||||
return $storage;
|
||||
|
@ -124,19 +132,19 @@ class StorageManager
|
|||
*
|
||||
* @param string $name Backend name
|
||||
*
|
||||
* @return Storage\IStorageConfiguration|false
|
||||
* @return ICanConfigureStorage|false
|
||||
*
|
||||
* @throws Storage\InvalidClassStorageException in case there's no backend class for the name
|
||||
* @throws Storage\StorageException in case of an unexpected failure during the hook call
|
||||
* @throws InvalidClassStorageException in case there's no backend class for the name
|
||||
* @throws StorageException in case of an unexpected failure during the hook call
|
||||
*/
|
||||
public function getConfigurationByName(string $name)
|
||||
{
|
||||
switch ($name) {
|
||||
// Try the filesystem backend
|
||||
case Storage\Filesystem::getName():
|
||||
return new Storage\FilesystemConfig($this->config, $this->l10n);
|
||||
case Type\Filesystem::getName():
|
||||
return new Type\FilesystemConfig($this->config, $this->l10n);
|
||||
// try the database backend
|
||||
case Storage\Database::getName():
|
||||
case Type\Database::getName():
|
||||
return false;
|
||||
default:
|
||||
$data = [
|
||||
|
@ -145,13 +153,13 @@ class StorageManager
|
|||
];
|
||||
try {
|
||||
Hook::callAll('storage_config', $data);
|
||||
if (!($data['storage_config'] ?? null) instanceof Storage\IStorageConfiguration) {
|
||||
throw new Storage\InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name));
|
||||
if (!($data['storage_config'] ?? null) instanceof ICanConfigureStorage) {
|
||||
throw new InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name));
|
||||
}
|
||||
|
||||
return $data['storage_config'];
|
||||
} catch (InternalServerErrorException $exception) {
|
||||
throw new Storage\StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception);
|
||||
throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -162,36 +170,36 @@ class StorageManager
|
|||
* @param string $name Backend name
|
||||
* @param string[]|null $validBackends possible, manual override of the valid backends
|
||||
*
|
||||
* @return Storage\IStorage
|
||||
* @return ICanReadFromStorage
|
||||
*
|
||||
* @throws Storage\InvalidClassStorageException in case there's no backend class for the name
|
||||
* @throws Storage\StorageException in case of an unexpected failure during the hook call
|
||||
* @throws InvalidClassStorageException in case there's no backend class for the name
|
||||
* @throws StorageException in case of an unexpected failure during the hook call
|
||||
*/
|
||||
public function getByName(string $name, array $validBackends = null): Storage\IStorage
|
||||
public function getByName(string $name, array $validBackends = null): ICanReadFromStorage
|
||||
{
|
||||
// If there's no cached instance create a new instance
|
||||
if (!isset($this->backendInstances[$name])) {
|
||||
// If the current name isn't a valid backend (or the SystemResource instance) create it
|
||||
if (!$this->isValidBackend($name, $validBackends)) {
|
||||
throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
|
||||
throw new InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
|
||||
}
|
||||
|
||||
switch ($name) {
|
||||
// Try the filesystem backend
|
||||
case Storage\Filesystem::getName():
|
||||
$storageConfig = new Storage\FilesystemConfig($this->config, $this->l10n);
|
||||
$this->backendInstances[$name] = new Storage\Filesystem($storageConfig->getStoragePath());
|
||||
case Type\Filesystem::getName():
|
||||
$storageConfig = new Type\FilesystemConfig($this->config, $this->l10n);
|
||||
$this->backendInstances[$name] = new Type\Filesystem($storageConfig->getStoragePath());
|
||||
break;
|
||||
// try the database backend
|
||||
case Storage\Database::getName():
|
||||
$this->backendInstances[$name] = new Storage\Database($this->dba);
|
||||
case Type\Database::getName():
|
||||
$this->backendInstances[$name] = new Type\Database($this->dba);
|
||||
break;
|
||||
// at least, try if there's an addon for the backend
|
||||
case Storage\SystemResource::getName():
|
||||
$this->backendInstances[$name] = new Storage\SystemResource();
|
||||
case Type\SystemResource::getName():
|
||||
$this->backendInstances[$name] = new Type\SystemResource();
|
||||
break;
|
||||
case Storage\ExternalResource::getName():
|
||||
$this->backendInstances[$name] = new Storage\ExternalResource();
|
||||
case Type\ExternalResource::getName():
|
||||
$this->backendInstances[$name] = new Type\ExternalResource();
|
||||
break;
|
||||
default:
|
||||
$data = [
|
||||
|
@ -200,13 +208,13 @@ class StorageManager
|
|||
];
|
||||
try {
|
||||
Hook::callAll('storage_instance', $data);
|
||||
if (!($data['storage'] ?? null) instanceof Storage\IStorage) {
|
||||
throw new Storage\InvalidClassStorageException(sprintf('Backend %s was not found', $name));
|
||||
if (!($data['storage'] ?? null) instanceof ICanReadFromStorage) {
|
||||
throw new InvalidClassStorageException(sprintf('Backend %s was not found', $name));
|
||||
}
|
||||
|
||||
$this->backendInstances[$data['name'] ?? $name] = $data['storage'];
|
||||
} catch (InternalServerErrorException $exception) {
|
||||
throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
|
||||
throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -227,8 +235,8 @@ class StorageManager
|
|||
{
|
||||
$validBackends = $validBackends ?? array_merge($this->validBackends,
|
||||
[
|
||||
Storage\SystemResource::getName(),
|
||||
Storage\ExternalResource::getName(),
|
||||
Type\SystemResource::getName(),
|
||||
Type\ExternalResource::getName(),
|
||||
]);
|
||||
return in_array($name, $validBackends);
|
||||
}
|
||||
|
@ -236,11 +244,11 @@ class StorageManager
|
|||
/**
|
||||
* Set current storage backend class
|
||||
*
|
||||
* @param Storage\IWritableStorage $storage The storage class
|
||||
* @param ICanWriteToStorage $storage The storage class
|
||||
*
|
||||
* @return boolean True, if the set was successful
|
||||
*/
|
||||
public function setBackend(Storage\IWritableStorage $storage): bool
|
||||
public function setBackend(ICanWriteToStorage $storage): bool
|
||||
{
|
||||
if ($this->config->set('storage', 'name', $storage::getName())) {
|
||||
$this->currentBackend = $storage;
|
||||
|
@ -271,9 +279,8 @@ class StorageManager
|
|||
*/
|
||||
public function register(string $class): bool
|
||||
{
|
||||
if (is_subclass_of($class, Storage\IStorage::class)) {
|
||||
/** @var Storage\IStorage $class */
|
||||
|
||||
if (is_subclass_of($class, ICanReadFromStorage::class)) {
|
||||
/** @var ICanReadFromStorage $class */
|
||||
if ($this->isValidBackend($class::getName(), $this->validBackends)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -299,15 +306,14 @@ class StorageManager
|
|||
*
|
||||
* @return boolean True, if unregistering was successful
|
||||
*
|
||||
* @throws Storage\StorageException
|
||||
* @throws StorageException
|
||||
*/
|
||||
public function unregister(string $class): bool
|
||||
{
|
||||
if (is_subclass_of($class, Storage\IStorage::class)) {
|
||||
/** @var Storage\IStorage $class */
|
||||
|
||||
if (is_subclass_of($class, ICanReadFromStorage::class)) {
|
||||
/** @var ICanReadFromStorage $class */
|
||||
if ($this->currentBackend::getName() == $class::getName()) {
|
||||
throw new Storage\StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName()));
|
||||
throw new StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName()));
|
||||
}
|
||||
|
||||
$key = array_search($class::getName(), $this->validBackends);
|
||||
|
@ -336,18 +342,18 @@ class StorageManager
|
|||
* Copy existing data to destination storage and delete from source.
|
||||
* This method cannot move to legacy in-table `data` field.
|
||||
*
|
||||
* @param Storage\IWritableStorage $destination Destination storage class name
|
||||
* @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
|
||||
* @param int $limit Limit of the process batch size, defaults to 5000
|
||||
* @param ICanWriteToStorage $destination Destination storage class name
|
||||
* @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
|
||||
* @param int $limit Limit of the process batch size, defaults to 5000
|
||||
*
|
||||
* @return int Number of moved resources
|
||||
* @throws Storage\StorageException
|
||||
* @throws StorageException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
|
||||
public function move(ICanWriteToStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
|
||||
{
|
||||
if (!$this->isValidBackend($destination, $this->validBackends)) {
|
||||
throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
|
||||
throw new StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
|
||||
}
|
||||
|
||||
$moved = 0;
|
||||
|
@ -369,10 +375,10 @@ class StorageManager
|
|||
$source = $this->getWritableStorageByName($resource['backend-class'] ?? '');
|
||||
$this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
|
||||
$data = $source->get($sourceRef);
|
||||
} catch (Storage\InvalidClassStorageException $exception) {
|
||||
} catch (InvalidClassStorageException $exception) {
|
||||
$this->logger->info('Get data from DB resource field.', ['oldReference' => $sourceRef]);
|
||||
$data = $resource['data'];
|
||||
} catch (Storage\ReferenceStorageException $exception) {
|
||||
} catch (ReferenceStorageException $exception) {
|
||||
$this->logger->info('Invalid source reference.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
|
||||
continue;
|
||||
}
|
||||
|
@ -385,7 +391,7 @@ class StorageManager
|
|||
$this->logger->info('update row');
|
||||
if ($this->dba->update($table, ['backend-class' => $destination::getName(), 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
|
||||
if (!empty($source)) {
|
||||
$this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
|
||||
$this->logger->info('Deleted data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
|
||||
$source->delete($sourceRef);
|
||||
}
|
||||
$moved++;
|
131
src/Core/Storage/Type/Database.php
Normal file
131
src/Core/Storage/Type/Database.php
Normal file
|
@ -0,0 +1,131 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Type;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
||||
use Friendica\Core\Storage\Exception\StorageException;
|
||||
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
|
||||
use Friendica\Database\Database as DBA;
|
||||
|
||||
/**
|
||||
* Database based storage system
|
||||
*
|
||||
* This class manage data stored in database table.
|
||||
*/
|
||||
class Database implements ICanWriteToStorage
|
||||
{
|
||||
const NAME = 'Database';
|
||||
|
||||
/** @var DBA */
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @param DBA $dba
|
||||
*/
|
||||
public function __construct(DBA $dba)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $reference): string
|
||||
{
|
||||
try {
|
||||
$result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
|
||||
if (!$this->dba->isResult($result)) {
|
||||
throw new ReferenceStorageException(sprintf('Database storage cannot find data for reference %s', $reference));
|
||||
}
|
||||
|
||||
return $result['data'];
|
||||
} catch (Exception $exception) {
|
||||
if ($exception instanceof ReferenceStorageException) {
|
||||
throw $exception;
|
||||
} else {
|
||||
throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function put(string $data, string $reference = ''): string
|
||||
{
|
||||
if ($reference !== '') {
|
||||
try {
|
||||
$result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
|
||||
} catch (Exception $exception) {
|
||||
throw new StorageException(sprintf('Database storage failed to update %s', $reference), $exception->getCode(), $exception);
|
||||
}
|
||||
if ($result === false) {
|
||||
throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
|
||||
}
|
||||
|
||||
return $reference;
|
||||
} else {
|
||||
try {
|
||||
$result = $this->dba->insert('storage', ['data' => $data]);
|
||||
} catch (Exception $exception) {
|
||||
throw new StorageException(sprintf('Database storage failed to insert %s', $reference), $exception->getCode(), $exception);
|
||||
}
|
||||
if ($result === false) {
|
||||
throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
|
||||
}
|
||||
|
||||
return $this->dba->lastInsertId();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function delete(string $reference)
|
||||
{
|
||||
try {
|
||||
if (!$this->dba->delete('storage', ['id' => $reference]) || $this->dba->affectedRows() === 0) {
|
||||
throw new ReferenceStorageException(sprintf('Database storage failed to delete %s', $reference));
|
||||
}
|
||||
} catch (Exception $exception) {
|
||||
if ($exception instanceof ReferenceStorageException) {
|
||||
throw $exception;
|
||||
} else {
|
||||
throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return self::getName();
|
||||
}
|
||||
}
|
81
src/Core/Storage/Type/ExternalResource.php
Normal file
81
src/Core/Storage/Type/ExternalResource.php
Normal file
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Type;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
||||
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
|
||||
use Friendica\Util\HTTPSignature;
|
||||
|
||||
/**
|
||||
* External resource storage class
|
||||
*
|
||||
* This class is used to load external resources, like images.
|
||||
* Is not intended to be selectable by admins as default storage class.
|
||||
*/
|
||||
class ExternalResource implements ICanReadFromStorage
|
||||
{
|
||||
const NAME = 'ExternalResource';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $reference): string
|
||||
{
|
||||
$data = json_decode($reference);
|
||||
if (empty($data->url)) {
|
||||
throw new ReferenceStorageException(sprintf('Invalid reference %s, cannot retrieve URL', $reference));
|
||||
}
|
||||
|
||||
$parts = parse_url($data->url);
|
||||
if (empty($parts['scheme']) || empty($parts['host'])) {
|
||||
throw new ReferenceStorageException(sprintf('Invalid reference %s, cannot extract scheme and host', $reference));
|
||||
}
|
||||
|
||||
try {
|
||||
$fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => []]);
|
||||
} catch (Exception $exception) {
|
||||
throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception);
|
||||
}
|
||||
if ($fetchResult->isSuccess()) {
|
||||
return $fetchResult->getBody();
|
||||
} else {
|
||||
throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $fetchResult->getReturnCode(), new Exception($fetchResult->getBody()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
}
|
185
src/Core/Storage/Type/Filesystem.php
Normal file
185
src/Core/Storage/Type/Filesystem.php
Normal file
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Type;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
||||
use Friendica\Core\Storage\Exception\StorageException;
|
||||
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
/**
|
||||
* Filesystem based storage backend
|
||||
*
|
||||
* This class manage data on filesystem.
|
||||
* Base folder for storage is set in storage.filesystem_path.
|
||||
* Best would be for storage folder to be outside webserver folder, we are using a
|
||||
* folder relative to code tree root as default to ease things for users in shared hostings.
|
||||
* Each new resource gets a value as reference and is saved in a
|
||||
* folder tree stucture created from that value.
|
||||
*/
|
||||
class Filesystem implements ICanWriteToStorage
|
||||
{
|
||||
const NAME = 'Filesystem';
|
||||
|
||||
/** @var string */
|
||||
private $basePath;
|
||||
|
||||
/**
|
||||
* Filesystem constructor.
|
||||
*
|
||||
* @param string $filesystemPath
|
||||
*
|
||||
* @throws StorageException in case the path doesn't exist or isn't writeable
|
||||
*/
|
||||
public function __construct(string $filesystemPath = FilesystemConfig::DEFAULT_BASE_FOLDER)
|
||||
{
|
||||
$path = $filesystemPath;
|
||||
$this->basePath = rtrim($path, '/');
|
||||
|
||||
if (!is_dir($this->basePath) || !is_writable($this->basePath)) {
|
||||
throw new StorageException(sprintf('Path "%s" does not exist or is not writeable.', $this->basePath));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Split data ref and return file path
|
||||
*
|
||||
* @param string $reference Data reference
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function pathForRef(string $reference): string
|
||||
{
|
||||
$fold1 = substr($reference, 0, 2);
|
||||
$fold2 = substr($reference, 2, 2);
|
||||
$file = substr($reference, 4);
|
||||
|
||||
return implode('/', [$this->basePath, $fold1, $fold2, $file]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create directory tree to store file, with .htaccess and index.html files
|
||||
*
|
||||
* @param string $file Path and filename
|
||||
*
|
||||
* @throws StorageException
|
||||
*/
|
||||
private function createFoldersForFile(string $file)
|
||||
{
|
||||
$path = dirname($file);
|
||||
|
||||
if (!is_dir($path)) {
|
||||
if (!mkdir($path, 0770, true)) {
|
||||
throw new StorageException(sprintf('Filesystem storage failed to create "%s". Check you write permissions.', $path));
|
||||
}
|
||||
}
|
||||
|
||||
while ($path !== $this->basePath) {
|
||||
if (!is_file($path . '/index.html')) {
|
||||
file_put_contents($path . '/index.html', '');
|
||||
}
|
||||
chmod($path . '/index.html', 0660);
|
||||
chmod($path, 0770);
|
||||
$path = dirname($path);
|
||||
}
|
||||
if (!is_file($path . '/index.html')) {
|
||||
file_put_contents($path . '/index.html', '');
|
||||
chmod($path . '/index.html', 0660);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $reference): string
|
||||
{
|
||||
$file = $this->pathForRef($reference);
|
||||
if (!is_file($file)) {
|
||||
throw new ReferenceStorageException(sprintf('Filesystem storage failed to get the file %s, The file is invalid', $reference));
|
||||
}
|
||||
|
||||
$result = file_get_contents($file);
|
||||
|
||||
if ($result === false) {
|
||||
throw new StorageException(sprintf('Filesystem storage failed to get data to "%s". Check your write permissions', $file));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function put(string $data, string $reference = ''): string
|
||||
{
|
||||
if ($reference === '') {
|
||||
try {
|
||||
$reference = Strings::getRandomHex();
|
||||
} catch (Exception $exception) {
|
||||
throw new StorageException('Filesystem storage failed to generate a random hex', $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
$file = $this->pathForRef($reference);
|
||||
|
||||
$this->createFoldersForFile($file);
|
||||
|
||||
$result = file_put_contents($file, $data);
|
||||
|
||||
// just in case the result is REALLY false, not zero or empty or anything else, throw the exception
|
||||
if ($result === false) {
|
||||
throw new StorageException(sprintf('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
|
||||
}
|
||||
|
||||
chmod($file, 0660);
|
||||
return $reference;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function delete(string $reference)
|
||||
{
|
||||
$file = $this->pathForRef($reference);
|
||||
if (!is_file($file)) {
|
||||
throw new ReferenceStorageException(sprintf('File with reference "%s" doesn\'t exist', $reference));
|
||||
}
|
||||
|
||||
if (!unlink($file)) {
|
||||
throw new StorageException(sprintf('Cannot delete with file with reference "%s"', $reference));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return self::getName();
|
||||
}
|
||||
}
|
100
src/Core/Storage/Type/FilesystemConfig.php
Normal file
100
src/Core/Storage/Type/FilesystemConfig.php
Normal file
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Type;
|
||||
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Core\Storage\Capability\ICanConfigureStorage;
|
||||
|
||||
/**
|
||||
* Filesystem based storage backend configuration
|
||||
*/
|
||||
class FilesystemConfig implements ICanConfigureStorage
|
||||
{
|
||||
// Default base folder
|
||||
const DEFAULT_BASE_FOLDER = 'storage';
|
||||
|
||||
/** @var IManageConfigValues */
|
||||
private $config;
|
||||
|
||||
/** @var string */
|
||||
private $storagePath;
|
||||
|
||||
/** @var L10n */
|
||||
private $l10n;
|
||||
|
||||
/**
|
||||
* Returns the current storage path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getStoragePath(): string
|
||||
{
|
||||
return $this->storagePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filesystem constructor.
|
||||
*
|
||||
* @param IManageConfigValues $config
|
||||
* @param L10n $l10n
|
||||
*/
|
||||
public function __construct(IManageConfigValues $config, L10n $l10n)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->l10n = $l10n;
|
||||
|
||||
$path = $this->config->get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
|
||||
$this->storagePath = rtrim($path, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'storagepath' => [
|
||||
'input',
|
||||
$this->l10n->t('Storage base path'),
|
||||
$this->storagePath,
|
||||
$this->l10n->t('Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function saveOptions(array $data): array
|
||||
{
|
||||
$storagePath = $data['storagepath'] ?? '';
|
||||
if ($storagePath === '' || !is_dir($storagePath)) {
|
||||
return [
|
||||
'storagepath' => $this->l10n->t('Enter a valid existing folder')
|
||||
];
|
||||
};
|
||||
$this->config->set('storage', 'filesystem_path', $storagePath);
|
||||
$this->storagePath = $storagePath;
|
||||
return [];
|
||||
}
|
||||
}
|
77
src/Core/Storage/Type/SystemResource.php
Normal file
77
src/Core/Storage/Type/SystemResource.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Storage\Type;
|
||||
|
||||
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
||||
use Friendica\Core\Storage\Exception\StorageException;
|
||||
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
|
||||
|
||||
/**
|
||||
* System resource storage class
|
||||
*
|
||||
* This class is used to load system resources, like images.
|
||||
* Is not intended to be selectable by admins as default storage class.
|
||||
*/
|
||||
class SystemResource implements ICanReadFromStorage
|
||||
{
|
||||
const NAME = 'SystemResource';
|
||||
|
||||
// Valid folders to look for resources
|
||||
const VALID_FOLDERS = ["images"];
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $reference): string
|
||||
{
|
||||
$folder = dirname($reference);
|
||||
if (!in_array($folder, self::VALID_FOLDERS)) {
|
||||
throw new ReferenceStorageException(sprintf('System Resource is invalid for reference %s, no valid folder found', $reference));
|
||||
}
|
||||
if (!file_exists($reference)) {
|
||||
throw new StorageException(sprintf('System Resource is invalid for reference %s, the file doesn\'t exist', $reference));
|
||||
}
|
||||
$content = file_get_contents($reference);
|
||||
|
||||
if ($content === false) {
|
||||
throw new StorageException(sprintf('Cannot get content for reference %s', $reference));
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
}
|
|
@ -132,7 +132,7 @@ class Update
|
|||
|
||||
// Compare the current structure with the defined structure
|
||||
// If the Lock is acquired, never release it automatically to avoid double updates
|
||||
if (DI::lock()->acquire('dbupdate', 0, Cache\Duration::INFINITE)) {
|
||||
if (DI::lock()->acquire('dbupdate', 0, Cache\Enum\Duration::INFINITE)) {
|
||||
|
||||
Logger::notice('Update starting.', ['from' => $stored, 'to' => $current]);
|
||||
|
||||
|
@ -246,7 +246,7 @@ class Update
|
|||
// If the update fails or times-out completely you may need to
|
||||
// delete the config entry to try again.
|
||||
|
||||
if (DI::lock()->acquire('dbupdate_function', 120, Cache\Duration::INFINITE)) {
|
||||
if (DI::lock()->acquire('dbupdate_function', 120, Cache\Enum\Duration::INFINITE)) {
|
||||
|
||||
// call the specific update
|
||||
Logger::notice('Pre update function start.', ['function' => $funcname]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue