xref: /plugin/simplenavi/syntax.php (revision 303e14058d2800ae86b9997bab748b2574fdd6e6)
11169a1acSAndreas Gohr<?php
21169a1acSAndreas Gohr/**
31169a1acSAndreas Gohr * DokuWiki Plugin simplenavi (Syntax Component)
41169a1acSAndreas Gohr *
51169a1acSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
61169a1acSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
71169a1acSAndreas Gohr */
81169a1acSAndreas Gohr
91169a1acSAndreas Gohr// must be run within Dokuwiki
101169a1acSAndreas Gohrif (!defined('DOKU_INC')) die();
111169a1acSAndreas Gohr
121169a1acSAndreas Gohrif (!defined('DOKU_LF')) define('DOKU_LF', "\n");
131169a1acSAndreas Gohrif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
141169a1acSAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
151169a1acSAndreas Gohr
161169a1acSAndreas Gohrrequire_once DOKU_PLUGIN.'syntax.php';
17e306992cSAndreas Gohrrequire_once DOKU_INC.'inc/search.php';
181169a1acSAndreas Gohr
191169a1acSAndreas Gohrclass syntax_plugin_simplenavi extends DokuWiki_Syntax_Plugin {
201169a1acSAndreas Gohr    function getType() {
211169a1acSAndreas Gohr        return 'substition';
221169a1acSAndreas Gohr    }
231169a1acSAndreas Gohr
241169a1acSAndreas Gohr    function getPType() {
251169a1acSAndreas Gohr        return 'block';
261169a1acSAndreas Gohr    }
271169a1acSAndreas Gohr
281169a1acSAndreas Gohr    function getSort() {
291169a1acSAndreas Gohr        return 155;
301169a1acSAndreas Gohr    }
311169a1acSAndreas Gohr
321169a1acSAndreas Gohr
331169a1acSAndreas Gohr    function connectTo($mode) {
341169a1acSAndreas Gohr        $this->Lexer->addSpecialPattern('{{simplenavi>[^}]*}}',$mode,'plugin_simplenavi');
351169a1acSAndreas Gohr    }
361169a1acSAndreas Gohr
37c7bf8c86SMichael Große    function handle($match, $state, $pos, Doku_Handler $handler){
381169a1acSAndreas Gohr        $data = array(cleanID(substr($match,13,-2)));
391169a1acSAndreas Gohr
401169a1acSAndreas Gohr        return $data;
411169a1acSAndreas Gohr    }
421169a1acSAndreas Gohr
43c7bf8c86SMichael Große    function render($mode, Doku_Renderer $R, $pass) {
441169a1acSAndreas Gohr        if($mode != 'xhtml') return false;
451169a1acSAndreas Gohr
461169a1acSAndreas Gohr        global $conf;
471169a1acSAndreas Gohr        global $INFO;
481169a1acSAndreas Gohr        $R->info['cache'] = false;
491169a1acSAndreas Gohr
501169a1acSAndreas Gohr        $ns = utf8_encodeFN(str_replace(':','/',$pass[0]));
511169a1acSAndreas Gohr        $data = array();
52306670aeSMichael Große        search($data,$conf['datadir'],array($this,'_search'),array('ns' => $INFO['id']),$ns,1,'natural');
53daf2dc98SOliver        if ($this->getConf('sortByTitle') == true) {
54daf2dc98SOliver            $this->_sortByTitle($data,"id");
55daf2dc98SOliver        } else {
56db559ff9SMichael Große            if ($this->getConf('sort') == 'ascii') {
57ba93e3e3Slainme                uksort($data, array($this, '_cmp'));
58db559ff9SMichael Große            }
59daf2dc98SOliver        }
601169a1acSAndreas Gohr
611169a1acSAndreas Gohr        $R->doc .= '<div class="plugin__simplenavi">';
621169a1acSAndreas Gohr        $R->doc .= html_buildlist($data,'idx',array($this,'_list'),array($this,'_li'));
631169a1acSAndreas Gohr        $R->doc .= '</div>';
641169a1acSAndreas Gohr
651169a1acSAndreas Gohr        return true;
661169a1acSAndreas Gohr    }
671169a1acSAndreas Gohr
681169a1acSAndreas Gohr    function _list($item){
69492ddc4eSAndreas Gohr        global $INFO;
70492ddc4eSAndreas Gohr
71492ddc4eSAndreas Gohr        if(($item['type'] == 'd' && $item['open']) || $INFO['id'] == $item['id']){
720afc1916SAndreas Gohr            return '<strong>'.html_wikilink(':'.$item['id'],$this->_title($item['id'])).'</strong>';
73492ddc4eSAndreas Gohr        }else{
74e306992cSAndreas Gohr            return html_wikilink(':'.$item['id'],$this->_title($item['id']));
75492ddc4eSAndreas Gohr        }
761169a1acSAndreas Gohr
771169a1acSAndreas Gohr    }
781169a1acSAndreas Gohr
791169a1acSAndreas Gohr    function _li($item){
801169a1acSAndreas Gohr        if($item['type'] == "f"){
811169a1acSAndreas Gohr            return '<li class="level'.$item['level'].'">';
821169a1acSAndreas Gohr        }elseif($item['open']){
831169a1acSAndreas Gohr            return '<li class="open">';
841169a1acSAndreas Gohr        }else{
851169a1acSAndreas Gohr            return '<li class="closed">';
861169a1acSAndreas Gohr        }
871169a1acSAndreas Gohr    }
881169a1acSAndreas Gohr
891169a1acSAndreas Gohr    function _search(&$data,$base,$file,$type,$lvl,$opts){
901169a1acSAndreas Gohr        global $conf;
911169a1acSAndreas Gohr        $return = true;
921169a1acSAndreas Gohr
931169a1acSAndreas Gohr        $item = array();
941169a1acSAndreas Gohr
951169a1acSAndreas Gohr        $id = pathID($file);
961169a1acSAndreas Gohr
971169a1acSAndreas Gohr        if($type == 'd' && !(
981169a1acSAndreas Gohr            preg_match('#^'.$id.'(:|$)#',$opts['ns']) ||
991169a1acSAndreas Gohr            preg_match('#^'.$id.'(:|$)#',getNS($opts['ns']))
1001169a1acSAndreas Gohr
1011169a1acSAndreas Gohr        )){
1021169a1acSAndreas Gohr            //add but don't recurse
1031169a1acSAndreas Gohr            $return = false;
104*303e1405SMichael Große        }elseif($type == 'f' && (!empty($opts['nofiles']) || substr($file,-4) != '.txt')){
1051169a1acSAndreas Gohr            //don't add
1061169a1acSAndreas Gohr            return false;
1071169a1acSAndreas Gohr        }
1081169a1acSAndreas Gohr
1091169a1acSAndreas Gohr        if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
1101169a1acSAndreas Gohr            return false;
1111169a1acSAndreas Gohr        }
1121169a1acSAndreas Gohr
1131169a1acSAndreas Gohr        if($type == 'd'){
1141169a1acSAndreas Gohr            // link directories to their start pages
1151169a1acSAndreas Gohr            $exists = false;
1161169a1acSAndreas Gohr            $id = "$id:";
1171169a1acSAndreas Gohr            resolve_pageid('',$id,$exists);
1181169a1acSAndreas Gohr            $this->startpages[$id] = 1;
119*303e1405SMichael Große        }elseif(!empty($this->startpages[$id])){
1201169a1acSAndreas Gohr            // skip already shown start pages
1211169a1acSAndreas Gohr            return false;
1221169a1acSAndreas Gohr        }elseif(noNS($id) == $conf['start']){
1231169a1acSAndreas Gohr            // skip the main start page
1241169a1acSAndreas Gohr            return false;
1251169a1acSAndreas Gohr        }
1261169a1acSAndreas Gohr
1271169a1acSAndreas Gohr        //check hidden
1281169a1acSAndreas Gohr        if(isHiddenPage($id)){
1291169a1acSAndreas Gohr            return false;
1301169a1acSAndreas Gohr        }
1311169a1acSAndreas Gohr
1321169a1acSAndreas Gohr        //check ACL
1331169a1acSAndreas Gohr        if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
1341169a1acSAndreas Gohr            return false;
1351169a1acSAndreas Gohr        }
1361169a1acSAndreas Gohr
1374591df21Slainme        $data[$id]=array( 'id'    => $id,
1381169a1acSAndreas Gohr                       'type'  => $type,
1391169a1acSAndreas Gohr                       'level' => $lvl,
1401169a1acSAndreas Gohr                       'open'  => $return);
1411169a1acSAndreas Gohr        return $return;
1421169a1acSAndreas Gohr    }
1431169a1acSAndreas Gohr
144e306992cSAndreas Gohr    function _title($id) {
145e306992cSAndreas Gohr        global $conf;
146e306992cSAndreas Gohr
147e306992cSAndreas Gohr        if(useHeading('navigation')){
148e306992cSAndreas Gohr            $p = p_get_first_heading($id);
149e306992cSAndreas Gohr        }
150*303e1405SMichael Große        if(!empty($p)) return $p;
151e306992cSAndreas Gohr
152e306992cSAndreas Gohr        $p = noNS($id);
153e306992cSAndreas Gohr        if ($p == $conf['start'] || $p == false) {
154e306992cSAndreas Gohr            $p = noNS(getNS($id));
155e306992cSAndreas Gohr            if ($p == false) {
156e306992cSAndreas Gohr                return $conf['start'];
157e306992cSAndreas Gohr            }
158e306992cSAndreas Gohr        }
159e306992cSAndreas Gohr        return $p;
160e306992cSAndreas Gohr    }
1611169a1acSAndreas Gohr
1624591df21Slainme    function _cmp($a, $b) {
1634591df21Slainme        global $conf;
164c9936b5bSMichael Große        $a = preg_replace('/'.preg_quote($conf['start'], '/').'$/', '', $a);
165c9936b5bSMichael Große        $b = preg_replace('/'.preg_quote($conf['start'], '/').'$/', '', $b);
1662122c0d0SMichael Große        $a = str_replace(':', '/', $a);
1672122c0d0SMichael Große        $b = str_replace(':', '/', $b);
1684591df21Slainme
1694591df21Slainme        return strcmp($a, $b);
1704591df21Slainme    }
171daf2dc98SOliver
172daf2dc98SOliver    function _sortByTitle(&$array, $key) {
173daf2dc98SOliver        $sorter = array();
174daf2dc98SOliver        $ret = array();
175daf2dc98SOliver        reset($array);
176daf2dc98SOliver        foreach ($array as $ii => $va) {
177daf2dc98SOliver            $sorter[$ii] = $this->_title($va[$key]);
178daf2dc98SOliver        }
179daf2dc98SOliver        if ($this->getConf('sort') == 'ascii') {
180daf2dc98SOliver            uksort($sorter, array($this, '_cmp'));
181daf2dc98SOliver        } else {
182daf2dc98SOliver            natcasesort($sorter);
183daf2dc98SOliver        }
184daf2dc98SOliver        foreach ($sorter as $ii => $va) {
185daf2dc98SOliver            $ret[$ii] = $array[$ii];
186daf2dc98SOliver        }
187daf2dc98SOliver        $array = $ret;
188daf2dc98SOliver    }
189daf2dc98SOliver
1901169a1acSAndreas Gohr}
1911169a1acSAndreas Gohr
1924591df21Slainme// vim:ts=4:sw=4:et:
193