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