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 [-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]
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);
126 $start = $this->getOption(['s', 'start'], 0);
127 $count = $this->getOption(['c', 'count'], Pager::ITEMS_PER_PAGE);
129 $table = new Console_Table();
132 $table->setHeaders(['Name', 'Enabled']);
136 $table->setHeaders(['Name']);
139 $this->out($this->getHelp());
142 foreach (AddonCore::getAvailableList() as $addon) {
143 $addon_name = $addon[0];
144 $enabled = AddonCore::isEnabled($addon_name) ? "enabled" : "disabled";
147 $table->addRow([$addon_name, $enabled]);
153 $table->addRow([$addon_name]);
158 $table->addRow([$addon_name]);
163 $this->out($table->getTable());
169 * @return int Return code of this command
173 private function enable()
175 $addonname = $this->getArgument(1);
177 $addon = Strings::sanitizeFilePathItem($addonname);
178 if (!is_file("addon/$addon/$addon.php")) {
179 throw new RuntimeException($this->l10n->t('Addon not found'));
182 if (AddonCore::isEnabled($addon)) {
183 throw new RuntimeException($this->l10n->t('Addon already enabled'));
186 AddonCore::install($addon);
194 * @return int Return code of this command
198 private function disable()
200 $addonname = $this->getArgument(1);
202 $addon = Strings::sanitizeFilePathItem($addonname);
203 if (!is_file("addon/$addon/$addon.php")) {
204 throw new RuntimeException($this->l10n->t('Addon not found'));
207 if (!AddonCore::isEnabled($addon)) {
208 throw new RuntimeException($this->l10n->t('Addon already disabled'));
211 AddonCore::uninstall($addon);