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