]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Storage.php
Avoid memory issue in exception
[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                 if ($this->getOption('v')) {
41                         $this->out('Executable: ' . $this->executable);
42                         $this->out('Class: ' . __CLASS__);
43                         $this->out('Arguments: ' . var_export($this->args, true));
44                         $this->out('Options: ' . var_export($this->options, true));
45                 }
46
47                 if (count($this->args) == 0) {
48                         $this->out($this->getHelp());
49                         return -1;
50                 }
51
52                 switch($this->args[0]) {
53                 case 'list':
54                         return $this->do_list();
55                         break;
56                 case 'set':
57                         return $this->do_set();
58                         break;
59                 case 'move':
60                         return $this->do_move();
61                         break;
62                 }
63
64                 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
65                 return -1;
66         }
67
68         protected function do_list()
69         {
70                 $rowfmt = ' %-3s | %-20s';
71                 $current = StorageManager::getBackend();
72                 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
73                 $this->out('-----------------------');
74                 $isregisterd = false;
75                 foreach(StorageManager::listBackends() as $name => $class) {
76                         $issel = ' ';
77                         if ($current === $class) {
78                                 $issel = '*';
79                                 $isregisterd = true;
80                         };
81                         $this->out(sprintf($rowfmt, $issel , $name ));
82                 }
83
84                 if ($current === '') {
85                         $this->out();
86                         $this->out('This sistem is using legacy storage system');
87                 }
88                 if ($current !== '' && !$isregisterd) {
89                         $this->out();
90                         $this->out('The current storage class (' . $current . ') is not registered!');
91                 }
92                 return 0;
93         }
94
95         protected function do_set()
96         {
97                 if (count($this->args) !== 2) {
98                         throw new CommandArgsException('Invalid arguments');
99                 }
100
101                 $name = $this->args[1];
102                 $class = StorageManager::getByName($name);
103
104                 if ($class === '') {
105                         $this->out($name . ' is not a registered backend.');
106                         return -1;
107                 }
108
109                 StorageManager::setBackend($class);
110                 return 0;
111         }
112
113         protected function do_move()
114         {
115                 $table = null;
116                 if (count($this->args) < 1 || count($this->args) > 2) {
117                         throw new CommandArgsException('Invalid arguments');
118                 }
119                 if (count($this->args) == 2) {
120                         $table = strtolower($this->args[1]);
121                         if (!in_array($table, ['photo', 'attach'])) {
122                                 throw new CommandArgsException('Invalid table');
123                         }
124                 }
125
126                 $current = StorageManager::getBackend();
127                 $r = StorageManager::move($current);
128                 $this->out(sprintf('Moved %d files', $r));
129         }
130 }