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