3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use Asika\SimpleConsole\CommandArgsException;
25 use Friendica\Core\StorageManager;
28 * tool to manage storage backend and stored data from CLI
30 class Storage extends \Asika\SimpleConsole\Console
32 protected $helpOptions = ['h', 'help', '?'];
34 /** @var StorageManager */
35 private $storageManager;
38 * @param StorageManager $storageManager
40 public function __construct(StorageManager $storageManager, array $argv = [])
42 parent::__construct($argv);
44 $this->storageManager = $storageManager;
47 protected function getHelp()
50 console storage - manage storage backend and stored data
52 bin/console storage [-h|--help|-?] [-v]
55 bin/console storage list
56 List available storage backends
58 bin/console storage set <name>
59 Set current storage backend
60 name storage backend to use. see "list".
62 bin/console storage move [table] [-n 5000]
63 Move stored data to current storage backend.
64 table one of "photo" or "attach". default to both
65 -n limit of processed entry batch size
70 protected function doExecute()
72 if ($this->getOption('v')) {
73 $this->out('Executable: ' . $this->executable);
74 $this->out('Class: ' . __CLASS__);
75 $this->out('Arguments: ' . var_export($this->args, true));
76 $this->out('Options: ' . var_export($this->options, true));
79 if (count($this->args) == 0) {
80 $this->out($this->getHelp());
84 switch ($this->args[0]) {
86 return $this->doList();
89 return $this->doSet();
92 return $this->doMove();
96 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
100 protected function doList()
102 $rowfmt = ' %-3s | %-20s';
103 $current = $this->storageManager->getBackend();
104 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
105 $this->out('-----------------------');
106 $isregisterd = false;
107 foreach ($this->storageManager->listBackends() as $name => $class) {
109 if ($current::getName() == $name) {
113 $this->out(sprintf($rowfmt, $issel, $name));
116 if ($current === '') {
118 $this->out('This system is using legacy storage system');
120 if ($current !== '' && !$isregisterd) {
122 $this->out('The current storage class (' . $current . ') is not registered!');
127 protected function doSet()
129 if (count($this->args) !== 2) {
130 throw new CommandArgsException('Invalid arguments');
133 $name = $this->args[1];
134 $class = $this->storageManager->getByName($name);
137 $this->out($name . ' is not a registered backend.');
141 if (!$this->storageManager->setBackend($class)) {
142 $this->out($class . ' is not a valid backend storage class.');
149 protected function doMove()
151 if (count($this->args) < 1 || count($this->args) > 2) {
152 throw new CommandArgsException('Invalid arguments');
155 if (count($this->args) == 2) {
156 $table = strtolower($this->args[1]);
157 if (!in_array($table, ['photo', 'attach'])) {
158 throw new CommandArgsException('Invalid table');
162 $tables = StorageManager::TABLES;
165 $current = $this->storageManager->getBackend();
169 $moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
171 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
177 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));