]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Storage.php
805ef0aea9ef7e5d6d5f361e5f0288e42d04a924
[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] [-n 5000]
32         Move stored data to current storage backend.
33             table       one of "photo" or "attach". default to both
34             -n          limit of processed entry batch size
35 HELP;
36                 return $help;
37         }
38
39         protected function doExecute()
40         {
41                 if ($this->getOption('v')) {
42                         $this->out('Executable: ' . $this->executable);
43                         $this->out('Class: ' . __CLASS__);
44                         $this->out('Arguments: ' . var_export($this->args, true));
45                         $this->out('Options: ' . var_export($this->options, true));
46                 }
47
48                 if (count($this->args) == 0) {
49                         $this->out($this->getHelp());
50                         return -1;
51                 }
52
53                 switch ($this->args[0]) {
54                         case 'list':
55                                 return $this->doList();
56                                 break;
57                         case 'set':
58                                 return $this->doSet();
59                                 break;
60                         case 'move':
61                                 return $this->doMove();
62                                 break;
63                 }
64
65                 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
66                 return -1;
67         }
68
69         protected function doList()
70         {
71                 $rowfmt = ' %-3s | %-20s';
72                 $current = StorageManager::getBackend();
73                 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
74                 $this->out('-----------------------');
75                 $isregisterd = false;
76                 foreach (StorageManager::listBackends() as $name => $class) {
77                         $issel = ' ';
78                         if ($current === $class) {
79                                 $issel = '*';
80                                 $isregisterd = true;
81                         };
82                         $this->out(sprintf($rowfmt, $issel, $name));
83                 }
84
85                 if ($current === '') {
86                         $this->out();
87                         $this->out('This system is using legacy storage system');
88                 }
89                 if ($current !== '' && !$isregisterd) {
90                         $this->out();
91                         $this->out('The current storage class (' . $current . ') is not registered!');
92                 }
93                 return 0;
94         }
95
96         protected function doSet()
97         {
98                 if (count($this->args) !== 2) {
99                         throw new CommandArgsException('Invalid arguments');
100                 }
101
102                 $name = $this->args[1];
103                 $class = StorageManager::getByName($name);
104
105                 if ($class === '') {
106                         $this->out($name . ' is not a registered backend.');
107                         return -1;
108                 }
109
110                 if (!StorageManager::setBackend($class)) {
111                         $this->out($class . ' is not a valid backend storage class.');
112                         return -1;
113                 }
114
115                 return 0;
116         }
117
118         protected function doMove()
119         {
120                 $tables = null;
121                 if (count($this->args) < 1 || count($this->args) > 2) {
122                         throw new CommandArgsException('Invalid arguments');
123                 }
124
125                 if (count($this->args) == 2) {
126                         $table = strtolower($this->args[1]);
127                         if (!in_array($table, ['photo', 'attach'])) {
128                                 throw new CommandArgsException('Invalid table');
129                         }
130                         $tables = [$table];
131                 }
132
133                 $current = StorageManager::getBackend();
134                 $total = 0;
135
136                 do {
137                         $moved = StorageManager::move($current, $tables, $this->getOption('n', 5000));
138                         if ($moved) {
139                                 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
140                         }
141
142                         $total += $moved;
143                 } while ($moved);
144
145                 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));
146         }
147 }