3 namespace Friendica\Core\Console;
5 use Asika\SimpleConsole\CommandArgsException;
6 use Friendica\Core\StorageManager;
9 * @brief tool to manage storage backend and stored data from CLI
12 class Storage extends \Asika\SimpleConsole\Console
14 protected $helpOptions = ['h', 'help', '?'];
16 protected function getHelp()
19 console storage - manage storage backend and stored data
21 bin/console storage [-h|--help|-?] [-v]
24 bin/console storage list
25 List available storage backends
27 bin/console storage set <name>
28 Set current storage backend
29 name storage backend to use. see "list".
31 bin/console storage move [table]
32 Move stored data to current storage backend.
33 table one of "photo" or "attach". default to both
38 protected function doExecute()
40 if ($this->getOption('v')) {
41 $this->out('Executable: ' . $this->executable);
42 $this->out('Class: ' . __CLASS__);
43 $this->out('Arguments: ' . var_export($this->args, true));
44 $this->out('Options: ' . var_export($this->options, true));
47 if (count($this->args) == 0) {
48 $this->out($this->getHelp());
52 switch ($this->args[0]) {
54 return $this->doList();
57 return $this->doSet();
60 return $this->doMove();
64 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
68 protected function doList()
70 $rowfmt = ' %-3s | %-20s';
71 $current = StorageManager::getBackend();
72 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
73 $this->out('-----------------------');
75 foreach (StorageManager::listBackends() as $name => $class) {
77 if ($current === $class) {
81 $this->out(sprintf($rowfmt, $issel, $name));
84 if ($current === '') {
86 $this->out('This sistem is using legacy storage system');
88 if ($current !== '' && !$isregisterd) {
90 $this->out('The current storage class (' . $current . ') is not registered!');
95 protected function doSet()
97 if (count($this->args) !== 2) {
98 throw new CommandArgsException('Invalid arguments');
101 $name = $this->args[1];
102 $class = StorageManager::getByName($name);
105 $this->out($name . ' is not a registered backend.');
109 if (!StorageManager::setBackend($class)) {
110 $this->out($class . ' is not a valid backend storage class.');
117 protected function doMove()
120 if (count($this->args) < 1 || count($this->args) > 2) {
121 throw new CommandArgsException('Invalid arguments');
124 if (count($this->args) == 2) {
125 $table = strtolower($this->args[1]);
126 if (!in_array($table, ['photo', 'attach'])) {
127 throw new CommandArgsException('Invalid table');
132 $current = StorageManager::getBackend();
133 $r = StorageManager::move($current, $tables);
134 $this->out(sprintf('Moved %d files', $r));