]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
e1bab23acab70db1ff2da955949ce7ad759c6281
[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
8
9
10 /**
11  * @brief Manage storage backends
12  *
13  * Core code uses this class to get and set current storage backend class.
14  * Addons use this class to register and unregister additional backends.
15  */
16 class StorageManager
17 {
18         private static $default_backends = [
19                 'Filesystem' => Friendica\Model\Storage\Filesystem::class,
20                 'Database' => Friendica\Model\Storage\Database::class,
21         ];
22
23         private static $backends = [];
24
25         private static function setup()
26         {
27                 if (count(self::$backends)==0) {
28                         self::$backends = Config::get('storage', 'backends', self::$default_backends);
29                 }
30         }
31
32         /**
33          * @brief Return current storage backend class
34          * @return string
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          */
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         /**
78          * @brief Register a storage backend class
79          *
80          * @param string  $name   User readable backend name
81          * @param string  $class  Backend class name
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          */
97         public static function unregister($class)
98         {
99                 self::setup();
100                 unset(self::$backends[$name]);
101                 Config::set('storage', 'backends', self::$backends);
102         }
103
104
105         /**
106          * @brief Move resources to storage $dest
107          *
108          * @param string  $dest    Destination storage class name
109          * @param array   $tables  Tables to look in for resources. Optional, defaults to ['photo']
110          *
111          * @retur int Number of moved resources
112          */
113         public static function move($dest, $tables = null)
114         {
115                 if (is_null($tables)) {
116                         $tables = ['photo'];
117                 }
118
119                 $moved = 0;
120                 foreach ($tables as $table) {
121                         $rr = DBA::select($table, ['id', 'data', 'backend-class', 'backend-ref'], ['`backend-class` != ?', $dest]);
122                         if (DBA::isResult($rr)) {
123                                 while($r = $rr->fetch()) {
124                                         $id = $r['id'];
125                                         $data = $r['data'];
126                                         $backendClass = $r['backend-class'];
127                                         $backendRef = $r['backend-ref'];
128                                         if ($backendClass !== '') {
129                                                 $data = $backendClass::get($backendRef);
130                                         }
131                                         $ref = $dest::put($data);
132
133                                         if ($ref !== '') {
134                                                 $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
135                                                 if ($ru) {
136                                                         if ($backendClass !== '') {
137                                                                 $backendClass::delete($backendRef);
138                                                         }
139                                                         $moved++;
140                                                 }
141                                         }
142                                 }
143                         }
144                 }
145
146                 return $moved;
147         }
148 }