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
35 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
37 public static function getBackend()
39 return Config::get('storage', 'class', '');
43 * @brief Return storage backend class by registered name
45 * @param string $name Backend name
46 * @return string Empty if no backend registered at $name exists
48 public static function getByName($name)
51 return self::$backends[$name] ?? '';
55 * @brief Set current storage backend class
57 * @param string $class Backend class name
59 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
61 public static function setBackend($class)
63 if (!in_array('Friendica\Model\Storage\IStorage', class_implements($class))) {
67 Config::set('storage', 'class', $class);
73 * @brief Get registered backends
77 public static function listBackends()
80 return self::$backends;
85 * @brief Register a storage backend class
87 * @param string $name User readable backend name
88 * @param string $class Backend class name
89 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
91 public static function register($name, $class)
93 /// @todo Check that $class implements IStorage
95 self::$backends[$name] = $class;
96 Config::set('storage', 'backends', self::$backends);
101 * @brief Unregister a storage backend class
103 * @param string $name User readable backend name
104 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
106 public static function unregister($name)
109 unset(self::$backends[$name]);
110 Config::set('storage', 'backends', self::$backends);
115 * @brief Move up to 5000 resources to storage $dest
117 * Copy existing data to destination storage and delete from source.
118 * This method cannot move to legacy in-table `data` field.
120 * @param string $destination Storage class name
121 * @param array|null $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
122 * @param int $limit Limit of the process batch size, defaults to 5000
123 * @return int Number of moved resources
126 public static function move($destination, $tables = null, $limit = 5000)
128 if (empty($destination)) {
129 throw new \Exception('Can\'t move to NULL storage backend');
132 if (is_null($tables)) {
133 $tables = ['photo', 'attach'];
137 foreach ($tables as $table) {
138 // Get the rows where backend class is not the destination backend class
139 $resources = DBA::select(
141 ['id', 'data', 'backend-class', 'backend-ref'],
142 ['`backend-class` IS NULL or `backend-class` != ?', $destination],
146 while ($resource = DBA::fetch($resources)) {
147 $id = $resource['id'];
148 $data = $resource['data'];
149 /** @var IStorage $backendClass */
150 $backendClass = $resource['backend-class'];
151 $backendRef = $resource['backend-ref'];
152 if (!empty($backendClass)) {
153 Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
154 $data = $backendClass::get($backendRef);
157 Logger::log("save data to new backend " . $destination);
158 /** @var IStorage $destination */
159 $ref = $destination::put($data);
160 Logger::log("saved data as " . $ref);
163 Logger::log("update row");
164 if (DBA::update($table, ['backend-class' => $destination, 'backend-ref' => $ref, 'data' => ''], ['id' => $id])) {
165 if (!empty($backendClass)) {
166 Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
167 $backendClass::delete($backendRef);
174 DBA::close($resources);