aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/extension/GuiExtension.php
blob: a8c22dcbca49aa6233b7f2b97e5b2e4dca8a0e50 (plain) (blame)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php

namespace dokuwiki\plugin\extension;

class GuiExtension extends Gui
{
    const THUMB_WIDTH = 120;
    const THUMB_HEIGHT = 70;


    protected Extension $extension;

    public function __construct(Extension $extension)
    {
        parent::__construct();
        $this->extension = $extension;
    }


    public function render()
    {

        $classes = $this->getClasses();

        $html = "<section class=\"$classes\">";

        $html .= '<div class="screenshot">';
        $html .= $this->thumbnail();
        $html .= '</div>';

        $html.= '<h2>';
        $html .= '<bdi>' . hsc($this->extension->getDisplayName()) . '</bdi>';
        if ($this->extension->isBundled()) {
            $html .= ' <span class="version">' . hsc('<' . $this->getLang('status_bundled') . '>') . '</span>';
        } elseif ($this->extension->getInstalledVersion()) {
            $html .= ' <span class="version">' . hsc($this->extension->getInstalledVersion()) . '</span>';
        }
        $html .= $this->popularity();
        $html .= '</h2>';

        $html .= '<div class="main">';
        $html .= '<h3>' . $this->author() . '</h3>';
        $html .= '<p>' . hsc($this->extension->getDescription()) . '</p>';
        $html .= '</div>';




        $html .= '<div class="details">';
        $html .= $this->notices();
        $html .= $this->mainLinks();
        $html .= $this->details();
        $html .= '</div>';

        // show the available version if there is one
        if ($this->extension->getDownloadURL() && $this->extension->getLastUpdate()) {
            $html .= ' <div class="version">' . $this->getLang('available_version') . ' ' .
                hsc($this->extension->getLastUpdate()) . '</div>';
        }

        $html .= '<div class="actions">';
        $html .= $this->actions();
        $html .= '</div>';


        $html .= '</section>';

        return $html;
    }

    // region sections

    /**
     * Get the link and image tag for the screenshot/thumbnail
     *
     * @return string The HTML code
     */
    protected function thumbnail()
    {
        $screen = $this->extension->getScreenshotURL();
        $thumb = $this->extension->getThumbnailURL();

        $link = [];
        $img = [
            'width' => self::THUMB_WIDTH,
            'height' => self::THUMB_HEIGHT,
            'alt' => '',
        ];

        if ($screen) {
            $link = [
                'href' => $screen,
                'target' => '_blank',
                'class' => 'extension_screenshot',
                'title' => sprintf($this->getLang('screenshot'), $this->extension->getDisplayName())
            ];

            $img['src'] = $thumb;
            $img['alt'] = $link['title'];
        } elseif ($this->extension->isTemplate()) {
            $img['src'] = DOKU_BASE . 'lib/plugins/extension/images/template.png';
        } else {
            $img['src'] = DOKU_BASE . 'lib/plugins/extension/images/plugin.png';
        }

        $html = '';
        if ($link) $html .= '<a ' . buildAttributes($link) . '>';
        $html .= '<img ' . buildAttributes($img) . ' />';
        if ($link) $html .= '</a>';

        return $html;

    }

    /**
     * The main information about the extension
     *
     * @return string
     */
    protected function info()
    {



        return $html;
    }

    /**
     * Display the available notices for the extension
     *
     * @return string
     */
    protected function notices()
    {
        $notices = Notice::list($this->extension);

        $html = '';
        foreach ($notices as $type => $messages) {
            foreach ($messages as $message) {
                $message = hsc($message);
                $message = preg_replace('/`([^`]+)`/', '<bdi>$1</bdi>', $message);
                $html .= '<div class="msg ' . $type . '">' . $message . '</div>';
            }
        }
        return $html;
    }

    /**
     * Generate the link bar HTML code
     *
     * @return string The HTML code
     */
    public function mainLinks()
    {
        $html = '<div class="linkbar">';


        $homepage = $this->extension->getURL();
        if ($homepage) {
            $params = $this->prepareLinkAttributes($homepage, 'homepage');
            $html .= ' <a ' . buildAttributes($params, true) . '>' . $this->getLang('homepage_link') . '</a>';
        }

        $bugtracker = $this->extension->getBugtrackerURL();
        if ($bugtracker) {
            $params = $this->prepareLinkAttributes($bugtracker, 'bugs');
            $html .= ' <a ' . buildAttributes($params, true) . '>' . $this->getLang('bugs_features') . '</a>';
        }

        if ($this->extension->getDonationURL()) {
            $params = $this->prepareLinkAttributes($this->extension->getDonationURL(), 'donate');
            $html .= ' <a ' . buildAttributes($params, true) . '>' . $this->getLang('donate_action') . '</a>';
        }


        $html .= '</div>';

        return $html;
    }

