2018-11-29 09:27:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2018-12-01 17:44:54 +01:00
|
|
|
use Friendica\Database\DBA;
|
2018-11-29 09:27:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Manage storage backends
|
|
|
|
*
|
|
|
|
* Core code uses this class to get and set current storage backend class.
|
|
|
|
* Addons use this class to register and unregister additional backends.
|
|
|
|
*/
|
|
|
|
class StorageManager
|
|
|
|
{
|
2018-11-29 12:57:57 +01:00
|
|
|
private static $default_backends = [
|
2018-12-14 18:03:16 +01:00
|
|
|
'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
|
|
|
|
'Database' => \Friendica\Model\Storage\Database::class,
|
2018-11-29 09:27:04 +01:00
|
|
|
];
|
|
|
|
|
2018-11-29 12:57:57 +01:00
|
|
|
private static $backends = [];
|
2018-11-29 09:27:04 +01:00
|
|
|
|
|
|
|
private static function setup()
|
|
|
|
{
|
2018-11-29 12:57:57 +01:00
|
|
|
if (count(self::$backends)==0) {
|
|
|
|
self::$backends = Config::get('storage', 'backends', self::$default_backends);
|
2018-11-29 09:27:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return current storage backend class
|
|
|
|
* @return string
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 09:27:04 +01:00
|
|
|
*/
|
|
|
|
public static function getBackend()
|
|
|
|
{
|
|
|
|
return Config::get('storage', 'class', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return storage backend class by registered name
|
|
|
|
*
|
|
|
|
* @param string $name Backend name
|
|
|
|
* @return string Empty if no backend registered at $name exists
|
|
|
|
*/
|
|
|
|
public static function getByName($name)
|
|
|
|
{
|
|
|
|
self::setup();
|
2018-11-29 12:57:57 +01:00
|
|
|
return defaults(self::$backends, $name, '');
|
2018-11-29 09:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set current storage backend class
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param string $class Backend class name
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 09:27:04 +01:00
|
|
|
*/
|
|
|
|
public static function setBackend($class)
|
|
|
|
{
|
|
|
|
/// @todo Check that $class implements IStorage
|
|
|
|
Config::set('storage', 'class', $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get registered backends
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function listBackends()
|
|
|
|
{
|
|
|
|
self::setup();
|
|
|
|
return self::$backends;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Register a storage backend class
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param string $name User readable backend name
|
|
|
|
* @param string $class Backend class name
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 09:27:04 +01:00
|
|
|
*/
|
|
|
|
public static function register($name, $class)
|
|
|
|
{
|
|
|
|
/// @todo Check that $class implements IStorage
|
|
|
|
self::setup();
|
2018-11-29 12:57:57 +01:00
|
|
|
self::$backends[$name] = $class;
|
|
|
|
Config::set('storage', 'backends', self::$backends);
|
2018-11-29 09:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unregister a storage backend class
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param string $name User readable backend name
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 09:27:04 +01:00
|
|
|
*/
|
2018-12-12 20:35:18 +01:00
|
|
|
public static function unregister($name)
|
2018-11-29 09:27:04 +01:00
|
|
|
{
|
|
|
|
self::setup();
|
2018-11-29 12:57:57 +01:00
|
|
|
unset(self::$backends[$name]);
|
|
|
|
Config::set('storage', 'backends', self::$backends);
|
2018-11-29 09:27:04 +01:00
|
|
|
}
|
2018-12-01 17:44:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Move resources to storage $dest
|
|
|
|
*
|
2018-12-12 17:50:34 +01:00
|
|
|
* Copy existing data to destination storage and delete from source.
|
|
|
|
* This method cannot move to legacy in-table `data` field.
|
|
|
|
*
|
2018-12-01 17:44:54 +01:00
|
|
|
* @param string $dest Destination storage class name
|
2019-01-03 22:35:46 +01:00
|
|
|
* @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
|
2018-12-01 17:44:54 +01:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
|
|
|
* @return int Number of moved resources
|
2018-12-01 17:44:54 +01:00
|
|
|
*/
|
2018-12-12 20:35:18 +01:00
|
|
|
public static function move($dest, $tables = null)
|
2018-12-01 17:44:54 +01:00
|
|
|
{
|
2018-12-12 17:50:34 +01:00
|
|
|
if (is_null($dest) || empty($dest)) {
|
|
|
|
throw Exception('Can\'t move to NULL storage backend');
|
|
|
|
}
|
|
|
|
|
2018-12-01 17:44:54 +01:00
|
|
|
if (is_null($tables)) {
|
2019-01-03 22:35:46 +01:00
|
|
|
$tables = ['photo', 'attach'];
|
2018-12-01 17:44:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$moved = 0;
|
|
|
|
foreach ($tables as $table) {
|
2018-12-12 17:50:34 +01:00
|
|
|
// Get the rows where backend class is not the destination backend class
|
|
|
|
$rr = DBA::select(
|
|
|
|
$table,
|
|
|
|
['id', 'data', 'backend-class', 'backend-ref'],
|
|
|
|
['`backend-class` IS NULL or `backend-class` != ?' , $dest ]
|
|
|
|
);
|
|
|
|
|
2018-12-01 17:44:54 +01:00
|
|
|
if (DBA::isResult($rr)) {
|
2018-12-18 16:59:40 +01:00
|
|
|
while($r = DBA::fetch($rr)) {
|
2018-12-01 17:44:54 +01:00
|
|
|
$id = $r['id'];
|
|
|
|
$data = $r['data'];
|
|
|
|
$backendClass = $r['backend-class'];
|
|
|
|
$backendRef = $r['backend-ref'];
|
2018-12-12 17:50:34 +01:00
|
|
|
if (!is_null($backendClass) && $backendClass !== '') {
|
|
|
|
Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
|
2018-12-01 17:44:54 +01:00
|
|
|
$data = $backendClass::get($backendRef);
|
|
|
|
}
|
2018-12-12 17:50:34 +01:00
|
|
|
|
|
|
|
Logger::log("save data to new backend " . $dest);
|
2018-12-01 17:44:54 +01:00
|
|
|
$ref = $dest::put($data);
|
2018-12-12 17:50:34 +01:00
|
|
|
Logger::log("saved data as " . $ref);
|
2018-12-01 17:44:54 +01:00
|
|
|
|
2018-12-01 17:48:37 +01:00
|
|
|
if ($ref !== '') {
|
2018-12-12 17:50:34 +01:00
|
|
|
Logger::log("update row");
|
2018-12-01 17:48:37 +01:00
|
|
|
$ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
|
2018-12-12 17:50:34 +01:00
|
|
|
|
2018-12-01 17:44:54 +01:00
|
|
|
if ($ru) {
|
2018-12-12 17:50:34 +01:00
|
|
|
if (!is_null($backendClass) && $backendClass !== '') {
|
|
|
|
Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
|
2018-12-01 17:44:54 +01:00
|
|
|
$backendClass::delete($backendRef);
|
|
|
|
}
|
|
|
|
$moved++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $moved;
|
|
|
|
}
|
2018-12-12 17:50:34 +01:00
|
|
|
}
|
|
|
|
|