3 * @copyright Copyright (C) 2021, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Console;
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;
34 * tool to manage addons on the current node
36 class Addon extends \Asika\SimpleConsole\Console
38 protected $helpOptions = ['h', 'help', '?'];
53 protected function getHelp()
56 console user - Modify addon settings per console commands.
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]
65 Modify addon settings per console commands.
68 -h|--help|-? Show help information
69 -v Show more debug information
74 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
76 parent::__construct($argv);
78 $this->appMode = $appMode;
82 AddonCore::loadAddons();
85 protected function doExecute()
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));
93 if (count($this->args) == 0) {
94 $this->out($this->getHelp());
98 if ($this->appMode->isInstall()) {
99 throw new RuntimeException('Database isn\'t ready or populated yet');
102 $command = $this->getArgument(0);
106 return $this->list();
108 return $this->enable();
110 return $this->disable();
112 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
119 * @return int Return code of this command
123 private function list()
125 $subCmd = $this->getArgument(1);
127 $table = new Console_Table();
130 $table->setHeaders(['Name', 'Enabled']);
134 $table->setHeaders(['Name']);
137 $this->out($this->getHelp());
140 foreach (AddonCore::getAvailableList() as $addon) {
141 $addon_name = $addon[0];
142 $enabled = AddonCore::isEnabled($addon_name) ? "enabled" : "disabled";
145 $table->addRow([$addon_name, $enabled]);
151 $table->addRow([$addon_name]);
156 $table->addRow([$addon_name]);
161 $this->out($table->getTable());
167 * @return int Return code of this command
171 private function enable()
173 $addonname = $this->getArgument(1);
175 $addon = Strings::sanitizeFilePathItem($addonname);
176 if (!is_file("addon/$addon/$addon.php")) {
177 throw new RuntimeException($this->l10n->t('Addon not found'));
180 if (AddonCore::isEnabled($addon)) {
181 throw new RuntimeException($this->l10n->t('Addon already enabled'));
184 AddonCore::install($addon);
192 * @return int Return code of this command
196 private function disable()
198 $addonname = $this->getArgument(1);
200 $addon = Strings::sanitizeFilePathItem($addonname);
201 if (!is_file("addon/$addon/$addon.php")) {
202 throw new RuntimeException($this->l10n->t('Addon not found'));
205 if (!AddonCore::isEnabled($addon)) {
206 throw new RuntimeException($this->l10n->t('Addon already disabled'));
209 AddonCore::uninstall($addon);