    /**
     * Create the details section
     *
     * @return string
     */
    protected function details()
    {
        $html = '<details>';
        $html .= '<summary>' . 'FIXME label' . '</summary>';


        $default = $this->getLang('unknown');
        $list = [];

        if (!$this->extension->isBundled()) {
            $list['downloadurl'] = $this->shortlink($this->extension->getDownloadURL(), 'download', $default);
            $list['repository'] = $this->shortlink($this->extension->getSourcerepoURL(), 'repo', $default);
        }

        if ($this->extension->isInstalled()) {
            if ($this->extension->isBundled()) {
                $list['installed_version'] = $this->getLang('status_bundled');
            } else {
                if ($this->extension->getInstalledVersion()) {
                    $list['installed_version'] = hsc($this->extension->getInstalledVersion());
                }
                if (!$this->extension->isBundled()) {
                    $updateDate = $this->extension->getManager()->getLastUpdate();
                    $list['install_date'] = $updateDate ? hsc($updateDate) : $default;
                }
            }
        }

        if (!$this->extension->isInstalled() || $this->extension->isUpdateAvailable()) {
            $list['available_version'] = $this->extension->getLastUpdate()
                ? hsc($this->extension->getLastUpdate())
                : $default;
        }


        if (!$this->extension->isBundled() && $this->extension->getCompatibleVersions()) {
            $list['compatible'] = join(', ', array_map(
                function ($date, $version) {
                    return '<bdi>' . $version['label'] . ' (' . $date . ')</bdi>';
                },
                array_keys($this->extension->getCompatibleVersions()),
                array_values($this->extension->getCompatibleVersions())
            ));
        }

        $tags = $this->extension->getTags();
        if ($tags) {
            $list['tags'] = join(', ', array_map(function ($tag) {
                $url = $this->tabURL('search', ['q' => 'tag:' . $tag]);
                return '<bdi><a href="' . $url . '">' . hsc($tag) . '</a></bdi>';
            }, $tags));
        }

        if ($this->extension->getDependencyList()) {
            $list['depends'] = $this->linkExtensions($this->extension->getDependencyList());
        }

        if ($this->extension->getSimilarList()) {
            $list['similar'] = $this->linkExtensions($this->extension->getSimilarList());
        }

        if ($this->extension->getConflictList()) {
            $list['conflicts'] = $this->linkExtensions($this->extension->getConflictList());
        }

        $html .= '<dl>';
        foreach ($list as $key => $value) {
            $html .= '<dt>' . $this->getLang($key) . '</dt>';
            $html .= '<dd>' . $value . '</dd>';
        }
        $html .= '</dl>';

        $html .= '</details>';
        return $html;
    }

    /**
     * Generate a link to the author of the extension
     *
     * @return string The HTML code of the link
     */
    protected function author()
    {
        if (!$this->extension->getAuthor()) {
            return '<em class="author">' . $this->getLang('unknown_author') . '</em>';
        }

        $mailid = $this->extension->getEmailID();
        if ($mailid) {
            $url = $this->tabURL('search', ['q' => 'authorid:' . $mailid]);
            $html = '<a href="' . $url . '" class="author" title="' . $this->getLang('author_hint') . '" >' .
                '<img src="//www.gravatar.com/avatar/' . $mailid .
                '?s=60&amp;d=mm" width="20" height="20" alt="" /> ' .
                hsc($this->extension->getAuthor()) . '</a>';
        } else {
            $html = '<span class="author">' . hsc($this->extension->getAuthor()) . '</span>';
        }
        return '<bdi>' . $html . '</bdi>';
    }

    /**
     * The popularity bar
     *
     * @return string
     */
    protected function popularity()
    {
        $popularity = $this->extension->getPopularity();
        if (!$popularity) return '';
        if ($this->extension->isBundled()) return '';

        $popularityText = sprintf($this->getLang('popularity'), round($popularity * 100, 2));
        return '<div class="popularity" title="' . $popularityText . '">' .
            '<div style="width: ' . ($popularity * 100) . '%;">' .
            '<span class="a11y">' . $popularityText . '</span>' .
            '</div></div>';

    }

