1<?php 2 3namespace dokuwiki\plugin\extension; 4 5class GuiExtension extends Gui 6{ 7 public const THUMB_WIDTH = 120; 8 public const THUMB_HEIGHT = 70; 9 10 11 protected Extension $extension; 12 13 public function __construct(Extension $extension) 14 { 15 parent::__construct(); 16 $this->extension = $extension; 17 } 18 19 20 public function render() 21 { 22 23 $classes = $this->getClasses(); 24 25 $html = "<section class=\"$classes\" data-ext=\"{$this->extension->getId()}\">"; 26 27 $html .= '<div class="screenshot">'; 28 $html .= $this->thumbnail(); 29 $html .= '<span class="id" title="' . hsc($this->extension->getBase()) . '">' . 30 hsc($this->extension->getBase()) . '</span>'; 31 $html .= $this->popularity(); 32 $html .= '</div>'; 33 34 $html .= '<div class="main">'; 35 $html .= $this->main(); 36 $html .= '</div>'; 37 38 $html .= '<div class="notices">'; 39 $html .= $this->notices(); 40 $html .= '</div>'; 41 42 $html .= '<div class="details">'; 43 $html .= $this->details(); 44 $html .= '</div>'; 45 46 $html .= '<div class="actions">'; 47 // show the available update if there is one 48 if ($this->extension->isUpdateAvailable()) { 49 $html .= ' <div class="version">' . $this->getLang('available_version') . ' ' . 50 hsc($this->extension->getLastUpdate()) . '</div>'; 51 } 52 53 $html .= $this->actions(); 54 $html .= '</div>'; 55 56 57 $html .= '</section>'; 58 59 return $html; 60 } 61 62 // region sections 63 64 /** 65 * Get the link and image tag for the screenshot/thumbnail 66 * 67 * @return string The HTML code 68 */ 69 protected function thumbnail() 70 { 71 $screen = $this->extension->getScreenshotURL(); 72 $thumb = $this->extension->getThumbnailURL(); 73 74 $link = []; 75 $img = [ 76 'width' => self::THUMB_WIDTH, 77 'height' => self::THUMB_HEIGHT, 78 'alt' => '', 79 ]; 80 81 if ($screen) { 82 $link = [ 83 'href' => $screen, 84 'target' => '_blank', 85 'class' => 'extension_screenshot', 86 'title' => sprintf($this->getLang('screenshot'), $this->extension->getDisplayName()) 87 ]; 88 89 $img['src'] = $thumb; 90 $img['alt'] = $link['title']; 91 } elseif ($this->extension->isTemplate()) { 92 $img['src'] = DOKU_BASE . 'lib/plugins/extension/images/template.png'; 93 } else { 94 $img['src'] = DOKU_BASE . 'lib/plugins/extension/images/plugin.png'; 95 } 96 97 $html = ''; 98 if ($link) $html .= '<a ' . buildAttributes($link) . '>'; 99 $html .= '<img ' . buildAttributes($img) . ' />'; 100 if ($link) $html .= '</a>'; 101 102 return $html; 103 } 104 105 /** 106 * The main information about the extension 107 * 108 * @return string 109 */ 110 protected function main() 111 { 112 $html = ''; 113 $html .= '<h2>'; 114 $html .= '<div>'; 115 $html .= sprintf($this->getLang('extensionby'), hsc($this->extension->getDisplayName()), $this->author()); 116 $html .= '</div>'; 117 118 $html .= '<div class="version">'; 119 if ($this->extension->isBundled()) { 120 $html .= hsc('<' . $this->getLang('status_bundled') . '>'); 121 } elseif ($this->extension->getInstalledVersion()) { 122 $html .= hsc($this->extension->getInstalledVersion()); 123 } 124 $html .= '</div>'; 125 $html .= '</h2>'; 126 127 $html .= '<p>' . hsc($this->extension->getDescription()) . '</p>'; 128 $html .= $this->mainLinks(); 129 130 return $html; 131 } 132 133 /** 134 * Display the available notices for the extension 135 * 136 * @return string 137 */ 138 protected function notices() 139 { 140 $notices = Notice::list($this->extension); 141 142 $html = '<ul>'; 143 foreach ($notices as $type => $messages) { 144 foreach ($messages as $message) { 145 $message = hsc($message); 146 $message = nl2br($message); 147 $message = '<span>' . Notice::ICONS[$type] . '</span> ' . $message; 148 $message = preg_replace('/`([^`]+)`/', '<bdi>$1</bdi>', $message); 149 $html .= '<li class="' . $type . '"><div class="li">' . $message . '</div></li>'; 150 } 151 } 152 $html .= '</ul>'; 153 return $html; 154 } 155 156 /** 157 * Generate the link bar HTML code 158 * 159 * @return string The HTML code 160 */ 161 public function mainLinks() 162 { 163 $html = '<div class="linkbar">'; 164 165 166 $homepage = $this->extension->getURL(); 167 if ($homepage) { 168 $params = $this->prepareLinkAttributes($homepage, 'homepage'); 169 $html .= ' <a ' . buildAttributes($params, true) . '>' . $this->getLang('homepage_link') . '</a>'; 170 } 171 172 $bugtracker = $this->extension->getBugtrackerURL(); 173 if ($bugtracker) { 174 $params = $this->prepareLinkAttributes($bugtracker, 'bugs'); 175 $html .= ' <a ' . buildAttributes($params, true) . '>' . $this->getLang('bugs_features') . '</a>'; 176 } 177 178 if ($this->extension->getDonationURL()) { 179 $params = $this->prepareLinkAttributes($this->extension->getDonationURL(), 'donate'); 180 $html .= ' <a ' . buildAttributes($params, true) . '>' . $this->getLang('donate_action') . '</a>'; 181 } 182 183 184 $html .= '</div>'; 185 186 return $html; 187 } 188 189 /** 190 * Create the details section 191 * 192 * @return string 193 */ 194 protected function details() 195 { 196 $html = '<details>'; 197 $html .= '<summary>' . $this->getLang('details') . '</summary>'; 198 199 200 $default = $this->getLang('unknown'); 201 $list = []; 202 203 if (!$this->extension->isBundled()) { 204 $list['downloadurl'] = $this->shortlink($this->extension->getDownloadURL(), 'download', $default); 205 $list['repository'] = $this->shortlink($this->extension->getSourcerepoURL(), 'repo', $default); 206 } 207 208 if ($this->extension->isInstalled()) { 209 if ($this->extension->isBundled()) { 210 $list['installed_version'] = $this->getLang('status_bundled'); 211 } else { 212 if ($this->extension->getInstalledVersion()) { 213 $list['installed_version'] = hsc($this->extension->getInstalledVersion()); 214 } 215 if (!$this->extension->isBundled()) { 216 $installDate = $this->extension->getManager()->getInstallDate(); 217 $list['installed'] = $installDate ? dformat($installDate->getTimestamp()) : $default; 218 219 $updateDate = $this->extension->getManager()->getLastUpdate(); 220 $list['install_date'] = $updateDate ? dformat($updateDate->getTimestamp()) : $default; 221 } 222 } 223 } 224 225 if (!$this->extension->isInstalled() || $this->extension->isUpdateAvailable()) { 226 $list['available_version'] = $this->extension->getLastUpdate() 227 ? hsc($this->extension->getLastUpdate()) 228 : $default; 229 } 230 231 232 if (!$this->extension->isBundled() && $this->extension->getCompatibleVersions()) { 233 $list['compatible'] = implode(', ', array_map( 234 static fn($date, $version) => '<bdi>' . $version['label'] . ' (' . $date . ')</bdi>', 235 array_keys($this->extension->getCompatibleVersions()), 236 array_values($this->extension->getCompatibleVersions()) 237 )); 238 } 239 240 $tags = $this->extension->getTags(); 241 if ($tags) { 242 $list['tags'] = implode(', ', array_map(function ($tag) { 243 $url = $this->tabURL('search', ['q' => 'tag:' . $tag]); 244 return '<bdi><a href="' . $url . '">' . hsc($tag) . '</a></bdi>'; 245 }, $tags)); 246 } 247 248 if ($this->extension->getDependencyList()) { 249 $list['depends'] = $this->linkExtensions($this->extension->getDependencyList()); 250 } 251 252 if ($this->extension->getSimilarList()) { 253 $list['similar'] = $this->linkExtensions($this->extension->getSimilarList()); 254 } 255 256 if ($this->extension->getConflictList()) { 257 $list['conflicts'] = $this->linkExtensions($this->extension->getConflictList()); 258 } 259 260 $html .= '<dl>'; 261 foreach ($list as $key => $value) { 262 $html .= '<dt>' . rtrim($this->getLang($key), ':') . '</dt>'; 263 $html .= '<dd>' . $value . '</dd>'; 264 } 265 $html .= '</dl>'; 266 267 $html .= '</details>'; 268 return $html; 269 } 270 271 /** 272 * Generate a link to the author of the extension 273 * 274 * @return string The HTML code of the link 275 */ 276 protected function author() 277 { 278 if (!$this->extension->getAuthor()) { 279 return '<em class="author">' . $this->getLang('unknown_author') . '</em>'; 280 } 281 282 $names = explode(',', $this->extension->getAuthor()); 283 $names = array_map('trim', $names); 284 if (count($names) > 2) { 285 $names = array_slice($names, 0, 2); 286 $names[] = '…'; 287 } 288 $name = implode(', ', $names); 289 290 $mailid = $this->extension->getEmailID(); 291 if ($mailid) { 292 $url = $this->tabURL('search', ['q' => 'authorid:' . $mailid]); 293 $html = '<a href="' . $url . '" class="author" title="' . $this->getLang('author_hint') . '" >' . 294 '<img src="//www.gravatar.com/avatar/' . $mailid . 295 '?s=60&d=mm" width="20" height="20" alt="" /> ' . 296 hsc($name) . '</a>'; 297 } else { 298 $html = '<span class="author">' . hsc($this->extension->getAuthor()) . '</span>'; 299 } 300 return '<bdi>' . $html . '</bdi>'; 301 } 302 303 /** 304 * The popularity bar 305 * 306 * @return string 307 */ 308 protected function popularity() 309 { 310 $popularity = $this->extension->getPopularity(); 311 if (!$popularity) return ''; 312 if ($this->extension->isBundled()) return ''; 313 314 if ($popularity > 0.25) { 315 $title = $this->getLang('popularity_high'); 316 $emoji = ''; 317 } elseif ($popularity > 0.15) { 318 $title = $this->getLang('popularity_medium'); 319 $emoji = ''; 320 } elseif ($popularity > 0.05) { 321 $title = $this->getLang('popularity_low'); 322 $emoji = ''; 323 } else { 324 return ''; 325 } 326 $title .= ' (' . round($popularity * 100) . '%)'; 327 328 return '<span class="popularity" title="' . $title . '">' . $emoji . '</span>'; 329 } 330 331 /** 332 * Generate the action buttons 333 * 334 * @return string 335 */ 336 protected function actions() 337 { 338 $html = ''; 339 $actions = []; 340 341 // check permissions 342 try { 343 Installer::ensurePermissions($this->extension); 344 } catch (\Exception $e) { 345 return ''; 346 } 347 348 // gather available actions 349 if ($this->extension->isInstalled()) { 350 if (!$this->extension->isProtected()) $actions[] = 'uninstall'; 351 if ($this->extension->getDownloadURL()) { 352 $actions[] = $this->extension->isUpdateAvailable() ? 'update' : 'reinstall'; 353 } 354 // no enable/disable for templates 355 if (!$this->extension->isProtected() && !$this->extension->isTemplate()) { 356 $actions[] = $this->extension->isEnabled() ? 'disable' : 'enable'; 357 } 358 } elseif ($this->extension->getDownloadURL()) { 359 $actions[] = 'install'; 360 } 361 362 // output the buttons 363 foreach ($actions as $action) { 364 $attr = [ 365 'class' => 'button ' . $action, 366 'type' => 'submit', 367 'name' => 'fn[' . $action . '][' . $this->extension->getID() . ']', 368 ]; 369 $html .= '<button ' . buildAttributes($attr) . '>' . $this->getLang('btn_' . $action) . '</button>'; 370 } 371 372 return $html; 373 } 374 375 376 // endregion 377 // region utility functions 378 379 /** 380 * Create the classes representing the state of the extension 381 * 382 * @return string 383 */ 384 protected function getClasses() 385 { 386 $classes = ['extension', $this->extension->getType()]; 387 if ($this->extension->isInstalled()) $classes[] = 'installed'; 388 if ($this->extension->isUpdateAvailable()) $classes[] = 'update'; 389 $classes[] = $this->extension->isEnabled() ? 'enabled' : 'disabled'; 390 return implode(' ', $classes); 391 } 392 393 /** 394 * Create an attributes array for a link 395 * 396 * Handles interwiki links to dokuwiki.org 397 * 398 * @param string $url The URL to link to 399 * @param string $class Additional classes to add 400 * @return array 401 */ 402 protected function prepareLinkAttributes($url, $class) 403 { 404 global $conf; 405 406 $attributes = [ 407 'href' => $url, 408 'class' => 'urlextern', 409 'target' => $conf['target']['extern'], 410 'rel' => 'noopener', 411 'title' => $url, 412 ]; 413 414 if ($conf['relnofollow']) { 415 $attributes['rel'] .= ' ugc nofollow'; 416 } 417 418 if (preg_match('/^https?:\/\/(www\.)?dokuwiki\.org\//i', $url)) { 419 $attributes['class'] = 'interwiki iw_doku'; 420 $attributes['target'] = $conf['target']['interwiki']; 421 $attributes['rel'] = ''; 422 } 423 424 $attributes['class'] .= ' ' . $class; 425 return $attributes; 426 } 427 428 /** 429 * Create a link from the given URL 430 * 431 * Shortens the URL for display 432 * 433 * @param string $url 434 * @param string $class Additional classes to add 435 * @param string $fallback If URL is empty return this fallback (raw HTML) 436 * @return string HTML link 437 */ 438 protected function shortlink($url, $class, $fallback = '') 439 { 440 if (!$url) return $fallback; 441 442 $link = parse_url($url); 443 $base = $link['host']; 444 if (!empty($link['port'])) $base .= $base . ':' . $link['port']; 445 $long = $link['path']; 446 if (!empty($link['query'])) $long .= $link['query']; 447 448 $name = shorten($base, $long, 55); 449 450 $params = $this->prepareLinkAttributes($url, $class); 451 $html = '<a ' . buildAttributes($params, true) . '>' . hsc($name) . '</a>'; 452 return $html; 453 } 454 455 /** 456 * Generate a list of links for extensions 457 * 458 * Links to the search tab with the extension name 459 * 460 * @param array $extensions The extension names 461 * @return string The HTML code 462 */ 463 public function linkExtensions($extensions) 464 { 465 $html = ''; 466 foreach ($extensions as $link) { 467 $html .= '<bdi><a href="' . 468 $this->tabURL('search', ['q' => 'ext:' . $link]) . '">' . 469 hsc($link) . '</a></bdi>, '; 470 } 471 return rtrim($html, ', '); 472 } 473 474 // endregion 475} 476