]> git.mxchange.org Git - friendica.git/commitdiff
Add move function to storage manager and console command
authorfabrixxm <fabrix.xm@gmail.com>
Sat, 1 Dec 2018 16:44:54 +0000 (17:44 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 21 Jan 2019 14:11:35 +0000 (09:11 -0500)
src/Core/Console/Storage.php
src/Core/StorageManager.php

index a03743f253da1d4efde29adf30319f482ae7c498..9db2a123f2d335087ca910ea6a8b1a3c6a1fedb6 100644 (file)
@@ -105,6 +105,12 @@ HELP;
 
        protected function do_move()
        {
+               if (count($this->args) !== 1) {
+                       throw new CommandArgsException('Invalid arguments');
+               }
 
+               $current = StorageManager::getBackend();
+               $r = StorageManager::move($current);
+               $this->out(sprintf("Moved %d files", $r));
        }
 }
index 5c14fdbf3388646c954441b30d8aadce96ebe753..6156c9731bcacb0d49197b1a91506304c32a8739 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Friendica\Core;
 
+use Friendica\Database\DBA;
 use Friendica\Core\Config;
 
 
@@ -15,8 +16,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 = [];
@@ -99,4 +100,49 @@ class StorageManager
                unset(self::$backends[$name]);
                Config::set('storage', 'backends', self::$backends);
        }
+
+
+       /**
+        * @brief Move resources to storage $dest
+        *
+        * @param string  $dest    Destination storage class name
+        * @param array   $tables  Tables to look in for resources. Optional, defaults to ['photo']
+        *
+        * @retur int Number of moved resources
+        */
+       public static function move($dest, $tables = null)
+       {
+               if (is_null($tables)) {
+                       $tables = ['photo'];
+               }
+
+               $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++;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               return $moved;
+       }
 }
\ No newline at end of file