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