3 namespace Friendica\Core;
5 use Friendica\Database\DBA;
6 use Friendica\Core\Config;
7 use Friendica\Core\Logger;
12 * @brief Manage storage backends
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.
19 private static $default_backends = [
20 'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
21 'Database' => \Friendica\Model\Storage\Database::class,
24 private static $backends = [];
26 private static function setup()
28 if (count(self::$backends)==0) {
29 self::$backends = Config::get('storage', 'backends', self::$default_backends);
34 * @brief Return current storage backend class
36 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38 public static function getBackend()
40 return Config::get('storage', 'class', '');
44 * @brief Return storage backend class by registered name
46 * @param string $name Backend name
47 * @return string Empty if no backend registered at $name exists
49 public static function getByName($name)
52 return defaults(self::$backends, $name, '');
56 * @brief Set current storage backend class
58 * @param string $class Backend class name
59 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
61 public static function setBackend($class)
63 /// @todo Check that $class implements IStorage
64 Config::set('storage', 'class', $class);
68 * @brief Get registered backends
72 public static function listBackends()
75 return self::$backends;
80 * @brief Register a storage backend class
82 * @param string $name User readable backend name
83 * @param string $class Backend class name
84 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
86 public static function register($name, $class)
88 /// @todo Check that $class implements IStorage
90 self::$backends[$name] = $class;
91 Config::set('storage', 'backends', self::$backends);
96 * @brief Unregister a storage backend class
98 * @param string $name User readable backend name
99 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
101 public static function unregister($name)
104 unset(self::$backends[$name]);
105 Config::set('storage', 'backends', self::$backends);
110 * @brief Move resources to storage $dest
112 * Copy existing data to destination storage and delete from source.
113 * This method cannot move to legacy in-table `data` field.
115 * @param string $dest Destination storage class name
116 * @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
119 * @return int Number of moved resources
121 public static function move($dest, $tables = null)
123 if (is_null($dest) || empty($dest)) {
124 throw Exception('Can\'t move to NULL storage backend');
127 if (is_null($tables)) {
128 $tables = ['photo', 'attach'];
132 foreach ($tables as $table) {
133 // Get the rows where backend class is not the destination backend class
136 ['id', 'data', 'backend-class', 'backend-ref'],
137 ['`backend-class` IS NULL or `backend-class` != ?' , $dest ]
140 if (DBA::isResult($rr)) {
141 while($r = DBA::fetch($rr)) {
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);
151 Logger::log("save data to new backend " . $dest);
152 $ref = $dest::put($data);
153 Logger::log("saved data as " . $ref);
156 Logger::log("update row");
157 $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
160 if (!is_null($backendClass) && $backendClass !== '') {
161 Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
162 $backendClass::delete($backendRef);