xref: /dokuwiki/lib/plugins/extension/GuiExtension.php (revision 3e63733dc277872f88f60e618798f71311214102)
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        $list['provides'] = implode(', ', array_map('hsc', $this->extension->getComponentTypes()));
241
242        $tags = $this->extension->getTags();
243        if ($tags) {
244            $list['tags'] = implode(', ', array_map(function ($tag) {
245                $url = $this->tabURL('search', ['q' => 'tag:' . $tag]);
246                return '<bdi><a href="' . $url . '">' . hsc($tag) . '</a></bdi>';
247            }, $tags));
248        }
249
250        if ($this->extension->getDependencyList()) {
251            $list['depends'] = $this->linkExtensions($this->extension->getDependencyList());
252        }
253
254        if ($this->extension->getSimilarList()) {
255            $list['similar'] = $this->linkExtensions($this->extension->getSimilarList());
256        }
257
258        if ($this->extension->getConflictList()) {
259            $list['conflicts'] = $this->linkExtensions($this->extension->getConflictList());
260        }
261
262        $html .= '<dl>';
263        foreach ($list as $key => $value) {
264            $html .= '<dt>' . rtrim($this->getLang($key), ':') . '</dt>';
265            $html .= '<dd>' . $value . '</dd>';
266        }
267        $html .= '</dl>';
268
269        $html .= '</details>';
270        return $html;
271    }
272
273    /**
274     * Generate a link to the author of the extension
275     *
276     * @return string The HTML code of the link
277     */
278    protected function author()
279    {
280        if (!$this->extension->getAuthor()) {
281            return '<em class="author">' . $this->getLang('unknown_author') . '</em>';
282        }
283
284        $names = explode(',', $this->extension->getAuthor());
285        $names = array_map('trim', $names);
286        if (count($names) > 2) {
287            $names = array_slice($names, 0, 2);
288            $names[] = '…';
289        }
290        $name = implode(', ', $names);
291
292        $mailid = $this->extension->getEmailID();
293        if ($mailid) {
294            $url = $this->tabURL('search', ['q' => 'authorid:' . $mailid]);
295            $html = '<a href="' . $url . '" class="author" title="' . $this->getLang('author_hint') . '" >' .
296                '<img src="//www.gravatar.com/avatar/' . $mailid .
297                '?s=60&amp;d=mm" width="20" height="20" alt="" /> ' .
298                hsc($name) . '</a>';
299        } else {
300            $html = '<span class="author">' . hsc($this->extension->getAuthor()) . '</span>';
301        }
302        return '<bdi>' . $html . '</bdi>';
303    }
304
305    /**
306     * The popularity bar
307     *
308     * @return string
309     */
310    protected function popularity()
311    {
312        $popularity = $this->extension->getPopularity();
313        if (!$popularity) return '';
314        if ($this->extension->isBundled()) return '';
315
316        if ($popularity > 0.25) {
317            $title = $this->getLang('popularity_high');
318            $emoji = '������';
319        } elseif ($popularity > 0.15) {
320            $title = $this->getLang('popularity_medium');
321            $emoji = '����';
322        } elseif ($popularity > 0.05) {
323            $title = $this->getLang('popularity_low');
324            $emoji = '��';
325        } else {
326            return '';
327        }
328        $title .= ' (' . round($popularity * 100) . '%)';
329
330        return '<span class="popularity" title="' . $title . '">' . $emoji . '</span>';
331    }
332
333    /**
334     * Generate the action buttons
335     *
336     * @return string
337     */
338    protected function actions()
339    {
340        $html = '';
341        $actions = [];
342
343        // check permissions
344        try {
345            Installer::ensurePermissions($this->extension);
346        } catch (\Exception $e) {
347            return '';
348        }
349
350        // gather available actions
351        if ($this->extension->isInstalled()) {
352            if (!$this->extension->isProtected()) $actions[] = 'uninstall';
353            if ($this->extension->getDownloadURL()) {
354                $actions[] = $this->extension->isUpdateAvailable() ? 'update' : 'reinstall';
355            }
356            // no enable/disable for templates
357            if (!$this->extension->isProtected() && !$this->extension->isTemplate()) {
358                $actions[] = $this->extension->isEnabled() ? 'disable' : 'enable';
359            }
360        } elseif ($this->extension->getDownloadURL()) {
361            $actions[] = 'install';
362        }
363
364        // output the buttons
365        foreach ($actions as $action) {
366            $attr = [
367                'class' => 'button ' . $action,
368                'type' => 'submit',
369                'name' => 'fn[' . $action . '][' . $this->extension->getID() . ']',
370            ];
371            $html .= '<button ' . buildAttributes($attr) . '>' . $this->getLang('btn_' . $action) . '</button>';
372        }
373
374        return $html;
375    }
376
377
378    // endregion
379    // region utility functions
380
381    /**
382     * Create the classes representing the state of the extension
383     *
384     * @return string
385     */
386    protected function getClasses()
387    {
388        $classes = ['extension', $this->extension->getType()];
389        if ($this->extension->isInstalled()) $classes[] = 'installed';
390        if ($this->extension->isUpdateAvailable()) $classes[] = 'update';
391        $classes[] = $this->extension->isEnabled() ? 'enabled' : 'disabled';
392        return implode(' ', $classes);
393    }
394
395    /**
396     * Create an attributes array for a link
397     *
398     * Handles interwiki links to dokuwiki.org
399     *
400     * @param string $url The URL to link to
401     * @param string $class Additional classes to add
402     * @return array
403     */
404    protected function prepareLinkAttributes($url, $class)
405    {
406        global $conf;
407
408        $attributes = [
409            'href' => $url,
410            'class' => 'urlextern',
411            'target' => $conf['target']['extern'],
412            'rel' => 'noopener',
413            'title' => $url,
414        ];
415
416        if ($conf['relnofollow']) {
417            $attributes['rel'] .= ' ugc nofollow';
418        }
419
420        if (preg_match('/^https?:\/\/(www\.)?dokuwiki\.org\//i', $url)) {
421            $attributes['class'] = 'interwiki iw_doku';
422            $attributes['target'] = $conf['target']['interwiki'];
423            $attributes['rel'] = '';
424        }
425
426        $attributes['class'] .= ' ' . $class;
427        return $attributes;
428    }
429
430    /**
431     * Create a link from the given URL
432     *
433     * Shortens the URL for display
434     *
435     * @param string $url
436     * @param string $class Additional classes to add
437     * @param string $fallback If URL is empty return this fallback (raw HTML)
438     * @return string  HTML link
439     */
440    protected function shortlink($url, $class, $fallback = '')
441    {
442        if (!$url) return $fallback;
443
444        $link = parse_url($url);
445        $base = $link['host'];
446        if (!empty($link['port'])) $base .= $base . ':' . $link['port'];
447        $long = $link['path'];
448        if (!empty($link['query'])) $long .= $link['query'];
449
450        $name = shorten($base, $long, 55);
451
452        $params = $this->prepareLinkAttributes($url, $class);
453        $html = '<a ' . buildAttributes($params, true) . '>' . hsc($name) . '</a>';
454        return $html;
455    }
456
457    /**
458     * Generate a list of links for extensions
459     *
460     * Links to the search tab with the extension name
461     *
462     * @param array $extensions The extension names
463     * @return string The HTML code
464     */
465    public function linkExtensions($extensions)
466    {
467        $html = '';
468        foreach ($extensions as $link) {
469            $html .= '<bdi><a href="' .
470                $this->tabURL('search', ['q' => 'ext:' . $link]) . '">' .
471                hsc($link) . '</a></bdi>, ';
472        }
473        return rtrim($html, ', ');
474    }
475
476    // endregion
477}
478