]> git.mxchange.org Git - friendica.git/blob - src/Console/Storage.php
Merge pull request #8727 from MrPetovan/task/8676-ap-attachments
[friendica.git] / src / Console / Storage.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Asika\SimpleConsole\CommandArgsException;
25 use Friendica\Core\StorageManager;
26
27 /**
28  * tool to manage storage backend and stored data from CLI
29  */
30 class Storage extends \Asika\SimpleConsole\Console
31 {
32         protected $helpOptions = ['h', 'help', '?'];
33
34         /** @var StorageManager */
35         private $storageManager;
36
37         /**
38          * @param StorageManager $storageManager
39          */
40         public function __construct(StorageManager $storageManager, array $argv = [])
41         {
42                 parent::__construct($argv);
43
44                 $this->storageManager = $storageManager;
45         }
46
47         protected function getHelp()
48         {
49                 $help = <<<HELP
50 console storage - manage storage backend and stored data
51 Synopsis
52     bin/console storage [-h|--help|-?] [-v]
53         Show this help
54     
55     bin/console storage list
56         List available storage backends
57     
58     bin/console storage set <name>
59         Set current storage backend
60             name        storage backend to use. see "list".
61     
62     bin/console storage move [table] [-n 5000]
63         Move stored data to current storage backend.
64             table       one of "photo" or "attach". default to both
65             -n          limit of processed entry batch size
66 HELP;
67                 return $help;
68         }
69
70         protected function doExecute()
71         {
72                 if ($this->getOption('v')) {
73                         $this->out('Executable: ' . $this->executable);
74                         $this->out('Class: ' . __CLASS__);
75                         $this->out('Arguments: ' . var_export($this->args, true));
76                         $this->out('Options: ' . var_export($this->options, true));
77                 }
78
79                 if (count($this->args) == 0) {
80                         $this->out($this->getHelp());
81                         return -1;
82                 }
83
84                 switch ($this->args[0]) {
85                         case 'list':
86                                 return $this->doList();
87                                 break;
88                         case 'set':
89                                 return $this->doSet();
90                                 break;
91                         case 'move':
92                                 return $this->doMove();
93                                 break;
94                 }
95
96                 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
97                 return -1;
98         }
99
100         protected function doList()
101         {
102                 $rowfmt = ' %-3s | %-20s';
103                 $current = $this->storageManager->getBackend();
104                 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
105                 $this->out('-----------------------');
106                 $isregisterd = false;
107                 foreach ($this->storageManager->listBackends() as $name => $class) {
108                         $issel = ' ';
109                         if ($current::getName() == $name) {
110                                 $issel = '*';
111                                 $isregisterd = true;
112                         };
113                         $this->out(sprintf($rowfmt, $issel, $name));
114                 }
115
116                 if ($current === '') {
117                         $this->out();
118                         $this->out('This system is using legacy storage system');
119                 }
120                 if ($current !== '' && !$isregisterd) {
121                         $this->out();
122                         $this->out('The current storage class (' . $current . ') is not registered!');
123                 }
124                 return 0;
125         }
126
127         protected function doSet()
128         {
129                 if (count($this->args) !== 2) {
130                         throw new CommandArgsException('Invalid arguments');
131                 }
132
133                 $name = $this->args[1];
134                 $class = $this->storageManager->getByName($name);
135
136                 if ($class === '') {
137                         $this->out($name . ' is not a registered backend.');
138                         return -1;
139                 }
140
141                 if (!$this->storageManager->setBackend($class)) {
142                         $this->out($class . ' is not a valid backend storage class.');
143                         return -1;
144                 }
145
146                 return 0;
147         }
148
149         protected function doMove()
150         {
151                 if (count($this->args) < 1 || count($this->args) > 2) {
152                         throw new CommandArgsException('Invalid arguments');
153                 }
154
155                 if (count($this->args) == 2) {
156                         $table = strtolower($this->args[1]);
157                         if (!in_array($table, ['photo', 'attach'])) {
158                                 throw new CommandArgsException('Invalid table');
159                         }
160                         $tables = [$table];
161                 } else {
162                         $tables = StorageManager::TABLES;
163                 }
164
165                 $current = $this->storageManager->getBackend();
166                 $total = 0;
167
168                 do {
169                         $moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
170                         if ($moved) {
171                                 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
172                         }
173
174                         $total += $moved;
175                 } while ($moved);
176
177                 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));
178         }
179 }