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 defaults(self::$backends, $name, '');
55 * @brief Set current storage backend class
56 * If $class is an empty string, legacy db storage is used.
58 * @param string $class Backend class name
60 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
62 public static function setBackend($class)
64 if ($class !== "" && !in_array('Friendica\Model\Storage\IStorage', class_implements($class))) {
68 Config::set('storage', 'class', $class);
74 * @brief Get registered backends
78 public static function listBackends()
81 return self::$backends;
86 * @brief Register a storage backend class
88 * @param string $name User readable backend name
89 * @param string $class Backend class name
90 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
92 public static function register($name, $class)
94 /// @todo Check that $class implements IStorage
96 self::$backends[$name] = $class;
97 Config::set('storage', 'backends', self::$backends);
102 * @brief Unregister a storage backend class
104 * @param string $name User readable backend name
105 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
107 public static function unregister($name)
110 unset(self::$backends[$name]);
111 Config::set('storage', 'backends', self::$backends);
116 * @brief Move up to 5000 resources to storage $dest
118 * Copy existing data to destination storage and delete from source.
119 * This method cannot move to legacy in-table `data` field.
121 * @param string $destination Storage class name
122 * @param array|null $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
123 * @param int $limit Limit of the process batch size, defaults to 5000
124 * @return int Number of moved resources
127 public static function move($destination, $tables = null, $limit = 5000)
129 if (empty($destination)) {
130 throw new \Exception('Can\'t move to NULL storage backend');
133 if (is_null($tables)) {
134 $tables = ['photo', 'attach'];
138 foreach ($tables as $table) {
139 // Get the rows where backend class is not the destination backend class
140 $resources = DBA::select(
142 ['id', 'data', 'backend-class', 'backend-ref'],
143 ['`backend-class` IS NULL or `backend-class` != ?', $destination],
147 while ($resource = DBA::fetch($resources)) {
148 $id = $resource['id'];
149 $data = $resource['data'];
150 /** @var IStorage $backendClass */
151 $backendClass = $resource['backend-class'];
152 $backendRef = $resource['backend-ref'];
153 if (!empty($backendClass)) {
154 Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
155 $data = $backendClass::get($backendRef);
158 Logger::log("save data to new backend " . $destination);
159 /** @var IStorage $destination */
160 $ref = $destination::put($data);
161 Logger::log("saved data as " . $ref);
164 Logger::log("update row");
165 if (DBA::update($table, ['backend-class' => $destination, 'backend-ref' => $ref, 'data' => ''], ['id' => $id])) {
166 if (!empty($backendClass)) {
167 Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
168 $backendClass::delete($backendRef);
175 DBA::close($resources);