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