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
43 const DEFAULT_BACKENDS = [
44 Storage\Filesystem::NAME => Storage\Filesystem::class,
45 Storage\Database::NAME => Storage\Database::class,
51 * @var Storage\IStorage[] A local cache for storage instances
53 private $backendInstances = [];
59 /** @var LoggerInterface */
64 /** @var Storage\IWritableStorage */
65 private $currentBackend;
68 * @param Database $dba
69 * @param IConfig $config
70 * @param LoggerInterface $logger
73 * @throws Storage\InvalidClassStorageException in case the active backend class is invalid
74 * @throws Storage\StorageException in case of unexpected errors during the active backend class loading
76 public function __construct(Database $dba, IConfig $config, LoggerInterface $logger, L10n $l10n)
79 $this->config = $config;
80 $this->logger = $logger;
82 $this->backends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
84 $currentName = $this->config->get('storage', 'name');
86 // you can only use user backends as a "default" backend, so the second parameter is true
87 $this->currentBackend = $this->getWritableStorageByName($currentName);
91 * Return current storage backend class
93 * @return Storage\IWritableStorage
95 public function getBackend()
97 return $this->currentBackend;
101 * Returns a writable storage backend class by registered name
103 * @param string $name Backend name
105 * @return Storage\IWritableStorage
107 * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
108 * @throws Storage\StorageException in case of an unexpected failure during the hook call
110 public function getWritableStorageByName(string $name): Storage\IWritableStorage
112 $storage = $this->getByName($name, $this->backends);
113 if ($storage instanceof Storage\IWritableStorage) {
116 throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
121 * Return storage backend class by registered name
123 * @param string $name Backend name
124 * @param array|null $validBackends possible, manual override of the valid backends
126 * @return Storage\IStorage
128 * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
129 * @throws Storage\StorageException in case of an unexpected failure during the hook call
131 public function getByName(string $name, array $validBackends = null): Storage\IStorage
133 // If there's no cached instance create a new instance
134 if (!isset($this->backendInstances[$name])) {
135 // If the current name isn't a valid backend (or the SystemResource instance) create it
136 if ($this->isValidBackend($name, $validBackends)) {
138 // Try the filesystem backend
139 case Storage\Filesystem::getName():
140 $this->backendInstances[$name] = new Storage\Filesystem($this->config, $this->l10n);
142 // try the database backend
143 case Storage\Database::getName():
144 $this->backendInstances[$name] = new Storage\Database($this->dba);
146 // at least, try if there's an addon for the backend
147 case Storage\SystemResource::getName():
148 $this->backendInstances[$name] = new Storage\SystemResource();
150 case Storage\ExternalResource::getName():
151 $this->backendInstances[$name] = new Storage\ExternalResource();
159 Hook::callAll('storage_instance', $data);
160 if (($data['storage'] ?? null) instanceof Storage\IStorage) {
161 $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
163 throw new Storage\InvalidClassStorageException(sprintf('Backend %s was not found', $name));
165 } catch (InternalServerErrorException $exception) {
166 throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
171 throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
175 return $this->backendInstances[$name];
179 * Checks, if the storage is a valid backend
181 * @param string|null $name The name or class of the backend
182 * @param array|null $validBackends Possible, valid backends to check
184 * @return boolean True, if the backend is a valid backend
186 public function isValidBackend(string $name = null, array $validBackends = null): bool
188 $validBackends = $validBackends ?? array_merge($this->backends,
190 Storage\SystemResource::getName() => '',
191 Storage\ExternalResource::getName() => ''
193 return array_key_exists($name, $validBackends);
197 * Set current storage backend class
199 * @param Storage\IWritableStorage $storage The storage class
201 * @return boolean True, if the set was successful
203 public function setBackend(Storage\IWritableStorage $storage): bool
205 if ($this->config->set('storage', 'name', $storage::getName())) {
206 $this->currentBackend = $storage;
214 * Get registered backends
218 public function listBackends(): array
220 return $this->backends;
224 * Register a storage backend class
226 * You have to register the hook "storage_instance" as well to make this class work!
228 * @param string $class Backend class name
230 * @return boolean True, if the registration was successful
232 public function register(string $class): bool
234 if (is_subclass_of($class, Storage\IStorage::class)) {
235 /** @var Storage\IStorage $class */
237 $backends = $this->backends;
238 $backends[$class::getName()] = $class;
240 if ($this->config->set('storage', 'backends', $backends)) {
241 $this->backends = $backends;
252 * Unregister a storage backend class
254 * @param string $class Backend class name
256 * @return boolean True, if unregistering was successful
258 public function unregister(string $class): bool
260 if (is_subclass_of($class, Storage\IStorage::class)) {
261 /** @var Storage\IStorage $class */
263 unset($this->backends[$class::getName()]);
265 if ($this->currentBackend instanceof $class) {
266 $this->config->set('storage', 'name', null);
267 $this->currentBackend = null;
270 return $this->config->set('storage', 'backends', $this->backends);
277 * Move up to 5000 resources to storage $dest
279 * Copy existing data to destination storage and delete from source.
280 * This method cannot move to legacy in-table `data` field.
282 * @param Storage\IWritableStorage $destination Destination storage class name
283 * @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
284 * @param int $limit Limit of the process batch size, defaults to 5000
286 * @return int Number of moved resources
287 * @throws Storage\StorageException
290 public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
292 if (!$this->isValidBackend($destination, $this->backends)) {
293 throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
297 foreach ($tables as $table) {
298 // Get the rows where backend class is not the destination backend class
299 $resources = $this->dba->select(
301 ['id', 'data', 'backend-class', 'backend-ref'],
302 ['`backend-class` IS NULL or `backend-class` != ?', $destination::getName()],
306 while ($resource = $this->dba->fetch($resources)) {
307 $id = $resource['id'];
308 $sourceRef = $resource['backend-ref'];
312 $source = $this->getWritableStorageByName($resource['backend-class'] ?? '');
313 $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
314 $data = $source->get($sourceRef);
315 } catch (Storage\InvalidClassStorageException $exception) {
316 $this->logger->info('Get data from DB resource field.', ['oldReference' => $sourceRef]);
317 $data = $resource['data'];
318 } catch (Storage\ReferenceStorageException $exception) {
319 $this->logger->info('Invalid source reference.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
323 $this->logger->info('Save data to new backend.', ['newBackend' => $destination::getName()]);
324 $destinationRef = $destination->put($data);
325 $this->logger->info('Saved data.', ['newReference' => $destinationRef]);
327 if ($destinationRef !== '') {
328 $this->logger->info('update row');
329 if ($this->dba->update($table, ['backend-class' => $destination::getName(), 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
330 if (!empty($source)) {
331 $this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
332 $source->delete($sourceRef);
339 $this->dba->close($resources);