    protected function actions()
    {
        global $conf;

        $html = '';
        $actions = [];
        $errors = [];

        // gather available actions and possible errors to show
        try {
            Installer::ensurePermissions($this->extension);

            if ($this->extension->isInstalled()) {

                if (!$this->extension->isProtected()) $actions[] = 'uninstall';
                if ($this->extension->getDownloadURL()) {
                    $actions[] = $this->extension->isUpdateAvailable() ? 'update' : 'reinstall';

                    if ($this->extension->isGitControlled()) {
                        $errors[] = $this->getLang('git');
                    }
                }

                if (!$this->extension->isProtected() && !$this->extension->isTemplate()) { // no enable/disable for templates
                    $actions[] = $this->extension->isEnabled() ? 'disable' : 'enable';

                    if (
                        $this->extension->isEnabled() &&
                        in_array('Auth', $this->extension->getComponentTypes()) &&
                        $conf['authtype'] != $this->extension->getID()
                    ) {
                        $errors[] = $this->getLang('auth');
                    }
                }
            } else {
                if ($this->extension->getDownloadURL()) {
                    $actions[] = 'install';
                }
            }
        } catch (\Exception $e) {
            $errors[] = $e->getMessage();
        }

        foreach ($actions as $action) {
            $html .= '<button name="fn[' . $action . '][' . $this->extension->getID() . ']" class="button" type="submit">' .
                $this->getLang('btn_' . $action) . '</button>';
        }

        foreach ($errors as $error) {
            $html .= '<div class="msg error">' . hsc($error) . '</div>';
        }

        return $html;
    }


    // endregion
    // region utility functions

    /**
     * Create the classes representing the state of the extension
     *
     * @return string
     */
    protected function getClasses()
    {
        $classes = ['extension', $this->extension->getType()];
        if ($this->extension->isInstalled()) $classes[] = 'installed';
        if ($this->extension->isUpdateAvailable()) $classes[] = 'update';
        $classes[] = $this->extension->isEnabled() ? 'enabled' : 'disabled';
        return implode(' ', $classes);
    }

    /**
     * Create an attributes array for a link
     *
     * Handles interwiki links to dokuwiki.org
     *
     * @param string $url The URL to link to
     * @param string $class Additional classes to add
     * @return array
     */
    protected function prepareLinkAttributes($url, $class)
    {
        global $conf;

        $attributes = [
            'href' => $url,
            'class' => 'urlextern',
            'target' => $conf['target']['extern'],
            'rel' => 'noopener',
            'title' => $url,
        ];

        if ($conf['relnofollow']) {
            $attributes['rel'] .= ' ugc nofollow';
        }

        if (preg_match('/^https?:\/\/(www\.)?dokuwiki\.org\//i', $url)) {
            $attributes['class'] = 'interwiki iw_doku';
            $attributes['target'] = $conf['target']['interwiki'];
            $attributes['rel'] = '';
        }

        $attributes['class'] .= ' ' . $class;
        return $attributes;
    }

    /**
     * Create a link from the given URL
     *
     * Shortens the URL for display
     *
     * @param string $url
     * @param string $class Additional classes to add
     * @param string $fallback If URL is empty return this fallback
     * @return string  HTML link
     */
    protected function shortlink($url, $class, $fallback = '')
    {
        if (!$url) return hsc($fallback);

        $link = parse_url($url);
        $base = $link['host'];
        if (!empty($link['port'])) $base .= $base . ':' . $link['port'];
        $long = $link['path'];
        if (!empty($link['query'])) $long .= $link['query'];

        $name = shorten($base, $long, 55);

        $params = $this->prepareLinkAttributes($url, $class);
        $html = '<a ' . buildAttributes($params, true) . '>' . hsc($name) . '</a>';
        return $html;
    }

    /**
     * Generate a list of links for extensions
     *
     * Links to the search tab with the extension name
     *
     * @param array $extensions The extension names
     * @return string The HTML code
     */
    public function linkExtensions($extensions)
    {
        $html = '';
        foreach ($extensions as $link) {
            $html .= '<bdi><a href="' .
                $this->tabURL('search', ['q' => 'ext:' . $link]) . '">' .
                hsc($link) . '</a></bdi>, ';
        }
        return rtrim($html, ', ');
    }

    // endregion
}