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