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