]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
Merge pull request #10912 from MrPetovan/bug/fatal-errors
[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         /** @var string[]  */
44         const DEFAULT_BACKENDS = [
45                 Storage\Filesystem::NAME,
46                 Storage\Database::NAME,
47         ];
48
49         /** @var string[] List of valid backend classes */
50         private $validBackends;
51
52         /**
53          * @var Storage\IStorage[] A local cache for storage instances
54          */
55         private $backendInstances = [];
56
57         /** @var Database */
58         private $dba;
59         /** @var IConfig */
60         private $config;
61         /** @var LoggerInterface */
62         private $logger;
63         /** @var L10n */
64         private $l10n;
65
66         /** @var Storage\IWritableStorage */
67         private $currentBackend;
68
69         /**
70          * @param Database        $dba
71          * @param IConfig         $config
72          * @param LoggerInterface $logger
73          * @param L10n            $l10n
74          *
75          * @throws Storage\InvalidClassStorageException in case the active backend class is invalid
76          * @throws Storage\StorageException in case of unexpected errors during the active backend class loading
77          */
78         public function __construct(Database $dba, IConfig $config, LoggerInterface $logger, L10n $l10n)
79         {
80                 $this->dba           = $dba;
81                 $this->config        = $config;
82                 $this->logger        = $logger;
83                 $this->l10n          = $l10n;
84                 $this->validBackends = $config->get('storage', 'backends', self::DEFAULT_BACKENDS);
85
86                 $currentName = $this->config->get('storage', 'name');
87
88                 // you can only use user backends as a "default" backend, so the second parameter is true
89                 $this->currentBackend = $this->getWritableStorageByName($currentName);
90         }
91
92         /**
93          * Return current storage backend class
94          *
95          * @return Storage\IWritableStorage
96          */
97         public function getBackend()
98         {
99                 return $this->currentBackend;
100         }
101
102         /**
103          * Returns a writable storage backend class by registered name
104          *
105          * @param string $name Backend name
106          *
107          * @return Storage\IWritableStorage
108          *
109          * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
110          * @throws Storage\StorageException in case of an unexpected failure during the hook call
111          */
112         public function getWritableStorageByName(string $name): Storage\IWritableStorage
113         {
114                 $storage = $this->getByName($name, $this->validBackends);
115                 if (!$storage instanceof Storage\IWritableStorage) {
116                         throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
117                 }
118
119                 return $storage;
120         }
121
122         /**
123          * Return storage backend configuration by registered name
124          *
125          * @param string     $name Backend name
126          *
127          * @return Storage\IStorageConfiguration|false
128          *
129          * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
130          * @throws Storage\StorageException in case of an unexpected failure during the hook call
131          */
132         public function getConfigurationByName(string $name)
133         {
134                 switch ($name) {
135                         // Try the filesystem backend
136                         case Storage\Filesystem::getName():
137                                 return new Storage\FilesystemConfig($this->config, $this->l10n);
138                         // try the database backend
139                         case Storage\Database::getName():
140                                 return false;
141                         default:
142                                 $data = [
143                                         'name'           => $name,
144                                         'storage_config' => null,
145                                 ];
146                                 try {
147                                         Hook::callAll('storage_config', $data);
148                                         if (!($data['storage_config'] ?? null) instanceof Storage\IStorageConfiguration) {
149                                                 throw new Storage\InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name));
150                                         }
151
152                                         return $data['storage_config'];
153                                 } catch (InternalServerErrorException $exception) {
154                                         throw new Storage\StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception);
155                                 }
156                 }
157         }
158
159         /**
160          * Return storage backend class by registered name
161          *
162          * @param string     $name Backend name
163          * @param string[]|null $validBackends possible, manual override of the valid backends
164          *
165          * @return Storage\IStorage
166          *
167          * @throws Storage\InvalidClassStorageException in case there's no backend class for the name
168          * @throws Storage\StorageException in case of an unexpected failure during the hook call
169          */
170         public function getByName(string $name, array $validBackends = null): Storage\IStorage
171         {
172                 // If there's no cached instance create a new instance
173                 if (!isset($this->backendInstances[$name])) {
174                         // If the current name isn't a valid backend (or the SystemResource instance) create it
175                         if (!$this->isValidBackend($name, $validBackends)) {
176                                 throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
177                         }
178
179                         switch ($name) {
180                                 // Try the filesystem backend
181                                 case Storage\Filesystem::getName():
182                                         $storageConfig                 = new Storage\FilesystemConfig($this->config, $this->l10n);
183                                         $this->backendInstances[$name] = new Storage\Filesystem($storageConfig->getStoragePath());
184                                         break;
185                                 // try the database backend
186                                 case Storage\Database::getName():
187                                         $this->backendInstances[$name] = new Storage\Database($this->dba);
188                                         break;
189                                 // at least, try if there's an addon for the backend
190                                 case Storage\SystemResource::getName():
191                                         $this->backendInstances[$name] = new Storage\SystemResource();
192                                         break;
193                                 case Storage\ExternalResource::getName():
194                                         $this->backendInstances[$name] = new Storage\ExternalResource();
195                                         break;
196                                 default:
197                                         $data = [
198                                                 'name'    => $name,
199                                                 'storage' => null,
200                                         ];
201                                         try {
202                                                 Hook::callAll('storage_instance', $data);
203                                                 if (!($data['storage'] ?? null) instanceof Storage\IStorage) {
204                                                         throw new Storage\InvalidClassStorageException(sprintf('Backend %s was not found', $name));
205                                                 }
206
207                                                 $this->backendInstances[$data['name'] ?? $name] = $data['storage'];
208                                         } catch (InternalServerErrorException $exception) {
209                                                 throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
210                                         }
211                                         break;
212                         }
213                 }
214
215                 return $this->backendInstances[$name];
216         }
217
218         /**
219          * Checks, if the storage is a valid backend
220          *
221          * @param string|null   $name          The name or class of the backend
222          * @param string[]|null $validBackends Possible, valid backends to check
223          *
224          * @return boolean True, if the backend is a valid backend
225          */
226         public function isValidBackend(string $name = null, array $validBackends = null): bool
227         {
228                 $validBackends = $validBackends ?? array_merge($this->validBackends,
229                                 [
230                                         Storage\SystemResource::getName(),
231                                         Storage\ExternalResource::getName(),
232                                 ]);
233                 return in_array($name, $validBackends);
234         }
235
236         /**
237          * Set current storage backend class
238          *
239          * @param Storage\IWritableStorage $storage The storage class
240          *
241          * @return boolean True, if the set was successful
242          */
243         public function setBackend(Storage\IWritableStorage $storage): bool
244         {
245                 if ($this->config->set('storage', 'name', $storage::getName())) {
246                         $this->currentBackend = $storage;
247                         return true;
248                 } else {
249                         return false;
250                 }
251         }
252
253         /**
254          * Get registered backends
255          *
256          * @return string[]
257          */
258         public function listBackends(): array
259         {
260                 return $this->validBackends;
261         }
262
263         /**
264          * Register a storage backend class
265          *
266          * You have to register the hook "storage_instance" as well to make this class work!
267          *
268          * @param string $class Backend class name
269          *
270          * @return boolean True, if the registration was successful
271          */
272         public function register(string $class): bool
273         {
274                 if (is_subclass_of($class, Storage\IStorage::class)) {
275                         /** @var Storage\IStorage $class */
276
277                         if ($this->isValidBackend($class::getName(), $this->validBackends)) {
278                                 return true;
279                         }
280
281                         $backends   = $this->validBackends;
282                         $backends[] = $class::getName();
283
284                         if ($this->config->set('storage', 'backends', $backends)) {
285                                 $this->validBackends = $backends;
286                                 return true;
287                         } else {
288                                 return false;
289                         }
290                 } else {
291                         return false;
292                 }
293         }
294
295         /**
296          * Unregister a storage backend class
297          *
298          * @param string $class Backend class name
299          *
300          * @return boolean True, if unregistering was successful
301          *
302          * @throws Storage\StorageException
303          */
304         public function unregister(string $class): bool
305         {
306                 if (is_subclass_of($class, Storage\IStorage::class)) {
307                         /** @var Storage\IStorage $class */
308
309                         if ($this->currentBackend::getName() == $class::getName()) {
310                                 throw new Storage\StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName()));
311                         }
312
313                         $key = array_search($class::getName(), $this->validBackends);
314
315                         if ($key !== false) {
316                                 $backends = $this->validBackends;
317                                 unset($backends[$key]);
318                                 $backends = array_values($backends);
319                                 if ($this->config->set('storage', 'backends', $backends)) {
320                                         $this->validBackends = $backends;
321                                         return true;
322                                 } else {
323                                         return false;
324                                 }
325                         } else {
326                                 return true;
327                         }
328                 } else {
329                         return false;
330                 }
331         }
332
333         /**
334          * Move up to 5000 resources to storage $dest
335          *
336          * Copy existing data to destination storage and delete from source.
337          * This method cannot move to legacy in-table `data` field.
338          *
339          * @param Storage\IWritableStorage $destination Destination storage class name
340          * @param array                    $tables      Tables to look in for resources. Optional, defaults to ['photo', 'attach']
341          * @param int                      $limit       Limit of the process batch size, defaults to 5000
342          *
343          * @return int Number of moved resources
344          * @throws Storage\StorageException
345          * @throws Exception
346          */
347         public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
348         {
349                 if (!$this->isValidBackend($destination, $this->validBackends)) {
350                         throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
351                 }
352
353                 $moved = 0;
354                 foreach ($tables as $table) {
355                         // Get the rows where backend class is not the destination backend class
356                         $resources = $this->dba->select(
357                                 $table,
358                                 ['id', 'data', 'backend-class', 'backend-ref'],
359                                 ['`backend-class` IS NULL or `backend-class` != ?', $destination::getName()],
360                                 ['limit' => $limit]
361                         );
362
363                         while ($resource = $this->dba->fetch($resources)) {
364                                 $id        = $resource['id'];
365                                 $sourceRef = $resource['backend-ref'];
366                                 $source    = null;
367
368                                 try {
369                                         $source = $this->getWritableStorageByName($resource['backend-class'] ?? '');
370                                         $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
371                                         $data = $source->get($sourceRef);
372                                 } catch (Storage\InvalidClassStorageException $exception) {
373                                         $this->logger->info('Get data from DB resource field.', ['oldReference' => $sourceRef]);
374                                         $data = $resource['data'];
375                                 } catch (Storage\ReferenceStorageException $exception) {
376                                         $this->logger->info('Invalid source reference.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
377                                         continue;
378                                 }
379
380                                 $this->logger->info('Save data to new backend.', ['newBackend' => $destination::getName()]);
381                                 $destinationRef = $destination->put($data);
382                                 $this->logger->info('Saved data.', ['newReference' => $destinationRef]);
383
384                                 if ($destinationRef !== '') {
385                                         $this->logger->info('update row');
386                                         if ($this->dba->update($table, ['backend-class' => $destination::getName(), 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
387                                                 if (!empty($source)) {
388                                                         $this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
389                                                         $source->delete($sourceRef);
390                                                 }
391                                                 $moved++;
392                                         }
393                                 }
394                         }
395
396                         $this->dba->close($resources);
397                 }
398
399                 return $moved;
400         }
401 }