3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Storage\Repository;
25 use Friendica\Core\Addon;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Storage\Exception\InvalidClassStorageException;
30 use Friendica\Core\Storage\Exception\ReferenceStorageException;
31 use Friendica\Core\Storage\Exception\StorageException;
32 use Friendica\Core\Storage\Capability\ICanReadFromStorage;
33 use Friendica\Core\Storage\Capability\ICanConfigureStorage;
34 use Friendica\Core\Storage\Capability\ICanWriteToStorage;
35 use Friendica\Database\Database;
36 use Friendica\Core\Storage\Type;
37 use Friendica\Network\HTTPException\InternalServerErrorException;
38 use Psr\Log\LoggerInterface;
41 * Manage storage backends
43 * Core code uses this class to get and set current storage backend class.
44 * Addons use this class to register and unregister additional backends.
48 // Default tables to look for data
49 const TABLES = ['photo', 'attach'];
51 // Default storage backends
53 const DEFAULT_BACKENDS = [
54 Type\Filesystem::NAME,
58 /** @var string[] List of valid backend classes */
59 private $validBackends;
62 * @var ICanReadFromStorage[] A local cache for storage instances
64 private $backendInstances = [];
68 /** @var IManageConfigValues */
70 /** @var LoggerInterface */
75 /** @var ICanWriteToStorage */
76 private $currentBackend;
79 * @param Database $dba
80 * @param IManageConfigValues $config
81 * @param LoggerInterface $logger
83 * @param bool $includeAddon (DEVELOP ONLY) Used for testing only - avoids loading addons because of DB direct access
85 * @throws InvalidClassStorageException in case the active backend class is invalid
86 * @throws StorageException in case of unexpected errors during the active backend class loading
88 public function __construct(Database $dba, IManageConfigValues $config, LoggerInterface $logger, L10n $l10n, bool $includeAddon = true)
91 $this->config = $config;
92 $this->logger = $logger;
94 $this->validBackends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
96 $currentName = $this->config->get('storage', 'name');
98 /// @fixme Loading the addons & hooks here is really bad practice, but solves https://github.com/friendica/friendica/issues/11178
99 /// clean solution = Making Addon & Hook dynamic and load them inside the constructor, so there's no custom load logic necessary anymore
105 // you can only use user backends as a "default" backend, so the second parameter is true
106 $this->currentBackend = $this->getWritableStorageByName($currentName);
110 * Return current storage backend class
112 * @return ICanWriteToStorage
114 public function getBackend(): ICanWriteToStorage
116 return $this->currentBackend;
120 * Returns a writable storage backend class by registered name
122 * @param string $name Backend name
124 * @return ICanWriteToStorage
126 * @throws InvalidClassStorageException in case there's no backend class for the name
127 * @throws StorageException in case of an unexpected failure during the hook call
129 public function getWritableStorageByName(string $name): ICanWriteToStorage
131 $storage = $this->getByName($name, $this->validBackends);
132 if (!$storage instanceof ICanWriteToStorage) {
133 throw new InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
140 * Return storage backend configuration by registered name
142 * @param string $name Backend name
144 * @return ICanConfigureStorage|false
146 * @throws InvalidClassStorageException in case there's no backend class for the name
147 * @throws StorageException in case of an unexpected failure during the hook call
149 public function getConfigurationByName(string $name)
152 // Try the filesystem backend
153 case Type\Filesystem::getName():
154 return new Type\FilesystemConfig($this->config, $this->l10n);
155 // try the database backend
156 case Type\Database::getName():
161 'storage_config' => null,
164 Hook::callAll('storage_config', $data);
165 if (!($data['storage_config'] ?? null) instanceof ICanConfigureStorage) {
166 throw new InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name));
169 return $data['storage_config'];
170 } catch (InternalServerErrorException $exception) {
171 throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception->__toString());
177 * Return storage backend class by registered name
179 * @param string $name Backend name
180 * @param string[]|null $validBackends possible, manual override of the valid backends
182 * @return ICanReadFromStorage
184 * @throws InvalidClassStorageException in case there's no backend class for the name
185 * @throws StorageException in case of an unexpected failure during the hook call
187 public function getByName(string $name, array $validBackends = null): ICanReadFromStorage
189 // If there's no cached instance create a new instance
190 if (!isset($this->backendInstances[$name])) {
191 // If the current name isn't a valid backend (or the SystemResource instance) create it
192 if (!$this->isValidBackend($name, $validBackends)) {
193 throw new InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
197 // Try the filesystem backend
198 case Type\Filesystem::getName():
199 $storageConfig = new Type\FilesystemConfig($this->config, $this->l10n);
200 $this->backendInstances[$name] = new Type\Filesystem($storageConfig->getStoragePath());
202 // try the database backend
203 case Type\Database::getName():
204 $this->backendInstances[$name] = new Type\Database($this->dba);
206 // at least, try if there's an addon for the backend
207 case Type\SystemResource::getName():
208 $this->backendInstances[$name] = new Type\SystemResource();
210 case Type\ExternalResource::getName():
211 $this->backendInstances[$name] = new Type\ExternalResource($this->logger);
219 Hook::callAll('storage_instance', $data);
220 if (!($data['storage'] ?? null) instanceof ICanReadFromStorage) {
221 throw new InvalidClassStorageException(sprintf('Backend %s was not found', $name));
224 $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
225 } catch (InternalServerErrorException $exception) {
226 throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception->__toString());
232 return $this->backendInstances[$name];
236 * Checks, if the storage is a valid backend
238 * @param string|null $name The name or class of the backend
239 * @param string[]|null $validBackends Possible, valid backends to check
241 * @return boolean True, if the backend is a valid backend
243 public function isValidBackend(string $name = null, array $validBackends = null): bool
245 $validBackends = $validBackends ?? array_merge($this->validBackends,
247 Type\SystemResource::getName(),
248 Type\ExternalResource::getName(),
250 return in_array($name, $validBackends);
254 * Set current storage backend class
256 * @param ICanWriteToStorage $storage The storage class
258 * @return boolean True, if the set was successful
260 public function setBackend(ICanWriteToStorage $storage): bool
262 if ($this->config->set('storage', 'name', $storage::getName())) {
263 $this->currentBackend = $storage;
271 * Get registered backends
275 public function listBackends(): array
277 return $this->validBackends;
281 * Register a storage backend class
283 * You have to register the hook "storage_instance" as well to make this class work!
285 * @param string $class Backend class name
287 * @return boolean True, if the registration was successful
289 public function register(string $class): bool
291 if (is_subclass_of($class, ICanReadFromStorage::class)) {
292 /** @var ICanReadFromStorage $class */
293 if ($this->isValidBackend($class::getName(), $this->validBackends)) {
297 $backends = $this->validBackends;
298 $backends[] = $class::getName();
300 if ($this->config->set('storage', 'backends', $backends)) {
301 $this->validBackends = $backends;
312 * Unregister a storage backend class
314 * @param string $class Backend class name
316 * @return boolean True, if unregistering was successful
318 * @throws StorageException
320 public function unregister(string $class): bool
322 if (is_subclass_of($class, ICanReadFromStorage::class)) {
323 /** @var ICanReadFromStorage $class */
324 if ($this->currentBackend::getName() == $class::getName()) {
325 throw new StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName()));
328 $key = array_search($class::getName(), $this->validBackends);
330 if ($key !== false) {
331 $backends = $this->validBackends;
332 unset($backends[$key]);
333 $backends = array_values($backends);
334 if ($this->config->set('storage', 'backends', $backends)) {
335 $this->validBackends = $backends;
349 * Move up to 5000 resources to storage $dest
351 * Copy existing data to destination storage and delete from source.
352 * This method cannot move to legacy in-table `data` field.
354 * @param ICanWriteToStorage $destination Destination storage class name
355 * @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
356 * @param int $limit Limit of the process batch size, defaults to 5000
358 * @return int Number of moved resources
359 * @throws StorageException
362 public function move(ICanWriteToStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
364 if (!$this->isValidBackend($destination, $this->validBackends)) {
365 throw new StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
369 foreach ($tables as $table) {
370 // Get the rows where backend class is not the destination backend class
371 $resources = $this->dba->select(
373 ['id', 'data', 'backend-class', 'backend-ref'],
374 ['`backend-class` IS NULL or `backend-class` != ?', $destination::getName()],
378 while ($resource = $this->dba->fetch($resources)) {
379 $id = $resource['id'];
380 $sourceRef = $resource['backend-ref'];
384 $source = $this->getWritableStorageByName($resource['backend-class'] ?? '');
385 $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
386 $data = $source->get($sourceRef);
387 } catch (InvalidClassStorageException $exception) {
388 $this->logger->info('Get data from DB resource field.', ['oldReference' => $sourceRef]);
389 $data = $resource['data'];
390 } catch (ReferenceStorageException $exception) {
391 $this->logger->info('Invalid source reference.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
395 $this->logger->info('Save data to new backend.', ['newBackend' => $destination::getName()]);
396 $destinationRef = $destination->put($data);
397 $this->logger->info('Saved data.', ['newReference' => $destinationRef]);
399 if ($destinationRef !== '') {
400 $this->logger->info('update row');
401 if ($this->dba->update($table, ['backend-class' => $destination::getName(), 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
402 if (!empty($source)) {
403 $this->logger->info('Deleted data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
404 $source->delete($sourceRef);
411 $this->dba->close($resources);