1a8d2f3cbSAndreas Gohr<?php 2a8d2f3cbSAndreas Gohr 38553d24dSAndreas Gohruse dokuwiki\Extension\CLIPlugin; 47c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Exception as ExtensionException; 57c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Extension; 625d28a01SAndreas Gohruse dokuwiki\plugin\extension\Installer; 77c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Local; 8*4fd6a1d7SAndreas Gohruse dokuwiki\plugin\extension\Notice; 97c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Repository; 107c9966a5SAndreas Gohruse splitbrain\phpcli\Colors; 117c9966a5SAndreas Gohruse splitbrain\phpcli\Exception; 12fe2dcfd5SAndreas Gohruse splitbrain\phpcli\Options; 13fe2dcfd5SAndreas Gohruse splitbrain\phpcli\TableFormatter; 14a8d2f3cbSAndreas Gohr 15a8d2f3cbSAndreas Gohr/** 16a8d2f3cbSAndreas Gohr * Class cli_plugin_extension 17a8d2f3cbSAndreas Gohr * 18a8d2f3cbSAndreas Gohr * Command Line component for the extension manager 19a8d2f3cbSAndreas Gohr * 20a8d2f3cbSAndreas Gohr * @license GPL2 21a8d2f3cbSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 22a8d2f3cbSAndreas Gohr */ 238553d24dSAndreas Gohrclass cli_plugin_extension extends CLIPlugin 24a8d2f3cbSAndreas Gohr{ 25a8d2f3cbSAndreas Gohr /** @inheritdoc */ 26fe2dcfd5SAndreas Gohr protected function setup(Options $options) 27a8d2f3cbSAndreas Gohr { 28a8d2f3cbSAndreas Gohr // general setup 297c9966a5SAndreas Gohr $options->useCompactHelp(); 30b9daa2f5SAndreas Gohr $options->setHelp( 31b9daa2f5SAndreas Gohr "Manage plugins and templates for this DokuWiki instance\n\n" . 32b9daa2f5SAndreas Gohr "Status codes:\n" . 33*4fd6a1d7SAndreas Gohr " i - installed " . Notice::ICONS[Notice::SECURITY] . " - security issue\n" . 34*4fd6a1d7SAndreas Gohr " b - bundled with DokuWiki " . Notice::ICONS[Notice::ERROR] . " - extension error\n" . 35*4fd6a1d7SAndreas Gohr " g - installed via git " . Notice::ICONS[Notice::WARNING] . " - extension warning\n" . 36*4fd6a1d7SAndreas Gohr " d - disabled " . Notice::ICONS[Notice::INFO] . " - extension info\n" . 37160d3688SAndreas Gohr " u - update available\n" 38b9daa2f5SAndreas Gohr ); 39a8d2f3cbSAndreas Gohr 40a8d2f3cbSAndreas Gohr // search 41a8d2f3cbSAndreas Gohr $options->registerCommand('search', 'Search for an extension'); 42a8d2f3cbSAndreas Gohr $options->registerOption('max', 'Maximum number of results (default 10)', 'm', 'number', 'search'); 43a8d2f3cbSAndreas Gohr $options->registerOption('verbose', 'Show detailed extension information', 'v', false, 'search'); 44a8d2f3cbSAndreas Gohr $options->registerArgument('query', 'The keyword(s) to search for', true, 'search'); 45a8d2f3cbSAndreas Gohr 46a8d2f3cbSAndreas Gohr // list 47a8d2f3cbSAndreas Gohr $options->registerCommand('list', 'List installed extensions'); 48a8d2f3cbSAndreas Gohr $options->registerOption('verbose', 'Show detailed extension information', 'v', false, 'list'); 49b9daa2f5SAndreas Gohr $options->registerOption('filter', 'Filter by this status', 'f', 'status', 'list'); 50a8d2f3cbSAndreas Gohr 51a8d2f3cbSAndreas Gohr // upgrade 52a8d2f3cbSAndreas Gohr $options->registerCommand('upgrade', 'Update all installed extensions to their latest versions'); 53a8d2f3cbSAndreas Gohr 54a8d2f3cbSAndreas Gohr // install 55a8d2f3cbSAndreas Gohr $options->registerCommand('install', 'Install or upgrade extensions'); 56dccd6b2bSAndreas Gohr $options->registerArgument( 57dccd6b2bSAndreas Gohr 'extensions...', 58dccd6b2bSAndreas Gohr 'One or more extensions to install. Either by name or download URL', 59dccd6b2bSAndreas Gohr true, 60dccd6b2bSAndreas Gohr 'install' 61e2170488SAndreas Gohr ); 62a8d2f3cbSAndreas Gohr 63a8d2f3cbSAndreas Gohr // uninstall 64a8d2f3cbSAndreas Gohr $options->registerCommand('uninstall', 'Uninstall a new extension'); 65a8d2f3cbSAndreas Gohr $options->registerArgument('extensions...', 'One or more extensions to install', true, 'uninstall'); 66a8d2f3cbSAndreas Gohr 67a8d2f3cbSAndreas Gohr // enable 68a8d2f3cbSAndreas Gohr $options->registerCommand('enable', 'Enable installed extensions'); 69a8d2f3cbSAndreas Gohr $options->registerArgument('extensions...', 'One or more extensions to enable', true, 'enable'); 70a8d2f3cbSAndreas Gohr 71a8d2f3cbSAndreas Gohr // disable 72a8d2f3cbSAndreas Gohr $options->registerCommand('disable', 'Disable installed extensions'); 73a8d2f3cbSAndreas Gohr $options->registerArgument('extensions...', 'One or more extensions to disable', true, 'disable'); 74a8d2f3cbSAndreas Gohr } 75a8d2f3cbSAndreas Gohr 76a8d2f3cbSAndreas Gohr /** @inheritdoc */ 77fe2dcfd5SAndreas Gohr protected function main(Options $options) 78a8d2f3cbSAndreas Gohr { 797c9966a5SAndreas Gohr $repo = Repository::getInstance(); 807c9966a5SAndreas Gohr try { 817c9966a5SAndreas Gohr $repo->checkAccess(); 827c9966a5SAndreas Gohr } catch (ExtensionException $e) { 83ed3520eeSAndreas Gohr $this->warning('Extension Repository API is not accessible, no remote info available!'); 84ed3520eeSAndreas Gohr } 85ed3520eeSAndreas Gohr 86a8d2f3cbSAndreas Gohr switch ($options->getCmd()) { 87a8d2f3cbSAndreas Gohr case 'list': 88b9daa2f5SAndreas Gohr $ret = $this->cmdList($options->getOpt('verbose'), $options->getOpt('filter', '')); 89a8d2f3cbSAndreas Gohr break; 90a8d2f3cbSAndreas Gohr case 'search': 91a8d2f3cbSAndreas Gohr $ret = $this->cmdSearch( 92a8d2f3cbSAndreas Gohr implode(' ', $options->getArgs()), 93a8d2f3cbSAndreas Gohr $options->getOpt('verbose'), 94a8d2f3cbSAndreas Gohr (int)$options->getOpt('max', 10) 95a8d2f3cbSAndreas Gohr ); 96a8d2f3cbSAndreas Gohr break; 97a8d2f3cbSAndreas Gohr case 'install': 98a8d2f3cbSAndreas Gohr $ret = $this->cmdInstall($options->getArgs()); 99a8d2f3cbSAndreas Gohr break; 100a8d2f3cbSAndreas Gohr case 'uninstall': 101a8d2f3cbSAndreas Gohr $ret = $this->cmdUnInstall($options->getArgs()); 102a8d2f3cbSAndreas Gohr break; 103a8d2f3cbSAndreas Gohr case 'enable': 104a8d2f3cbSAndreas Gohr $ret = $this->cmdEnable(true, $options->getArgs()); 105a8d2f3cbSAndreas Gohr break; 106a8d2f3cbSAndreas Gohr case 'disable': 107a8d2f3cbSAndreas Gohr $ret = $this->cmdEnable(false, $options->getArgs()); 108a8d2f3cbSAndreas Gohr break; 109a8d2f3cbSAndreas Gohr case 'upgrade': 110a8d2f3cbSAndreas Gohr $ret = $this->cmdUpgrade(); 111a8d2f3cbSAndreas Gohr break; 112a8d2f3cbSAndreas Gohr default: 113a8d2f3cbSAndreas Gohr echo $options->help(); 114a8d2f3cbSAndreas Gohr $ret = 0; 115a8d2f3cbSAndreas Gohr } 116a8d2f3cbSAndreas Gohr 117a8d2f3cbSAndreas Gohr exit($ret); 118a8d2f3cbSAndreas Gohr } 119a8d2f3cbSAndreas Gohr 120a8d2f3cbSAndreas Gohr /** 121a8d2f3cbSAndreas Gohr * Upgrade all extensions 122a8d2f3cbSAndreas Gohr * 123a8d2f3cbSAndreas Gohr * @return int 124a8d2f3cbSAndreas Gohr */ 125a8d2f3cbSAndreas Gohr protected function cmdUpgrade() 126a8d2f3cbSAndreas Gohr { 127160d3688SAndreas Gohr $local = new Local(); 128160d3688SAndreas Gohr $extensions = []; 129160d3688SAndreas Gohr foreach ($local->getExtensions() as $ext) { 130*4fd6a1d7SAndreas Gohr if ($ext->isUpdateAvailable()) $extensions[] = $ext->getID(); 131a8d2f3cbSAndreas Gohr } 132160d3688SAndreas Gohr return $this->cmdInstall($extensions); 133a8d2f3cbSAndreas Gohr } 134a8d2f3cbSAndreas Gohr 135a8d2f3cbSAndreas Gohr /** 136a8d2f3cbSAndreas Gohr * Enable or disable one or more extensions 137a8d2f3cbSAndreas Gohr * 138a8d2f3cbSAndreas Gohr * @param bool $set 139a8d2f3cbSAndreas Gohr * @param string[] $extensions 140a8d2f3cbSAndreas Gohr * @return int 141a8d2f3cbSAndreas Gohr */ 142a8d2f3cbSAndreas Gohr protected function cmdEnable($set, $extensions) 143a8d2f3cbSAndreas Gohr { 144a8d2f3cbSAndreas Gohr $ok = 0; 145a8d2f3cbSAndreas Gohr foreach ($extensions as $extname) { 146160d3688SAndreas Gohr $ext = Extension::createFromId($extname); 147a8d2f3cbSAndreas Gohr 148160d3688SAndreas Gohr try { 149a8d2f3cbSAndreas Gohr if ($set) { 150160d3688SAndreas Gohr $ext->enable(); 151a8d2f3cbSAndreas Gohr $msg = 'msg_enabled'; 152a8d2f3cbSAndreas Gohr } else { 153160d3688SAndreas Gohr $ext->disable(); 154a8d2f3cbSAndreas Gohr $msg = 'msg_disabled'; 155a8d2f3cbSAndreas Gohr } 156160d3688SAndreas Gohr $this->success(sprintf($this->getLang($msg), $ext->getID())); 157160d3688SAndreas Gohr } catch (ExtensionException $e) { 158160d3688SAndreas Gohr $this->error($e->getMessage()); 159fe2dcfd5SAndreas Gohr ++$ok; 160a8d2f3cbSAndreas Gohr continue; 161a8d2f3cbSAndreas Gohr } 162a8d2f3cbSAndreas Gohr } 163a8d2f3cbSAndreas Gohr 164a8d2f3cbSAndreas Gohr return $ok; 165a8d2f3cbSAndreas Gohr } 166a8d2f3cbSAndreas Gohr 167a8d2f3cbSAndreas Gohr /** 168a8d2f3cbSAndreas Gohr * Uninstall one or more extensions 169a8d2f3cbSAndreas Gohr * 170a8d2f3cbSAndreas Gohr * @param string[] $extensions 171a8d2f3cbSAndreas Gohr * @return int 172a8d2f3cbSAndreas Gohr */ 173a8d2f3cbSAndreas Gohr protected function cmdUnInstall($extensions) 174a8d2f3cbSAndreas Gohr { 175160d3688SAndreas Gohr $installer = new Installer(); 176a8d2f3cbSAndreas Gohr 177a8d2f3cbSAndreas Gohr $ok = 0; 178a8d2f3cbSAndreas Gohr foreach ($extensions as $extname) { 179160d3688SAndreas Gohr $ext = Extension::createFromId($extname); 180a8d2f3cbSAndreas Gohr 181160d3688SAndreas Gohr try { 182160d3688SAndreas Gohr $installer->uninstall($ext); 183a8d2f3cbSAndreas Gohr $this->success(sprintf($this->getLang('msg_delete_success'), $ext->getID())); 184160d3688SAndreas Gohr } catch (ExtensionException $e) { 185160d3688SAndreas Gohr $this->debug($e->getTraceAsString()); 186160d3688SAndreas Gohr $this->error($e->getMessage()); 187160d3688SAndreas Gohr $ok++; // error code is number of failed uninstalls 188a8d2f3cbSAndreas Gohr } 189a8d2f3cbSAndreas Gohr } 190a8d2f3cbSAndreas Gohr return $ok; 191a8d2f3cbSAndreas Gohr } 192a8d2f3cbSAndreas Gohr 193a8d2f3cbSAndreas Gohr /** 194a8d2f3cbSAndreas Gohr * Install one or more extensions 195a8d2f3cbSAndreas Gohr * 196a8d2f3cbSAndreas Gohr * @param string[] $extensions 197a8d2f3cbSAndreas Gohr * @return int 198a8d2f3cbSAndreas Gohr */ 199a8d2f3cbSAndreas Gohr protected function cmdInstall($extensions) 200a8d2f3cbSAndreas Gohr { 201a8d2f3cbSAndreas Gohr $ok = 0; 202a8d2f3cbSAndreas Gohr foreach ($extensions as $extname) { 203160d3688SAndreas Gohr $installer = new Installer(true); 204160d3688SAndreas Gohr 20525d28a01SAndreas Gohr try { 206cc16762dSLocness if (preg_match("/^https?:\/\//i", $extname)) { 20725d28a01SAndreas Gohr $installer->installFromURL($extname, true); 208cc16762dSLocness } else { 20925d28a01SAndreas Gohr $installer->installFromId($extname); 210a8d2f3cbSAndreas Gohr } 21125d28a01SAndreas Gohr } catch (ExtensionException $e) { 21225d28a01SAndreas Gohr $this->debug($e->getTraceAsString()); 2135aaea2b0SLocness $this->error($e->getMessage()); 21425d28a01SAndreas Gohr $ok++; // error code is number of failed installs 2155aaea2b0SLocness } 2165aaea2b0SLocness 21725d28a01SAndreas Gohr $processed = $installer->getProcessed(); 21825d28a01SAndreas Gohr foreach ($processed as $id => $status) { 21925d28a01SAndreas Gohr if ($status == Installer::STATUS_INSTALLED) { 22025d28a01SAndreas Gohr $this->success(sprintf($this->getLang('msg_install_success'), $id)); 22125d28a01SAndreas Gohr } else if ($status == Installer::STATUS_UPDATED) { 22225d28a01SAndreas Gohr $this->success(sprintf($this->getLang('msg_update_success'), $id)); 223a8d2f3cbSAndreas Gohr } 224cc16762dSLocness } 225160d3688SAndreas Gohr } 22625d28a01SAndreas Gohr 227a8d2f3cbSAndreas Gohr return $ok; 228a8d2f3cbSAndreas Gohr } 229a8d2f3cbSAndreas Gohr 230a8d2f3cbSAndreas Gohr /** 231a8d2f3cbSAndreas Gohr * Search for an extension 232a8d2f3cbSAndreas Gohr * 233a8d2f3cbSAndreas Gohr * @param string $query 234a8d2f3cbSAndreas Gohr * @param bool $showdetails 235a8d2f3cbSAndreas Gohr * @param int $max 236a8d2f3cbSAndreas Gohr * @return int 2379b36c1fcSsplitbrain * @throws Exception 238a8d2f3cbSAndreas Gohr */ 239a8d2f3cbSAndreas Gohr protected function cmdSearch($query, $showdetails, $max) 240a8d2f3cbSAndreas Gohr { 2417c9966a5SAndreas Gohr $repo = Repository::getInstance(); 2427c9966a5SAndreas Gohr $result = $repo->searchExtensions($query); 243a8d2f3cbSAndreas Gohr if ($max) { 244a8d2f3cbSAndreas Gohr $result = array_slice($result, 0, $max); 245a8d2f3cbSAndreas Gohr } 246a8d2f3cbSAndreas Gohr 247a8d2f3cbSAndreas Gohr $this->listExtensions($result, $showdetails); 248a8d2f3cbSAndreas Gohr return 0; 249a8d2f3cbSAndreas Gohr } 250a8d2f3cbSAndreas Gohr 251a8d2f3cbSAndreas Gohr /** 252a8d2f3cbSAndreas Gohr * @param bool $showdetails 253b9daa2f5SAndreas Gohr * @param string $filter 254a8d2f3cbSAndreas Gohr * @return int 2559b36c1fcSsplitbrain * @throws Exception 256a8d2f3cbSAndreas Gohr */ 257b9daa2f5SAndreas Gohr protected function cmdList($showdetails, $filter) 258a8d2f3cbSAndreas Gohr { 2597c9966a5SAndreas Gohr $this->listExtensions((new Local())->getExtensions(), $showdetails, $filter); 260a8d2f3cbSAndreas Gohr return 0; 261a8d2f3cbSAndreas Gohr } 262a8d2f3cbSAndreas Gohr 263a8d2f3cbSAndreas Gohr /** 264a8d2f3cbSAndreas Gohr * List the given extensions 265a8d2f3cbSAndreas Gohr * 2667c9966a5SAndreas Gohr * @param Extension[] $list 267a8d2f3cbSAndreas Gohr * @param bool $details display details 268b9daa2f5SAndreas Gohr * @param string $filter filter for this status 2699b36c1fcSsplitbrain * @throws Exception 270160d3688SAndreas Gohr * @todo break into smaller methods 271a8d2f3cbSAndreas Gohr */ 272b9daa2f5SAndreas Gohr protected function listExtensions($list, $details, $filter = '') 273a8d2f3cbSAndreas Gohr { 274fe2dcfd5SAndreas Gohr $tr = new TableFormatter($this->colors); 2757c9966a5SAndreas Gohr foreach ($list as $ext) { 276a8d2f3cbSAndreas Gohr 277a8d2f3cbSAndreas Gohr $status = ''; 278a8d2f3cbSAndreas Gohr if ($ext->isInstalled()) { 279a8d2f3cbSAndreas Gohr $date = $ext->getInstalledVersion(); 280a8d2f3cbSAndreas Gohr $avail = $ext->getLastUpdate(); 281a8d2f3cbSAndreas Gohr $status = 'i'; 282a8d2f3cbSAndreas Gohr if ($avail && $avail > $date) { 283e5688dc7SAndreas Gohr $vcolor = Colors::C_RED; 284b9daa2f5SAndreas Gohr $status .= 'u'; 285a8d2f3cbSAndreas Gohr } else { 286e5688dc7SAndreas Gohr $vcolor = Colors::C_GREEN; 287a8d2f3cbSAndreas Gohr } 288a8d2f3cbSAndreas Gohr if ($ext->isGitControlled()) $status = 'g'; 28999fd248dSAndreas Gohr if ($ext->isBundled()) { 29099fd248dSAndreas Gohr $status = 'b'; 29199fd248dSAndreas Gohr $date = '<bundled>'; 29299fd248dSAndreas Gohr $vcolor = null; 29399fd248dSAndreas Gohr } 294e5688dc7SAndreas Gohr if ($ext->isEnabled()) { 295e5688dc7SAndreas Gohr $ecolor = Colors::C_BROWN; 296e5688dc7SAndreas Gohr } else { 297e5688dc7SAndreas Gohr $ecolor = Colors::C_DARKGRAY; 298e5688dc7SAndreas Gohr $status .= 'd'; 299e5688dc7SAndreas Gohr } 300a8d2f3cbSAndreas Gohr } else { 301d915fa09SAndreas Gohr $ecolor = null; 302a8d2f3cbSAndreas Gohr $date = $ext->getLastUpdate(); 303e5688dc7SAndreas Gohr $vcolor = null; 304a8d2f3cbSAndreas Gohr } 305a8d2f3cbSAndreas Gohr 306b9daa2f5SAndreas Gohr if ($filter && strpos($status, $filter) === false) { 307b9daa2f5SAndreas Gohr continue; 308b9daa2f5SAndreas Gohr } 309a8d2f3cbSAndreas Gohr 310*4fd6a1d7SAndreas Gohr $notices = Notice::list($ext); 311*4fd6a1d7SAndreas Gohr if ($notices[Notice::SECURITY]) $status .= Notice::ICONS[Notice::SECURITY]; 312*4fd6a1d7SAndreas Gohr if ($notices[Notice::ERROR]) $status .= Notice::ICONS[Notice::ERROR]; 313*4fd6a1d7SAndreas Gohr if ($notices[Notice::WARNING]) $status .= Notice::ICONS[Notice::WARNING]; 314*4fd6a1d7SAndreas Gohr if ($notices[Notice::INFO]) $status .= Notice::ICONS[Notice::INFO]; 31599fd248dSAndreas Gohr 316a8d2f3cbSAndreas Gohr echo $tr->format( 317160d3688SAndreas Gohr [20, 5, 12, '*'], 318a8d2f3cbSAndreas Gohr [ 319a8d2f3cbSAndreas Gohr $ext->getID(), 320a8d2f3cbSAndreas Gohr $status, 321a8d2f3cbSAndreas Gohr $date, 322a8d2f3cbSAndreas Gohr strip_tags(sprintf( 323a8d2f3cbSAndreas Gohr $this->getLang('extensionby'), 324a8d2f3cbSAndreas Gohr $ext->getDisplayName(), 325dccd6b2bSAndreas Gohr $this->colors->wrap($ext->getAuthor(), Colors::C_PURPLE) 326dccd6b2bSAndreas Gohr )) 327a8d2f3cbSAndreas Gohr ], 328a8d2f3cbSAndreas Gohr [ 329e5688dc7SAndreas Gohr $ecolor, 330a8d2f3cbSAndreas Gohr Colors::C_YELLOW, 331e5688dc7SAndreas Gohr $vcolor, 332a8d2f3cbSAndreas Gohr null, 333a8d2f3cbSAndreas Gohr ] 334a8d2f3cbSAndreas Gohr ); 335a8d2f3cbSAndreas Gohr 33699fd248dSAndreas Gohr 337a8d2f3cbSAndreas Gohr if (!$details) continue; 338a8d2f3cbSAndreas Gohr 339a8d2f3cbSAndreas Gohr echo $tr->format( 340160d3688SAndreas Gohr [7, '*'], 341a8d2f3cbSAndreas Gohr ['', $ext->getDescription()], 342a8d2f3cbSAndreas Gohr [null, Colors::C_CYAN] 343a8d2f3cbSAndreas Gohr ); 344*4fd6a1d7SAndreas Gohr foreach ($notices as $type => $msgs) { 345*4fd6a1d7SAndreas Gohr if (!$msgs) continue; 346*4fd6a1d7SAndreas Gohr foreach ($msgs as $msg) { 34799fd248dSAndreas Gohr echo $tr->format( 348160d3688SAndreas Gohr [7, '*'], 349*4fd6a1d7SAndreas Gohr ['', Notice::ICONS[$type] . ' ' . $msg], 35099fd248dSAndreas Gohr [null, Colors::C_LIGHTBLUE] 35199fd248dSAndreas Gohr ); 35299fd248dSAndreas Gohr } 353160d3688SAndreas Gohr } 354a8d2f3cbSAndreas Gohr } 355a8d2f3cbSAndreas Gohr } 356a8d2f3cbSAndreas Gohr} 357