]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Storage.php
Small fixes
[friendica.git] / src / Core / Console / Storage.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Asika\SimpleConsole\CommandArgsException;
6 use Friendica\Core\StorageManager;
7
8 /**
9  * @brief tool to manage storage backend and stored data from CLI
10  *
11  */
12 class Storage extends \Asika\SimpleConsole\Console
13 {
14         protected $helpOptions = ['h', 'help', '?'];
15
16         protected function getHelp()
17         {
18                 $help = <<<HELP
19 console storage - manage storage backend and stored data
20 Synopsis
21     bin/console storage [-h|--help|-?] [-v]
22         Show this help
23     
24     bin/console storage list
25         List available storage backends
26     
27     bin/console storage set <name>
28         Set current storage backend
29             name        storage backend to use. see "list".
30     
31     bin/console storage move [table]
32         Move stored data to current storage backend.
33             table       one of "photo" or "attach". default to both
34 HELP;
35                 return $help;
36         }
37
38         protected function doExecute()
39         {
40                 $a = \Friendica\BaseObject::getApp();
41
42                 if ($this->getOption('v')) {
43                         $this->out('Executable: ' . $this->executable);
44                         $this->out('Class: ' . __CLASS__);
45                         $this->out('Arguments: ' . var_export($this->args, true));
46                         $this->out('Options: ' . var_export($this->options, true));
47                 }
48
49                 if (count($this->args) == 0) {
50                         $this->out($this->getHelp());
51                         return -1;
52                 }
53
54                 switch($this->args[0]) {
55                 case 'list':
56                         return $this->do_list();
57                         break;
58                 case 'set':
59                         return $this->do_set();
60                         break;
61                 case 'move':
62                         return $this->do_move();
63                         break;
64                 }
65
66                 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
67                 return -1;
68         }
69
70         protected function do_list()
71         {
72                 $rowfmt = ' %-3s | %-20s';
73                 $current = StorageManager::getBackend();
74                 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
75                 $this->out('-----------------------');
76                 $isregisterd = false;
77                 foreach(StorageManager::listBackends() as $name => $class) {
78                         $issel = ' ';
79                         if ($current === $class) {
80                                 $issel = '*';
81                                 $isregisterd = true;
82                         };
83                         $this->out(sprintf($rowfmt, $issel , $name ));
84                 }
85
86                 if ($current === '') {
87                         $this->out();
88                         $this->out('This sistem is using legacy storage system');
89                 }
90                 if ($current !== '' && !$isregisterd) {
91                         $this->out();
92                         $this->out('The current storage class (' . $current . ') is not registered!');
93                 }
94                 return 0;
95         }
96
97         protected function do_set()
98         {
99                 if (count($this->args) !== 2) {
100                         throw new CommandArgsException('Invalid arguments');
101                 }
102
103                 $name = $this->args[1];
104                 $class = StorageManager::getByName($name);
105
106                 if ($class === '') {
107                         $this->out($name . ' is not a registered backend.');
108                         return -1;
109                 }
110
111                 StorageManager::setBackend($class);
112                 return 0;
113         }
114
115         protected function do_move()
116         {
117                 $table = null;
118                 if (count($this->args) < 1 || count($this->args) > 2) {
119                         throw new CommandArgsException('Invalid arguments');
120                 }
121                 if (count($this->args) == 2) {
122                         $table = strtolower($this->args[1]);
123                         if (!in_array($table, ['photo', 'attach'])) {
124                                 throw new CommandArgsException('Invalid table');
125                         }
126                 }
127
128                 $current = StorageManager::getBackend();
129                 $r = StorageManager::move($current);
130                 $this->out(sprintf('Moved %d files', $r));
131         }
132 }