add status code to System::externalRedirect
[friendica.git/.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          *
57          * @param string $class Backend class name
58          * @return bool
59          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60          */
61         public static function setBackend($class)
62         {
63                 if (!in_array('Friendica\Model\Storage\IStorage', class_implements($class))) {
64                         return false;
65                 }
66
67                 Config::set('storage', 'class', $class);
68
69                 return true;
70         }
71
72         /**
73          * @brief Get registered backends
74          *
75          * @return array
76          */
77         public static function listBackends()
78         {
79                 self::setup();
80                 return self::$backends;
81         }
82
83
84         /**
85          * @brief Register a storage backend class
86          *
87          * @param string $name  User readable backend name
88          * @param string $class Backend class name
89          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
90          */
91         public static function register($name, $class)
92         {
93                 /// @todo Check that $class implements IStorage
94                 self::setup();
95                 self::$backends[$name] = $class;
96                 Config::set('storage', 'backends', self::$backends);
97         }
98
99
100         /**
101          * @brief Unregister a storage backend class
102          *
103          * @param string $name User readable backend name
104          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
105          */
106         public static function unregister($name)
107         {
108                 self::setup();
109                 unset(self::$backends[$name]);
110                 Config::set('storage', 'backends', self::$backends);
111         }
112
113
114         /**
115          * @brief Move up to 5000 resources to storage $dest
116          *
117          * Copy existing data to destination storage and delete from source.
118          * This method cannot move to legacy in-table `data` field.
119          *
120          * @param string     $destination Storage class name
121          * @param array|null $tables      Tables to look in for resources. Optional, defaults to ['photo', 'attach']
122          * @param int        $limit       Limit of the process batch size, defaults to 5000
123          * @return int Number of moved resources
124          * @throws \Exception
125          */
126         public static function move($destination, $tables = null, $limit = 5000)
127         {
128                 if (empty($destination)) {
129                         throw new \Exception('Can\'t move to NULL storage backend');
130                 }
131                 
132                 if (is_null($tables)) {
133                         $tables = ['photo', 'attach'];
134                 }
135
136                 $moved = 0;
137                 foreach ($tables as $table) {
138                         // Get the rows where backend class is not the destination backend class
139                         $resources = DBA::select(
140                                 $table, 
141                                 ['id', 'data', 'backend-class', 'backend-ref'],
142                                 ['`backend-class` IS NULL or `backend-class` != ?', $destination],
143                                 ['limit' => $limit]
144                         );
145
146                         while ($resource = DBA::fetch($resources)) {
147                                 $id = $resource['id'];
148                                 $data = $resource['data'];
149                                 /** @var IStorage $backendClass */
150                                 $backendClass = $resource['backend-class'];
151                                 $backendRef = $resource['backend-ref'];
152                                 if (!empty($backendClass)) {
153                                         Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
154                                         $data = $backendClass::get($backendRef);
155                                 }
156
157                                 Logger::log("save data to new backend " . $destination);
158                                 /** @var IStorage $destination */
159                                 $ref = $destination::put($data);
160                                 Logger::log("saved data as " . $ref);
161
162                                 if ($ref !== '') {
163                                         Logger::log("update row");
164                                         if (DBA::update($table, ['backend-class' => $destination, 'backend-ref' => $ref, 'data' => ''], ['id' => $id])) {
165                                                 if (!empty($backendClass)) {
166                                                         Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
167                                                         $backendClass::delete($backendRef);
168                                                 }
169                                                 $moved++;
170                                         }
171                                 }
172                         }
173
174                         DBA::close($resources);
175                 }
176
177                 return $moved;
178         }
179 }
180