3 use splitbrain\phpcli\Exception
;
4 use dokuwiki\Extension\CLIPlugin
;
5 use splitbrain\phpcli\Options
;
6 use splitbrain\phpcli\TableFormatter
;
7 use splitbrain\phpcli\Colors
;
10 * Class cli_plugin_extension
12 * Command Line component for the extension manager
15 * @author Andreas Gohr <andi@splitbrain.org>
17 class cli_plugin_extension
extends CLIPlugin
20 protected function setup(Options
$options)
24 "Manage plugins and templates for this DokuWiki instance\n\n" .
27 " b - bundled with DokuWiki\n" .
28 " g - installed via git\n" .
30 " u - update available\n"
34 $options->registerCommand('search', 'Search for an extension');
35 $options->registerOption('max', 'Maximum number of results (default 10)', 'm', 'number', 'search');
36 $options->registerOption('verbose', 'Show detailed extension information', 'v', false, 'search');
37 $options->registerArgument('query', 'The keyword(s) to search for', true, 'search');
40 $options->registerCommand('list', 'List installed extensions');
41 $options->registerOption('verbose', 'Show detailed extension information', 'v', false, 'list');
42 $options->registerOption('filter', 'Filter by this status', 'f', 'status', 'list');
45 $options->registerCommand('upgrade', 'Update all installed extensions to their latest versions');
48 $options->registerCommand('install', 'Install or upgrade extensions');
49 $options->registerArgument(
51 'One or more extensions to install. Either by name or download URL',
57 $options->registerCommand('uninstall', 'Uninstall a new extension');
58 $options->registerArgument('extensions...', 'One or more extensions to install', true, 'uninstall');
61 $options->registerCommand('enable', 'Enable installed extensions');
62 $options->registerArgument('extensions...', 'One or more extensions to enable', true, 'enable');
65 $options->registerCommand('disable', 'Disable installed extensions');
66 $options->registerArgument('extensions...', 'One or more extensions to disable', true, 'disable');
70 protected function main(Options
$options)
72 /** @var helper_plugin_extension_repository $repo */
73 $repo = plugin_load('helper', 'extension_repository');
74 if (!$repo->hasAccess(false)) {
75 $this->warning('Extension Repository API is not accessible, no remote info available!');
78 switch ($options->getCmd()) {
80 $ret = $this->cmdList($options->getOpt('verbose'), $options->getOpt('filter', ''));
83 $ret = $this->cmdSearch(
84 implode(' ', $options->getArgs()),
85 $options->getOpt('verbose'),
86 (int)$options->getOpt('max', 10)
90 $ret = $this->cmdInstall($options->getArgs());
93 $ret = $this->cmdUnInstall($options->getArgs());
96 $ret = $this->cmdEnable(true, $options->getArgs());
99 $ret = $this->cmdEnable(false, $options->getArgs());
102 $ret = $this->cmdUpgrade();
105 echo $options->help();
113 * Upgrade all extensions
117 protected function cmdUpgrade()
119 /* @var helper_plugin_extension_extension $ext */
120 $ext = $this->loadHelper('extension_extension');
121 $list = $this->getInstalledExtensions();
124 foreach ($list as $extname) {
125 $ext->setExtension($extname);
126 $date = $ext->getInstalledVersion();
127 $avail = $ext->getLastUpdate();
128 if ($avail && $avail > $date && !$ext->isBundled()) {
129 $ok +
= $this->cmdInstall([$extname]);
137 * Enable or disable one or more extensions
140 * @param string[] $extensions
143 protected function cmdEnable($set, $extensions)
145 /* @var helper_plugin_extension_extension $ext */
146 $ext = $this->loadHelper('extension_extension');
149 foreach ($extensions as $extname) {
150 $ext->setExtension($extname);
151 if (!$ext->isInstalled()) {
152 $this->error(sprintf('Extension %s is not installed', $ext->getID()));
158 $status = $ext->enable();
159 $msg = 'msg_enabled';
161 $status = $ext->disable();
162 $msg = 'msg_disabled';
165 if ($status !== true) {
166 $this->error($status);
170 $this->success(sprintf($this->getLang($msg), $ext->getID()));
178 * Uninstall one or more extensions
180 * @param string[] $extensions
183 protected function cmdUnInstall($extensions)
185 /* @var helper_plugin_extension_extension $ext */
186 $ext = $this->loadHelper('extension_extension');
189 foreach ($extensions as $extname) {
190 $ext->setExtension($extname);
191 if (!$ext->isInstalled()) {
192 $this->error(sprintf('Extension %s is not installed', $ext->getID()));
197 $status = $ext->uninstall();
199 $this->success(sprintf($this->getLang('msg_delete_success'), $ext->getID()));
201 $this->error(sprintf($this->getLang('msg_delete_failed'), hsc($ext->getID())));
210 * Install one or more extensions
212 * @param string[] $extensions
215 protected function cmdInstall($extensions)
217 /* @var helper_plugin_extension_extension $ext */
218 $ext = $this->loadHelper('extension_extension');
221 foreach ($extensions as $extname) {
224 if (preg_match("/^https?:\/\//i", $extname)) {
226 $installed = $ext->installFromURL($extname, true);
227 } catch (Exception
$e) {
228 $this->error($e->getMessage());
232 $ext->setExtension($extname);
234 if (!$ext->getDownloadURL()) {
237 sprintf('Could not find download for %s', $ext->getID())
243 $installed = $ext->installOrUpdate();
244 } catch (Exception
$e) {
245 $this->error($e->getMessage());
250 foreach ($installed as $info) {
253 $this->getLang('msg_' . $info['type'] . '_' . $info['action'] . '_success'),
263 * Search for an extension
265 * @param string $query
266 * @param bool $showdetails
271 protected function cmdSearch($query, $showdetails, $max)
273 /** @var helper_plugin_extension_repository $repository */
274 $repository = $this->loadHelper('extension_repository');
275 $result = $repository->search($query);
277 $result = array_slice($result, 0, $max);
280 $this->listExtensions($result, $showdetails);
285 * @param bool $showdetails
286 * @param string $filter
290 protected function cmdList($showdetails, $filter)
292 $list = $this->getInstalledExtensions();
293 $this->listExtensions($list, $showdetails, $filter);
299 * Get all installed extensions
303 protected function getInstalledExtensions()
305 /** @var Doku_Plugin_Controller $plugin_controller */
306 global $plugin_controller;
307 $pluginlist = $plugin_controller->getList('', true);
308 $tpllist = glob(DOKU_INC
. 'lib/tpl/*', GLOB_ONLYDIR
);
309 $tpllist = array_map(static fn($path) => 'template:' . basename($path), $tpllist);
311 $list = array_merge($pluginlist, $tpllist);
317 * List the given extensions
319 * @param string[] $list
320 * @param bool $details display details
321 * @param string $filter filter for this status
324 protected function listExtensions($list, $details, $filter = '')
326 /** @var helper_plugin_extension_extension $ext */
327 $ext = $this->loadHelper('extension_extension');
328 $tr = new TableFormatter($this->colors
);
331 foreach ($list as $name) {
332 $ext->setExtension($name);
335 if ($ext->isInstalled()) {
336 $date = $ext->getInstalledVersion();
337 $avail = $ext->getLastUpdate();
339 if ($avail && $avail > $date) {
340 $vcolor = Colors
::C_RED
;
343 $vcolor = Colors
::C_GREEN
;
345 if ($ext->isGitControlled()) $status = 'g';
346 if ($ext->isBundled()) $status = 'b';
347 if ($ext->isEnabled()) {
348 $ecolor = Colors
::C_BROWN
;
350 $ecolor = Colors
::C_DARKGRAY
;
355 $date = $ext->getLastUpdate();
359 if ($filter && strpos($status, $filter) === false) {
370 $this->getLang('extensionby'),
371 $ext->getDisplayName(),
372 $this->colors
->wrap($ext->getAuthor(), Colors
::C_PURPLE
)
383 if (!$details) continue;
387 ['', $ext->getDescription()],
388 [null, Colors
::C_CYAN
]