]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
4b74035ee8635465a60757c12a418fd7ac187f4d
[friendica.git] / src / Core / StorageManager.php
1 <?php
2
3 namespace Friendica\Core;
4
5 use Friendica\Database\DBA;
6 use Friendica\Model\Storage\IStorage;
7
8
9 /**
10  * @brief Manage storage backends
11  *
12  * Core code uses this class to get and set current storage backend class.
13  * Addons use this class to register and unregister additional backends.
14  */
15 class StorageManager
16 {
17         private static $default_backends = [
18                 'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
19                 'Database' => \Friendica\Model\Storage\Database::class,
20         ];
21
22         private static $backends = [];
23
24         private static function setup()
25         {
26                 if (count(self::$backends) == 0) {
27                         self::$backends = Config::get('storage', 'backends', self::$default_backends);
28                 }
29         }
30
31         /**
32          * @brief Return current storage backend class 
33          * 
34          * @return string
35          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
36          */
37         public static function getBackend()
38         {
39                 return Config::get('storage', 'class', '');
40         }
41
42         /**
43          * @brief Return storage backend class by registered name
44          *
45          * @param string  $name  Backend name
46          * @return string Empty if no backend registered at $name exists
47          */
48         public static function getByName($name)
49         {
50                 self::setup();
51                 return defaults(self::$backends, $name, '');
52         }
53
54         /**
55          * @brief Set current storage backend class
56          * If $class is an empty string, legacy db storage is used.
57          *
58          * @param string $class Backend class name
59          * @return bool
60          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
61          */
62         public static function setBackend($class)
63         {
64                 if ($class !== "" && !in_array('Friendica\Model\Storage\IStorage', class_implements($class))) {
65                         return false;
66                 }
67
68                 Config::set('storage', 'class', $class);
69
70                 return true;
71         }
72
73         /**
74          * @brief Get registered backends
75          *
76          * @return array
77          */
78         public static function listBackends()
79         {
80                 self::setup();
81                 return self::$backends;
82         }
83
84
85         /**
86          * @brief Register a storage backend class
87          *
88          * @param string $name  User readable backend name
89          * @param string $class Backend class name
90          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
91          */
92         public static function register($name, $class)
93         {
94                 /// @todo Check that $class implements IStorage
95                 self::setup();
96                 self::$backends[$name] = $class;
97                 Config::set('storage', 'backends', self::$backends);
98         }
99
100
101         /**
102          * @brief Unregister a storage backend class
103          *
104          * @param string $name User readable backend name
105          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
106          */
107         public static function unregister($name)
108         {
109                 self::setup();
110                 unset(self::$backends[$name]);
111                 Config::set('storage', 'backends', self::$backends);
112         }
113
114
115         /**
116          * @brief Move up to 5000 resources to storage $dest
117          *
118          * Copy existing data to destination storage and delete from source.
119          * This method cannot move to legacy in-table `data` field.
120          *
121          * @param string     $destination Storage class name
122          * @param array|null $tables      Tables to look in for resources. Optional, defaults to ['photo', 'attach']
123          * @param int        $limit       Limit of the process batch size, defaults to 5000
124          * @return int Number of moved resources
125          * @throws \Exception
126          */
127         public static function move($destination, $tables = null, $limit = 5000)
128         {
129                 if (empty($destination)) {
130                         throw new \Exception('Can\'t move to NULL storage backend');
131                 }
132                 
133                 if (is_null($tables)) {
134                         $tables = ['photo', 'attach'];
135                 }
136
137                 $moved = 0;
138                 foreach ($tables as $table) {
139                         // Get the rows where backend class is not the destination backend class
140                         $resources = DBA::select(
141                                 $table, 
142                                 ['id', 'data', 'backend-class', 'backend-ref'],
143                                 ['`backend-class` IS NULL or `backend-class` != ?', $destination],
144                                 ['limit' => $limit]
145                         );
146
147                         while ($resource = DBA::fetch($resources)) {
148                                 $id = $resource['id'];
149                                 $data = $resource['data'];
150                                 /** @var IStorage $backendClass */
151                                 $backendClass = $resource['backend-class'];
152                                 $backendRef = $resource['backend-ref'];
153                                 if (!empty($backendClass)) {
154                                         Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
155                                         $data = $backendClass::get($backendRef);
156                                 }
157
158                                 Logger::log("save data to new backend " . $destination);
159                                 /** @var IStorage $destination */
160                                 $ref = $destination::put($data);
161                                 Logger::log("saved data as " . $ref);
162
163                                 if ($ref !== '') {
164                                         Logger::log("update row");
165                                         if (DBA::update($table, ['backend-class' => $destination, 'backend-ref' => $ref, 'data' => ''], ['id' => $id])) {
166                                                 if (!empty($backendClass)) {
167                                                         Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
168                                                         $backendClass::delete($backendRef);
169                                                 }
170                                                 $moved++;
171                                         }
172                                 }
173                         }
174
175                         DBA::close($resources);
176                 }
177
178                 return $moved;
179         }
180 }
181