]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
3bb1cc4511161960dcbc47aec33634eba7c8895f
[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 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     $dest    Destination storage class name
120          * @param array|null $tables  Tables to look in for resources. Optional, defaults to ['photo', 'attach']
121          *
122          * @throws \Exception
123          * @return int Number of moved resources
124          */
125         public static function move($dest, $tables = null)
126         {
127                 if (is_null($dest) || empty($dest)) {
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                         $rr = DBA::select(
139                                 $table, 
140                                 ['id', 'data', 'backend-class', 'backend-ref'],
141                                 ['`backend-class` IS NULL or `backend-class` != ?' , $dest ]
142                         );
143
144                         if (DBA::isResult($rr)) {
145                                 while($r = DBA::fetch($rr)) {
146                                         $id = $r['id'];
147                                         $data = $r['data'];
148                                         /** @var IStorage $backendClass */
149                                         $backendClass = $r['backend-class'];
150                                         $backendRef = $r['backend-ref'];
151                                         if (!is_null($backendClass) && $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 " . $dest);
157                                         /** @var IStorage $dest */
158                                         $ref = $dest::put($data);
159                                         Logger::log("saved data as " . $ref);
160
161                                         if ($ref !== '') {
162                                                 Logger::log("update row");
163                                                 $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
164                                                 
165                                                 if ($ru) {
166                                                         if (!is_null($backendClass) && $backendClass !== '') {
167                                                                 Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
168                                                                 $backendClass::delete($backendRef);
169                                                         }
170                                                         $moved++;
171                                                 }
172                                         }
173                                 }
174                         }
175                 }
176
177                 return $moved;
178         }
179 }
180