1<?php
2
3use dokuwiki\plugin\versionswitch\Version;
4use dokuwiki\Extension\SyntaxPlugin;
5
6/**
7 * DokuWiki Plugin versionswitch (Syntax Component)
8 *
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10 * @author Andreas Gohr <dokuwiki@cosmocode.de>
11 */
12class syntax_plugin_versionswitch extends SyntaxPlugin
13{
14    /** @inheritDoc */
15    public function getType()
16    {
17        return 'substition';
18    }
19
20    /** @inheritDoc */
21    public function getPType()
22    {
23        return 'normal';
24    }
25
26    /** @inheritDoc */
27    public function getSort()
28    {
29        return 255;
30    }
31
32    /** @inheritDoc */
33    public function connectTo($mode)
34    {
35        $this->Lexer->addSpecialPattern('~~VERSIONSWITCH~~', $mode, 'plugin_versionswitch');
36    }
37
38
39    /** @inheritDoc */
40    public function handle($match, $state, $pos, Doku_Handler $handler)
41    {
42        return [];
43    }
44
45    /** @inheritDoc */
46    public function render($mode, Doku_Renderer $renderer, $data)
47    {
48        if ($mode !== 'xhtml') {
49            return false;
50        }
51        $renderer->nocache();
52        $renderer->doc .= $this->versionSelector();
53        return true;
54    }
55
56    /**
57     * Render the version selector HTML
58     *
59     * @return string
60     */
61    public function versionSelector()
62    {
63        global $INFO;
64        $version = new Version($this->getConf('regexes'), $INFO['id']);
65        $base = $version->getBaseNamespace();
66        if ($base === '') return '';
67        $current = $version->getVersion();
68
69        $doc = '';
70        $doc .= '<ul class="plugin_versionswitch">';
71        foreach ($version->getVersions() as $ns => $title) {
72            $id = $base . ':' . $ns . ':' . $version->getIdPart();
73            if (auth_quickaclcheck($id) < AUTH_READ) continue; // skip if user can't read the target
74
75            $classes = [];
76            if ($ns === $current) $classes[] = 'current';
77            $classes[] = page_exists($id) ? 'exists' : 'missing';
78
79
80            $doc .= sprintf('<li class="%s">', implode(' ', $classes));
81            $doc .= html_wikilink($id, $title);
82            $doc .= '</li>';
83        }
84
85        $doc .= '</ul>';
86
87        return $doc;
88    }
89}
90