Merge remote-tracking branch 'upstream/2021.12-rc' into api-direct-messages

This commit is contained in:
Michael 2021-12-17 14:52:44 +00:00
commit ddb2d6f5b3
16 changed files with 148 additions and 57 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2021.12-rc (Siberian Iris) -- Friendica 2021.12-rc (Siberian Iris)
-- DB_UPDATE_VERSION 1445 -- DB_UPDATE_VERSION 1446
-- ------------------------------------------ -- ------------------------------------------

View file

@ -81,7 +81,7 @@ class Cache
} }
/** /**
* This method creates a CacheDriver for the given cache driver name * This method creates a CacheDriver for distributed caching with the given cache driver name
* *
* @param string|null $type The cache type to create (default is per config) * @param string|null $type The cache type to create (default is per config)
* *
@ -90,12 +90,42 @@ class Cache
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly * @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 * @throws CachePersistenceException In case the underlying cache has errors during persistence
*/ */
public function create(string $type = null): ICanCache public function createDistributed(string $type = null): ICanCache
{ {
if (empty($type)) { if ($type === Enum\Type::APCU) {
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE); throw new InvalidCacheDriverException('apcu doesn\'t support distributed caching.');
} }
return $this->create($type ?? $this->config->get('system', 'distributed_cache_driver', self::DEFAULT_TYPE));
}
/**
* This method creates a CacheDriver for local caching with 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 createLocal(string $type = null): ICanCache
{
return $this->create($type ?? $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE));
}
/**
* Creates a new Cache instance
*
* @param string $type The type of cache
*
* @return ICanCache
*
* @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
*/
protected function create(string $type): ICanCache
{
switch ($type) { switch ($type) {
case Enum\Type::MEMCACHE: case Enum\Type::MEMCACHE:
$cache = new Type\MemcacheCache($this->hostname, $this->config); $cache = new Type\MemcacheCache($this->hostname, $this->config);

View file

@ -96,8 +96,8 @@ class ConfigFileLoader
public function setupCache(Cache $config, array $server = [], bool $raw = false) public function setupCache(Cache $config, array $server = [], bool $raw = false)
{ {
// Load static config files first, the order is important // Load static config files first, the order is important
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE); $config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_STATIC);
$config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE); $config->load($this->loadStaticConfig('settings'), Cache::SOURCE_STATIC);
// try to load the legacy config first // try to load the legacy config first
$config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE); $config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);

View file

