2018-11-29 11:57:57 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-02 21:17:35 +00:00
|
|
|
namespace Friendica\Console;
|
2018-11-29 11:57:57 +00:00
|
|
|
|
|
|
|
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', '?'];
|
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
/** @var StorageManager */
|
|
|
|
private $storageManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StorageManager $storageManager
|
|
|
|
*/
|
|
|
|
public function __construct(StorageManager $storageManager, array $argv = [])
|
|
|
|
{
|
|
|
|
parent::__construct($argv);
|
|
|
|
|
|
|
|
$this->storageManager = $storageManager;
|
|
|
|
}
|
|
|
|
|
2018-11-29 11:57:57 +00:00
|
|
|
protected function getHelp()
|
|
|
|
{
|
|
|
|
$help = <<<HELP
|
|
|
|
console storage - manage storage backend and stored data
|
|
|
|
Synopsis
|
|
|
|
bin/console storage [-h|--help|-?] [-v]
|
2019-01-03 21:35:46 +00:00
|
|
|
Show this help
|
|
|
|
|
2018-11-29 11:57:57 +00:00
|
|
|
bin/console storage list
|
2019-01-04 06:45:08 +00:00
|
|
|
List available storage backends
|
2019-01-03 21:35:46 +00:00
|
|
|
|
2018-11-29 11:57:57 +00:00
|
|
|
bin/console storage set <name>
|
2019-01-03 21:35:46 +00:00
|
|
|
Set current storage backend
|
|
|
|
name storage backend to use. see "list".
|
|
|
|
|
2019-03-20 04:42:50 +00:00
|
|
|
bin/console storage move [table] [-n 5000]
|
2019-01-03 21:35:46 +00:00
|
|
|
Move stored data to current storage backend.
|
|
|
|
table one of "photo" or "attach". default to both
|
2019-03-20 04:42:50 +00:00
|
|
|
-n limit of processed entry batch size
|
2018-11-29 11:57:57 +00: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;
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:04:42 +00:00
|
|
|
switch ($this->args[0]) {
|
|
|
|
case 'list':
|
|
|
|
return $this->doList();
|
|
|
|
break;
|
|
|
|
case 'set':
|
|
|
|
return $this->doSet();
|
|
|
|
break;
|
|
|
|
case 'move':
|
|
|
|
return $this->doMove();
|
|
|
|
break;
|
2018-11-29 11:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->out(sprintf('Invalid action "%s"', $this->args[0]));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:04:42 +00:00
|
|
|
protected function doList()
|
2018-11-29 11:57:57 +00:00
|
|
|
{
|
|
|
|
$rowfmt = ' %-3s | %-20s';
|
2020-01-05 00:58:49 +00:00
|
|
|
$current = $this->storageManager->getBackend();
|
2018-11-29 11:57:57 +00:00
|
|
|
$this->out(sprintf($rowfmt, 'Sel', 'Name'));
|
|
|
|
$this->out('-----------------------');
|
|
|
|
$isregisterd = false;
|
2020-01-05 00:58:49 +00:00
|
|
|
foreach ($this->storageManager->listBackends() as $name => $class) {
|
2018-11-29 11:57:57 +00:00
|
|
|
$issel = ' ';
|
2020-01-18 09:46:53 +00:00
|
|
|
if ($current::getName() == $name) {
|
2018-11-29 11:57:57 +00:00
|
|
|
$issel = '*';
|
|
|
|
$isregisterd = true;
|
|
|
|
};
|
2019-03-17 23:04:42 +00:00
|
|
|
$this->out(sprintf($rowfmt, $issel, $name));
|
2018-11-29 11:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($current === '') {
|
|
|
|
$this->out();
|
2019-03-24 01:47:33 +00:00
|
|
|
$this->out('This system is using legacy storage system');
|
2018-11-29 11:57:57 +00:00
|
|
|
}
|
|
|
|
if ($current !== '' && !$isregisterd) {
|
|
|
|
$this->out();
|
|
|
|
$this->out('The current storage class (' . $current . ') is not registered!');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:04:42 +00:00
|
|
|
protected function doSet()
|
2018-11-29 11:57:57 +00:00
|
|
|
{
|
|
|
|
if (count($this->args) !== 2) {
|
|
|
|
throw new CommandArgsException('Invalid arguments');
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $this->args[1];
|
2020-01-05 00:58:49 +00:00
|
|
|
$class = $this->storageManager->getByName($name);
|
2018-11-29 11:57:57 +00:00
|
|
|
|
2018-12-01 16:48:37 +00:00
|
|
|
if ($class === '') {
|
2018-11-29 11:57:57 +00:00
|
|
|
$this->out($name . ' is not a registered backend.');
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
if (!$this->storageManager->setBackend($class)) {
|
2019-03-17 23:12:20 +00:00
|
|
|
$this->out($class . ' is not a valid backend storage class.');
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-29 11:57:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:04:42 +00:00
|
|
|
protected function doMove()
|
2018-11-29 11:57:57 +00:00
|
|
|
{
|
2019-01-03 21:35:46 +00:00
|
|
|
if (count($this->args) < 1 || count($this->args) > 2) {
|
2018-12-01 16:44:54 +00:00
|
|
|
throw new CommandArgsException('Invalid arguments');
|
|
|
|
}
|
2019-03-01 17:07:08 +00:00
|
|
|
|
2019-01-03 21:35:46 +00: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 17:07:08 +00:00
|
|
|
$tables = [$table];
|
2020-01-17 23:08:48 +00:00
|
|
|
} else {
|
|
|
|
$tables = StorageManager::TABLES;
|
2019-01-03 21:35:46 +00:00
|
|
|
}
|
2018-11-29 11:57:57 +00:00
|
|
|
|
2020-01-05 00:58:49 +00:00
|
|
|
$current = $this->storageManager->getBackend();
|
2019-03-20 04:42:50 +00:00
|
|
|
$total = 0;
|
|
|
|
|
|
|
|
do {
|
2020-01-05 00:58:49 +00:00
|
|
|
$moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
|
2019-03-20 04:42:50 +00:00
|
|
|
if ($moved) {
|
|
|
|
$this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
|
|
|
|
}
|
|
|
|
|
|
|
|
$total += $moved;
|
|
|
|
} while ($moved);
|
|
|
|
|
|
|
|
$this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));
|
2018-11-29 11:57:57 +00:00
|
|
|
}
|
|
|
|
}
|