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