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