1a8d2f3cbSAndreas Gohr<?php 2a8d2f3cbSAndreas Gohr 38553d24dSAndreas Gohruse dokuwiki\Extension\CLIPlugin; 4*7c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Exception as ExtensionException; 5*7c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Extension; 6*7c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Local; 7*7c9966a5SAndreas Gohruse dokuwiki\plugin\extension\Repository; 8*7c9966a5SAndreas Gohruse splitbrain\phpcli\Colors; 9*7c9966a5SAndreas Gohruse splitbrain\phpcli\Exception; 10fe2dcfd5SAndreas Gohruse splitbrain\phpcli\Options; 11fe2dcfd5SAndreas Gohruse splitbrain\phpcli\TableFormatter; 12a8d2f3cbSAndreas Gohr 13a8d2f3cbSAndreas Gohr/** 14a8d2f3cbSAndreas Gohr * Class cli_plugin_extension 15a8d2f3cbSAndreas Gohr * 16a8d2f3cbSAndreas Gohr * Command Line component for the extension manager 17a8d2f3cbSAndreas Gohr * 18a8d2f3cbSAndreas Gohr * @license GPL2 19a8d2f3cbSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 20a8d2f3cbSAndreas Gohr */ 218553d24dSAndreas Gohrclass cli_plugin_extension extends CLIPlugin 22a8d2f3cbSAndreas Gohr{ 23a8d2f3cbSAndreas Gohr /** @inheritdoc */ 24fe2dcfd5SAndreas Gohr protected function setup(Options $options) 25a8d2f3cbSAndreas Gohr { 26a8d2f3cbSAndreas Gohr // general setup 27*7c9966a5SAndreas Gohr $options->useCompactHelp(); 28b9daa2f5SAndreas Gohr $options->setHelp( 29b9daa2f5SAndreas Gohr "Manage plugins and templates for this DokuWiki instance\n\n" . 30b9daa2f5SAndreas Gohr "Status codes:\n" . 31b9daa2f5SAndreas Gohr " i - installed\n" . 32b9daa2f5SAndreas Gohr " b - bundled with DokuWiki\n" . 33b9daa2f5SAndreas Gohr " g - installed via git\n" . 34b9daa2f5SAndreas Gohr " d - disabled\n" . 35b9daa2f5SAndreas Gohr " u - update available\n" 36b9daa2f5SAndreas Gohr ); 37a8d2f3cbSAndreas Gohr 38a8d2f3cbSAndreas Gohr // search 39a8d2f3cbSAndreas Gohr $options->registerCommand('search', 'Search for an extension'); 40a8d2f3cbSAndreas Gohr $options->registerOption('max', 'Maximum number of results (default 10)', 'm', 'number', 'search'); 41a8d2f3cbSAndreas Gohr $options->registerOption('verbose', 'Show detailed extension information', 'v', false, 'search'); 42a8d2f3cbSAndreas Gohr $options->registerArgument('query', 'The keyword(s) to search for', true, 'search'); 43a8d2f3cbSAndreas Gohr 44a8d2f3cbSAndreas Gohr // list 45a8d2f3cbSAndreas Gohr $options->registerCommand('list', 'List installed extensions'); 46a8d2f3cbSAndreas Gohr $options->registerOption('verbose', 'Show detailed extension information', 'v', false, 'list'); 47b9daa2f5SAndreas Gohr $options->registerOption('filter', 'Filter by this status', 'f', 'status', 'list'); 48a8d2f3cbSAndreas Gohr 49a8d2f3cbSAndreas Gohr // upgrade 50a8d2f3cbSAndreas Gohr $options->registerCommand('upgrade', 'Update all installed extensions to their latest versions'); 51a8d2f3cbSAndreas Gohr 52a8d2f3cbSAndreas Gohr // install 53a8d2f3cbSAndreas Gohr $options->registerCommand('install', 'Install or upgrade extensions'); 54dccd6b2bSAndreas Gohr $options->registerArgument( 55dccd6b2bSAndreas Gohr 'extensions...', 56dccd6b2bSAndreas Gohr 'One or more extensions to install. Either by name or download URL', 57dccd6b2bSAndreas Gohr true, 58dccd6b2bSAndreas Gohr 'install' 59e2170488SAndreas Gohr ); 60a8d2f3cbSAndreas Gohr 61a8d2f3cbSAndreas Gohr // uninstall 62a8d2f3cbSAndreas Gohr $options->registerCommand('uninstall', 'Uninstall a new extension'); 63a8d2f3cbSAndreas Gohr $options->registerArgument('extensions...', 'One or more extensions to install', true, 'uninstall'); 64a8d2f3cbSAndreas Gohr 65a8d2f3cbSAndreas Gohr // enable 66a8d2f3cbSAndreas Gohr $options->registerCommand('enable', 'Enable installed extensions'); 67a8d2f3cbSAndreas Gohr $options->registerArgument('extensions...', 'One or more extensions to enable', true, 'enable'); 68a8d2f3cbSAndreas Gohr 69a8d2f3cbSAndreas Gohr // disable 70a8d2f3cbSAndreas Gohr $options->registerCommand('disable', 'Disable installed extensions'); 71a8d2f3cbSAndreas Gohr $options->registerArgument('extensions...', 'One or more extensions to disable', true, 'disable'); 72a8d2f3cbSAndreas Gohr } 73a8d2f3cbSAndreas Gohr 74a8d2f3cbSAndreas Gohr /** @inheritdoc */ 75fe2dcfd5SAndreas Gohr protected function main(Options $options) 76a8d2f3cbSAndreas Gohr { 77*7c9966a5SAndreas Gohr $repo = Repository::getInstance(); 78*7c9966a5SAndreas Gohr try { 79*7c9966a5SAndreas Gohr $repo->checkAccess(); 80*7c9966a5SAndreas Gohr } catch (ExtensionException $e) { 81ed3520eeSAndreas Gohr $this->warning('Extension Repository API is not accessible, no remote info available!'); 82ed3520eeSAndreas Gohr } 83ed3520eeSAndreas Gohr 84a8d2f3cbSAndreas Gohr switch ($options->getCmd()) { 85a8d2f3cbSAndreas Gohr case 'list': 86b9daa2f5SAndreas Gohr $ret = $this->cmdList($options->getOpt('verbose'), $options->getOpt('filter', '')); 87a8d2f3cbSAndreas Gohr break; 88a8d2f3cbSAndreas Gohr case 'search': 89a8d2f3cbSAndreas Gohr $ret = $this->cmdSearch( 90a8d2f3cbSAndreas Gohr implode(' ', $options->getArgs()), 91a8d2f3cbSAndreas Gohr $options->getOpt('verbose'), 92a8d2f3cbSAndreas Gohr (int)$options->getOpt('max', 10) 93a8d2f3cbSAndreas Gohr ); 94a8d2f3cbSAndreas Gohr break; 95a8d2f3cbSAndreas Gohr case 'install': 96a8d2f3cbSAndreas Gohr $ret = $this->cmdInstall($options->getArgs()); 97a8d2f3cbSAndreas Gohr break; 98a8d2f3cbSAndreas Gohr case 'uninstall': 99a8d2f3cbSAndreas Gohr $ret = $this->cmdUnInstall($options->getArgs()); 100a8d2f3cbSAndreas Gohr break; 101a8d2f3cbSAndreas Gohr case 'enable': 102a8d2f3cbSAndreas Gohr $ret = $this->cmdEnable(true, $options->getArgs()); 103a8d2f3cbSAndreas Gohr break; 104a8d2f3cbSAndreas Gohr case 'disable': 105a8d2f3cbSAndreas Gohr $ret = $this->cmdEnable(false, $options->getArgs()); 106a8d2f3cbSAndreas Gohr break; 107a8d2f3cbSAndreas Gohr case 'upgrade': 108a8d2f3cbSAndreas Gohr $ret = $this->cmdUpgrade(); 109a8d2f3cbSAndreas Gohr break; 110a8d2f3cbSAndreas Gohr default: 111a8d2f3cbSAndreas Gohr echo $options->help(); 112a8d2f3cbSAndreas Gohr $ret = 0; 113a8d2f3cbSAndreas Gohr } 114a8d2f3cbSAndreas Gohr 115a8d2f3cbSAndreas Gohr exit($ret); 116a8d2f3cbSAndreas Gohr } 117a8d2f3cbSAndreas Gohr 118a8d2f3cbSAndreas Gohr /** 119a8d2f3cbSAndreas Gohr * Upgrade all extensions 120a8d2f3cbSAndreas Gohr * 121a8d2f3cbSAndreas Gohr * @return int 122a8d2f3cbSAndreas Gohr */ 123a8d2f3cbSAndreas Gohr protected function cmdUpgrade() 124a8d2f3cbSAndreas Gohr { 125a8d2f3cbSAndreas Gohr /* @var helper_plugin_extension_extension $ext */ 126a8d2f3cbSAndreas Gohr $ext = $this->loadHelper('extension_extension'); 127a8d2f3cbSAndreas Gohr $list = $this->getInstalledExtensions(); 128a8d2f3cbSAndreas Gohr 129a8d2f3cbSAndreas Gohr $ok = 0; 130a8d2f3cbSAndreas Gohr foreach ($list as $extname) { 131a8d2f3cbSAndreas Gohr $ext->setExtension($extname); 132a8d2f3cbSAndreas Gohr $date = $ext->getInstalledVersion(); 133a8d2f3cbSAndreas Gohr $avail = $ext->getLastUpdate(); 134be15e516SAndreas Gohr if ($avail && $avail > $date && !$ext->isBundled()) { 135a8d2f3cbSAndreas Gohr $ok += $this->cmdInstall([$extname]); 136a8d2f3cbSAndreas Gohr } 137a8d2f3cbSAndreas Gohr } 138a8d2f3cbSAndreas Gohr 139a8d2f3cbSAndreas Gohr return $ok; 140a8d2f3cbSAndreas Gohr } 141a8d2f3cbSAndreas Gohr 142a8d2f3cbSAndreas Gohr /** 143a8d2f3cbSAndreas Gohr * Enable or disable one or more extensions 144a8d2f3cbSAndreas Gohr * 145a8d2f3cbSAndreas Gohr * @param bool $set 146a8d2f3cbSAndreas Gohr * @param string[] $extensions 147a8d2f3cbSAndreas Gohr * @return int 148a8d2f3cbSAndreas Gohr */ 149a8d2f3cbSAndreas Gohr protected function cmdEnable($set, $extensions) 150a8d2f3cbSAndreas Gohr { 151a8d2f3cbSAndreas Gohr /* @var helper_plugin_extension_extension $ext */ 152a8d2f3cbSAndreas Gohr $ext = $this->loadHelper('extension_extension'); 153a8d2f3cbSAndreas Gohr 154a8d2f3cbSAndreas Gohr $ok = 0; 155a8d2f3cbSAndreas Gohr foreach ($extensions as $extname) { 156a8d2f3cbSAndreas Gohr $ext->setExtension($extname); 157a8d2f3cbSAndreas Gohr if (!$ext->isInstalled()) { 158a8d2f3cbSAndreas Gohr $this->error(sprintf('Extension %s is not installed', $ext->getID())); 159fe2dcfd5SAndreas Gohr ++$ok; 160a8d2f3cbSAndreas Gohr continue; 161a8d2f3cbSAndreas Gohr } 162a8d2f3cbSAndreas Gohr 163a8d2f3cbSAndreas Gohr if ($set) { 164a8d2f3cbSAndreas Gohr $status = $ext->enable(); 165a8d2f3cbSAndreas Gohr $msg = 'msg_enabled'; 166a8d2f3cbSAndreas Gohr } else { 167a8d2f3cbSAndreas Gohr $status = $ext->disable(); 168a8d2f3cbSAndreas Gohr $msg = 'msg_disabled'; 169a8d2f3cbSAndreas Gohr } 170a8d2f3cbSAndreas Gohr 171a8d2f3cbSAndreas Gohr if ($status !== true) { 172a8d2f3cbSAndreas Gohr $this->error($status); 173fe2dcfd5SAndreas Gohr ++$ok; 174a8d2f3cbSAndreas Gohr continue; 175a8d2f3cbSAndreas Gohr } else { 176a8d2f3cbSAndreas Gohr $this->success(sprintf($this->getLang($msg), $ext->getID())); 177a8d2f3cbSAndreas Gohr } 178a8d2f3cbSAndreas Gohr } 179a8d2f3cbSAndreas Gohr 180a8d2f3cbSAndreas Gohr return $ok; 181a8d2f3cbSAndreas Gohr } 182a8d2f3cbSAndreas Gohr 183a8d2f3cbSAndreas Gohr /** 184a8d2f3cbSAndreas Gohr * Uninstall one or more extensions 185a8d2f3cbSAndreas Gohr * 186a8d2f3cbSAndreas Gohr * @param string[] $extensions 187a8d2f3cbSAndreas Gohr * @return int 188a8d2f3cbSAndreas Gohr */ 189a8d2f3cbSAndreas Gohr protected function cmdUnInstall($extensions) 190a8d2f3cbSAndreas Gohr { 191a8d2f3cbSAndreas Gohr /* @var helper_plugin_extension_extension $ext */ 192a8d2f3cbSAndreas Gohr $ext = $this->loadHelper('extension_extension'); 193a8d2f3cbSAndreas Gohr 194a8d2f3cbSAndreas Gohr $ok = 0; 195a8d2f3cbSAndreas Gohr foreach ($extensions as $extname) { 196a8d2f3cbSAndreas Gohr $ext->setExtension($extname); 197a8d2f3cbSAndreas Gohr if (!$ext->isInstalled()) { 198a8d2f3cbSAndreas Gohr $this->error(sprintf('Extension %s is not installed', $ext->getID())); 199fe2dcfd5SAndreas Gohr ++$ok; 200a8d2f3cbSAndreas Gohr continue; 201a8d2f3cbSAndreas Gohr } 202a8d2f3cbSAndreas Gohr 203a8d2f3cbSAndreas Gohr $status = $ext->uninstall(); 204a8d2f3cbSAndreas Gohr if ($status) { 205a8d2f3cbSAndreas Gohr $this->success(sprintf($this->getLang('msg_delete_success'), $ext->getID())); 206a8d2f3cbSAndreas Gohr } else { 207a8d2f3cbSAndreas Gohr $this->error(sprintf($this->getLang('msg_delete_failed'), hsc($ext->getID()))); 208a8d2f3cbSAndreas Gohr $ok = 1; 209a8d2f3cbSAndreas Gohr } 210a8d2f3cbSAndreas Gohr } 211a8d2f3cbSAndreas Gohr 212a8d2f3cbSAndreas Gohr return $ok; 213a8d2f3cbSAndreas Gohr } 214a8d2f3cbSAndreas Gohr 215a8d2f3cbSAndreas Gohr /** 216a8d2f3cbSAndreas Gohr * Install one or more extensions 217a8d2f3cbSAndreas Gohr * 218a8d2f3cbSAndreas Gohr * @param string[] $extensions 219a8d2f3cbSAndreas Gohr * @return int 220a8d2f3cbSAndreas Gohr */ 221a8d2f3cbSAndreas Gohr protected function cmdInstall($extensions) 222a8d2f3cbSAndreas Gohr { 223a8d2f3cbSAndreas Gohr 224a8d2f3cbSAndreas Gohr $ok = 0; 225a8d2f3cbSAndreas Gohr foreach ($extensions as $extname) { 2265aaea2b0SLocness $installed = []; 2275aaea2b0SLocness 228cc16762dSLocness if (preg_match("/^https?:\/\//i", $extname)) { 229cc16762dSLocness try { 230cc16762dSLocness $installed = $ext->installFromURL($extname, true); 231cc16762dSLocness } catch (Exception $e) { 232cc16762dSLocness $this->error($e->getMessage()); 233fe2dcfd5SAndreas Gohr ++$ok; 234cc16762dSLocness } 235cc16762dSLocness } else { 236a8d2f3cbSAndreas Gohr $ext->setExtension($extname); 237a8d2f3cbSAndreas Gohr 238a8d2f3cbSAndreas Gohr if (!$ext->getDownloadURL()) { 239fe2dcfd5SAndreas Gohr ++$ok; 240a8d2f3cbSAndreas Gohr $this->error( 241a8d2f3cbSAndreas Gohr sprintf('Could not find download for %s', $ext->getID()) 242a8d2f3cbSAndreas Gohr ); 243a8d2f3cbSAndreas Gohr continue; 244a8d2f3cbSAndreas Gohr } 245a8d2f3cbSAndreas Gohr 246a8d2f3cbSAndreas Gohr try { 247a8d2f3cbSAndreas Gohr $installed = $ext->installOrUpdate(); 2485aaea2b0SLocness } catch (Exception $e) { 2495aaea2b0SLocness $this->error($e->getMessage()); 250fe2dcfd5SAndreas Gohr ++$ok; 2515aaea2b0SLocness } 2525aaea2b0SLocness } 2535aaea2b0SLocness 254fe2dcfd5SAndreas Gohr foreach ($installed as $info) { 255cc16762dSLocness $this->success( 256cc16762dSLocness sprintf( 257a8d2f3cbSAndreas Gohr $this->getLang('msg_' . $info['type'] . '_' . $info['action'] . '_success'), 258cc16762dSLocness $info['base'] 259cc16762dSLocness ) 260a8d2f3cbSAndreas Gohr ); 261a8d2f3cbSAndreas Gohr } 262cc16762dSLocness } 263a8d2f3cbSAndreas Gohr return $ok; 264a8d2f3cbSAndreas Gohr } 265a8d2f3cbSAndreas Gohr 266a8d2f3cbSAndreas Gohr /** 267a8d2f3cbSAndreas Gohr * Search for an extension 268a8d2f3cbSAndreas Gohr * 269a8d2f3cbSAndreas Gohr * @param string $query 270a8d2f3cbSAndreas Gohr * @param bool $showdetails 271a8d2f3cbSAndreas Gohr * @param int $max 272a8d2f3cbSAndreas Gohr * @return int 2739b36c1fcSsplitbrain * @throws Exception 274a8d2f3cbSAndreas Gohr */ 275a8d2f3cbSAndreas Gohr protected function cmdSearch($query, $showdetails, $max) 276a8d2f3cbSAndreas Gohr { 277*7c9966a5SAndreas Gohr $repo = Repository::getInstance(); 278*7c9966a5SAndreas Gohr $result = $repo->searchExtensions($query); 279a8d2f3cbSAndreas Gohr if ($max) { 280a8d2f3cbSAndreas Gohr $result = array_slice($result, 0, $max); 281a8d2f3cbSAndreas Gohr } 282a8d2f3cbSAndreas Gohr 283a8d2f3cbSAndreas Gohr $this->listExtensions($result, $showdetails); 284a8d2f3cbSAndreas Gohr return 0; 285a8d2f3cbSAndreas Gohr } 286a8d2f3cbSAndreas Gohr 287a8d2f3cbSAndreas Gohr /** 288a8d2f3cbSAndreas Gohr * @param bool $showdetails 289b9daa2f5SAndreas Gohr * @param string $filter 290a8d2f3cbSAndreas Gohr * @return int 2919b36c1fcSsplitbrain * @throws Exception 292a8d2f3cbSAndreas Gohr */ 293b9daa2f5SAndreas Gohr protected function cmdList($showdetails, $filter) 294a8d2f3cbSAndreas Gohr { 295*7c9966a5SAndreas Gohr $this->listExtensions((new Local())->getExtensions(), $showdetails, $filter); 296a8d2f3cbSAndreas Gohr return 0; 297a8d2f3cbSAndreas Gohr } 298a8d2f3cbSAndreas Gohr 299a8d2f3cbSAndreas Gohr /** 300a8d2f3cbSAndreas Gohr * List the given extensions 301a8d2f3cbSAndreas Gohr * 302*7c9966a5SAndreas Gohr * @param Extension[] $list 303a8d2f3cbSAndreas Gohr * @param bool $details display details 304b9daa2f5SAndreas Gohr * @param string $filter filter for this status 3059b36c1fcSsplitbrain * @throws Exception 306a8d2f3cbSAndreas Gohr */ 307b9daa2f5SAndreas Gohr protected function listExtensions($list, $details, $filter = '') 308a8d2f3cbSAndreas Gohr { 309fe2dcfd5SAndreas Gohr $tr = new TableFormatter($this->colors); 310*7c9966a5SAndreas Gohr foreach ($list as $ext) { 311a8d2f3cbSAndreas Gohr 312a8d2f3cbSAndreas Gohr $status = ''; 313a8d2f3cbSAndreas Gohr if ($ext->isInstalled()) { 314a8d2f3cbSAndreas Gohr $date = $ext->getInstalledVersion(); 315a8d2f3cbSAndreas Gohr $avail = $ext->getLastUpdate(); 316a8d2f3cbSAndreas Gohr $status = 'i'; 317a8d2f3cbSAndreas Gohr if ($avail && $avail > $date) { 318e5688dc7SAndreas Gohr $vcolor = Colors::C_RED; 319b9daa2f5SAndreas Gohr $status .= 'u'; 320a8d2f3cbSAndreas Gohr } else { 321e5688dc7SAndreas Gohr $vcolor = Colors::C_GREEN; 322a8d2f3cbSAndreas Gohr } 323a8d2f3cbSAndreas Gohr if ($ext->isGitControlled()) $status = 'g'; 324a8d2f3cbSAndreas Gohr if ($ext->isBundled()) $status = 'b'; 325e5688dc7SAndreas Gohr if ($ext->isEnabled()) { 326e5688dc7SAndreas Gohr $ecolor = Colors::C_BROWN; 327e5688dc7SAndreas Gohr } else { 328e5688dc7SAndreas Gohr $ecolor = Colors::C_DARKGRAY; 329e5688dc7SAndreas Gohr $status .= 'd'; 330e5688dc7SAndreas Gohr } 331a8d2f3cbSAndreas Gohr } else { 332d915fa09SAndreas Gohr $ecolor = null; 333a8d2f3cbSAndreas Gohr $date = $ext->getLastUpdate(); 334e5688dc7SAndreas Gohr $vcolor = null; 335a8d2f3cbSAndreas Gohr } 336a8d2f3cbSAndreas Gohr 337b9daa2f5SAndreas Gohr if ($filter && strpos($status, $filter) === false) { 338b9daa2f5SAndreas Gohr continue; 339b9daa2f5SAndreas Gohr } 340a8d2f3cbSAndreas Gohr 341a8d2f3cbSAndreas Gohr echo $tr->format( 342a8d2f3cbSAndreas Gohr [20, 3, 12, '*'], 343a8d2f3cbSAndreas Gohr [ 344a8d2f3cbSAndreas Gohr $ext->getID(), 345a8d2f3cbSAndreas Gohr $status, 346a8d2f3cbSAndreas Gohr $date, 347a8d2f3cbSAndreas Gohr strip_tags(sprintf( 348a8d2f3cbSAndreas Gohr $this->getLang('extensionby'), 349a8d2f3cbSAndreas Gohr $ext->getDisplayName(), 350dccd6b2bSAndreas Gohr $this->colors->wrap($ext->getAuthor(), Colors::C_PURPLE) 351dccd6b2bSAndreas Gohr )) 352a8d2f3cbSAndreas Gohr ], 353a8d2f3cbSAndreas Gohr [ 354e5688dc7SAndreas Gohr $ecolor, 355a8d2f3cbSAndreas Gohr Colors::C_YELLOW, 356e5688dc7SAndreas Gohr $vcolor, 357a8d2f3cbSAndreas Gohr null, 358a8d2f3cbSAndreas Gohr ] 359a8d2f3cbSAndreas Gohr ); 360a8d2f3cbSAndreas Gohr 361a8d2f3cbSAndreas Gohr if (!$details) continue; 362a8d2f3cbSAndreas Gohr 363a8d2f3cbSAndreas Gohr echo $tr->format( 364a8d2f3cbSAndreas Gohr [5, '*'], 365a8d2f3cbSAndreas Gohr ['', $ext->getDescription()], 366a8d2f3cbSAndreas Gohr [null, Colors::C_CYAN] 367a8d2f3cbSAndreas Gohr ); 368a8d2f3cbSAndreas Gohr } 369a8d2f3cbSAndreas Gohr } 370a8d2f3cbSAndreas Gohr} 371