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