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