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
49 public function __construct(Database $dba, IConfiguration $config, LoggerInterface $logger, Dice $dice)
52 $this->config = $config;
53 $this->logger = $logger;
55 $this->backends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
57 $currentName = $this->config->get('storage', 'name', '');
59 if ($this->isValidBackend($currentName)) {
60 $this->currentBackend = $this->dice->create($this->backends[$currentName]);
62 $this->currentBackend = null;
67 * @brief Return current storage backend class
69 * @return Storage\IStorage|null
71 public function getBackend()
73 return $this->currentBackend;
77 * @brief Return storage backend class by registered name
79 * @param string $name Backend name
81 * @return Storage\IStorage|null null if no backend registered at $name
83 public function getByName(string $name)
85 if (!$this->isValidBackend($name)) {
89 return $this->dice->create($this->backends[$name]);
93 * Checks, if the storage is a valid backend
95 * @param string $name The name or class of the backend
97 * @return boolean True, if the backend is a valid backend
99 public function isValidBackend(string $name)
101 return array_key_exists($name, $this->backends);
105 * @brief Set current storage backend class
107 * @param string $name Backend class name
109 * @return boolean True, if the set was successful
111 public function setBackend(string $name)
113 if (!$this->isValidBackend($name)) {
117 if ($this->config->set('storage', 'name', $name)) {
118 $this->currentBackend = $this->dice->create($this->backends[$name]);
126 * @brief Get registered backends
130 public function listBackends()
132 return $this->backends;
136 * @brief Register a storage backend class
138 * @param string $name User readable backend name
139 * @param string $class Backend class name
141 * @return boolean True, if the registration was successful
143 public function register(string $name, string $class)
145 if (!is_subclass_of($class, Storage\IStorage::class)) {
149 $backends = $this->backends;
150 $backends[$name] = $class;
152 if ($this->config->set('storage', 'backends', $this->backends)) {
153 $this->backends = $backends;
161 * @brief Unregister a storage backend class
163 * @param string $name User readable backend name
165 * @return boolean True, if unregistering was successful
167 public function unregister(string $name)
169 unset($this->backends[$name]);
170 return $this->config->set('storage', 'backends', $this->backends);
174 * @brief Move up to 5000 resources to storage $dest
176 * Copy existing data to destination storage and delete from source.
177 * This method cannot move to legacy in-table `data` field.
179 * @param Storage\IStorage $destination Destination storage class name
180 * @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
181 * @param int $limit Limit of the process batch size, defaults to 5000
183 * @return int Number of moved resources
184 * @throws Storage\StorageException
187 public function move(Storage\IStorage $destination, array $tables = self::TABLES, int $limit = 5000)
189 if ($destination === null) {
190 throw new Storage\StorageException('Can\'t move to NULL storage backend');
194 foreach ($tables as $table) {
195 // Get the rows where backend class is not the destination backend class
196 $resources = $this->dba->select(
198 ['id', 'data', 'backend-class', 'backend-ref'],
199 ['`backend-class` IS NULL or `backend-class` != ?', $destination],
203 while ($resource = $this->dba->fetch($resources)) {
204 $id = $resource['id'];
205 $data = $resource['data'];
206 $source = $this->getByName($resource['backend-class']);
207 $sourceRef = $resource['backend-ref'];
209 if (!empty($source)) {
210 $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
211 $data = $source->get($sourceRef);
214 $this->logger->info('Save data to new backend.', ['newBackend' => $destination]);
215 $destinationRef = $destination->put($data);
216 $this->logger->info('Saved data.', ['newReference' => $destinationRef]);
218 if ($destinationRef !== '') {
219 $this->logger->info('update row');
220 if ($this->dba->update($table, ['backend-class' => $destination, 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
221 if (!empty($source)) {
222 $this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
223 $source->delete($sourceRef);
230 $this->dba->close($resources);