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