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