@ -31,14 +31,16 @@ use ParagonIE\HiddenString\HiddenString;
*/ */
class Cache class Cache
{ {
/** @var int Indicates that the cache entry is a default value - Lowest Priority */
const SOURCE_STATIC = 0;
/** @var int Indicates that the cache entry is set by file - Low Priority */ /** @var int Indicates that the cache entry is set by file - Low Priority */
const SOURCE_FILE = 0; const SOURCE_FILE = 1;
/** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */ /** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
const SOURCE_DB = 1; const SOURCE_DB = 2;
/** @var int Indicates that the cache entry is set by a server environment variable - High Priority */ /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
const SOURCE_ENV = 3; const SOURCE_ENV = 3;
/** @var int Indicates that the cache entry is fixed and must not be changed */ /** @var int Indicates that the cache entry is fixed and must not be changed */
const SOURCE_FIX = 4; const SOURCE_FIX = 5;
/** @var int Default value for a config source */ /** @var int Default value for a config source */
const SOURCE_DEFAULT = self::SOURCE_FILE; const SOURCE_DEFAULT = self::SOURCE_FILE;
@ -113,6 +115,19 @@ class Cache
} }
} }
/**
* Returns the source value of the current, cached config value
*
* @param string $cat Config category
* @param string $key Config key
*
* @return int
*/
public function getSource(string $cat, string $key): int
{
return $this->source[$cat][$key] ?? -1;
}
/** /**
* Sets a value in the config cache. Accepts raw output from the config table * Sets a value in the config cache. Accepts raw output from the config table
* *

View file

@ -82,22 +82,16 @@ class Lock
case Enum\Type::MEMCACHED: case Enum\Type::MEMCACHED:
case Enum\Type::REDIS: case Enum\Type::REDIS:
case Enum\Type::APCU: case Enum\Type::APCU:
$cache = $this->cacheFactory->create($lock_type); $cache = $this->cacheFactory->createLocal($lock_type);
if ($cache instanceof ICanCacheInMemory) { if ($cache instanceof ICanCacheInMemory) {
return new Type\CacheLock($cache); return new Type\CacheLock($cache);
} else { } else {
throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type)); throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type));
} }
break;
case 'database': case 'database':
return new Type\DatabaseLock($this->dba); return new Type\DatabaseLock($this->dba);
break;
case 'semaphore': case 'semaphore':
return new Type\SemaphoreLock(); return new Type\SemaphoreLock();
break;
default: default:
return self::useAutoDriver(); return self::useAutoDriver();
} }
@ -132,7 +126,7 @@ class Lock
$cache_type = $this->config->get('system', 'cache_driver', 'database'); $cache_type = $this->config->get('system', 'cache_driver', 'database');
if ($cache_type != Enum\Type::DATABASE) { if ($cache_type != Enum\Type::DATABASE) {
try { try {
$cache = $this->cacheFactory->create($cache_type); $cache = $this->cacheFactory->createLocal($cache_type);
if ($cache instanceof ICanCacheInMemory) { if ($cache instanceof ICanCacheInMemory) {
return new Type\CacheLock($cache); return new Type\CacheLock($cache);
} }

View file

@ -119,7 +119,7 @@ class Logger
case 'syslog': case 'syslog':
try { try {
$logger = new SyslogLogger($this->channel, $introspection, $loglevel); $logger = new SyslogLogger($this->channel, $introspection, $loglevel, $config->get('system', 'syslog_flags', SyslogLogger::DEFAULT_FLAGS), $config->get('system', 'syslog_facility', SyslogLogger::DEFAULT_FACILITY));
} catch (LogLevelException $exception) { } catch (LogLevelException $exception) {
// If there's a wrong config value for loglevel, try again with standard // If there's a wrong config value for loglevel, try again with standard
$logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE); $logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);

View file

@ -34,6 +34,11 @@ class SyslogLogger extends AbstractLogger
{ {
const IDENT = 'Friendica'; const IDENT = 'Friendica';
/** @var int The default syslog flags */
const DEFAULT_FLAGS = LOG_PID | LOG_ODELAY | LOG_CONS;
/** @var int The default syslog facility */
const DEFAULT_FACILITY = LOG_USER;
/** /**
* Translates LogLevel log levels to syslog log priorities. * Translates LogLevel log levels to syslog log priorities.
* @var array * @var array
@ -101,7 +106,7 @@ class SyslogLogger extends AbstractLogger
* @throws LogLevelException * @throws LogLevelException
* @throws LoggerException * @throws LoggerException
*/ */
public function __construct($channel, Introspection $introspection, string $level = LogLevel::NOTICE, int $logOpts = LOG_PID, int $logFacility = LOG_USER) public function __construct($channel, Introspection $introspection, string $level = LogLevel::NOTICE, int $logOpts = self::DEFAULT_FLAGS, int $logFacility = self::DEFAULT_FACILITY )
{ {
parent::__construct($channel, $introspection); parent::__construct($channel, $introspection);
$this->logOpts = $logOpts; $this->logOpts = $logOpts;

View file

@ -22,8 +22,8 @@
namespace Friendica\Core\Session\Factory; namespace Friendica\Core\Session\Factory;
use Friendica\App; use Friendica\App;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Enum; use Friendica\Core\Cache\Enum;
use Friendica\Core\Cache\Factory\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Type; use Friendica\Core\Session\Type;
@ -51,14 +51,14 @@ class Session
* @param App\BaseURL $baseURL * @param App\BaseURL $baseURL
* @param IManageConfigValues $config * @param IManageConfigValues $config
* @param Database $dba * @param Database $dba
* @param ICanCache $cache * @param Cache $cacheFactory
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param Profiler $profiler * @param Profiler $profiler
* @param array $server * @param array $server
* *
* @return IHandleSessions * @return IHandleSessions
*/ */
public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, ICanCache $cache, LoggerInterface $logger, Profiler $profiler, array $server = []) public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, Cache $cacheFactory, LoggerInterface $logger, Profiler $profiler, array $server = [])
{ {
$profiler->startRecording('session'); $profiler->startRecording('session');
$session = null; $session = null;
@ -75,6 +75,8 @@ class Session
$handler = new Handler\Database($dba, $logger, $server); $handler = new Handler\Database($dba, $logger, $server);
break; break;
case self::HANDLER_CACHE: case self::HANDLER_CACHE:
$cache = $cacheFactory->createDistributed();
// In case we're using the db as cache driver, use the native db session, not the 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) { if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) {
$handler = new Handler\Database($dba, $logger, $server); $handler = new Handler\Database($dba, $logger, $server);

View file

@ -26,12 +26,17 @@ use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Util\Network; use Friendica\Util\Network;
use Psr\Http\Message\ResponseInterface;
/** /**
* Apps class to register new OAuth clients * Apps class to register new OAuth clients
*/ */
class Apps extends BaseApi class Apps extends BaseApi
{ {
public function run(array $request = [], bool $scopecheck = true): ResponseInterface
{
return parent::run($request, false);
}
/** /**
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */

View file

@ -79,19 +79,21 @@ class BaseApi extends BaseModule
* *
* @throws HTTPException\ForbiddenException * @throws HTTPException\ForbiddenException
*/ */
public function run(array $request = []): ResponseInterface public function run(array $request = [], bool $scopecheck = true): ResponseInterface
{ {
switch ($this->server['REQUEST_METHOD'] ?? Router::GET) { if ($scopecheck) {
case Router::DELETE: switch ($this->server['REQUEST_METHOD'] ?? Router::GET) {
case Router::PATCH: case Router::DELETE:
case Router::POST: case Router::PATCH:
case Router::PUT: case Router::POST:
self::checkAllowedScope(self::SCOPE_WRITE); case Router::PUT:
self::checkAllowedScope(self::SCOPE_WRITE);
if (!self::getCurrentUserID()) {
throw new HTTPException\ForbiddenException($this->t('Permission denied.')); if (!self::getCurrentUserID()) {
} throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
break; }
break;
}
} }
return parent::run($request); return parent::run($request);

View file

@ -26,6 +26,7 @@ use Friendica\Content\Text\HTML;
use Friendica\Content\Text\Markdown; use Friendica\Content\Text\Markdown;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\APContact; use Friendica\Model\APContact;
@ -287,8 +288,12 @@ class Processor
$item['uri'] = $activity['id']; $item['uri'] = $activity['id'];
$item['created'] = DateTimeFormat::utc($activity['published']); if (empty($activity['published']) || empty($activity['updated'])) {
$item['edited'] = DateTimeFormat::utc($activity['updated']); DI::logger()->notice('published or updated keys are empty for activity', ['activity' => $activity, 'callstack' => System::callstack(10)]);
}
$item['created'] = DateTimeFormat::utc($activity['published'] ?? 'now');
$item['edited'] = DateTimeFormat::utc($activity['updated'] ?? 'now');
$guid = $activity['sc:identifier'] ?: self::getGUIDByURL($item['uri']); $guid = $activity['sc:identifier'] ?: self::getGUIDByURL($item['uri']);
$item['guid'] = $activity['diaspora:guid'] ?: $guid; $item['guid'] = $activity['diaspora:guid'] ?: $guid;

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1445); define('DB_UPDATE_VERSION', 1446);
} }
return [ return [

View file

@ -141,6 +141,10 @@ return [
// Whether to use Memcache, Memcached, Redis or APCu to store temporary cache. // Whether to use Memcache, Memcached, Redis or APCu to store temporary cache.
'cache_driver' => 'database', 'cache_driver' => 'database',
// distributed_cache_driver (database|memcache|memcached|redis)
// Whether to use database, Memcache, Memcached or Redis as a distributed cache.
'distributed_cache_driver' => 'database',
// config_adapter (jit|preload) // config_adapter (jit|preload)
// Allow to switch the configuration adapter to improve performances at the cost of memory consumption. // Allow to switch the configuration adapter to improve performances at the cost of memory consumption.
'config_adapter' => 'jit', 'config_adapter' => 'jit',
@ -325,6 +329,14 @@ return [
// Sets the logging adapter of Friendica globally (monolog, syslog, stream) // Sets the logging adapter of Friendica globally (monolog, syslog, stream)
'logger_config' => 'stream', 'logger_config' => 'stream',
// syslog flags (Integer)
// Sets the syslog flags in case 'logger_config' is set to 'syslog'
'syslog_flags' => LOG_CONS | LOG_PID | LOG_ODELAY,
// syslog flags (Integer)
// Sets the syslog facility in case 'logger_config' is set to 'syslog'
'syslog_facility' => LOG_USER,
// maintenance_start (String) // maintenance_start (String)
// Start of the window for the daily maintenance cron call. // Start of the window for the daily maintenance cron call.
// The system timezone is used when no timezone is defined here. // The system timezone is used when no timezone is defined here.

View file

@ -160,13 +160,13 @@ return [
Cache\Capability\ICanCache::class => [ Cache\Capability\ICanCache::class => [
'instanceOf' => Cache\Factory\Cache::class, 'instanceOf' => Cache\Factory\Cache::class,
'call' => [ 'call' => [
['create', [], Dice::CHAIN_CALL], ['createLocal', [], Dice::CHAIN_CALL],
], ],
], ],
Cache\Capability\ICanCacheInMemory::class => [ Cache\Capability\ICanCacheInMemory::class => [
'instanceOf' => Cache\Factory\Cache::class, 'instanceOf' => Cache\Factory\Cache::class,
'call' => [ 'call' => [
['create', [], Dice::CHAIN_CALL], ['createLocal', [], Dice::CHAIN_CALL],
], ],
], ],
Lock\Capability\ICanLock::class => [ Lock\Capability\ICanLock::class => [

View file

@ -22,38 +22,42 @@
*/ */
return [ return [
'MYSQL_HOST' => ['database', 'hostname'], 'MYSQL_HOST' => ['database', 'hostname'],
'MYSQL_USERNAME' => ['database', 'username'], 'MYSQL_USERNAME' => ['database', 'username'],
'MYSQL_USER' => ['database', 'username'], 'MYSQL_USER' => ['database', 'username'],
'MYSQL_PORT' => ['database', 'port'], 'MYSQL_PORT' => ['database', 'port'],
'MYSQL_PASSWORD' => ['database', 'password'], 'MYSQL_PASSWORD' => ['database', 'password'],
'MYSQL_DATABASE' => ['database', 'database'], 'MYSQL_DATABASE' => ['database', 'database'],
// Core variables // Core variables
'FRIENDICA_ADMIN_MAIL' => ['config', 'admin_email'], 'FRIENDICA_ADMIN_MAIL' => ['config', 'admin_email'],
'FRIENDICA_URL' => ['system', 'url'], 'FRIENDICA_URL' => ['system', 'url'],
'FRIENDICA_TZ' => ['config', 'timezone'], 'FRIENDICA_TZ' => ['config', 'timezone'],
'FRIENDICA_LANG' => ['config', 'language'], 'FRIENDICA_LANG' => ['config', 'language'],
'FRIENDICA_SITENAME' => ['config', 'sitename'], 'FRIENDICA_SITENAME' => ['config', 'sitename'],
// Storage // Storage
'FRIENDICA_DATA' => ['storage', 'name'], 'FRIENDICA_DATA' => ['storage', 'name'],
'FRIENDICA_DATA_DIR' => ['storage', 'filesystem_path'], 'FRIENDICA_DATA_DIR' => ['storage', 'filesystem_path'],
// Debugging/Profiling // Debugging/Profiling
'FRIENDICA_DEBUGGING' => ['system', 'debugging'], 'FRIENDICA_DEBUGGING' => ['system', 'debugging'],
'FRIENDICA_LOGFILE' => ['system', 'logfile'], 'FRIENDICA_LOGFILE' => ['system', 'logfile'],
'FRIENDICA_LOGLEVEL'=> ['system', 'loglevel'], 'FRIENDICA_LOGLEVEL' => ['system', 'loglevel'],
'FRIENDICA_PROFILING' => ['system', 'profiler'], 'FRIENDICA_PROFILING' => ['system', 'profiler'],
'FRIENDICA_LOGGER' => ['system', 'logger_config'],
'FRIENDICA_SYSLOG_FLAGS' => ['system', 'syslog_flags'],
'FRIENDICA_SYSLOG_FACILITY' => ['system', 'syslog_facility'],
// Caching // Caching
'FRIENDICA_CACHE_DRIVER' => ['system', 'cache_driver'], 'FRIENDICA_CACHE_DRIVER' => ['system', 'cache_driver'],
'FRIENDICA_SESSION_HANDLER' => ['system', 'session_handler'], 'FRIENDICA_SESSION_HANDLER' => ['system', 'session_handler'],
'FRIENDICA_LOCK_DRIVER' => ['system', 'lock_driver'], 'FRIENDICA_DISTRIBUTED_CACHE_DRIVER' => ['system', 'distributed_cache_driver'],
'FRIENDICA_LOCK_DRIVER' => ['system', 'lock_driver'],
// Redis Config // Redis Config
'REDIS_HOST' => ['system', 'redis_host'], 'REDIS_HOST' => ['system', 'redis_host'],
'REDIS_PORT' => ['system', 'redis_port'], 'REDIS_PORT' => ['system', 'redis_port'],
'REDIS_PW' => ['system', 'redis_password'], 'REDIS_PW' => ['system', 'redis_password'],
'REDIS_DB' => ['system', 'redis_db'], 'REDIS_DB' => ['system', 'redis_db'],
]; ];

View file

@ -1070,3 +1070,20 @@ function update_1444()
return Update::SUCCESS; return Update::SUCCESS;
} }
function update_1446()
{
$distributed_cache_driver_source = DI::config()->getCache()->getSource('system', 'distributed_cache_driver');
$cache_driver_source = DI::config()->getCache()->getSource('system', 'cache_driver');
// In case the distributed cache driver is the default value, but the current cache driver isn't default,
// we assume that the distributed cache driver should be the same as the current cache driver
if (
$distributed_cache_driver_source === \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC
&& $cache_driver_source > \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC
) {
DI::config()->set('system', 'distributed_cache_driver', DI::config()->get('system', 'cache_driver'));
}
return Update::SUCCESS;
}