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
58 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60 public static function setBackend($class)
62 if (!in_array('Friendica\Model\Storage\IStorage', class_implements($class))) {
66 Config::set('storage', 'class', $class);
72 * @brief Get registered backends
76 public static function listBackends()
79 return self::$backends;
84 * @brief Register a storage backend class
86 * @param string $name User readable backend name
87 * @param string $class Backend class name
88 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
90 public static function register($name, $class)
92 /// @todo Check that $class implements IStorage
94 self::$backends[$name] = $class;
95 Config::set('storage', 'backends', self::$backends);
100 * @brief Unregister a storage backend class
102 * @param string $name User readable backend name
103 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
105 public static function unregister($name)
108 unset(self::$backends[$name]);
109 Config::set('storage', 'backends', self::$backends);
114 * @brief Move up to 5000 resources to storage $dest
116 * Copy existing data to destination storage and delete from source.
117 * This method cannot move to legacy in-table `data` field.
119 * @param string $destination Storage class name
120 * @param array|null $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
121 * @param int $limit Limit of the process batch size, defaults to 5000
122 * @return int Number of moved resources
125 public static function move($destination, $tables = null, $limit = 5000)
127 if (empty($destination)) {
128 throw new \Exception('Can\'t move to NULL storage backend');
131 if (is_null($tables)) {
132 $tables = ['photo', 'attach'];
136 foreach ($tables as $table) {
137 // Get the rows where backend class is not the destination backend class
138 $resources = DBA::select(
140 ['id', 'data', 'backend-class', 'backend-ref'],
141 ['`backend-class` IS NULL or `backend-class` != ?', $destination],
145 while ($resource = DBA::fetch($resources)) {
146 $id = $resource['id'];
147 $data = $resource['data'];
148 /** @var IStorage $backendClass */
149 $backendClass = $resource['backend-class'];
150 $backendRef = $resource['backend-ref'];
151 if (!empty($backendClass)) {
152 Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
153 $data = $backendClass::get($backendRef);
156 Logger::log("save data to new backend " . $destination);
157 /** @var IStorage $destination */
158 $ref = $destination::put($data);
159 Logger::log("saved data as " . $ref);
162 Logger::log("update row");
163 if (DBA::update($table, ['backend-class' => $destination, 'backend-ref' => $ref, 'data' => ''], ['id' => $id])) {
164 if (!empty($backendClass)) {
165 Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
166 $backendClass::delete($backendRef);
173 DBA::close($resources);