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] [-n 5000]
32 Move stored data to current storage backend.
33 table one of "photo" or "attach". default to both
34 -n limit of processed entry batch size
39 protected function doExecute()
41 if ($this->getOption('v')) {
42 $this->out('Executable: ' . $this->executable);
43 $this->out('Class: ' . __CLASS__);
44 $this->out('Arguments: ' . var_export($this->args, true));
45 $this->out('Options: ' . var_export($this->options, true));
48 if (count($this->args) == 0) {
49 $this->out($this->getHelp());
53 switch ($this->args[0]) {
55 return $this->doList();
58 return $this->doSet();
61 return $this->doMove();
65 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
69 protected function doList()
71 $rowfmt = ' %-3s | %-20s';
72 $current = StorageManager::getBackend();
73 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
74 $this->out('-----------------------');
76 foreach (StorageManager::listBackends() as $name => $class) {
78 if ($current === $class) {
82 $this->out(sprintf($rowfmt, $issel, $name));
85 if ($current === '') {
87 $this->out('This system is using legacy storage system');
89 if ($current !== '' && !$isregisterd) {
91 $this->out('The current storage class (' . $current . ') is not registered!');
96 protected function doSet()
98 if (count($this->args) !== 2) {
99 throw new CommandArgsException('Invalid arguments');
102 $name = $this->args[1];
103 $class = StorageManager::getByName($name);
106 $this->out($name . ' is not a registered backend.');
110 if (!StorageManager::setBackend($class)) {
111 $this->out($class . ' is not a valid backend storage class.');
118 protected function doMove()
121 if (count($this->args) < 1 || count($this->args) > 2) {
122 throw new CommandArgsException('Invalid arguments');
125 if (count($this->args) == 2) {
126 $table = strtolower($this->args[1]);
127 if (!in_array($table, ['photo', 'attach'])) {
128 throw new CommandArgsException('Invalid table');
133 $current = StorageManager::getBackend();
137 $moved = StorageManager::move($current, $tables, $this->getOption('n', 5000));
139 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
145 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));