]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
Introduce InvalidClassStorageException and adapt the code for it
[friendica.git] / src / Core / StorageManager.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Exception;
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;
30
31 /**
32  * Manage storage backends
33  *
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.
36  */
37 class StorageManager
38 {
39         // Default tables to look for data
40         const TABLES = ['photo', 'attach'];
41
42         // Default storage backends
43         const DEFAULT_BACKENDS = [
44                 Storage\Filesystem::NAME => Storage\Filesystem::class,
45                 Storage\Database::NAME   => Storage\Database::class,
46         ];
47
48         private $backends;
49
50         /**
51          * @var Storage\IStorage[] A local cache for storage instances
52          */
53         private $backendInstances = [];
54
55         /** @var Database */
56         private $dba;
57         /** @var IConfig */
58         private $config;
59         /** @var LoggerInterface */
60         private $logger;
61         /** @var L10n */
62         private $l10n;
63
64         /** @var Storage\IWritableStorage */
65         private $currentBackend;
66
67         /**
68          * @param Database        $dba
69          * @param IConfig         $config
70          * @param LoggerInterface $logger
71          * @param L10n            $l10n
72          *
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
75          */
76         public function __construct(Database $dba, IConfig $config, LoggerInterface $logger, L10n $l10n)
77         {
78                 $this->dba      = $dba;
79                 $this->config   = $config;
80                 $this->logger   = $logger;
81                 $this->l10n     = $l10n;
82                 $this->backends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
83
84                 $currentName = $this->config->get('storage', 'name');
85
86                 // you can only use user backends as a "default" backend, so the second parameter is true
87                 $this->currentBackend = $this->getWritableStorageByName($currentName);
88         }
89
90         /**
91          * Return current storage backend class
92          *
93          * @return Storage\IWritableStorage
94          */
95         public function getBackend()
96         {
97                 return $this->currentBackend;
98         }
99
100         /**
101          * Returns a writable storage backend class by registered name
102          *
103          * @param string $name Backend name
104          *
105          * @return Storage\IWritableStorage
106          *
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
109          */
110         public function getWritableStorageByName(string $name): Storage\IWritableStorage
111         {
112                 $storage = $this->getByName($name, $this->backends);
113                 if ($storage instanceof Storage\IWritableStorage) {
114                         return $storage;
115                 } else {
116                         throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
117                 }
118         }
119
120         /**
121          * Return storage backend class by registered name
122          *
123          * @param string     $name Backend name
124          * @param array|null $validBackends possible, manual override of the valid backends
125          *
126          * @return Storage\IStorage
127          *
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
130          */
131         public function getByName(string $name, array $validBackends = null): Storage\IStorage
132         {
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)) {
137                                 switch ($name) {
138                                         // Try the filesystem backend
139                                         case Storage\Filesystem::getName():
140                                                 $this->backendInstances[$name] = new Storage\Filesystem($this->config, $this->l10n);
141                                                 break;
142                                         // try the database backend
143                                         case Storage\Database::getName():
144                                                 $this->backendInstances[$name] = new Storage\Database($this->dba);
145                                                 break;
146                                         // at least, try if there's an addon for the backend
147                                         case Storage\SystemResource::getName():
148                                                 $this->backendInstances[$name] = new Storage\SystemResource();
149                                                 break;
150                                         case Storage\ExternalResource::getName():
151                                                 $this->backendInstances[$name] = new Storage\ExternalResource();
152                                                 break;
153                                         default:
154                                                 $data = [
155                                                         'name'    => $name,
156                                                         'storage' => null,
157                                                 ];
158                                                 try {
159                                                         Hook::callAll('storage_instance', $data);
160                                                         if (($data['storage'] ?? null) instanceof Storage\IStorage) {
161                                                                 $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
162                                                         } else {
163                                                                 throw new Storage\InvalidClassStorageException(sprintf('Backend %s was not found', $name));
164                                                         }
165                                                 } catch (InternalServerErrorException $exception) {
166                                                         throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
167                                                 }
168                                                 break;
169                                 }
170                         } else {
171                                 throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
172                         }
173                 }
174
175                 return $this->backendInstances[$name];
176         }
177
178         /**
179          * Checks, if the storage is a valid backend
180          *
181          * @param string|null $name          The name or class of the backend
182          * @param array|null  $validBackends Possible, valid backends to check
183          *
184          * @return boolean True, if the backend is a valid backend
185          */
186         public function isValidBackend(string $name = null, array $validBackends = null): bool
187         {
188                 $validBackends = $validBackends ?? array_merge($this->backends,
189                                 [
190                                         Storage\SystemResource::getName()   => '',
191                                         Storage\ExternalResource::getName() => ''
192                                 ]);
193                 return array_key_exists($name, $validBackends);
194         }
195
196         /**
197          * Set current storage backend class
198          *
199          * @param Storage\IWritableStorage $storage The storage class
200          *
201          * @return boolean True, if the set was successful
202          */
203         public function setBackend(Storage\IWritableStorage $storage): bool
204         {
205                 if ($this->config->set('storage', 'name', $storage::getName())) {
206                         $this->currentBackend = $storage;
207                         return true;
208                 } else {
209                         return false;
210                 }
211         }
212
213         /**
214          * Get registered backends
215          *
216          * @return array
217          */
218         public function listBackends(): array
219         {
220                 return $this->backends;
221         }
222
223         /**
224          * Register a storage backend class
225          *
226          * You have to register the hook "storage_instance" as well to make this class work!
227          *
228          * @param string $class Backend class name
229          *
230          * @return boolean True, if the registration was successful
231          */
232         public function register(string $class): bool
233         {
234                 if (is_subclass_of($class, Storage\IStorage::class)) {
235                         /** @var Storage\IStorage $class */
236
237                         $backends                    = $this->backends;
238                         $backends[$class::getName()] = $class;
239
240                         if ($this->config->set('storage', 'backends', $backends)) {
241                                 $this->backends = $backends;
242                                 return true;
243                         } else {
244                                 return false;
245                         }
246                 } else {
247                         return false;
248                 }
249         }
250
251         /**
252          * Unregister a storage backend class
253          *
254          * @param string $class Backend class name
255          *
256          * @return boolean True, if unregistering was successful
257          */
258         public function unregister(string $class): bool
259         {
260                 if (is_subclass_of($class, Storage\IStorage::class)) {
261                         /** @var Storage\IStorage $class */
262
263                         unset($this->backends[$class::getName()]);
264
265                         if ($this->currentBackend instanceof $class) {
266                                 $this->config->set('storage', 'name', null);
267                                 $this->currentBackend = null;
268                         }
269
270                         return $this->config->set('storage', 'backends', $this->backends);
271                 } else {
272                         return false;
273                 }
274         }
275
276         /**
277          * Move up to 5000 resources to storage $dest
278          *
279          * Copy existing data to destination storage and delete from source.
280          * This method cannot move to legacy in-table `data` field.
281          *
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
285          *
286          * @return int Number of moved resources
287          * @throws Storage\StorageException
288          * @throws Exception
289          */
290         public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
291         {
292                 if (!$this->isValidBackend($destination, $this->backends)) {
293                         throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
294                 }
295
296                 $moved = 0;
297                 foreach ($tables as $table) {
298                         // Get the rows where backend class is not the destination backend class
299                         $resources = $this->dba->select(
300                                 $table,
301                                 ['id', 'data', 'backend-class', 'backend-ref'],
302                                 ['`backend-class` IS NULL or `backend-class` != ?', $destination::getName()],
303                                 ['limit' => $limit]
304                         );
305
306                         while ($resource = $this->dba->fetch($resources)) {
307                                 $id        = $resource['id'];
308                                 $sourceRef = $resource['backend-ref'];
309                                 $source    = null;
310
311                                 try {
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]);
320                                         continue;
321                                 }
322
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]);
326
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);
333                                                 }
334                                                 $moved++;
335                                         }
336                                 }
337                         }
338
339                         $this->dba->close($resources);
340                 }
341
342                 return $moved;
343         }
344 }