1#!/usr/bin/env php
2<?php
3
4use dokuwiki\Extension\CLIPlugin;
5use dokuwiki\Extension\PluginController;
6use splitbrain\phpcli\CLI;
7use splitbrain\phpcli\Colors;
8use splitbrain\phpcli\Options;
9use splitbrain\phpcli\TableFormatter;
10
11if (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
12define('NOSESSION', 1);
13require_once(DOKU_INC . 'inc/init.php');
14
15class PluginCLI extends CLI
16{
17    /**
18     * Register options and arguments on the given $options object
19     *
20     * @param Options $options
21     * @return void
22     */
23    protected function setup(Options $options)
24    {
25        $options->setHelp('Excecutes Plugin command line tools');
26        $options->registerArgument('plugin', 'The plugin CLI you want to run. Leave off to see list', false);
27    }
28
29    /**
30     * Your main program
31     *
32     * Arguments and options have been parsed when this is run
33     *
34     * @param Options $options
35     * @return void
36     */
37    protected function main(Options $options)
38    {
39        global $argv;
40        $argv = $options->getArgs();
41
42        if ($argv) {
43            $plugin = $this->loadPlugin($argv[0]);
44            if ($plugin instanceof CLIPlugin) {
45                $plugin->run();
46            } else {
47                $this->fatal('Command {cmd} not found.', ['cmd' => $argv[0]]);
48            }
49        } else {
50            echo $options->help();
51            $this->listPlugins();
52        }
53    }
54
55    /**
56     * List available plugins
57     */
58    protected function listPlugins()
59    {
60        /** @var PluginController $plugin_controller */
61        global $plugin_controller;
62
63        echo "\n";
64        echo "\n";
65        echo $this->colors->wrap('AVAILABLE PLUGINS:', Colors::C_BROWN);
66        echo "\n";
67
68        $list = $plugin_controller->getList('cli');
69        sort($list);
70        if ($list === []) {
71            echo $this->colors->wrap("  No plugins providing CLI components available\n", Colors::C_RED);
72        } else {
73            $tf = new TableFormatter($this->colors);
74
75            foreach ($list as $name) {
76                $plugin = $this->loadPlugin($name);
77                if (!$plugin instanceof CLIPlugin) continue;
78                $info = $plugin->getInfo();
79
80                echo $tf->format(
81                    [2, '30%', '*'],
82                    ['', $name, $info['desc']],
83                    ['', Colors::C_CYAN, '']
84                );
85            }
86        }
87    }
88
89    /**
90     * Instantiate a CLI plugin
91     *
92     * @param string $name
93     * @return CLIPlugin|null
94     */
95    protected function loadPlugin($name)
96    {
97        [$basename] = explode('_', $name); // might be a sub component
98        if (plugin_isdisabled($basename)) return null;
99
100        // execute the plugin CLI
101        $class = "cli_plugin_$name";
102        if (class_exists($class)) {
103            return new $class();
104        }
105        return null;
106    }
107}
108
109// Main
110$cli = new PluginCLI();
111$cli->run();
112