2018-11-29 06:57:57 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Console;
|
|
|
|
|
|
|
|
use Asika\SimpleConsole\CommandArgsException;
|
|
|
|
use Friendica\Core\StorageManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief tool to manage storage backend and stored data from CLI
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Storage extends \Asika\SimpleConsole\Console
|
|
|
|
{
|
|
|
|
protected $helpOptions = ['h', 'help', '?'];
|
|
|
|
|
|
|
|
protected function getHelp()
|
|
|
|
{
|
|
|
|
$help = <<<HELP
|
|
|
|
console storage - manage storage backend and stored data
|
|
|
|
Synopsis
|
|
|
|
bin/console storage [-h|--help|-?] [-v]
|
2019-01-03 16:35:46 -05:00
|
|
|
Show this help
|
|
|
|
|
2018-11-29 06:57:57 -05:00
|
|
|
bin/console storage list
|
2019-01-04 01:45:08 -05:00
|
|
|
List available storage backends
|
2019-01-03 16:35:46 -05:00
|
|
|
|
2018-11-29 06:57:57 -05:00
|
|
|
bin/console storage set <name>
|
2019-01-03 16:35:46 -05:00
|
|
|
Set current storage backend
|
|
|
|
name storage backend to use. see "list".
|
|
|
|
|
|
|
|
bin/console storage move [table]
|
|
|
|
Move stored data to current storage backend.
|
|
|
|
table one of "photo" or "attach". default to both
|
2018-11-29 06:57:57 -05:00
|
|
|
HELP;
|
|
|
|
return $help;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doExecute()
|
|
|
|
{
|
|
|
|
if ($this->getOption('v')) {
|
|
|
|
$this->out('Executable: ' . $this->executable);
|
|
|
|
$this->out('Class: ' . __CLASS__);
|
|
|
|
$this->out('Arguments: ' . var_export($this->args, true));
|
|
|
|
$this->out('Options: ' . var_export($this->options, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->args) == 0) {
|
|
|
|
$this->out($this->getHelp());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($this->args[0]) {
|
|
|
|
case 'list':
|
|
|
|
return $this->do_list();
|
|
|
|
break;
|
|
|
|
case 'set':
|
|
|
|
return $this->do_set();
|
|
|
|
break;
|
|
|
|
case 'move':
|
|
|
|
return $this->do_move();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->out(sprintf('Invalid action "%s"', $this->args[0]));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function do_list()
|
|
|
|
{
|
|
|
|
$rowfmt = ' %-3s | %-20s';
|
|
|
|
$current = StorageManager::getBackend();
|
|
|
|
$this->out(sprintf($rowfmt, 'Sel', 'Name'));
|
|
|
|
$this->out('-----------------------');
|
|
|
|
$isregisterd = false;
|
|
|
|
foreach(StorageManager::listBackends() as $name => $class) {
|
|
|
|
$issel = ' ';
|
|
|
|
if ($current === $class) {
|
|
|
|
$issel = '*';
|
|
|
|
$isregisterd = true;
|
|
|
|
};
|
|
|
|
$this->out(sprintf($rowfmt, $issel , $name ));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($current === '') {
|
|
|
|
$this->out();
|
|
|
|
$this->out('This sistem is using legacy storage system');
|
|
|
|
}
|
|
|
|
if ($current !== '' && !$isregisterd) {
|
|
|
|
$this->out();
|
|
|
|
$this->out('The current storage class (' . $current . ') is not registered!');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function do_set()
|
|
|
|
{
|
|
|
|
if (count($this->args) !== 2) {
|
|
|
|
throw new CommandArgsException('Invalid arguments');
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $this->args[1];
|
|
|
|
$class = StorageManager::getByName($name);
|
|
|
|
|
2018-12-01 11:48:37 -05:00
|
|
|
if ($class === '') {
|
2018-11-29 06:57:57 -05:00
|
|
|
$this->out($name . ' is not a registered backend.');
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageManager::setBackend($class);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function do_move()
|
|
|
|
{
|
2019-03-01 12:07:08 -05:00
|
|
|
$tables = null;
|
2019-01-03 16:35:46 -05:00
|
|
|
if (count($this->args) < 1 || count($this->args) > 2) {
|
2018-12-01 11:44:54 -05:00
|
|
|
throw new CommandArgsException('Invalid arguments');
|
|
|
|
}
|
2019-03-01 12:07:08 -05:00
|
|
|
|
2019-01-03 16:35:46 -05:00
|
|
|
if (count($this->args) == 2) {
|
|
|
|
$table = strtolower($this->args[1]);
|
|
|
|
if (!in_array($table, ['photo', 'attach'])) {
|
|
|
|
throw new CommandArgsException('Invalid table');
|
|
|
|
}
|
2019-03-01 12:07:08 -05:00
|
|
|
$tables = [$table];
|
2019-01-03 16:35:46 -05:00
|
|
|
}
|
2018-11-29 06:57:57 -05:00
|
|
|
|
2018-12-01 11:44:54 -05:00
|
|
|
$current = StorageManager::getBackend();
|
2019-03-01 12:07:08 -05:00
|
|
|
$r = StorageManager::move($current, $tables);
|
2018-12-01 11:48:37 -05:00
|
|
|
$this->out(sprintf('Moved %d files', $r));
|
2018-11-29 06:57:57 -05:00
|
|
|
}
|
|
|
|
}
|