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