1<?php
2/**
3 * Info Plugin: Simple related namespaces plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 * @author     Christophe Gragnic <christophegragnic@yahoo.fr>
8 */
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_nsrelation extends DokuWiki_Syntax_Plugin {
16
17    var $relatedns = array();
18    var $relatedns_display = array();
19    var $rootns   = '';
20
21    /**
22     * Initialize
23     */
24    function syntax_plugin_nsrelation(){
25        // load wanted ns in the relation into array
26        $this->relatedns = array_unique(array_filter(explode('|',$this->getConf('nsrelations'))));
27        $this->relatedns_display = array_unique(array_filter(explode('|',$this->getConf('nsrelationsdisplay'))));
28        // if there is not enough display strings, fill with raw namespaces
29        if (count($this->relatedns_display) < $this->relatedns)
30            for($i=count($this->relatedns_display);$i<count($this->relatedns);$i++)
31                $this->relatedns_display[] = $this->relatedns[$i];
32
33        $this->rootns = cleanID($this->getConf('nsrelationrootns'));
34        if($this->rootns) $this->rootns .= ':';
35    }
36
37    /**
38     * return some info
39     */
40    function getInfo(){
41        return confToHash(dirname(__FILE__).'/info.txt');
42    }
43
44    /**
45     * What kind of syntax are we?
46     */
47    function getType(){
48        return 'substition';
49    }
50
51    /**
52     * Where to sort in?
53     */
54    function getSort(){
55        return 155;
56    }
57
58
59    /**
60     * Connect pattern to lexer
61     */
62    function connectTo($mode) {
63        $this->Lexer->addSpecialPattern('~~NO_NSREL~~',$mode,'plugin_nsrelation');
64    }
65
66
67    /**
68     * Handle the match
69     */
70    function handle($match, $state, $pos, &$handler){
71        return array('no_nsrel');
72    }
73
74    /**
75     * Create output
76     */
77    function render($format, &$renderer, $data) {
78        // store info in metadata
79        if($format == 'metadata'){
80            $renderer->meta['plugin']['nsrelation']['no_nsrel'] = true;
81        }
82        return false;
83    }
84
85    /**
86     * Returns a link to the wanted and related namespace
87     */
88    function _buildNSRelLink($ns,$idpart){
89        global $conf;
90        global $saved_conf;
91        $link = ':'.$this->rootns.$ns.':'.$idpart;
92        $name = $this->relatedns_display[array_search($ns,$this->relatedns)];
93        return html_wikilink($link,$name);
94    }
95
96    /**
97     * Displays the available and configured namespaces. Needs to be placed in the template.
98     */
99    function _showNSRelations(){
100        global $ACT;
101        global $ID;
102        global $conf;
103
104        if($ACT != 'show') return;
105        if($this->rootns && strpos($ID,$this->rootns) !== 0) return;
106        $meta = p_get_metadata($ID);
107        if($meta['plugin']['nsrelation']['no_nsrel']) return;
108
109        $currentns = '';
110        foreach($this->relatedns as $ns){
111            if(strpos($ID,$this->rootns.$ns) === 0 ){
112                $currentns = $ns;
113                break;
114            }
115        }
116        if($currentns == '') return;
117
118        $rx = '/^'.$this->rootns.'(('.join('|',$this->relatedns).'):)?/';
119        $idpart = preg_replace($rx,'',$ID);
120
121        $out  = '<div class="plugin_nsrelation">';
122        $out .= '<span>'.$this->getLang('nsrelations');
123        if($this->getConf('about')){
124            $out .= '<sup>'.html_wikilink($this->getConf('about'),'?').'</sup>';
125        }
126        $out .= ':</span> ';
127        $out .= '<ul>';
128        foreach($this->relatedns as $ns){
129            $out .= '  <li'.($ns==$currentns?' class="current"':'').'><div class="li">'.$this->_buildNSRelLink($ns,$idpart).'</div></li>';
130        }
131        $out .= '</ul>';
132        $out .= '</div>';
133
134        return $out;
135    }
136
137}
138
139//Setup VIM: ex: et ts=4 enc=utf-8 :
140