1<?php
2
3namespace dokuwiki\Ui;
4
5/**
6 * DokuWiki Index Interface
7 *
8 * @package dokuwiki\Ui
9 */
10class Index extends Ui
11{
12    protected $ns;
13
14    /**
15     * Index Ui constructor
16     *
17     * @param string $ns  namespace
18    */
19    public function __construct($ns = '')
20    {
21        $this->ns = $ns;
22    }
23
24
25    /**
26     * Display page index
27     *
28     * @author   Andreas Gohr <andi@splitbrain.org>
29     *
30     * @return void
31     */
32    public function show()
33    {
34        // print intro
35        print p_locale_xhtml('index');
36
37        print $this->sitemap();
38    }
39
40    /**
41     * Build html of sitemap, unordered list of pages under the namespace
42     *
43     * @return string
44     */
45    public function sitemap()
46    {
47        global $conf;
48        global $ID;
49
50        $ns = cleanID($this->ns);
51        if (empty($ns)){
52            $ns = getNS($ID);
53            if ($ns === false) $ns = '';
54        }
55        $ns = utf8_encodeFN(str_replace(':', '/', $ns));
56        $data = array();
57        search($data, $conf['datadir'], 'search_index', array('ns' => $ns));
58
59        return '<div id="index__tree" class="index__tree">'
60              . html_buildlist($data, 'idx', [$this,'formatListItem'], [$this,'tagListItem'])
61              . '</div>';
62    }
63
64    /**
65     * Index item formatter
66     *
67     * User function for html_buildlist()
68     *
69     * @author Andreas Gohr <andi@splitbrain.org>
70     *
71     * @param array $item
72     * @return string
73     */
74    public function formatListItem($item)    // RENAMED from html_list_index()
75    {
76        global $ID, $conf;
77
78        // prevent searchbots needlessly following links
79        $nofollow = ($ID != $conf['start'] || $conf['sitemap']) ? 'rel="nofollow"' : '';
80
81        $html = '';
82        $base = ':'.$item['id'];
83        $base = substr($base, strrpos($base,':') +1);
84        if ($item['type'] == 'd') {
85            // FS#2766, no need for search bots to follow namespace links in the index
86            $link = wl($ID, 'idx='. rawurlencode($item['id']));
87            $html .= '<a href="'. $link .'" title="'. $item['id'] .'" class="idx_dir"' . $nofollow .'><strong>';
88            $html .= $base;
89            $html .= '</strong></a>';
90        } else {
91            // default is noNSorNS($id), but we want noNS($id) when useheading is off FS#2605
92            $html .= html_wikilink(':'.$item['id'], useHeading('navigation') ? null : noNS($item['id']));
93        }
94        return $html;
95    }
96
97    /**
98     * Index List item
99     *
100     * This user function is used in html_buildlist to build the
101     * <li> tags for namespaces when displaying the page index
102     * it gives different classes to opened or closed "folders"
103     *
104     * @author Andreas Gohr <andi@splitbrain.org>
105     *
106     * @param array $item
107     * @return string html
108     */
109    public function tagListItem($item)    // RENAMED from html_li_index()
110    {
111        global $INFO;
112        global $ACT;
113
114        $class = '';
115        $id = '';
116
117        if ($item['type'] == 'f') {
118            // scroll to the current item
119            if (isset($INFO) && $item['id'] == $INFO['id'] && $ACT == 'index') {
120                $id = ' id="scroll__here"';
121                $class = ' bounce';
122            }
123            return '<li class="level'.$item['level'].$class.'" '.$id.'>';
124        } elseif ($item['open']) {
125            return '<li class="open">';
126        } else {
127            return '<li class="closed">';
128        }
129    }
130
131}
132