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