3 namespace Friendica\Core;
5 use Friendica\Database\DBA;
9 * @brief Manage storage backends
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.
16 private static $default_backends = [
17 'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
18 'Database' => \Friendica\Model\Storage\Database::class,
21 private static $backends = [];
23 private static function setup()
25 if (count(self::$backends)==0) {
26 self::$backends = Config::get('storage', 'backends', self::$default_backends);
31 * @brief Return current storage backend class
33 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
35 public static function getBackend()
37 return Config::get('storage', 'class', '');
41 * @brief Return storage backend class by registered name
43 * @param string $name Backend name
44 * @return string Empty if no backend registered at $name exists
46 public static function getByName($name)
49 return defaults(self::$backends, $name, '');
53 * @brief Set current storage backend class
55 * @param string $class Backend class name
56 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
58 public static function setBackend($class)
60 /// @todo Check that $class implements IStorage
61 Config::set('storage', 'class', $class);
65 * @brief Get registered backends
69 public static function listBackends()
72 return self::$backends;
77 * @brief Register a storage backend class
79 * @param string $name User readable backend name
80 * @param string $class Backend class name
81 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
83 public static function register($name, $class)
85 /// @todo Check that $class implements IStorage
87 self::$backends[$name] = $class;
88 Config::set('storage', 'backends', self::$backends);
93 * @brief Unregister a storage backend class
95 * @param string $name User readable backend name
96 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
98 public static function unregister($name)
101 unset(self::$backends[$name]);
102 Config::set('storage', 'backends', self::$backends);
107 * @brief Move resources to storage $dest
109 * Copy existing data to destination storage and delete from source.
110 * This method cannot move to legacy in-table `data` field.
112 * @param string $dest Destination storage class name
113 * @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
116 * @return int Number of moved resources
118 public static function move($dest, $tables = null)
120 if (is_null($dest) || empty($dest)) {
121 throw Exception('Can\'t move to NULL storage backend');
124 if (is_null($tables)) {
125 $tables = ['photo', 'attach'];
129 foreach ($tables as $table) {
130 // Get the rows where backend class is not the destination backend class
133 ['id', 'data', 'backend-class', 'backend-ref'],
134 ['`backend-class` IS NULL or `backend-class` != ?' , $dest ]
137 if (DBA::isResult($rr)) {
138 while($r = DBA::fetch($rr)) {
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);
148 Logger::log("save data to new backend " . $dest);
149 $ref = $dest::put($data);
150 Logger::log("saved data as " . $ref);
153 Logger::log("update row");
154 $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
157 if (!is_null($backendClass) && $backendClass !== '') {
158 Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
159 $backendClass::delete($backendRef);