xref: /plugin/simplenavi/syntax.php (revision e75a33bf67169624ba61a7d08f13132b1856e309)
11169a1acSAndreas Gohr<?php
2d8ce5486SAndreas Gohr
3d8ce5486SAndreas Gohruse dokuwiki\File\PageResolver;
4*e75a33bfSAndreas Gohruse dokuwiki\Utf8\Sort;
5d8ce5486SAndreas Gohr
61169a1acSAndreas Gohr/**
71169a1acSAndreas Gohr * DokuWiki Plugin simplenavi (Syntax Component)
81169a1acSAndreas Gohr *
91169a1acSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
101169a1acSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
111169a1acSAndreas Gohr */
12d8ce5486SAndreas Gohrclass syntax_plugin_simplenavi extends DokuWiki_Syntax_Plugin
13d8ce5486SAndreas Gohr{
14d8ce5486SAndreas Gohr    private $startpages = [];
151169a1acSAndreas Gohr
16d8ce5486SAndreas Gohr    /** @inheritdoc */
17d8ce5486SAndreas Gohr    public function getType()
18d8ce5486SAndreas Gohr    {
191169a1acSAndreas Gohr        return 'substition';
201169a1acSAndreas Gohr    }
211169a1acSAndreas Gohr
22d8ce5486SAndreas Gohr    /** @inheritdoc */
23d8ce5486SAndreas Gohr    public function getPType()
24d8ce5486SAndreas Gohr    {
251169a1acSAndreas Gohr        return 'block';
261169a1acSAndreas Gohr    }
271169a1acSAndreas Gohr
28d8ce5486SAndreas Gohr    /** @inheritdoc */
29d8ce5486SAndreas Gohr    public function getSort()
30d8ce5486SAndreas Gohr    {
311169a1acSAndreas Gohr        return 155;
321169a1acSAndreas Gohr    }
331169a1acSAndreas Gohr
34d8ce5486SAndreas Gohr    /** @inheritdoc */
35d8ce5486SAndreas Gohr    public function connectTo($mode)
36d8ce5486SAndreas Gohr    {
371169a1acSAndreas Gohr        $this->Lexer->addSpecialPattern('{{simplenavi>[^}]*}}', $mode, 'plugin_simplenavi');
381169a1acSAndreas Gohr    }
391169a1acSAndreas Gohr
40d8ce5486SAndreas Gohr    /** @inheritdoc */
41d8ce5486SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
42d8ce5486SAndreas Gohr    {
435655937aSAndreas Gohr        return explode(' ', substr($match, 13, -2));
441169a1acSAndreas Gohr    }
451169a1acSAndreas Gohr
46d8ce5486SAndreas Gohr    /** @inheritdoc */
47d8ce5486SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
48d8ce5486SAndreas Gohr    {
49d8ce5486SAndreas Gohr        if ($format != 'xhtml') return false;
501169a1acSAndreas Gohr
511169a1acSAndreas Gohr        global $conf;
521169a1acSAndreas Gohr        global $INFO;
53b3e02951SAndreas Gohr        $renderer->nocache();
541169a1acSAndreas Gohr
55b3e02951SAndreas Gohr        // first data is namespace, rest is options
565655937aSAndreas Gohr        $ns = array_shift($data);
575655937aSAndreas Gohr        if ($ns && $ns[0] === '.') {
585655937aSAndreas Gohr            // resolve relative to current page
595655937aSAndreas Gohr            $ns = getNS((new PageResolver($INFO['id']))->resolveId("$ns:xxx"));
605655937aSAndreas Gohr        } else {
615655937aSAndreas Gohr            $ns = cleanID($ns);
625655937aSAndreas Gohr        }
635655937aSAndreas Gohr        // convert to path
645655937aSAndreas Gohr        $ns = utf8_encodeFN(str_replace(':', '/', $ns));
65b3e02951SAndreas Gohr
66*e75a33bfSAndreas Gohr        $items = $this->getSortedItems(
67*e75a33bfSAndreas Gohr            $ns,
68*e75a33bfSAndreas Gohr            $INFO['id'],
69*e75a33bfSAndreas Gohr            $this->getConf('usetitle'),
70*e75a33bfSAndreas Gohr            $this->getConf('natsort')
71*e75a33bfSAndreas Gohr        );
721169a1acSAndreas Gohr
73b3e02951SAndreas Gohr        $class = 'plugin__simplenavi';
74b3e02951SAndreas Gohr        if (in_array('filter', $data)) $class .= ' plugin__simplenavi_filter';
75b3e02951SAndreas Gohr
76b3e02951SAndreas Gohr        $renderer->doc .= '<div class="' . $class . '">';
77d8ce5486SAndreas Gohr        $renderer->doc .= html_buildlist($items, 'idx', [$this, 'cbList'], [$this, 'cbListItem']);
78d8ce5486SAndreas Gohr        $renderer->doc .= '</div>';
791169a1acSAndreas Gohr
801169a1acSAndreas Gohr        return true;
811169a1acSAndreas Gohr    }
821169a1acSAndreas Gohr
83d8ce5486SAndreas Gohr    /**
84*e75a33bfSAndreas Gohr     * Fetch the items to display
85*e75a33bfSAndreas Gohr     *
86*e75a33bfSAndreas Gohr     * This returns a flat list suitable for html_buildlist()
87*e75a33bfSAndreas Gohr     *
88*e75a33bfSAndreas Gohr     * @param string $ns the namespace to search in
89*e75a33bfSAndreas Gohr     * @param string $current the current page, the tree will be expanded to this
90*e75a33bfSAndreas Gohr     * @param bool $useTitle Sort by the title instead of the ID?
91*e75a33bfSAndreas Gohr     * @param bool $useNatSort Use natural sorting or just sort by ASCII?
92*e75a33bfSAndreas Gohr     * @return array
93*e75a33bfSAndreas Gohr     */
94*e75a33bfSAndreas Gohr    public function getSortedItems($ns, $current, $useTitle, $useNatSort)
95*e75a33bfSAndreas Gohr    {
96*e75a33bfSAndreas Gohr        global $conf;
97*e75a33bfSAndreas Gohr
98*e75a33bfSAndreas Gohr        // execute search using our own callback
99*e75a33bfSAndreas Gohr        $items = [];
100*e75a33bfSAndreas Gohr        search(
101*e75a33bfSAndreas Gohr            $items,
102*e75a33bfSAndreas Gohr            $conf['datadir'],
103*e75a33bfSAndreas Gohr            [$this, 'cbSearch'],
104*e75a33bfSAndreas Gohr            [
105*e75a33bfSAndreas Gohr                'currentID' => $current,
106*e75a33bfSAndreas Gohr                'usetitle' => $useTitle,
107*e75a33bfSAndreas Gohr            ],
108*e75a33bfSAndreas Gohr            $ns,
109*e75a33bfSAndreas Gohr            1,
110*e75a33bfSAndreas Gohr            '' // no sorting, we do ourselves
111*e75a33bfSAndreas Gohr        );
112*e75a33bfSAndreas Gohr
113*e75a33bfSAndreas Gohr        // split into separate levels
114*e75a33bfSAndreas Gohr        $current = 1;
115*e75a33bfSAndreas Gohr        $parents = [];
116*e75a33bfSAndreas Gohr        $levels = [];
117*e75a33bfSAndreas Gohr        foreach ($items as $idx => $item) {
118*e75a33bfSAndreas Gohr            if ($current < $item['level']) {
119*e75a33bfSAndreas Gohr                // previous item was the parent
120*e75a33bfSAndreas Gohr                $parents[] = array_key_last($levels[$current]);
121*e75a33bfSAndreas Gohr            }
122*e75a33bfSAndreas Gohr            $current = $item['level'];
123*e75a33bfSAndreas Gohr            $levels[$item['level']][$idx] = $item;
124*e75a33bfSAndreas Gohr        }
125*e75a33bfSAndreas Gohr
126*e75a33bfSAndreas Gohr        // sort each level separately
127*e75a33bfSAndreas Gohr        foreach ($levels as $level => $items) {
128*e75a33bfSAndreas Gohr            uasort($items, function ($a, $b) use ($useNatSort) {
129*e75a33bfSAndreas Gohr                return $this->itemComparator($a, $b, $useNatSort);
130*e75a33bfSAndreas Gohr            });
131*e75a33bfSAndreas Gohr            $levels[$level] = $items;
132*e75a33bfSAndreas Gohr        }
133*e75a33bfSAndreas Gohr
134*e75a33bfSAndreas Gohr        // merge levels into a flat list again
135*e75a33bfSAndreas Gohr        $levels = array_reverse($levels, true);
136*e75a33bfSAndreas Gohr        foreach ($levels as $level => $items) {
137*e75a33bfSAndreas Gohr            if ($level == 1) break;
138*e75a33bfSAndreas Gohr
139*e75a33bfSAndreas Gohr            $parent = array_pop($parents);
140*e75a33bfSAndreas Gohr            $pos = array_search($parent, array_keys($levels[$level - 1])) + 1;
141*e75a33bfSAndreas Gohr
142*e75a33bfSAndreas Gohr            $levels[$level - 1] = array_slice($levels[$level - 1], 0, $pos, true) +
143*e75a33bfSAndreas Gohr                $levels[$level] +
144*e75a33bfSAndreas Gohr                array_slice($levels[$level - 1], $pos, null, true);
145*e75a33bfSAndreas Gohr        }
146*e75a33bfSAndreas Gohr        $items = $levels[1];
147*e75a33bfSAndreas Gohr
148*e75a33bfSAndreas Gohr
149*e75a33bfSAndreas Gohr        return $items;
150*e75a33bfSAndreas Gohr    }
151*e75a33bfSAndreas Gohr
152*e75a33bfSAndreas Gohr    /**
153*e75a33bfSAndreas Gohr     * Compare two items
154*e75a33bfSAndreas Gohr     *
155*e75a33bfSAndreas Gohr     * @param array $a
156*e75a33bfSAndreas Gohr     * @param array $b
157*e75a33bfSAndreas Gohr     * @param bool $useNatSort
158*e75a33bfSAndreas Gohr     * @return int
159*e75a33bfSAndreas Gohr     */
160*e75a33bfSAndreas Gohr    public function itemComparator($a, $b, $useNatSort)
161*e75a33bfSAndreas Gohr    {
162*e75a33bfSAndreas Gohr        if ($useNatSort) {
163*e75a33bfSAndreas Gohr            return Sort::strcmp($a['title'], $b['title']);
164*e75a33bfSAndreas Gohr        } else {
165*e75a33bfSAndreas Gohr            return strcmp($a['title'], $b['title']);
166*e75a33bfSAndreas Gohr        }
167*e75a33bfSAndreas Gohr    }
168*e75a33bfSAndreas Gohr
169*e75a33bfSAndreas Gohr
170*e75a33bfSAndreas Gohr    /**
171d8ce5486SAndreas Gohr     * Create a list openening
172d8ce5486SAndreas Gohr     *
173d8ce5486SAndreas Gohr     * @param array $item
174d8ce5486SAndreas Gohr     * @return string
175d8ce5486SAndreas Gohr     * @see html_buildlist()
176d8ce5486SAndreas Gohr     */
177d8ce5486SAndreas Gohr    public function cbList($item)
178d8ce5486SAndreas Gohr    {
179492ddc4eSAndreas Gohr        global $INFO;
180492ddc4eSAndreas Gohr
181492ddc4eSAndreas Gohr        if (($item['type'] == 'd' && $item['open']) || $INFO['id'] == $item['id']) {
182*e75a33bfSAndreas Gohr            return '<strong>' . html_wikilink(':' . $item['id'], $item['title']) . '</strong>';
183492ddc4eSAndreas Gohr        } else {
184*e75a33bfSAndreas Gohr            return html_wikilink(':' . $item['id'], $item['title']);
185492ddc4eSAndreas Gohr        }
1861169a1acSAndreas Gohr
1871169a1acSAndreas Gohr    }
1881169a1acSAndreas Gohr
189d8ce5486SAndreas Gohr    /**
190d8ce5486SAndreas Gohr     * Create a list item
191d8ce5486SAndreas Gohr     *
192d8ce5486SAndreas Gohr     * @param array $item
193d8ce5486SAndreas Gohr     * @return string
194d8ce5486SAndreas Gohr     * @see html_buildlist()
195d8ce5486SAndreas Gohr     */
196d8ce5486SAndreas Gohr    public function cbListItem($item)
197d8ce5486SAndreas Gohr    {
1981169a1acSAndreas Gohr        if ($item['type'] == "f") {
1991169a1acSAndreas Gohr            return '<li class="level' . $item['level'] . '">';
2001169a1acSAndreas Gohr        } elseif ($item['open']) {
2011169a1acSAndreas Gohr            return '<li class="open">';
2021169a1acSAndreas Gohr        } else {
2031169a1acSAndreas Gohr            return '<li class="closed">';
2041169a1acSAndreas Gohr        }
2051169a1acSAndreas Gohr    }
2061169a1acSAndreas Gohr
207d8ce5486SAndreas Gohr    /**
208d8ce5486SAndreas Gohr     * Custom search callback
209d8ce5486SAndreas Gohr     *
210d8ce5486SAndreas Gohr     * @param $data
211d8ce5486SAndreas Gohr     * @param $base
212d8ce5486SAndreas Gohr     * @param $file
213d8ce5486SAndreas Gohr     * @param $type
214d8ce5486SAndreas Gohr     * @param $lvl
215*e75a33bfSAndreas Gohr     * @param array $opts - currentID is the currently shown page
216d8ce5486SAndreas Gohr     * @return bool
217d8ce5486SAndreas Gohr     */
218d8ce5486SAndreas Gohr    public function cbSearch(&$data, $base, $file, $type, $lvl, $opts)
219d8ce5486SAndreas Gohr    {
2201169a1acSAndreas Gohr        global $conf;
2211169a1acSAndreas Gohr        $return = true;
2221169a1acSAndreas Gohr
2231169a1acSAndreas Gohr        $id = pathID($file);
2241169a1acSAndreas Gohr
2251169a1acSAndreas Gohr        if ($type == 'd' && !(
226*e75a33bfSAndreas Gohr                preg_match('#^' . $id . '(:|$)#', $opts['currentID']) ||
227*e75a33bfSAndreas Gohr                preg_match('#^' . $id . '(:|$)#', getNS($opts['currentID']))
2281169a1acSAndreas Gohr
2291169a1acSAndreas Gohr            )) {
2301169a1acSAndreas Gohr            //add but don't recurse
2311169a1acSAndreas Gohr            $return = false;
232303e1405SMichael Große        } elseif ($type == 'f' && (!empty($opts['nofiles']) || substr($file, -4) != '.txt')) {
2331169a1acSAndreas Gohr            //don't add
2341169a1acSAndreas Gohr            return false;
2351169a1acSAndreas Gohr        }
2361169a1acSAndreas Gohr
237660b56c3SAndreas Gohr        // for sneaky index, check access to the namespace's start page
238660b56c3SAndreas Gohr        if ($type == 'd' && $conf['sneaky_index']) {
239660b56c3SAndreas Gohr            $sp = (new PageResolver(''))->resolveId($id . ':');
240660b56c3SAndreas Gohr            if (auth_quickaclcheck($sp) < AUTH_READ) {
2411169a1acSAndreas Gohr                return false;
2421169a1acSAndreas Gohr            }
243660b56c3SAndreas Gohr        }
2441169a1acSAndreas Gohr
2451169a1acSAndreas Gohr        if ($type == 'd') {
2461169a1acSAndreas Gohr            // link directories to their start pages
247*e75a33bfSAndreas Gohr            $original = $id;
2481169a1acSAndreas Gohr            $id = "$id:";
249d8ce5486SAndreas Gohr            $id = (new PageResolver(''))->resolveId($id);
2501169a1acSAndreas Gohr            $this->startpages[$id] = 1;
251*e75a33bfSAndreas Gohr
252*e75a33bfSAndreas Gohr            // if the resolve id is in the same namespace as the original it's a start page named like the dir
253*e75a33bfSAndreas Gohr            if (getNS($original) == getNS($id)) {
254*e75a33bfSAndreas Gohr                $useNS = $original;
255*e75a33bfSAndreas Gohr            }
256*e75a33bfSAndreas Gohr
257303e1405SMichael Große        } elseif (!empty($this->startpages[$id])) {
2581169a1acSAndreas Gohr            // skip already shown start pages
2591169a1acSAndreas Gohr            return false;
2601169a1acSAndreas Gohr        } elseif (noNS($id) == $conf['start']) {
2611169a1acSAndreas Gohr            // skip the main start page
2621169a1acSAndreas Gohr            return false;
2631169a1acSAndreas Gohr        }
2641169a1acSAndreas Gohr
2651169a1acSAndreas Gohr        //check hidden
2661169a1acSAndreas Gohr        if (isHiddenPage($id)) {
2671169a1acSAndreas Gohr            return false;
2681169a1acSAndreas Gohr        }
2691169a1acSAndreas Gohr
2701169a1acSAndreas Gohr        //check ACL
2711169a1acSAndreas Gohr        if ($type == 'f' && auth_quickaclcheck($id) < AUTH_READ) {
2721169a1acSAndreas Gohr            return false;
2731169a1acSAndreas Gohr        }
2741169a1acSAndreas Gohr
275*e75a33bfSAndreas Gohr        $data[$id] = [
276d8ce5486SAndreas Gohr            'id' => $id,
2771169a1acSAndreas Gohr            'type' => $type,
2781169a1acSAndreas Gohr            'level' => $lvl,
279d8ce5486SAndreas Gohr            'open' => $return,
280*e75a33bfSAndreas Gohr            'title' => $this->getTitle($id, $opts['usetitle']),
281*e75a33bfSAndreas Gohr            'ns' => $useNS ?? (string)getNS($id),
282*e75a33bfSAndreas Gohr        ];
283*e75a33bfSAndreas Gohr
2841169a1acSAndreas Gohr        return $return;
2851169a1acSAndreas Gohr    }
2861169a1acSAndreas Gohr
287d8ce5486SAndreas Gohr    /**
288d8ce5486SAndreas Gohr     * Get the title for the given page ID
289d8ce5486SAndreas Gohr     *
290d8ce5486SAndreas Gohr     * @param string $id
291*e75a33bfSAndreas Gohr     * @param bool $usetitle - use the first heading as title
292d8ce5486SAndreas Gohr     * @return string
293d8ce5486SAndreas Gohr     */
294*e75a33bfSAndreas Gohr    protected function getTitle($id, $usetitle)
295d8ce5486SAndreas Gohr    {
296e306992cSAndreas Gohr        global $conf;
297e306992cSAndreas Gohr
298*e75a33bfSAndreas Gohr        if ($usetitle) {
299e306992cSAndreas Gohr            $p = p_get_first_heading($id);
300303e1405SMichael Große            if (!empty($p)) return $p;
301*e75a33bfSAndreas Gohr        }
302e306992cSAndreas Gohr
303e306992cSAndreas Gohr        $p = noNS($id);
304d8ce5486SAndreas Gohr        if ($p == $conf['start'] || !$p) {
305e306992cSAndreas Gohr            $p = noNS(getNS($id));
306d8ce5486SAndreas Gohr            if (!$p) {
307e306992cSAndreas Gohr                return $conf['start'];
308e306992cSAndreas Gohr            }
309e306992cSAndreas Gohr        }
310e306992cSAndreas Gohr        return $p;
311e306992cSAndreas Gohr    }
3121169a1acSAndreas Gohr}
313