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