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