3 * @copyright Copyright (C) 2010-2021, 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;
25 use Friendica\Core\Config\IConfig;
26 use Friendica\Database\Database;
27 use Friendica\Model\Storage;
28 use Friendica\Network\HTTPException\InternalServerErrorException;
29 use Psr\Log\LoggerInterface;
32 * Manage storage backends
34 * Core code uses this class to get and set current storage backend class.
35 * Addons use this class to register and unregister additional backends.
39 // Default tables to look for data
40 const TABLES = ['photo', 'attach'];
42 // Default storage backends
44 const DEFAULT_BACKENDS = [
45 Storage\Filesystem::NAME,
46 Storage\Database::NAME,
49 /** @var string[] List of valid backend classes */
50 private $validBackends;
53 * @var Storage\IStorage[] A local cache for storage instances
55 private $backendInstances = [];
61 /** @var LoggerInterface */
66 /** @var Storage\IWritableStorage */
67 private $currentBackend;
70 * @param Database $dba
71 * @param IConfig $config
72 * @param LoggerInterface $logger
75 * @throws Storage\InvalidClassStorageException in case the active backend class is invalid
76 * @throws Storage\StorageException in case of unexpected errors during the active backend class loading
78 public function __construct(Database $dba, IConfig $config, LoggerInterface $logger, L10n $l10n)
81 $this->config = $config;
82 $this->logger = $logger;
84 $this->validBackends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
86 $currentName = $this->config->get('storage', 'name');
88 // you can only use user backends as a "default" backend, so the second parameter is true
89 $this->currentBackend = $this->getWritableStorageByName($currentName);
93 * Return current storage backend class
95 * @return Storage\IWritableStorage
97 public function getBackend()
99 return $this->currentBackend;
103 * Returns a writable storage backend class by registered name
105 * @param string $name Backend name
107 * @return Storage\IWritableStorage
109 * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
110 * @throws Storage\StorageException in case of an unexpected failure during the hook call
112 public function getWritableStorageByName(string $name): Storage\IWritableStorage
114 $storage = $this->getByName($name, $this->validBackends);
115 if (!$storage instanceof Storage\IWritableStorage) {
116 throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
123 * Return storage backend configuration by registered name
125 * @param string $name Backend name
127 * @return Storage\IStorageConfiguration|false
129 * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
130 * @throws Storage\StorageException in case of an unexpected failure during the hook call
132 public function getConfigurationByName(string $name)
135 // Try the filesystem backend
136 case Storage\Filesystem::getName():
137 return new Storage\FilesystemConfig($this->config, $this->l10n);
138 // try the database backend
139 case Storage\Database::getName():
144 'storage_config' => null,
147 Hook::callAll('storage_config', $data);
148 if (!($data['storage_config'] ?? null) instanceof Storage\IStorageConfiguration) {
149 throw new Storage\InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name));
152 return $data['storage_config'];
153 } catch (InternalServerErrorException $exception) {
154 throw new Storage\StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception);
160 * Return storage backend class by registered name
162 * @param string $name Backend name
163 * @param string[]|null $validBackends possible, manual override of the valid backends
165 * @return Storage\IStorage
167 * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
168 * @throws Storage\StorageException in case of an unexpected failure during the hook call
170 public function getByName(string $name, array $validBackends = null): Storage\IStorage
172 // If there's no cached instance create a new instance
173 if (!isset($this->backendInstances[$name])) {
174 // If the current name isn't a valid backend (or the SystemResource instance) create it
175 if (!$this->isValidBackend($name, $validBackends)) {
176 throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
180 // Try the filesystem backend
181 case Storage\Filesystem::getName():
182 $storageConfig = new Storage\FilesystemConfig($this->config, $this->l10n);
183 $this->backendInstances[$name] = new Storage\Filesystem($storageConfig->getStoragePath());
185 // try the database backend
186 case Storage\Database::getName():
187 $this->backendInstances[$name] = new Storage\Database($this->dba);
189 // at least, try if there's an addon for the backend
190 case Storage\SystemResource::getName():
191 $this->backendInstances[$name] = new Storage\SystemResource();
193 case Storage\ExternalResource::getName():
194 $this->backendInstances[$name] = new Storage\ExternalResource();
202 Hook::callAll('storage_instance', $data);
203 if (!($data['storage'] ?? null) instanceof Storage\IStorage) {
204 throw new Storage\InvalidClassStorageException(sprintf('Backend %s was not found', $name));
207 $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
208 } catch (InternalServerErrorException $exception) {
209 throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
215 return $this->backendInstances[$name];
219 * Checks, if the storage is a valid backend
221 * @param string|null $name The name or class of the backend
222 * @param string[]|null $validBackends Possible, valid backends to check
224 * @return boolean True, if the backend is a valid backend
226 public function isValidBackend(string $name = null, array $validBackends = null): bool
228 $validBackends = $validBackends ?? array_merge($this->validBackends,
230 Storage\SystemResource::getName(),
231 Storage\ExternalResource::getName(),
233 return in_array($name, $validBackends);
237 * Set current storage backend class
239 * @param Storage\IWritableStorage $storage The storage class
241 * @return boolean True, if the set was successful
243 public function setBackend(Storage\IWritableStorage $storage): bool
245 if ($this->config->set('storage', 'name', $storage::getName())) {
246 $this->currentBackend = $storage;
254 * Get registered backends
258 public function listBackends(): array
260 return $this->validBackends;
264 * Register a storage backend class
266 * You have to register the hook "storage_instance" as well to make this class work!
268 * @param string $class Backend class name
270 * @return boolean True, if the registration was successful
272 public function register(string $class): bool
274 if (is_subclass_of($class, Storage\IStorage::class)) {
275 /** @var Storage\IStorage $class */
277 if ($this->isValidBackend($class::getName(), $this->validBackends)) {
281 $backends = $this->validBackends;
282 $backends[] = $class::getName();
284 if ($this->config->set('storage', 'backends', $backends)) {
285 $this->validBackends = $backends;
296 * Unregister a storage backend class
298 * @param string $class Backend class name
300 * @return boolean True, if unregistering was successful
302 * @throws Storage\StorageException
304 public function unregister(string $class): bool
306 if (is_subclass_of($class, Storage\IStorage::class)) {
307 /** @var Storage\IStorage $class */
309 if ($this->currentBackend::getName() == $class::getName()) {
310 throw new Storage\StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName()));
313 $key = array_search($class::getName(), $this->validBackends);
315 if ($key !== false) {
316 $backends = $this->validBackends;
317 unset($backends[$key]);
318 $backends = array_values($backends);
319 if ($this->config->set('storage', 'backends', $backends)) {
320 $this->validBackends = $backends;
334 * Move up to 5000 resources to storage $dest
336 * Copy existing data to destination storage and delete from source.
337 * This method cannot move to legacy in-table `data` field.
339 * @param Storage\IWritableStorage $destination Destination storage class name
340 * @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
341 * @param int $limit Limit of the process batch size, defaults to 5000
343 * @return int Number of moved resources
344 * @throws Storage\StorageException
347 public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
349 if (!$this->isValidBackend($destination, $this->validBackends)) {
350 throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
354 foreach ($tables as $table) {
355 // Get the rows where backend class is not the destination backend class
356 $resources = $this->dba->select(
358 ['id', 'data', 'backend-class', 'backend-ref'],
359 ['`backend-class` IS NULL or `backend-class` != ?', $destination::getName()],
363 while ($resource = $this->dba->fetch($resources)) {
364 $id = $resource['id'];
365 $sourceRef = $resource['backend-ref'];
369 $source = $this->getWritableStorageByName($resource['backend-class'] ?? '');
370 $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
371 $data = $source->get($sourceRef);
372 } catch (Storage\InvalidClassStorageException $exception) {
373 $this->logger->info('Get data from DB resource field.', ['oldReference' => $sourceRef]);
374 $data = $resource['data'];
375 } catch (Storage\ReferenceStorageException $exception) {
376 $this->logger->info('Invalid source reference.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
380 $this->logger->info('Save data to new backend.', ['newBackend' => $destination::getName()]);
381 $destinationRef = $destination->put($data);
382 $this->logger->info('Saved data.', ['newReference' => $destinationRef]);
384 if ($destinationRef !== '') {
385 $this->logger->info('update row');
386 if ($this->dba->update($table, ['backend-class' => $destination::getName(), 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
387 if (!empty($source)) {
388 $this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
389 $source->delete($sourceRef);
396 $this->dba->close($resources);