3 * @copyright Copyright (C) 2010-2022, 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\Storage\Repository\StorageManager;
26 use Friendica\Core\Storage\Exception\ReferenceStorageException;
27 use Friendica\Core\Storage\Exception\StorageException;
30 * tool to manage storage backend and stored data from CLI
32 class Storage extends \Asika\SimpleConsole\Console
34 protected $helpOptions = ['h', 'help', '?'];
36 /** @var StorageManager */
37 private $storageManager;
40 * @param StorageManager $storageManager
42 public function __construct(StorageManager $storageManager, array $argv = [])
44 parent::__construct($argv);
46 $this->storageManager = $storageManager;
49 protected function getHelp()
52 console storage - manage storage backend and stored data
54 bin/console storage [-h|--help|-?] [-v]
57 bin/console storage list
58 List available storage backends
60 bin/console storage set <name>
61 Set current storage backend
62 name storage backend to use. see "list".
64 bin/console storage move [table] [-n 5000]
65 Move stored data to current storage backend.
66 table one of "photo" or "attach". default to both
67 -n limit of processed entry batch size
72 protected function doExecute(): int
74 if ($this->getOption('v')) {
75 $this->out('Executable: ' . $this->executable);
76 $this->out('Class: ' . __CLASS__);
77 $this->out('Arguments: ' . var_export($this->args, true));
78 $this->out('Options: ' . var_export($this->options, true));
81 if (count($this->args) == 0) {
82 $this->out($this->getHelp());
86 switch ($this->args[0]) {
88 return $this->doList();
91 return $this->doSet();
94 return $this->doMove();
98 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
102 protected function doList()
104 $rowfmt = ' %-3s | %-20s';
105 $current = $this->storageManager->getBackend();
106 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
107 $this->out('-----------------------');
108 $isregisterd = false;
109 foreach ($this->storageManager->listBackends() as $name) {
111 if ($current && $current::getName() == $name) {
115 $this->out(sprintf($rowfmt, $issel, $name));
118 if ($current === '') {
120 $this->out('This system is using legacy storage system');
122 if ($current !== '' && !$isregisterd) {
124 $this->out('The current storage class (' . $current . ') is not registered!');
129 protected function doSet()
131 if (count($this->args) !== 2 || empty($this->args[1])) {
132 throw new CommandArgsException('Invalid arguments');
135 $name = $this->args[1];
137 $class = $this->storageManager->getWritableStorageByName($name);
139 if (!$this->storageManager->setBackend($class)) {
140 $this->out($class . ' is not a valid backend storage class.');
143 } catch (ReferenceStorageException $exception) {
144 $this->out($name . ' is not a registered backend.');
151 protected function doMove()
153 if (count($this->args) < 1 || count($this->args) > 2) {
154 throw new CommandArgsException('Invalid arguments');
157 if (count($this->args) == 2) {
158 $table = strtolower($this->args[1]);
159 if (!in_array($table, ['photo', 'attach'])) {
160 throw new CommandArgsException('Invalid table');
164 $tables = StorageManager::TABLES;
167 $current = $this->storageManager->getBackend();
170 if (is_null($current)) {
171 throw new StorageException(sprintf("Cannot move to legacy storage. Please select a storage backend."));
175 $moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
177 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
183 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));