]> git.mxchange.org Git - friendica.git/blob - src/Console/Addon.php
Inverted condition
[friendica.git] / src / Console / Addon.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2021, 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 Console_Table;
25 use Friendica\App;
26 use Friendica\Content\Pager;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Addon as AddonCore;
29 use Friendica\Database\Database;
30 use Friendica\Util\Strings;
31 use RuntimeException;
32
33 /**
34  * tool to manage addons on the current node
35  */
36 class Addon extends \Asika\SimpleConsole\Console
37 {
38         protected $helpOptions = ['h', 'help', '?'];
39
40         /**
41          * @var App\Mode
42          */
43         private $appMode;
44         /**
45          * @var L10n
46          */
47         private $l10n;
48         /**
49          * @var Database
50          */
51         private $dba;
52
53         protected function getHelp()
54         {
55                 $help = <<<HELP
56 console user - Modify addon settings per console commands.
57 Usage
58         bin/console addon list all [-h|--help|-?] [-v]
59         bin/console addon list enabled [-h|--help|-?] [-v]
60         bin/console addon list disabled [-h|--help|-?] [-v]
61         bin/console addon enable <addonname> [-h|--help|-?] [-v]
62         bin/console addon disable <addonname> [-h|--help|-?] [-v]
63
64 Description
65         Modify addon settings per console commands.
66
67 Options
68     -h|--help|-? Show help information
69     -v           Show more debug information
70 HELP;
71                 return $help;
72         }
73
74         public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
75         {
76                 parent::__construct($argv);
77
78                 $this->appMode     = $appMode;
79                 $this->l10n        = $l10n;
80                 $this->dba         = $dba;
81
82                 AddonCore::loadAddons();
83         }
84
85         protected function doExecute()
86         {
87                 if ($this->getOption('v')) {
88                         $this->out('Class: ' . __CLASS__);
89                         $this->out('Arguments: ' . var_export($this->args, true));
90                         $this->out('Options: ' . var_export($this->options, true));
91                 }
92
93                 if (count($this->args) == 0) {
94                         $this->out($this->getHelp());
95                         return 0;
96                 }
97
98                 if ($this->appMode->isInstall()) {
99                         throw new RuntimeException('Database isn\'t ready or populated yet');
100                 }
101
102                 $command = $this->getArgument(0);
103
104                 switch ($command) {
105                         case 'list':
106                                 return $this->list();
107                         case 'enable':
108                                 return $this->enable();
109                         case 'disable':
110                                 return $this->disable();
111                         default:
112                                 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
113                 }
114         }
115
116         /**
117          * Lists plugins
118          *
119          * @return int Return code of this command
120          *
121          * @throws \Exception
122          */
123         private function list()
124         {
125                 $subCmd = $this->getArgument(1);
126
127                 $table = new Console_Table();
128                 switch ($subCmd) {
129                         case 'all':
130                                 $table->setHeaders(['Name', 'Enabled']);
131                                 break;
132                         case 'enabled':
133                         case 'disabled':
134                                 $table->setHeaders(['Name']);
135                                 break;
136                         default:
137                                 $this->out($this->getHelp());
138                                 return false;
139                 }
140                 foreach (AddonCore::getAvailableList() as $addon) {
141                         $addon_name = $addon[0];
142                         $enabled = AddonCore::isEnabled($addon_name) ? "enabled" : "disabled";
143                         switch ($subCmd) {
144                                 case 'all':
145                                         $table->addRow([$addon_name, $enabled]);
146                                         break;
147                                 case 'enabled':
148                                         if (!$enabled) {
149                                                 continue 2;
150                                         }
151                                         $table->addRow([$addon_name]);
152                                 case 'disabled':
153                                         if ($enabled) {
154                                                 continue 2;
155                                         }
156                                         $table->addRow([$addon_name]);
157                                         break;
158                         }
159
160                 }
161                 $this->out($table->getTable());
162         }
163
164         /**
165          * Enables an addon
166          *
167          * @return int Return code of this command
168          *
169          * @throws \Exception
170          */
171         private function enable()
172         {
173                 $addonname = $this->getArgument(1);
174
175                 $addon = Strings::sanitizeFilePathItem($addonname);
176                 if (!is_file("addon/$addon/$addon.php")) {
177                         throw new RuntimeException($this->l10n->t('Addon not found'));
178                 }
179
180                 if (AddonCore::isEnabled($addon)) {
181                         throw new RuntimeException($this->l10n->t('Addon already enabled'));
182                 }
183
184                 AddonCore::install($addon);
185
186                 return 0;
187         }
188
189         /**
190          * Disables an addon
191          *
192          * @return int Return code of this command
193          *
194          * @throws \Exception
195          */
196         private function disable()
197         {
198                 $addonname = $this->getArgument(1);
199
200                 $addon = Strings::sanitizeFilePathItem($addonname);
201                 if (!is_file("addon/$addon/$addon.php")) {
202                         throw new RuntimeException($this->l10n->t('Addon not found'));
203                 }
204
205                 if (!AddonCore::isEnabled($addon)) {
206                         throw new RuntimeException($this->l10n->t('Addon already disabled'));
207                 }
208
209                 AddonCore::uninstall($addon);
210
211                 return 0;
212         }
213 }