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