]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/StorageManager.php
Merge pull request #6612 from MrPetovan/bug/6605-cache-config-adapter-connection
[friendica.git] / src / Core / StorageManager.php
index 6156c9731bcacb0d49197b1a91506304c32a8739..cfefa5f35a7dcc91738a2d63d9bc500221d6aa4b 100644 (file)
@@ -3,8 +3,7 @@
 namespace Friendica\Core;
 
 use Friendica\Database\DBA;
-use Friendica\Core\Config;
-
+use Friendica\Model\Storage\IStorage;
 
 
 /**
@@ -16,8 +15,8 @@ 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 = [];
@@ -32,6 +31,7 @@ class StorageManager
        /**
         * @brief Return current storage backend class
         * @return string
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function getBackend()
        {
@@ -53,7 +53,8 @@ class StorageManager
        /**
         * @brief Set current storage backend class
         *
-        * @param string  $class  Backend class name
+        * @param string $class Backend class name
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function setBackend($class)
        {
@@ -73,12 +74,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 +93,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]);
@@ -105,35 +107,58 @@ class StorageManager
        /**
         * @brief Move resources to storage $dest
         *
+        * Copy existing data to destination storage and delete from source.
+        * This method cannot move to legacy in-table `data` field.
+        *
         * @param string  $dest    Destination storage class name
-        * @param array   $tables  Tables to look in for resources. Optional, defaults to ['photo']
+        * @param array   $tables  Tables to look in for resources. Optional, defaults to ['photo', 'attach']
         *
-        * @retur int Number of moved resources
+        * @throws \Exception
+        * @return int Number of moved resources
         */
        public static function move($dest, $tables = null)
        {
+               if (is_null($dest) || empty($dest)) {
+                       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]);
+                       // 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 ]
+                       );
+
                        if (DBA::isResult($rr)) {
-                               while($r = $rr->fetch()) {
+                               while($r = DBA::fetch($rr)) {
                                        $id = $r['id'];
                                        $data = $r['data'];
+                                       /** @var IStorage $backendClass */
                                        $backendClass = $r['backend-class'];
                                        $backendRef = $r['backend-ref'];
-                                       if ($backendClass !== '') {
+                                       if (!is_null($backendClass) && $backendClass !== '') {
+                                               Logger::log("get data from old backend " .  $backendClass . " : " . $backendRef);
                                                $data = $backendClass::get($backendRef);
                                        }
+                                       
+                                       Logger::log("save data to new backend " . $dest);
+                                       /** @var IStorage $dest */
                                        $ref = $dest::put($data);
+                                       Logger::log("saved data as " . $ref);
 
-                                       if ($ref !== "") {
-                                               $ru = DBA::update($table, ["backend-class" => $dest, "backend-ref" => $ref, "data" => ""], ["id" => $id]);
+                                       if ($ref !== '') {
+                                               Logger::log("update row");
+                                               $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
+                                               
                                                if ($ru) {
-                                                       if ($backendClass !== "") {
+                                                       if (!is_null($backendClass) && $backendClass !== '') {
+                                                               Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
                                                                $backendClass::delete($backendRef);
                                                        }
                                                        $moved++;
@@ -145,4 +170,5 @@ class StorageManager
 
                return $moved;
        }
-}
\ No newline at end of file
+}
+