]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
43a72650cc821b967967077d4233e07a79c26daf
[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          */
37         public static function getBackend()
38         {
39                 return Config::get('storage', 'class', '');
40         }
41
42         /**
43          * @brief Return storage backend class by registered name
44          *
45          * @param string  $name  Backend name
46          * @return string Empty if no backend registered at $name exists
47          */
48         public static function getByName($name)
49         {
50                 self::setup();
51                 return defaults(self::$backends, $name, '');
52         }
53
54         /**
55          * @brief Set current storage backend class
56          *
57          * @param string  $class  Backend class name
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         /**
79          * @brief Register a storage backend class
80          *
81          * @param string  $name   User readable backend name
82          * @param string  $class  Backend class name
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          */
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          * @retur int Number of moved resources
116          */
117         public static function move($dest, $tables = null)
118         {
119                 if (is_null($dest) || empty($dest)) {
120                         throw Exception('Can\'t move to NULL storage backend');
121                 }
122                 
123                 if (is_null($tables)) {
124                         $tables = ['photo', 'attach'];
125                 }
126
127                 $moved = 0;
128                 foreach ($tables as $table) {
129                         // Get the rows where backend class is not the destination backend class
130                         $rr = DBA::select(
131                                 $table, 
132                                 ['id', 'data', 'backend-class', 'backend-ref'],
133                                 ['`backend-class` IS NULL or `backend-class` != ?' , $dest ]
134                         );
135
136                         if (DBA::isResult($rr)) {
137                                 while($r = DBA::fetch($rr)) {
138                                         $id = $r['id'];
139                                         $data = $r['data'];
140                                         $backendClass = $r['backend-class'];
141                                         $backendRef = $r['backend-ref'];
142                                         if (!is_null($backendClass) && $backendClass !== '') {
143                                                 Logger::log("get data from old backend " .  $backendClass . " : " . $backendRef);
144                                                 $data = $backendClass::get($backendRef);
145                                         }
146                                         
147                                         Logger::log("save data to new backend " . $dest);
148                                         $ref = $dest::put($data);
149                                         Logger::log("saved data as " . $ref);
150
151                                         if ($ref !== '') {
152                                                 Logger::log("update row");
153                                                 $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
154                                                 
155                                                 if ($ru) {
156                                                         if (!is_null($backendClass) && $backendClass !== '') {
157                                                                 Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
158                                                                 $backendClass::delete($backendRef);
159                                                         }
160                                                         $moved++;
161                                                 }
162                                         }
163                                 }
164                         }
165                 }
166
167                 return $moved;
168         }
169 }
170