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