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