1<?php
2/* This entire page is adapted from the indexmenu2 plugin
3 * https://www.dokuwiki.org/plugin:indexmenu2 */
4
5function indexmenu_search_index(&$data, $base, $file, $type, $lvl, $opts) {
6    global $conf;
7    $ret = true;
8
9    $item = array();
10    if ($type == 'f' && !preg_match('#\.txt$#', $file)) {
11        // don't add
12        return false;
13    }
14
15    // get page id by filename
16    $id = pathID($file);
17
18    // check hiddens
19    if ($type=='f' && isHiddenPage($id)) {
20        return false;
21    }
22
23    //  bugfix for the
24    //  /ns/
25    //  /<ns>.txt
26    //  case, need to force the 'directory' type
27    if ($type == 'f' && file_exists(dirname(wikiFN($id.":".noNS($id))))) $type = 'd';
28
29    // page target id = global id
30    $target = $id;
31    if ($type == 'd') {
32        // this will check 3 kinds of headpage:
33        // 1. /<ns>/<ns>.txt
34        // 2. /<ns>/
35        //    /<ns>.txt
36        // 3. /<ns>/
37        //    /<ns>/<start_page>
38        $nsa = array( $id.":".noNS($id),
39            $id,
40            $id.":".$conf['start']
41        );
42        $nspage = false;
43        foreach ($nsa as $nsp) {
44            if (@file_exists(wikiFN($nsp)) && auth_quickaclcheck($nsp) >= AUTH_READ) {
45                $nspage = $nsp;
46                break;
47            }
48        }
49        //headpage exists
50        if ($nspage) {
51            $target = $nspage;
52        } else {
53            // open namespace index, if headpage does not exists
54            $target = $target.':';
55        }
56    }
57
58    $data[]=array(
59        'id'     => $id
60        ,'date'   => @filectime(wikiFN($target))
61        ,'type'   => $type
62        ,'target' => $target  // id to be used in the menu
63        ,'title'  => ($conf['useheading'] && ($title = p_get_first_heading($target)))?$title:$id // NS title
64        ,'level'  => $lvl );
65    if (substr_count($id, ":") > 2) $ret = 0;
66    return $ret;
67}
68
69function array2tree($source_arr, $parent_id, $key_children='child_nodes', $key_id='id', $key_parent_id='parent_id') {
70    $tree = array();
71    if (empty($source_arr))
72        return $tree;
73    _array2treer($source_arr, $tree, $parent_id, $parent_id, $key_children, $key_id, $key_parent_id);
74    return $tree;
75}
76function _array2treer($source_arr, &$_this, $parent_id, $_this_id, $key_children, $key_id, $key_parent_id) {
77    // populate current children
78    foreach ($source_arr as $value)
79        if ($value[$key_parent_id]===$_this_id)
80            $_this[$key_children][$value[$key_id]]=$value;
81    if (isset($_this[$key_children])) {
82        // populate children of the current children
83        foreach ($_this[$key_children] as $value)
84            _array2treer($source_arr, $_this[$key_children][$value[$key_id]], $parent_id, $value[$key_id], $key_children, $key_id, $key_parent_id);
85        // make the tree root look pretty (more convenient to use such tree)
86        if ($_this_id===$parent_id)
87            $_this=$_this[$key_children];
88    }
89}
90
91function _html_buildlist(&$data, $level) {
92    $ret = array();
93
94    foreach ($data as $item) {
95        $ret[] = "<li class=\"level" . $level . (($item['type'] == 'd') ? (" dir") : '') . "\">";
96        $ret[] = "<div class=\"li\">" . preg_replace("#^<span[^>]+>(.+)</span>$#i", "$1", html_wikilink(":" . $item['target'], null)) . "</div>";
97        // append child nodes, if exists
98        if ($item['type']=='d') { //isset($item['child_nodes'])) {
99            if (isset($item['child_nodes'])) {
100                $ret[] = "<ul>";
101                // static method used to be able to make menu w/o make class object
102                $ret[] = _html_buildlist($item['child_nodes'], $level + 1);
103                $ret[] = "</ul>";
104            }
105        }
106        $ret[] = "</li>";
107    }
108    return join("\n",$ret);
109}
110
111// TODO: add caching
112
113$data = array();
114
115search($data, $conf['datadir'], "indexmenu_search_index", $opts, "/".utf8_encodeFN(str_replace(':','/',$ns)), 2);
116
117foreach ($data as $k => $v) {
118    $data[$k]['parent_id'] = (string)getNS($v['id']);
119}
120
121$data = array2tree($data,'');
122
123$data = "<ul class=\"generated-index\">" . _html_buildlist($data, 1) . "</ul>";
124echo $data;
125
126?>
127