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