3 namespace Friendica\Core;
7 use Friendica\Core\Config\IConfiguration;
8 use Friendica\Database\Database;
9 use Friendica\Model\Storage;
10 use Psr\Log\LoggerInterface;
14 * @brief Manage storage backends
16 * Core code uses this class to get and set current storage backend class.
17 * Addons use this class to register and unregister additional backends.
21 // Default tables to look for data
22 const TABLES = ['photo', 'attach'];
24 // Default storage backends
25 const DEFAULT_BACKENDS = [
26 Storage\Filesystem::NAME => Storage\Filesystem::class,
27 Storage\Database::NAME => Storage\Database::class,
30 private $backends = [];
34 /** @var IConfiguration */
36 /** @var LoggerInterface */
41 /** @var Storage\IStorage */
42 private $currentBackend;
45 * @param Database $dba
46 * @param IConfiguration $config
47 * @param LoggerInterface $logger
50 public function __construct(Database $dba, IConfiguration $config, LoggerInterface $logger, Dice $dice)
53 $this->config = $config;
54 $this->logger = $logger;
56 $this->backends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
58 $currentName = $this->config->get('storage', 'name', '');
60 if ($this->isValidBackend($currentName)) {
61 $this->currentBackend = $this->dice->create($this->backends[$currentName]);
63 $this->currentBackend = null;
68 * @brief Return current storage backend class
70 * @return Storage\IStorage|null
72 public function getBackend()
74 return $this->currentBackend;
78 * @brief Return storage backend class by registered name
80 * @param string|null $name Backend name
82 * @return Storage\IStorage|null null if no backend registered at $name
84 public function getByName(string $name = null)
86 if (!$this->isValidBackend($name) &&
87 $name !== Storage\SystemResource::getName()) {
91 /** @var Storage\IStorage $storage */
94 // If the storage of the file is a system resource,
95 // create it directly since it isn't listed in the registered backends
96 if ($name === Storage\SystemResource::getName()) {
97 $storage = $this->dice->create(Storage\SystemResource::class);
99 $storage = $this->dice->create($this->backends[$name]);
106 * Checks, if the storage is a valid backend
108 * @param string|null $name The name or class of the backend
110 * @return boolean True, if the backend is a valid backend
112 public function isValidBackend(string $name = null)
114 return array_key_exists($name, $this->backends);
118 * @brief Set current storage backend class
120 * @param string $name Backend class name
122 * @return boolean True, if the set was successful
124 public function setBackend(string $name = null)
126 if (!$this->isValidBackend($name)) {
130 if ($this->config->set('storage', 'name', $name)) {
131 $this->currentBackend = $this->dice->create($this->backends[$name]);
139 * @brief Get registered backends
143 public function listBackends()
145 return $this->backends;
149 * @brief Register a storage backend class
151 * @param string $class Backend class name
153 * @return boolean True, if the registration was successful
155 public function register(string $class)
157 if (is_subclass_of($class, Storage\IStorage::class)) {
158 /** @var Storage\IStorage $class */
160 $backends = $this->backends;
161 $backends[$class::getName()] = $class;
163 if ($this->config->set('storage', 'backends', $backends)) {
164 $this->backends = $backends;
175 * @brief Unregister a storage backend class
177 * @param string $class Backend class name
179 * @return boolean True, if unregistering was successful
181 public function unregister(string $class)
183 if (is_subclass_of($class, Storage\IStorage::class)) {
184 /** @var Storage\IStorage $class */
186 unset($this->backends[$class::getName()]);
188 if ($this->currentBackend instanceof $class) {
189 $this->config->set('storage', 'name', null);
190 $this->currentBackend = null;
193 return $this->config->set('storage', 'backends', $this->backends);
200 * @brief Move up to 5000 resources to storage $dest
202 * Copy existing data to destination storage and delete from source.
203 * This method cannot move to legacy in-table `data` field.
205 * @param Storage\IStorage $destination Destination storage class name
206 * @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
207 * @param int $limit Limit of the process batch size, defaults to 5000
209 * @return int Number of moved resources
210 * @throws Storage\StorageException
213 public function move(Storage\IStorage $destination, array $tables = self::TABLES, int $limit = 5000)
215 if ($destination === null) {
216 throw new Storage\StorageException('Can\'t move to NULL storage backend');
220 foreach ($tables as $table) {
221 // Get the rows where backend class is not the destination backend class
222 $resources = $this->dba->select(
224 ['id', 'data', 'backend-class', 'backend-ref'],
225 ['`backend-class` IS NULL or `backend-class` != ?', $destination::getName()],
229 while ($resource = $this->dba->fetch($resources)) {
230 $id = $resource['id'];
231 $data = $resource['data'];
232 $source = $this->getByName($resource['backend-class']);
233 $sourceRef = $resource['backend-ref'];
235 if (!empty($source)) {
236 $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
237 $data = $source->get($sourceRef);
240 $this->logger->info('Save data to new backend.', ['newBackend' => $destination]);
241 $destinationRef = $destination->put($data);
242 $this->logger->info('Saved data.', ['newReference' => $destinationRef]);
244 if ($destinationRef !== '') {
245 $this->logger->info('update row');
246 if ($this->dba->update($table, ['backend-class' => $destination, 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
247 if (!empty($source)) {
248 $this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
249 $source->delete($sourceRef);
256 $this->dba->close($resources);