X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FStorageManager.php;h=8cd7d439531824b3b836e086a4cfd0d71db2ca05;hb=a2c6240da02de21dc974b1fbe65a943f2abb37fb;hp=e1bab23acab70db1ff2da955949ce7ad759c6281;hpb=f7b6fef1978975cc1784fdbf663523ebc999d1c0;p=friendica.git diff --git a/src/Core/StorageManager.php b/src/Core/StorageManager.php index e1bab23aca..8cd7d43953 100644 --- a/src/Core/StorageManager.php +++ b/src/Core/StorageManager.php @@ -3,8 +3,7 @@ namespace Friendica\Core; use Friendica\Database\DBA; -use Friendica\Core\Config; - +use Friendica\Model\Storage\IStorage; /** @@ -16,22 +15,24 @@ use Friendica\Core\Config; class StorageManager { private static $default_backends = [ - 'Filesystem' => Friendica\Model\Storage\Filesystem::class, - 'Database' => Friendica\Model\Storage\Database::class, + 'Filesystem' => \Friendica\Model\Storage\Filesystem::class, + 'Database' => \Friendica\Model\Storage\Database::class, ]; private static $backends = []; private static function setup() { - if (count(self::$backends)==0) { + if (count(self::$backends) == 0) { self::$backends = Config::get('storage', 'backends', self::$default_backends); } } /** * @brief Return current storage backend class + * * @return string + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function getBackend() { @@ -53,12 +54,19 @@ class StorageManager /** * @brief Set current storage backend class * - * @param string $class Backend class name + * @param string $class Backend class name + * @return bool + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function setBackend($class) { - /// @todo Check that $class implements IStorage + if (!in_array('Friendica\Model\Storage\IStorage', class_implements($class))) { + return false; + } + Config::set('storage', 'class', $class); + + return true; } /** @@ -73,12 +81,12 @@ class StorageManager } - /** * @brief Register a storage backend class * - * @param string $name User readable backend name - * @param string $class Backend class name + * @param string $name User readable backend name + * @param string $class Backend class name + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function register($name, $class) { @@ -92,9 +100,10 @@ class StorageManager /** * @brief Unregister a storage backend class * - * @param string $name User readable backend name + * @param string $name User readable backend name + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function unregister($class) + public static function unregister($name) { self::setup(); unset(self::$backends[$name]); @@ -103,46 +112,69 @@ class StorageManager /** - * @brief Move resources to storage $dest + * @brief Move up to 5000 resources to storage $dest * - * @param string $dest Destination storage class name - * @param array $tables Tables to look in for resources. Optional, defaults to ['photo'] + * Copy existing data to destination storage and delete from source. + * This method cannot move to legacy in-table `data` field. * - * @retur int Number of moved resources + * @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 + * @return int Number of moved resources + * @throws \Exception */ - public static function move($dest, $tables = null) + public static function move($destination, $tables = null, $limit = 5000) { + if (empty($destination)) { + throw new \Exception('Can\'t move to NULL storage backend'); + } + if (is_null($tables)) { - $tables = ['photo']; + $tables = ['photo', 'attach']; } $moved = 0; foreach ($tables as $table) { - $rr = DBA::select($table, ['id', 'data', 'backend-class', 'backend-ref'], ['`backend-class` != ?', $dest]); - if (DBA::isResult($rr)) { - while($r = $rr->fetch()) { - $id = $r['id']; - $data = $r['data']; - $backendClass = $r['backend-class']; - $backendRef = $r['backend-ref']; - if ($backendClass !== '') { - $data = $backendClass::get($backendRef); - } - $ref = $dest::put($data); - - if ($ref !== '') { - $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]); - if ($ru) { - if ($backendClass !== '') { - $backendClass::delete($backendRef); - } - $moved++; + // Get the rows where backend class is not the destination backend class + $resources = DBA::select( + $table, + ['id', 'data', 'backend-class', 'backend-ref'], + ['`backend-class` IS NULL or `backend-class` != ?', $destination], + ['limit' => $limit] + ); + + 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); } + $moved++; } } } + + DBA::close($resources); } return $moved; } -} \ No newline at end of file +} +