3 namespace Friendica\Core;
5 use Friendica\Database\DBA;
6 use Friendica\Model\Storage\IStorage;
10 * @brief Manage storage backends
12 * Core code uses this class to get and set current storage backend class.
13 * Addons use this class to register and unregister additional backends.
17 private static $default_backends = [
18 'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
19 'Database' => \Friendica\Model\Storage\Database::class,
22 private static $backends = [];
24 private static function setup()
26 if (count(self::$backends) == 0) {
27 self::$backends = Config::get('storage', 'backends', self::$default_backends);
32 * @brief Return current storage backend class
34 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
36 public static function getBackend()
38 return Config::get('storage', 'class', '');
42 * @brief Return storage backend class by registered name
44 * @param string $name Backend name
45 * @return string Empty if no backend registered at $name exists
47 public static function getByName($name)
50 return defaults(self::$backends, $name, '');
54 * @brief Set current storage backend class
56 * @param string $class Backend class name
57 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
59 public static function setBackend($class)
61 /// @todo Check that $class implements IStorage
62 Config::set('storage', 'class', $class);
66 * @brief Get registered backends
70 public static function listBackends()
73 return self::$backends;
78 * @brief Register a storage backend class
80 * @param string $name User readable backend name
81 * @param string $class Backend class name
82 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
84 public static function register($name, $class)
86 /// @todo Check that $class implements IStorage
88 self::$backends[$name] = $class;
89 Config::set('storage', 'backends', self::$backends);
94 * @brief Unregister a storage backend class
96 * @param string $name User readable backend name
97 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
99 public static function unregister($name)
102 unset(self::$backends[$name]);
103 Config::set('storage', 'backends', self::$backends);
108 * @brief Move resources to storage $dest
110 * Copy existing data to destination storage and delete from source.
111 * This method cannot move to legacy in-table `data` field.
113 * @param string $dest Destination storage class name
114 * @param array|null $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
117 * @return int Number of moved resources
119 public static function move($dest, $tables = null)
121 if (is_null($dest) || empty($dest)) {
122 throw new \Exception('Can\'t move to NULL storage backend');
125 if (is_null($tables)) {
126 $tables = ['photo', 'attach'];
130 foreach ($tables as $table) {
131 // Get the rows where backend class is not the destination backend class
134 ['id', 'data', 'backend-class', 'backend-ref'],
135 ['`backend-class` IS NULL or `backend-class` != ?' , $dest ]
138 if (DBA::isResult($rr)) {
139 while($r = DBA::fetch($rr)) {
142 /** @var IStorage $backendClass */
143 $backendClass = $r['backend-class'];
144 $backendRef = $r['backend-ref'];
145 if (!is_null($backendClass) && $backendClass !== '') {
146 Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
147 $data = $backendClass::get($backendRef);
150 Logger::log("save data to new backend " . $dest);
151 /** @var IStorage $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);