xref: /plugin/simplenavi/syntax.php (revision 4591df213c0843271c01901fb3dd32fa7d2794f4)
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
371169a1acSAndreas Gohr    function handle($match, $state, $pos, &$handler){
381169a1acSAndreas Gohr        $data = array(cleanID(substr($match,13,-2)));
391169a1acSAndreas Gohr
401169a1acSAndreas Gohr        return $data;
411169a1acSAndreas Gohr    }
421169a1acSAndreas Gohr
431169a1acSAndreas Gohr    function render($mode, &$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();
521169a1acSAndreas Gohr        search($data,$conf['datadir'],array($this,'_search'),array('ns' => $INFO['id']),$ns);
53*4591df21Slainme        ksort($data);
541169a1acSAndreas Gohr
551169a1acSAndreas Gohr        $R->doc .= '<div class="plugin__simplenavi">';
561169a1acSAndreas Gohr        $R->doc .= html_buildlist($data,'idx',array($this,'_list'),array($this,'_li'));
571169a1acSAndreas Gohr        $R->doc .= '</div>';
581169a1acSAndreas Gohr
591169a1acSAndreas Gohr        return true;
601169a1acSAndreas Gohr    }
611169a1acSAndreas Gohr
621169a1acSAndreas Gohr    function _list($item){
63492ddc4eSAndreas Gohr        global $INFO;
64492ddc4eSAndreas Gohr
65492ddc4eSAndreas Gohr        if(($item['type'] == 'd' && $item['open']) || $INFO['id'] == $item['id']){
660afc1916SAndreas Gohr            return '<strong>'.html_wikilink(':'.$item['id'],$this->_title($item['id'])).'</strong>';
67492ddc4eSAndreas Gohr        }else{
68e306992cSAndreas Gohr            return html_wikilink(':'.$item['id'],$this->_title($item['id']));
69492ddc4eSAndreas Gohr        }
701169a1acSAndreas Gohr
711169a1acSAndreas Gohr    }
721169a1acSAndreas Gohr
731169a1acSAndreas Gohr    function _li($item){
741169a1acSAndreas Gohr        if($item['type'] == "f"){
751169a1acSAndreas Gohr            return '<li class="level'.$item['level'].'">';
761169a1acSAndreas Gohr        }elseif($item['open']){
771169a1acSAndreas Gohr            return '<li class="open">';
781169a1acSAndreas Gohr        }else{
791169a1acSAndreas Gohr            return '<li class="closed">';
801169a1acSAndreas Gohr        }
811169a1acSAndreas Gohr    }
821169a1acSAndreas Gohr
831169a1acSAndreas Gohr    function _search(&$data,$base,$file,$type,$lvl,$opts){
841169a1acSAndreas Gohr        global $conf;
851169a1acSAndreas Gohr        $return = true;
861169a1acSAndreas Gohr
871169a1acSAndreas Gohr        $item = array();
881169a1acSAndreas Gohr
891169a1acSAndreas Gohr        $id = pathID($file);
901169a1acSAndreas Gohr
911169a1acSAndreas Gohr        if($type == 'd' && !(
921169a1acSAndreas Gohr            preg_match('#^'.$id.'(:|$)#',$opts['ns']) ||
931169a1acSAndreas Gohr            preg_match('#^'.$id.'(:|$)#',getNS($opts['ns']))
941169a1acSAndreas Gohr
951169a1acSAndreas Gohr        )){
961169a1acSAndreas Gohr            //add but don't recurse
971169a1acSAndreas Gohr            $return = false;
981169a1acSAndreas Gohr        }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
991169a1acSAndreas Gohr            //don't add
1001169a1acSAndreas Gohr            return false;
1011169a1acSAndreas Gohr        }
1021169a1acSAndreas Gohr
1031169a1acSAndreas Gohr        if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
1041169a1acSAndreas Gohr            return false;
1051169a1acSAndreas Gohr        }
1061169a1acSAndreas Gohr
1071169a1acSAndreas Gohr        if($type == 'd'){
1081169a1acSAndreas Gohr            // link directories to their start pages
1091169a1acSAndreas Gohr            $exists = false;
1101169a1acSAndreas Gohr            $id = "$id:";
1111169a1acSAndreas Gohr            resolve_pageid('',$id,$exists);
1121169a1acSAndreas Gohr            $this->startpages[$id] = 1;
1131169a1acSAndreas Gohr        }elseif($this->startpages[$id]){
1141169a1acSAndreas Gohr            // skip already shown start pages
1151169a1acSAndreas Gohr            return false;
1161169a1acSAndreas Gohr        }elseif(noNS($id) == $conf['start']){
1171169a1acSAndreas Gohr            // skip the main start page
1181169a1acSAndreas Gohr            return false;
1191169a1acSAndreas Gohr        }
1201169a1acSAndreas Gohr
1211169a1acSAndreas Gohr        //check hidden
1221169a1acSAndreas Gohr        if(isHiddenPage($id)){
1231169a1acSAndreas Gohr            return false;
1241169a1acSAndreas Gohr        }
1251169a1acSAndreas Gohr
1261169a1acSAndreas Gohr        //check ACL
1271169a1acSAndreas Gohr        if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
1281169a1acSAndreas Gohr            return false;
1291169a1acSAndreas Gohr        }
1301169a1acSAndreas Gohr
131*4591df21Slainme        $data[$id]=array( 'id'    => $id,
1321169a1acSAndreas Gohr                       'type'  => $type,
1331169a1acSAndreas Gohr                       'level' => $lvl,
1341169a1acSAndreas Gohr                       'open'  => $return);
1351169a1acSAndreas Gohr        return $return;
1361169a1acSAndreas Gohr    }
1371169a1acSAndreas Gohr
138e306992cSAndreas Gohr    function _title($id) {
139e306992cSAndreas Gohr        global $conf;
140e306992cSAndreas Gohr
141e306992cSAndreas Gohr        if(useHeading('navigation')){
142e306992cSAndreas Gohr            $p = p_get_first_heading($id);
143e306992cSAndreas Gohr        }
144e306992cSAndreas Gohr        if($p) return $p;
145e306992cSAndreas Gohr
146e306992cSAndreas Gohr        $p = noNS($id);
147e306992cSAndreas Gohr        if ($p == $conf['start'] || $p == false) {
148e306992cSAndreas Gohr            $p = noNS(getNS($id));
149e306992cSAndreas Gohr            if ($p == false) {
150e306992cSAndreas Gohr                return $conf['start'];
151e306992cSAndreas Gohr            }
152e306992cSAndreas Gohr        }
153e306992cSAndreas Gohr        return $p;
154e306992cSAndreas Gohr    }
1551169a1acSAndreas Gohr
156*4591df21Slainme    function _cmp($a, $b) {
157*4591df21Slainme        global $conf;
158*4591df21Slainme
159*4591df21Slainme        $a = preg_replace('/:'.$conf['start'].'$/', '', $a);
160*4591df21Slainme        $b = preg_replace('/:'.$conf['start'].'$/', '', $b);
161*4591df21Slainme
162*4591df21Slainme        return strcmp($a, $b);
163*4591df21Slainme    }
1641169a1acSAndreas Gohr}
1651169a1acSAndreas Gohr
166*4591df21Slainme// vim:ts=4:sw=4:et:
167