]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
Implement Hook::callAll('storage_instance') call for addons and add a description...
[friendica.git] / src / Core / StorageManager.php
1 <?php
2
3 namespace Friendica\Core;
4
5 use Exception;
6 use Friendica\Core\Config\IConfiguration;
7 use Friendica\Core\L10n\L10n;
8 use Friendica\Database\Database;
9 use Friendica\Model\Storage;
10 use Psr\Log\LoggerInterface;
11
12
13 /**
14  * @brief Manage storage backends
15  *
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.
18  */
19 class StorageManager
20 {
21         // Default tables to look for data
22         const TABLES = ['photo', 'attach'];
23
24         // Default storage backends
25         const DEFAULT_BACKENDS = [
26                 Storage\Filesystem::NAME => Storage\Filesystem::class,
27                 Storage\Database::NAME   => Storage\Database::class,
28         ];
29
30         private $backends = [];
31
32         /**
33          * @var Storage\IStorage[] A local cache for storage instances
34          */
35         private $backendInstances = [];
36
37         /** @var Database */
38         private $dba;
39         /** @var IConfiguration */
40         private $config;
41         /** @var LoggerInterface */
42         private $logger;
43         /** @var L10n */
44         private $l10n;
45
46         /** @var Storage\IStorage */
47         private $currentBackend;
48
49         /**
50          * @param Database        $dba
51          * @param IConfiguration  $config
52          * @param LoggerInterface $logger
53          * @param L10n            $l10n
54          */
55         public function __construct(Database $dba, IConfiguration $config, LoggerInterface $logger, L10n $l10n)
56         {
57                 $this->dba      = $dba;
58                 $this->config   = $config;
59                 $this->logger   = $logger;
60                 $this->l10n     = $l10n;
61                 $this->backends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
62
63                 $currentName = $this->config->get('storage', 'name', '');
64
65                 $this->currentBackend = $this->getByName($currentName);
66         }
67
68         /**
69          * @brief Return current storage backend class
70          *
71          * @return Storage\IStorage|null
72          */
73         public function getBackend()
74         {
75                 return $this->currentBackend;
76         }
77
78         /**
79          * @brief Return storage backend class by registered name
80          *
81          * @param string|null $name        Backend name
82          * @param boolean     $userBackend Just return instances in case it's a user backend (e.g. not SystemResource)
83          *
84          * @return Storage\IStorage|null null if no backend registered at $name
85          *
86          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
87          */
88         public function getByName(string $name = null, $userBackend = true)
89         {
90                 // If there's no cached instance create a new instance
91                 if (!isset($this->backendInstances[$name])) {
92                         // If the current name isn't a valid backend (or the SystemResource instance) create it
93                         if ($this->isValidBackend($name, $userBackend)) {
94                                 switch ($name) {
95                                         // Try the filesystem backend
96                                         case Storage\Filesystem::getName():
97                                                 $this->backendInstances[$name] = new Storage\Filesystem($this->config, $this->logger, $this->l10n);
98                                                 break;
99                                         // try the database backend
100                                         case Storage\Database::getName():
101                                                 $this->backendInstances[$name] = new Storage\Database($this->dba, $this->logger, $this->l10n);
102                                                 break;
103                                         // at least, try if there's an addon for the backend
104                                         case Storage\SystemResource::getName():
105                                                 $this->backendInstances[$name] =  new Storage\SystemResource();
106                                                 break;
107                                         default:
108                                                 $data = [
109                                                         'name' => $name,
110                                                         'storage' => null,
111                                                 ];
112                                                 Hook::callAll('storage_instance', $data);
113                                                 if (($data['storage'] ?? null) instanceof Storage\IStorage) {
114                                                         $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
115                                                 } else {
116                                                         return null;
117                                                 }
118                                                 break;
119                                 }
120                         } else {
121                                 return null;
122                         }
123                 }
124
125                 return $this->backendInstances[$name];
126         }
127
128         /**
129          * Checks, if the storage is a valid backend
130          *
131          * @param string|null $name        The name or class of the backend
132          * @param boolean     $userBackend True, if just user backend should get returned (e.g. not SystemResource)
133          *
134          * @return boolean True, if the backend is a valid backend
135          */
136         public function isValidBackend(string $name = null, bool $userBackend = true)
137         {
138                 return array_key_exists($name, $this->backends) ||
139                        (!$userBackend && $name === Storage\SystemResource::getName());
140         }
141
142         /**
143          * @brief Set current storage backend class
144          *
145          * @param string $name Backend class name
146          *
147          * @return boolean True, if the set was successful
148          */
149         public function setBackend(string $name = null)
150         {
151                 if (!$this->isValidBackend($name)) {
152                         return false;
153                 }
154
155                 if ($this->config->set('storage', 'name', $name)) {
156                         $this->currentBackend = $this->getByName($name);
157                         return true;
158                 } else {
159                         return false;
160                 }
161         }
162
163         /**
164          * @brief Get registered backends
165          *
166          * @return array
167          */
168         public function listBackends()
169         {
170                 return $this->backends;
171         }
172
173         /**
174          * Register a storage backend class
175          *
176          * You have to register the hook "storage_instance" as well to make this class work!
177          *
178          * @param string $class Backend class name
179          *
180          * @return boolean True, if the registration was successful
181          */
182         public function register(string $class)
183         {
184                 if (is_subclass_of($class, Storage\IStorage::class)) {
185                         /** @var Storage\IStorage $class */
186
187                         $backends        = $this->backends;
188                         $backends[$class::getName()] = $class;
189
190                         if ($this->config->set('storage', 'backends', $backends)) {
191                                 $this->backends = $backends;
192                                 return true;
193                         } else {
194                                 return false;
195                         }
196                 } else {
197                         return false;
198                 }
199         }
200
201         /**
202          * @brief Unregister a storage backend class
203          *
204          * @param string $class Backend class name
205          *
206          * @return boolean True, if unregistering was successful
207          */
208         public function unregister(string $class)
209         {
210                 if (is_subclass_of($class, Storage\IStorage::class)) {
211                         /** @var Storage\IStorage $class */
212
213                         unset($this->backends[$class::getName()]);
214
215                         if ($this->currentBackend instanceof $class) {
216                             $this->config->set('storage', 'name', null);
217                                 $this->currentBackend = null;
218                         }
219
220                         return $this->config->set('storage', 'backends', $this->backends);
221                 } else {
222                         return false;
223                 }
224         }
225
226         /**
227          * @brief Move up to 5000 resources to storage $dest
228          *
229          * Copy existing data to destination storage and delete from source.
230          * This method cannot move to legacy in-table `data` field.
231          *
232          * @param Storage\IStorage $destination Destination storage class name
233          * @param array            $tables      Tables to look in for resources. Optional, defaults to ['photo', 'attach']
234          * @param int              $limit       Limit of the process batch size, defaults to 5000
235          *
236          * @return int Number of moved resources
237          * @throws Storage\StorageException
238          * @throws Exception
239          */
240         public function move(Storage\IStorage $destination, array $tables = self::TABLES, int $limit = 5000)
241         {
242                 if ($destination === null) {
243                         throw new Storage\StorageException('Can\'t move to NULL storage backend');
244                 }
245
246                 $moved = 0;
247                 foreach ($tables as $table) {
248                         // Get the rows where backend class is not the destination backend class
249                         $resources = $this->dba->select(
250                                 $table,
251                                 ['id', 'data', 'backend-class', 'backend-ref'],
252                                 ['`backend-class` IS NULL or `backend-class` != ?', $destination::getName()],
253                                 ['limit' => $limit]
254                         );
255
256                         while ($resource = $this->dba->fetch($resources)) {
257                                 $id        = $resource['id'];
258                                 $data      = $resource['data'];
259                                 $source    = $this->getByName($resource['backend-class']);
260                                 $sourceRef = $resource['backend-ref'];
261
262                                 if (!empty($source)) {
263                                         $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
264                                         $data = $source->get($sourceRef);
265                                 }
266
267                                 $this->logger->info('Save data to new backend.', ['newBackend' => $destination]);
268                                 $destinationRef = $destination->put($data);
269                                 $this->logger->info('Saved data.', ['newReference' => $destinationRef]);
270
271                                 if ($destinationRef !== '') {
272                                         $this->logger->info('update row');
273                                         if ($this->dba->update($table, ['backend-class' => $destination, 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
274                                                 if (!empty($source)) {
275                                                         $this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
276                                                         $source->delete($sourceRef);
277                                                 }
278                                                 $moved++;
279                                         }
280                                 }
281                         }
282
283                         $this->dba->close($resources);
284                 }
285
286                 return $moved;
287         }
288 }