]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Console/Storage.php
Improved protocol storing
[friendica.git] / src / Core / Console / Storage.php
index 9db2a123f2d335087ca910ea6a8b1a3c6a1fedb6..805ef0aea9ef7e5d6d5f361e5f0288e42d04a924 100644 (file)
@@ -19,17 +19,25 @@ class Storage extends \Asika\SimpleConsole\Console
 console storage - manage storage backend and stored data
 Synopsis
     bin/console storage [-h|--help|-?] [-v]
+        Show this help
+    
     bin/console storage list
+        List available storage backends
+    
     bin/console storage set <name>
-    bin/console storage move
+        Set current storage backend
+            name        storage backend to use. see "list".
+    
+    bin/console storage move [table] [-n 5000]
+        Move stored data to current storage backend.
+            table       one of "photo" or "attach". default to both
+            -n          limit of processed entry batch size
 HELP;
                return $help;
        }
 
        protected function doExecute()
        {
-               $a = \Friendica\BaseObject::getApp();
-
                if ($this->getOption('v')) {
                        $this->out('Executable: ' . $this->executable);
                        $this->out('Class: ' . __CLASS__);
@@ -42,41 +50,41 @@ HELP;
                        return -1;
                }
 
-               switch($this->args[0]) {
-               case 'list':
-                       return $this->do_list();
-                       break;
-               case 'set':
-                       return $this->do_set();
-                       break;
-               case 'move':
-                       return $this->do_move();
-                       break;
+               switch ($this->args[0]) {
+                       case 'list':
+                               return $this->doList();
+                               break;
+                       case 'set':
+                               return $this->doSet();
+                               break;
+                       case 'move':
+                               return $this->doMove();
+                               break;
                }
 
                $this->out(sprintf('Invalid action "%s"', $this->args[0]));
                return -1;
        }
 
-       protected function do_list()
+       protected function doList()
        {
                $rowfmt = ' %-3s | %-20s';
                $current = StorageManager::getBackend();
                $this->out(sprintf($rowfmt, 'Sel', 'Name'));
                $this->out('-----------------------');
                $isregisterd = false;
-               foreach(StorageManager::listBackends() as $name => $class) {
+               foreach (StorageManager::listBackends() as $name => $class) {
                        $issel = ' ';
                        if ($current === $class) {
                                $issel = '*';
                                $isregisterd = true;
                        };
-                       $this->out(sprintf($rowfmt, $issel , $name ));
+                       $this->out(sprintf($rowfmt, $issel, $name));
                }
 
                if ($current === '') {
                        $this->out();
-                       $this->out('This sistem is using legacy storage system');
+                       $this->out('This system is using legacy storage system');
                }
                if ($current !== '' && !$isregisterd) {
                        $this->out();
@@ -85,7 +93,7 @@ HELP;
                return 0;
        }
 
-       protected function do_set()
+       protected function doSet()
        {
                if (count($this->args) !== 2) {
                        throw new CommandArgsException('Invalid arguments');
@@ -94,23 +102,46 @@ HELP;
                $name = $this->args[1];
                $class = StorageManager::getByName($name);
 
-               if ($class === "") {
+               if ($class === '') {
                        $this->out($name . ' is not a registered backend.');
                        return -1;
                }
 
-               StorageManager::setBackend($class);
+               if (!StorageManager::setBackend($class)) {
+                       $this->out($class . ' is not a valid backend storage class.');
+                       return -1;
+               }
+
                return 0;
        }
 
-       protected function do_move()
+       protected function doMove()
        {
-               if (count($this->args) !== 1) {
+               $tables = null;
+               if (count($this->args) < 1 || count($this->args) > 2) {
                        throw new CommandArgsException('Invalid arguments');
                }
 
+               if (count($this->args) == 2) {
+                       $table = strtolower($this->args[1]);
+                       if (!in_array($table, ['photo', 'attach'])) {
+                               throw new CommandArgsException('Invalid table');
+                       }
+                       $tables = [$table];
+               }
+
                $current = StorageManager::getBackend();
-               $r = StorageManager::move($current);
-               $this->out(sprintf("Moved %d files", $r));
+               $total = 0;
+
+               do {
+                       $moved = StorageManager::move($current, $tables, $this->getOption('n', 5000));
+                       if ($moved) {
+                               $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
+                       }
+
+                       $total += $moved;
+               } while ($moved);
+
+               $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));
        }
 }