Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.5 KiB

3 years ago
3 years ago
  1. <?php
  2. namespace Friendica\Core\Console;
  3. use Asika\SimpleConsole\CommandArgsException;
  4. use Friendica\Core\StorageManager;
  5. /**
  6. * @brief tool to manage storage backend and stored data from CLI
  7. *
  8. */
  9. class Storage extends \Asika\SimpleConsole\Console
  10. {
  11. protected $helpOptions = ['h', 'help', '?'];
  12. protected function getHelp()
  13. {
  14. $help = <<<HELP
  15. console storage - manage storage backend and stored data
  16. Synopsis
  17. bin/console storage [-h|--help|-?] [-v]
  18. bin/console storage list
  19. bin/console storage set <name>
  20. bin/console storage move
  21. HELP;
  22. return $help;
  23. }
  24. protected function doExecute()
  25. {
  26. $a = \Friendica\BaseObject::getApp();
  27. if ($this->getOption('v')) {
  28. $this->out('Executable: ' . $this->executable);
  29. $this->out('Class: ' . __CLASS__);
  30. $this->out('Arguments: ' . var_export($this->args, true));
  31. $this->out('Options: ' . var_export($this->options, true));
  32. }
  33. if (count($this->args) == 0) {
  34. $this->out($this->getHelp());
  35. return -1;
  36. }
  37. switch($this->args[0]) {
  38. case 'list':
  39. return $this->do_list();
  40. break;
  41. case 'set':
  42. return $this->do_set();
  43. break;
  44. case 'move':
  45. return $this->do_move();
  46. break;
  47. }
  48. $this->out(sprintf('Invalid action "%s"', $this->args[0]));
  49. return -1;
  50. }
  51. protected function do_list()
  52. {
  53. $rowfmt = ' %-3s | %-20s';
  54. $current = StorageManager::getBackend();
  55. $this->out(sprintf($rowfmt, 'Sel', 'Name'));
  56. $this->out('-----------------------');
  57. $isregisterd = false;
  58. foreach(StorageManager::listBackends() as $name => $class) {
  59. $issel = ' ';
  60. if ($current === $class) {
  61. $issel = '*';
  62. $isregisterd = true;
  63. };
  64. $this->out(sprintf($rowfmt, $issel , $name ));
  65. }
  66. if ($current === '') {
  67. $this->out();
  68. $this->out('This sistem is using legacy storage system');
  69. }
  70. if ($current !== '' && !$isregisterd) {
  71. $this->out();
  72. $this->out('The current storage class (' . $current . ') is not registered!');
  73. }
  74. return 0;
  75. }
  76. protected function do_set()
  77. {
  78. if (count($this->args) !== 2) {
  79. throw new CommandArgsException('Invalid arguments');
  80. }
  81. $name = $this->args[1];
  82. $class = StorageManager::getByName($name);
  83. if ($class === '') {
  84. $this->out($name . ' is not a registered backend.');
  85. return -1;
  86. }
  87. StorageManager::setBackend($class);
  88. return 0;
  89. }
  90. protected function do_move()
  91. {
  92. if (count($this->args) !== 1) {
  93. throw new CommandArgsException('Invalid arguments');
  94. }
  95. $current = StorageManager::getBackend();
  96. $r = StorageManager::move($current);
  97. $this->out(sprintf('Moved %d files', $r));
  98. }
  99. }