3 namespace Friendica\Console;
5 use Asika\SimpleConsole\CommandArgsException;
6 use Friendica\Core\StorageManager;
9 * tool to manage storage backend and stored data from CLI
12 class Storage extends \Asika\SimpleConsole\Console
14 protected $helpOptions = ['h', 'help', '?'];
16 /** @var StorageManager */
17 private $storageManager;
20 * @param StorageManager $storageManager
22 public function __construct(StorageManager $storageManager, array $argv = [])
24 parent::__construct($argv);
26 $this->storageManager = $storageManager;
29 protected function getHelp()
32 console storage - manage storage backend and stored data
34 bin/console storage [-h|--help|-?] [-v]
37 bin/console storage list
38 List available storage backends
40 bin/console storage set <name>
41 Set current storage backend
42 name storage backend to use. see "list".
44 bin/console storage move [table] [-n 5000]
45 Move stored data to current storage backend.
46 table one of "photo" or "attach". default to both
47 -n limit of processed entry batch size
52 protected function doExecute()
54 if ($this->getOption('v')) {
55 $this->out('Executable: ' . $this->executable);
56 $this->out('Class: ' . __CLASS__);
57 $this->out('Arguments: ' . var_export($this->args, true));
58 $this->out('Options: ' . var_export($this->options, true));
61 if (count($this->args) == 0) {
62 $this->out($this->getHelp());
66 switch ($this->args[0]) {
68 return $this->doList();
71 return $this->doSet();
74 return $this->doMove();
78 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
82 protected function doList()
84 $rowfmt = ' %-3s | %-20s';
85 $current = $this->storageManager->getBackend();
86 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
87 $this->out('-----------------------');
89 foreach ($this->storageManager->listBackends() as $name => $class) {
91 if ($current::getName() == $name) {
95 $this->out(sprintf($rowfmt, $issel, $name));
98 if ($current === '') {
100 $this->out('This system is using legacy storage system');
102 if ($current !== '' && !$isregisterd) {
104 $this->out('The current storage class (' . $current . ') is not registered!');
109 protected function doSet()
111 if (count($this->args) !== 2) {
112 throw new CommandArgsException('Invalid arguments');
115 $name = $this->args[1];
116 $class = $this->storageManager->getByName($name);
119 $this->out($name . ' is not a registered backend.');
123 if (!$this->storageManager->setBackend($class)) {
124 $this->out($class . ' is not a valid backend storage class.');
131 protected function doMove()
133 if (count($this->args) < 1 || count($this->args) > 2) {
134 throw new CommandArgsException('Invalid arguments');
137 if (count($this->args) == 2) {
138 $table = strtolower($this->args[1]);
139 if (!in_array($table, ['photo', 'attach'])) {
140 throw new CommandArgsException('Invalid table');
144 $tables = StorageManager::TABLES;
147 $current = $this->storageManager->getBackend();
151 $moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
153 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
159 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));