xref: /dokuwiki/bin/plugin.php (revision d868eb89f182718a31113373a6272670bd7f8012)
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
158c7c53b0SAndreas Gohrclass PluginCLI extends CLI
168c7c53b0SAndreas 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     */
24*d868eb89SAndreas Gohr    protected function setup(Options $options)
25*d868eb89SAndreas Gohr    {
26eb8d789eSAndreas Gohr        $options->setHelp('Excecutes Plugin command line tools');
27eb8d789eSAndreas Gohr        $options->registerArgument('plugin', 'The plugin CLI you want to run. Leave off to see list', false);
28eb8d789eSAndreas Gohr    }
29eb8d789eSAndreas Gohr
30eb8d789eSAndreas Gohr    /**
31eb8d789eSAndreas Gohr     * Your main program
32eb8d789eSAndreas Gohr     *
33eb8d789eSAndreas Gohr     * Arguments and options have been parsed when this is run
34eb8d789eSAndreas Gohr     *
35eb8d789eSAndreas Gohr     * @param Options $options
36eb8d789eSAndreas Gohr     * @return void
37eb8d789eSAndreas Gohr     */
38*d868eb89SAndreas Gohr    protected function main(Options $options)
39*d868eb89SAndreas Gohr    {
40eb8d789eSAndreas Gohr        global $argv;
41eb8d789eSAndreas Gohr        $argv = $options->getArgs();
42eb8d789eSAndreas Gohr
43eb8d789eSAndreas Gohr        if($argv) {
44eb8d789eSAndreas Gohr            $plugin = $this->loadPlugin($argv[0]);
45b1f206e1SAndreas Gohr            if($plugin instanceof CLIPlugin) {
46eb8d789eSAndreas Gohr                $plugin->run();
47eb8d789eSAndreas Gohr            } else {
48eb8d789eSAndreas Gohr                $this->fatal('Command {cmd} not found.', ['cmd' => $argv[0]]);
49eb8d789eSAndreas Gohr            }
50eb8d789eSAndreas Gohr        } else {
51eb8d789eSAndreas Gohr            echo $options->help();
52eb8d789eSAndreas Gohr            $this->listPlugins();
53eb8d789eSAndreas Gohr        }
54eb8d789eSAndreas Gohr    }
55eb8d789eSAndreas Gohr
56eb8d789eSAndreas Gohr    /**
57eb8d789eSAndreas Gohr     * List available plugins
58eb8d789eSAndreas Gohr     */
59*d868eb89SAndreas Gohr    protected function listPlugins()
60*d868eb89SAndreas Gohr    {
613a7140a1SAndreas Gohr        /** @var PluginController $plugin_controller */
62eb8d789eSAndreas Gohr        global $plugin_controller;
63eb8d789eSAndreas Gohr
64eb8d789eSAndreas Gohr        echo "\n";
65eb8d789eSAndreas Gohr        echo "\n";
66eb8d789eSAndreas Gohr        echo $this->colors->wrap('AVAILABLE PLUGINS:', Colors::C_BROWN);
67eb8d789eSAndreas Gohr        echo "\n";
68eb8d789eSAndreas Gohr
69eb8d789eSAndreas Gohr        $list = $plugin_controller->getList('cli');
70eb8d789eSAndreas Gohr        sort($list);
71b1f206e1SAndreas Gohr        if($list === []) {
72eb8d789eSAndreas Gohr            echo $this->colors->wrap("  No plugins providing CLI components available\n", Colors::C_RED);
73eb8d789eSAndreas Gohr        } else {
74b1f206e1SAndreas Gohr            $tf = new TableFormatter($this->colors);
75eb8d789eSAndreas Gohr
76eb8d789eSAndreas Gohr            foreach($list as $name) {
77eb8d789eSAndreas Gohr                $plugin = $this->loadPlugin($name);
78b1f206e1SAndreas Gohr                if(!$plugin instanceof CLIPlugin) continue;
79eb8d789eSAndreas Gohr                $info = $plugin->getInfo();
80eb8d789eSAndreas Gohr
81eb8d789eSAndreas Gohr                echo $tf->format(
82eb8d789eSAndreas Gohr                    [2, '30%', '*'],
83eb8d789eSAndreas Gohr                    ['', $name, $info['desc']],
84eb8d789eSAndreas Gohr                    ['', Colors::C_CYAN, '']
85eb8d789eSAndreas Gohr
86eb8d789eSAndreas Gohr                );
87eb8d789eSAndreas Gohr            }
88eb8d789eSAndreas Gohr        }
89eb8d789eSAndreas Gohr    }
90eb8d789eSAndreas Gohr
91eb8d789eSAndreas Gohr    /**
92eb8d789eSAndreas Gohr     * Instantiate a CLI plugin
93eb8d789eSAndreas Gohr     *
94eb8d789eSAndreas Gohr     * @param string $name
95b1f206e1SAndreas Gohr     * @return CLIPlugin|null
96eb8d789eSAndreas Gohr     */
97*d868eb89SAndreas Gohr    protected function loadPlugin($name)
98*d868eb89SAndreas Gohr    {
997bbc0dcaSAndreas Gohr        if(plugin_isdisabled($name)) return null;
1007bbc0dcaSAndreas Gohr
101eb8d789eSAndreas Gohr        // execute the plugin CLI
102eb8d789eSAndreas Gohr        $class = "cli_plugin_$name";
103eb8d789eSAndreas Gohr        if(class_exists($class)) {
104eb8d789eSAndreas Gohr            return new $class();
105eb8d789eSAndreas Gohr        }
106eb8d789eSAndreas Gohr        return null;
107eb8d789eSAndreas Gohr    }
108eb8d789eSAndreas Gohr}
109eb8d789eSAndreas Gohr
110eb8d789eSAndreas Gohr// Main
111eb8d789eSAndreas Gohr$cli = new PluginCLI();
112eb8d789eSAndreas Gohr$cli->run();
113