]> git.mxchange.org Git - friendica.git/blob - src/Console/Storage.php
Move server domain pattern blocklist features to its own class
[friendica.git] / src / Console / Storage.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Storage\Repository\StorageManager;
26 use Friendica\Core\Storage\Exception\ReferenceStorageException;
27 use Friendica\Core\Storage\Exception\StorageException;
28
29 /**
30  * tool to manage storage backend and stored data from CLI
31  */
32 class Storage extends \Asika\SimpleConsole\Console
33 {
34         protected $helpOptions = ['h', 'help', '?'];
35
36         /** @var StorageManager */
37         private $storageManager;
38
39         /**
40          * @param StorageManager $storageManager
41          */
42         public function __construct(StorageManager $storageManager, array $argv = [])
43         {
44                 parent::__construct($argv);
45
46                 $this->storageManager = $storageManager;
47         }
48
49         protected function getHelp()
50         {
51                 $help = <<<HELP
52 console storage - manage storage backend and stored data
53 Synopsis
54     bin/console storage [-h|--help|-?] [-v]
55         Show this help
56     
57     bin/console storage list
58         List available storage backends
59     
60     bin/console storage set <name>
61         Set current storage backend
62             name        storage backend to use. see "list".
63     
64     bin/console storage move [table] [-n 5000]
65         Move stored data to current storage backend.
66             table       one of "photo" or "attach". default to both
67             -n          limit of processed entry batch size
68 HELP;
69                 return $help;
70         }
71
72         protected function doExecute(): int
73         {
74                 if ($this->getOption('v')) {
75                         $this->out('Executable: ' . $this->executable);
76                         $this->out('Class: ' . __CLASS__);
77                         $this->out('Arguments: ' . var_export($this->args, true));
78                         $this->out('Options: ' . var_export($this->options, true));
79                 }
80
81                 if (count($this->args) == 0) {
82                         $this->out($this->getHelp());
83                         return -1;
84                 }
85
86                 switch ($this->args[0]) {
87                         case 'list':
88                                 return $this->doList();
89                                 break;
90                         case 'set':
91                                 return $this->doSet();
92                                 break;
93                         case 'move':
94                                 return $this->doMove();
95                                 break;
96                 }
97
98                 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
99                 return -1;
100         }
101
102         protected function doList()
103         {
104                 $rowfmt = ' %-3s | %-20s';
105                 $current = $this->storageManager->getBackend();
106                 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
107                 $this->out('-----------------------');
108                 $isregisterd = false;
109                 foreach ($this->storageManager->listBackends() as $name) {
110                         $issel = ' ';
111                         if ($current && $current::getName() == $name) {
112                                 $issel = '*';
113                                 $isregisterd = true;
114                         };
115                         $this->out(sprintf($rowfmt, $issel, $name));
116                 }
117
118                 if ($current === '') {
119                         $this->out();
120                         $this->out('This system is using legacy storage system');
121                 }
122                 if ($current !== '' && !$isregisterd) {
123                         $this->out();
124                         $this->out('The current storage class (' . $current . ') is not registered!');
125                 }
126                 return 0;
127         }
128
129         protected function doSet()
130         {
131                 if (count($this->args) !== 2 || empty($this->args[1])) {
132                         throw new CommandArgsException('Invalid arguments');
133                 }
134
135                 $name = $this->args[1];
136                 try {
137                         $class = $this->storageManager->getWritableStorageByName($name);
138
139                         if (!$this->storageManager->setBackend($class)) {
140                                 $this->out($class . ' is not a valid backend storage class.');
141                                 return -1;
142                         }
143                 } catch (ReferenceStorageException $exception) {
144                         $this->out($name . ' is not a registered backend.');
145                         return -1;
146                 }
147
148                 return 0;
149         }
150
151         protected function doMove()
152         {
153                 if (count($this->args) < 1 || count($this->args) > 2) {
154                         throw new CommandArgsException('Invalid arguments');
155                 }
156
157                 if (count($this->args) == 2) {
158                         $table = strtolower($this->args[1]);
159                         if (!in_array($table, ['photo', 'attach'])) {
160                                 throw new CommandArgsException('Invalid table');
161                         }
162                         $tables = [$table];
163                 } else {
164                         $tables = StorageManager::TABLES;
165                 }
166
167                 $current = $this->storageManager->getBackend();
168                 $total = 0;
169
170                 if (is_null($current)) {
171                         throw new StorageException(sprintf("Cannot move to legacy storage. Please select a storage backend."));
172                 }
173
174                 do {
175                         $moved = $this->storageManager->move($current, $tables, $this->getOption('n', 5000));
176                         if ($moved) {
177                                 $this->out(date('[Y-m-d H:i:s] ') . sprintf('Moved %d files', $moved));
178                         }
179
180                         $total += $moved;
181                 } while ($moved);
182
183                 $this->out(sprintf(date('[Y-m-d H:i:s] ') . 'Moved %d files total', $total));
184         }
185 }