2018-11-29 08:27:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2018-12-01 16:44:54 +00:00
|
|
|
use Friendica\Database\DBA;
|
2019-01-21 21:51:59 +00:00
|
|
|
use Friendica\Model\Storage\IStorage;
|
2018-11-29 08:27:04 +00: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 11:57:57 +00:00
|
|
|
private static $default_backends = [
|
2018-12-14 17:03:16 +00:00
|
|
|
'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
|
|
|
|
'Database' => \Friendica\Model\Storage\Database::class,
|
2018-11-29 08:27:04 +00:00
|
|
|
];
|
|
|
|
|
2018-11-29 11:57:57 +00:00
|
|
|
private static $backends = [];
|
2018-11-29 08:27:04 +00:00
|
|
|
|
|
|
|
private static function setup()
|
|
|
|
{
|
2019-03-17 23:04:42 +00:00
|
|
|
if (count(self::$backends) == 0) {
|
2018-11-29 11:57:57 +00:00
|
|
|
self::$backends = Config::get('storage', 'backends', self::$default_backends);
|
2018-11-29 08:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-10 06:35:44 +00:00
|
|
|
* @brief Return current storage backend class
|
|
|
|
*
|
2018-11-29 08:27:04 +00:00
|
|
|
* @return string
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 08:27:04 +00: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 11:57:57 +00:00
|
|
|
return defaults(self::$backends, $name, '');
|
2018-11-29 08:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set current storage backend class
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $class Backend class name
|
2019-03-17 23:12:20 +00:00
|
|
|
* @return bool
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 08:27:04 +00:00
|
|
|
*/
|
|
|
|
public static function setBackend($class)
|
|
|
|
{
|
2019-04-10 06:35:44 +00:00
|
|
|
if (!in_array('Friendica\Model\Storage\IStorage', class_implements($class))) {
|
2019-03-17 23:12:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:27:04 +00:00
|
|
|
Config::set('storage', 'class', $class);
|
2019-03-17 23:12:20 +00:00
|
|
|
|
|
|
|
return true;
|
2018-11-29 08:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get registered backends
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function listBackends()
|
|
|
|
{
|
|
|
|
self::setup();
|
|
|
|
return self::$backends;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Register a storage backend class
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $name User readable backend name
|
|
|
|
* @param string $class Backend class name
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 08:27:04 +00:00
|
|
|
*/
|
|
|
|
public static function register($name, $class)
|
|
|
|
{
|
|
|
|
/// @todo Check that $class implements IStorage
|
|
|
|
self::setup();
|
2018-11-29 11:57:57 +00:00
|
|
|
self::$backends[$name] = $class;
|
|
|
|
Config::set('storage', 'backends', self::$backends);
|
2018-11-29 08:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unregister a storage backend class
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $name User readable backend name
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-11-29 08:27:04 +00:00
|
|
|
*/
|
2018-12-12 19:35:18 +00:00
|
|
|
public static function unregister($name)
|
2018-11-29 08:27:04 +00:00
|
|
|
{
|
|
|
|
self::setup();
|
2018-11-29 11:57:57 +00:00
|
|
|
unset(self::$backends[$name]);
|
|
|
|
Config::set('storage', 'backends', self::$backends);
|
2018-11-29 08:27:04 +00:00
|
|
|
}
|
2018-12-01 16:44:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-20 04:41:57 +00:00
|
|
|
* @brief Move up to 5000 resources to storage $dest
|
2018-12-01 16:44:54 +00:00
|
|
|
*
|
2018-12-12 16:50:34 +00:00
|
|
|
* Copy existing data to destination storage and delete from source.
|
|
|
|
* This method cannot move to legacy in-table `data` field.
|
|
|
|
*
|
2019-03-20 04:41:57 +00:00
|
|
|
* @param string $destination Storage class name
|
|
|
|
* @param array|null $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
|
|
|
|
* @param int $limit Limit of the process batch size, defaults to 5000
|
2019-01-06 21:06:53 +00:00
|
|
|
* @return int Number of moved resources
|
2019-03-20 04:41:57 +00:00
|
|
|
* @throws \Exception
|
2018-12-01 16:44:54 +00:00
|
|
|
*/
|
2019-03-20 04:41:57 +00:00
|
|
|
public static function move($destination, $tables = null, $limit = 5000)
|
2018-12-01 16:44:54 +00:00
|
|
|
{
|
2019-03-20 04:41:57 +00:00
|
|
|
if (empty($destination)) {
|
2019-01-07 18:28:40 +00:00
|
|
|
throw new \Exception('Can\'t move to NULL storage backend');
|
2018-12-12 16:50:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 16:44:54 +00:00
|
|
|
if (is_null($tables)) {
|
2019-01-03 21:35:46 +00:00
|
|
|
$tables = ['photo', 'attach'];
|
2018-12-01 16:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$moved = 0;
|
|
|
|
foreach ($tables as $table) {
|
2018-12-12 16:50:34 +00:00
|
|
|
// Get the rows where backend class is not the destination backend class
|
2019-03-20 04:41:57 +00:00
|
|
|
$resources = DBA::select(
|
2018-12-12 16:50:34 +00:00
|
|
|
$table,
|
|
|
|
['id', 'data', 'backend-class', 'backend-ref'],
|
2019-03-20 04:41:57 +00:00
|
|
|
['`backend-class` IS NULL or `backend-class` != ?', $destination],
|
|
|
|
['limit' => $limit]
|
2018-12-12 16:50:34 +00:00
|
|
|
);
|
|
|
|
|
2019-03-20 04:41:57 +00:00
|
|
|
while ($resource = DBA::fetch($resources)) {
|
|
|
|
$id = $resource['id'];
|
|
|
|
$data = $resource['data'];
|
|
|
|
/** @var IStorage $backendClass */
|
|
|
|
$backendClass = $resource['backend-class'];
|
|
|
|
$backendRef = $resource['backend-ref'];
|
|
|
|
if (!empty($backendClass)) {
|
|
|
|
Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
|
|
|
|
$data = $backendClass::get($backendRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::log("save data to new backend " . $destination);
|
|
|
|
/** @var IStorage $destination */
|
|
|
|
$ref = $destination::put($data);
|
|
|
|
Logger::log("saved data as " . $ref);
|
|
|
|
|
|
|
|
if ($ref !== '') {
|
|
|
|
Logger::log("update row");
|
|
|
|
if (DBA::update($table, ['backend-class' => $destination, 'backend-ref' => $ref, 'data' => ''], ['id' => $id])) {
|
|
|
|
if (!empty($backendClass)) {
|
|
|
|
Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
|
|
|
|
$backendClass::delete($backendRef);
|
2018-12-01 16:44:54 +00:00
|
|
|
}
|
2019-03-20 04:41:57 +00:00
|
|
|
$moved++;
|
2018-12-01 16:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 04:41:57 +00:00
|
|
|
|
|
|
|
DBA::close($resources);
|
2018-12-01 16:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $moved;
|
|
|
|
}
|
2018-12-12 16:50:34 +00:00
|
|
|
}
|
|
|
|
|