]> git.mxchange.org Git - friendica.git/blob - src/Console/Addon.php
Add "addon" console command to enable and disable addons
[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 [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
59         bin/console addon list enabled [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
60         bin/console addon list disabled [-s|--start=0] [-c|--count=50] [-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                 $start = $this->getOption(['s', 'start'], 0);
127                 $count = $this->getOption(['c', 'count'], Pager::ITEMS_PER_PAGE);
128
129                 $table = new Console_Table();
130                 switch ($subCmd) {
131                         case 'all':
132                                 $table->setHeaders(['Name', 'Enabled']);
133                                 break;
134                         case 'enabled':
135                         case 'disabled':
136                                 $table->setHeaders(['Name']);
137                                 break;
138                         default:
139                                 $this->out($this->getHelp());
140                                 return false;
141                 }
142                 foreach (AddonCore::getAvailableList() as $addon) {
143                         $addon_name = $addon[0];
144                         $enabled = AddonCore::isEnabled($addon_name) ? "enabled" : "disabled";
145                         switch ($subCmd) {
146                                 case 'all':
147                                         $table->addRow([$addon_name, $enabled]);
148                                         break;
149                                 case 'enabled':
150                                         if (!$enabled) {
151                                                 continue 2;
152                                         }
153                                         $table->addRow([$addon_name]);
154                                 case 'disabled':
155                                         if ($enabled) {
156                                                 continue 2;
157                                         }
158                                         $table->addRow([$addon_name]);
159                                         break;
160                         }
161
162                 }
163                 $this->out($table->getTable());
164         }
165
166         /**
167          * Enables an addon
168          *
169          * @return int Return code of this command
170          *
171          * @throws \Exception
172          */
173         private function enable()
174         {
175                 $addonname = $this->getArgument(1);
176
177                 $addon = Strings::sanitizeFilePathItem($addonname);
178                 if (!is_file("addon/$addon/$addon.php")) {
179                         throw new RuntimeException($this->l10n->t('Addon not found'));
180                 }
181
182                 if (AddonCore::isEnabled($addon)) {
183                         throw new RuntimeException($this->l10n->t('Addon already enabled'));
184                 }
185
186                 AddonCore::install($addon);
187
188                 return 0;
189         }
190
191         /**
192          * Disables an addon
193          *
194          * @return int Return code of this command
195          *
196          * @throws \Exception
197          */
198         private function disable()
199         {
200                 $addonname = $this->getArgument(1);
201
202                 $addon = Strings::sanitizeFilePathItem($addonname);
203                 if (!is_file("addon/$addon/$addon.php")) {
204                         throw new RuntimeException($this->l10n->t('Addon not found'));
205                 }
206
207                 if (!AddonCore::isEnabled($addon)) {
208                         throw new RuntimeException($this->l10n->t('Addon already disabled'));
209                 }
210
211                 AddonCore::uninstall($addon);
212
213                 return 0;
214         }
215 }