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