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