3 * @copyright Copyright (C) 2010-2021, the Friendica project
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;
26 use Friendica\Model\Storage\StorageException;
29 * tool to manage storage backend and stored data from CLI
31 class Storage extends \Asika\SimpleConsole\Console
33 protected $helpOptions = ['h', 'help', '?'];
35 /** @var StorageManager */
36 private $storageManager;
39 * @param StorageManager $storageManager
41 public function __construct(StorageManager $storageManager, array $argv = [])
43 parent::__construct($argv);
45 $this->storageManager = $storageManager;
48 protected function getHelp()
51 console storage - manage storage backend and stored data
53 bin/console storage [-h|--help|-?] [-v]
56 bin/console storage list
57 List available storage backends
59 bin/console storage set <name>
60 Set current storage backend
61 name storage backend to use. see "list".
63 bin/console storage move [table] [-n 5000]
64 Move stored data to current storage backend.
65 table one of "photo" or "attach". default to both
66 -n limit of processed entry batch size
71 protected function doExecute()
73 if ($this->getOption('v')) {
74 $this->out('Executable: ' . $this->executable);
75 $this->out('Class: ' . __CLASS__);
76 $this->out('Arguments: ' . var_export($this->args, true));
77 $this->out('Options: ' . var_export($this->options, true));
80 if (count($this->args) == 0) {
81 $this->out($this->getHelp());
85 switch ($this->args[0]) {
87 return $this->doList();
90 return $this->doSet();
93 return $this->doMove();
97 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
101 protected function doList()
103 $rowfmt = ' %-3s | %-20s';
104 $current = $this->storageManager->getBackend();
105 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
106 $this->out('-----------------------');
107 $isregisterd = false;
108 foreach ($this->storageManager->listBackends() as $name => $class) {
110 if ($current && $current::getName() == $name) {
114 $this->out(sprintf($rowfmt, $issel, $name));
117 if ($current === '') {
119 $this->out('This system is using legacy storage system');
121 if ($current !== '' && !$isregisterd) {
123 $this->out('The current storage class (' . $current . ') is not registered!');
128 protected function doSet()
130 if (count($this->args) !== 2) {
131 throw new CommandArgsException('Invalid arguments');
134 $name = $this->args[1];
135 $class = $this->storageManager->getByName($name);
138 $this->out($name . ' is not a registered backend.');
142 if (!$this->storageManager->setBackend($class)) {
143 $this->out($class . ' is not a valid backend storage class.');
150 protected function doMove()
152 if (count($this->args) < 1 || count($this->args) > 2) {
153 throw new CommandArgsException('Invalid arguments');
156 if (count($this->args) == 2) {
157 $table = strtolower($this->args[1]);
158 if (!in_array($table, ['photo', 'attach'])) {
159 throw new CommandArgsException('Invalid table');
163 $tables = StorageManager::TABLES;
166 $current = $this->storageManager->getBackend();
169 if (is_null($current)) {
170 throw new StorageException(sprintf("Cannot move to legacy storage. Please select a storage backend."));
174 $moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
176 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
182 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));