xref: /dokuwiki/lib/plugins/extension/GuiExtension.php (revision 80bc92fb6cebd3143ca97b0ad5aa529a28f2cc39)
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\">";
26
27        $html .= '<div class="screenshot">';
28        $html .= $this->thumbnail();
29        $html .= '<span class="id">'. hsc($this->extension->getBase()) .'</span>';
30        $html .= $this->popularity();
31        $html .= '</div>';
32
33        $html .= '<div class="main">';
34        $html .= $this->main();
35        $html .= '</div>';
36
37        $html .= '<div class="notices">';
38        $html .= $this->notices();
39        $html .= '</div>';
40
41        $html .= '<div class="details">';
42        $html .= $this->details();
43        $html .= '</div>';
44
45        $html .= '<div class="actions">';
46        // show the available update if there is one
47        if ($this->extension->isUpdateAvailable()) {
48            $html .= ' <div class="version">' . $this->getLang('available_version') . ' ' .
49                hsc($this->extension->getLastUpdate()) . '</div>';
50        }
51
52        $html .= $this->actions();
53        $html .= '</div>';
54
55
56        $html .= '</section>';
57
58        return $html;
59    }
60
61    // region sections
62
63    /**
64     * Get the link and image tag for the screenshot/thumbnail
65     *
66     * @return string The HTML code
67     */
68    protected function thumbnail()
69    {
70        $screen = $this->extension->getScreenshotURL();
71        $thumb = $this->extension->getThumbnailURL();
72
73        $link = [];
74        $img = [
75            'width' => self::THUMB_WIDTH,
76            'height' => self::THUMB_HEIGHT,
77            'alt' => '',
78        ];
79
80        if ($screen) {
81            $link = [
82                'href' => $screen,
83                'target' => '_blank',
84                'class' => 'extension_screenshot',
85                'title' => sprintf($this->getLang('screenshot'), $this->extension->getDisplayName())
86            ];
87
88            $img['src'] = $thumb;
89            $img['alt'] = $link['title'];
90        } elseif ($this->extension->isTemplate()) {
91            $img['src'] = DOKU_BASE . 'lib/plugins/extension/images/template.png';
92        } else {
93            $img['src'] = DOKU_BASE . 'lib/plugins/extension/images/plugin.png';
94        }
95
96        $html = '';
97        if ($link) $html .= '<a ' . buildAttributes($link) . '>';
98        $html .= '<img ' . buildAttributes($img) . ' />';
99        if ($link) $html .= '</a>';
100
101        return $html;
102
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                    $updateDate = $this->extension->getManager()->getLastUpdate();
217                    $list['install_date'] = $updateDate ? hsc($updateDate) : $default;
218                }
219            }
220        }
221
222        if (!$this->extension->isInstalled() || $this->extension->isUpdateAvailable()) {
223            $list['available_version'] = $this->extension->getLastUpdate()
224                ? hsc($this->extension->getLastUpdate())
225                : $default;
226        }
227
228
229        if (!$this->extension->isBundled() && $this->extension->getCompatibleVersions()) {
230            $list['compatible'] = join(', ', array_map(
231                function ($date, $version) {
232                    return '<bdi>' . $version['label'] . ' (' . $date . ')</bdi>';
233                },
234                array_keys($this->extension->getCompatibleVersions()),
235                array_values($this->extension->getCompatibleVersions())
236            ));
237        }
238
239        $tags = $this->extension->getTags();
240        if ($tags) {
241            $list['tags'] = join(', ', array_map(function ($tag) {
242                $url = $this->tabURL('search', ['q' => 'tag:' . $tag]);
243                return '<bdi><a href="' . $url . '">' . hsc($tag) . '</a></bdi>';
244            }, $tags));
245        }
246
247        if ($this->extension->getDependencyList()) {
248            $list['depends'] = $this->linkExtensions($this->extension->getDependencyList());
249        }
250
251        if ($this->extension->getSimilarList()) {
252            $list['similar'] = $this->linkExtensions($this->extension->getSimilarList());
253        }
254
255        if ($this->extension->getConflictList()) {
256            $list['conflicts'] = $this->linkExtensions($this->extension->getConflictList());
257        }
258
259        $html .= '<dl>';
260        foreach ($list as $key => $value) {
261            $html .= '<dt>' . $this->getLang($key) . '</dt>';
262            $html .= '<dd>' . $value . '</dd>';
263        }
264        $html .= '</dl>';
265
266        $html .= '</details>';
267        return $html;
268    }
269
270    /**
271     * Generate a link to the author of the extension
272     *
273     * @return string The HTML code of the link
274     */
275    protected function author()
276    {
277        if (!$this->extension->getAuthor()) {
278            return '<em class="author">' . $this->getLang('unknown_author') . '</em>';
279        }
280
281        $names = explode(',', $this->extension->getAuthor());
282        $names = array_map('trim', $names);
283        if (count($names) > 2) {
284            $names = array_slice($names, 0, 2);
285            $names[] = '…';
286        }
287        $name = join(', ', $names);
288
289        $mailid = $this->extension->getEmailID();
290        if ($mailid) {
291            $url = $this->tabURL('search', ['q' => 'authorid:' . $mailid]);
292            $html = '<a href="' . $url . '" class="author" title="' . $this->getLang('author_hint') . '" >' .
293                '<img src="//www.gravatar.com/avatar/' . $mailid .
294                '?s=60&amp;d=mm" width="20" height="20" alt="" /> ' .
295                hsc($name) . '</a>';
296        } else {
297            $html = '<span class="author">' . hsc($this->extension->getAuthor()) . '</span>';
298        }
299        return '<bdi>' . $html . '</bdi>';
300    }
301
302    /**
303     * The popularity bar
304     *
305     * @return string
306     */
307    protected function popularity()
308    {
309        $popularity = $this->extension->getPopularity();
310        if (!$popularity) return '';
311        if ($this->extension->isBundled()) return '';
312
313        if ($popularity > 0.25) {
314            $title = $this->getLang('popularity_high');
315            $emoji = '������';
316        } else if ($popularity > 0.15) {
317            $title = $this->getLang('popularity_medium');
318            $emoji = '����';
319        } else if ($popularity > 0.05) {
320            $title = $this->getLang('popularity_low');
321            $emoji = '��';
322        } else {
323            return '';
324        }
325        $title .= ' (' . round($popularity * 100) . '%)';
326
327        return '<span class="popularity" title="' . $title . '">' . $emoji . '</span>';
328
329    }
330
331    protected function actions()
332    {
333        global $conf;
334
335        $html = '';
336        $actions = [];
337        $errors = [];
338
339        // gather available actions and possible errors to show
340        try {
341            Installer::ensurePermissions($this->extension);
342
343            if ($this->extension->isInstalled()) {
344
345                if (!$this->extension->isProtected()) $actions[] = 'uninstall';
346                if ($this->extension->getDownloadURL()) {
347                    $actions[] = $this->extension->isUpdateAvailable() ? 'update' : 'reinstall';
348                }
349
350                if (!$this->extension->isProtected() && !$this->extension->isTemplate()) { // no enable/disable for templates
351                    $actions[] = $this->extension->isEnabled() ? 'disable' : 'enable';
352                }
353            } else {
354                if ($this->extension->getDownloadURL()) {
355                    $actions[] = 'install';
356                }
357            }
358        } catch (\Exception $e) {
359        }
360
361        foreach ($actions as $action) {
362            $html .= '<button name="fn[' . $action . '][' . $this->extension->getID() . ']" class="button" type="submit">' .
363                $this->getLang('btn_' . $action) . '</button>';
364        }
365
366        foreach ($errors as $error) {
367            $html .= '<div class="msg error">' . hsc($error) . '</div>';
368        }
369
370        return $html;
371    }
372
373
374    // endregion
375    // region utility functions
376
377    /**
378     * Create the classes representing the state of the extension
379     *
380     * @return string
381     */
382    protected function getClasses()
383    {
384        $classes = ['extension', $this->extension->getType()];
385        if ($this->extension->isInstalled()) $classes[] = 'installed';
386        if ($this->extension->isUpdateAvailable()) $classes[] = 'update';
387        $classes[] = $this->extension->isEnabled() ? 'enabled' : 'disabled';
388        return implode(' ', $classes);
389    }
390
391    /**
392     * Create an attributes array for a link
393     *
394     * Handles interwiki links to dokuwiki.org
395     *
396     * @param string $url The URL to link to
397     * @param string $class Additional classes to add
398     * @return array
399     */
400    protected function prepareLinkAttributes($url, $class)
401    {
402        global $conf;
403
404        $attributes = [
405            'href' => $url,
406            'class' => 'urlextern',
407            'target' => $conf['target']['extern'],
408            'rel' => 'noopener',
409            'title' => $url,
410        ];
411
412        if ($conf['relnofollow']) {
413            $attributes['rel'] .= ' ugc nofollow';
414        }
415
416        if (preg_match('/^https?:\/\/(www\.)?dokuwiki\.org\//i', $url)) {
417            $attributes['class'] = 'interwiki iw_doku';
418            $attributes['target'] = $conf['target']['interwiki'];
419            $attributes['rel'] = '';
420        }
421
422        $attributes['class'] .= ' ' . $class;
423        return $attributes;
424    }
425
426    /**
427     * Create a link from the given URL
428     *
429     * Shortens the URL for display
430     *
431     * @param string $url
432     * @param string $class Additional classes to add
433     * @param string $fallback If URL is empty return this fallback (raw HTML)
434     * @return string  HTML link
435     */
436    protected function shortlink($url, $class, $fallback = '')
437    {
438        if (!$url) return $fallback;
439
440        $link = parse_url($url);
441        $base = $link['host'];
442        if (!empty($link['port'])) $base .= $base . ':' . $link['port'];
443        $long = $link['path'];
444        if (!empty($link['query'])) $long .= $link['query'];
445
446        $name = shorten($base, $long, 55);
447
448        $params = $this->prepareLinkAttributes($url, $class);
449        $html = '<a ' . buildAttributes($params, true) . '>' . hsc($name) . '</a>';
450        return $html;
451    }
452
453    /**
454     * Generate a list of links for extensions
455     *
456     * Links to the search tab with the extension name
457     *
458     * @param array $extensions The extension names
459     * @return string The HTML code
460     */
461    public function linkExtensions($extensions)
462    {
463        $html = '';
464        foreach ($extensions as $link) {
465            $html .= '<bdi><a href="' .
466                $this->tabURL('search', ['q' => 'ext:' . $link]) . '">' .
467                hsc($link) . '</a></bdi>, ';
468        }
469        return rtrim($html, ', ');
470    }
471
472    // endregion
473}
474