2018-11-29 06:57:57 -05:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2023-01-01 09:36:24 -05:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-29 06:57:57 -05:00
|
|
|
|
2019-05-02 17:17:35 -04:00
|
|
|
namespace Friendica\Console;
|
2018-11-29 06:57:57 -05:00
|
|
|
|
|
|
|
use Asika\SimpleConsole\CommandArgsException;
|
2021-10-23 06:11:38 -04:00
|
|
|
use Friendica\Core\Storage\Repository\StorageManager;
|
|
|
|
use Friendica\Core\Storage\Exception\ReferenceStorageException;
|
|
|
|
use Friendica\Core\Storage\Exception\StorageException;
|
2018-11-29 06:57:57 -05:00
|
|
|
|
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* tool to manage storage backend and stored data from CLI
|
2018-11-29 06:57:57 -05:00
|
|
|
*/
|
|
|
|
class Storage extends \Asika\SimpleConsole\Console
|
|
|
|
{
|
|
|
|
protected $helpOptions = ['h', 'help', '?'];
|
|
|
|
|
2021-10-27 14:06:04 -04:00
|
|
|
/** @var StorageManager */
|
2020-01-04 19:58:49 -05:00
|
|
|
private $storageManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StorageManager $storageManager
|
|
|
|
*/
|
|
|
|
public function __construct(StorageManager $storageManager, array $argv = [])
|
|
|
|
{
|
|
|
|
parent::__construct($argv);
|
|
|
|
|
|
|
|
$this->storageManager = $storageManager;
|
|
|
|
}
|
|
|
|
|
2018-11-29 06:57:57 -05:00
|
|
|
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".
|
|
|
|
|
2019-03-20 00:42:50 -04:00
|
|
|
bin/console storage move [table] [-n 5000]
|
2019-01-03 16:35:46 -05:00
|
|
|
Move stored data to current storage backend.
|
|
|
|
table one of "photo" or "attach". default to both
|
2019-03-20 00:42:50 -04:00
|
|
|
-n limit of processed entry batch size
|
2018-11-29 06:57:57 -05:00
|
|
|
HELP;
|
|
|
|
return $help;
|
|
|
|
}
|
|
|
|
|
2022-07-13 15:09:49 -04:00
|
|
|
protected function doExecute(): int
|
2018-11-29 06:57:57 -05:00
|
|
|
{
|
|
|
|
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 19:04:42 -04: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 06:57:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->out(sprintf('Invalid action "%s"', $this->args[0]));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:04:42 -04:00
|
|
|
protected function doList()
|
2018-11-29 06:57:57 -05:00
|
|
|
{
|
|
|
|
$rowfmt = ' %-3s | %-20s';
|
2020-01-04 19:58:49 -05:00
|
|
|
$current = $this->storageManager->getBackend();
|
2018-11-29 06:57:57 -05:00
|
|
|
$this->out(sprintf($rowfmt, 'Sel', 'Name'));
|
|
|
|
$this->out('-----------------------');
|
|
|
|
$isregisterd = false;
|
2021-08-15 18:10:37 -04:00
|
|
|
foreach ($this->storageManager->listBackends() as $name) {
|
2018-11-29 06:57:57 -05:00
|
|
|
$issel = ' ';
|
2020-09-03 16:06:52 -04:00
|
|
|
if ($current && $current::getName() == $name) {
|
2018-11-29 06:57:57 -05:00
|
|
|
$issel = '*';
|
|
|
|
$isregisterd = true;
|
|
|
|
};
|
2019-03-17 19:04:42 -04:00
|
|
|
$this->out(sprintf($rowfmt, $issel, $name));
|
2018-11-29 06:57:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($current === '') {
|
|
|
|
$this->out();
|
2019-03-23 21:47:33 -04:00
|
|
|
$this->out('This system is using legacy storage system');
|
2018-11-29 06:57:57 -05:00
|
|
|
}
|
|
|
|
if ($current !== '' && !$isregisterd) {
|
|
|
|
$this->out();
|
|
|
|
$this->out('The current storage class (' . $current . ') is not registered!');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:04:42 -04:00
|
|
|
protected function doSet()
|
2018-11-29 06:57:57 -05:00
|
|
|
{
|
2021-08-10 17:56:30 -04:00
|
|
|
if (count($this->args) !== 2 || empty($this->args[1])) {
|
2018-11-29 06:57:57 -05:00
|
|
|
throw new CommandArgsException('Invalid arguments');
|
|
|
|
}
|
|
|
|
|
2021-08-10 17:56:30 -04:00
|
|
|
$name = $this->args[1];
|
|
|
|
try {
|
|
|
|
$class = $this->storageManager->getWritableStorageByName($name);
|
2018-11-29 06:57:57 -05:00
|
|
|
|
2021-08-10 17:56:30 -04:00
|
|
|
if (!$this->storageManager->setBackend($class)) {
|
|
|
|
$this->out($class . ' is not a valid backend storage class.');
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} catch (ReferenceStorageException $exception) {
|
2018-11-29 06:57:57 -05:00
|
|
|
$this->out($name . ' is not a registered backend.');
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:04:42 -04:00
|
|
|
protected function doMove()
|
2018-11-29 06:57:57 -05:00
|
|
|
{
|
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];
|
2020-01-17 18:08:48 -05:00
|
|
|
} else {
|
|
|
|
$tables = StorageManager::TABLES;
|
2019-01-03 16:35:46 -05:00
|
|
|
}
|
2018-11-29 06:57:57 -05:00
|
|
|
|
2020-01-04 19:58:49 -05:00
|
|
|
$current = $this->storageManager->getBackend();
|
2019-03-20 00:42:50 -04:00
|
|
|
$total = 0;
|
|
|
|
|
2021-03-12 12:24:45 -05:00
|
|
|
if (is_null($current)) {
|
|
|
|
throw new StorageException(sprintf("Cannot move to legacy storage. Please select a storage backend."));
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:42:50 -04:00
|
|
|
do {
|
2020-01-04 19:58:49 -05:00
|
|
|
$moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
|
2019-03-20 00:42:50 -04: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 06:57:57 -05:00
|
|
|
}
|
|
|
|
}
|