xref: /dokuwiki/lib/plugins/info/syntax.php (revision c38b7fab9eeb9456aaf5fe8e4481c3b8569e5644)
1<?php
2/**
3 * Info Plugin: Displays information about various DokuWiki internals
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 * @author     Esther Brunner <wikidesign@gmail.com>
8 */
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_info extends DokuWiki_Syntax_Plugin {
17
18    /**
19     * What kind of syntax are we?
20     */
21    function getType(){
22        return 'substition';
23    }
24
25    /**
26     * What about paragraphs?
27     */
28    function getPType(){
29        return 'block';
30    }
31
32    /**
33     * Where to sort in?
34     */
35    function getSort(){
36        return 155;
37    }
38
39
40    /**
41     * Connect pattern to lexer
42     */
43    function connectTo($mode) {
44        $this->Lexer->addSpecialPattern('~~INFO:\w+~~',$mode,'plugin_info');
45    }
46
47
48    /**
49     * Handle the match
50     */
51    function handle($match, $state, $pos, &$handler){
52        $match = substr($match,7,-2); //strip ~~INFO: from start and ~~ from end
53        return array(strtolower($match));
54    }
55
56    /**
57     * Create output
58     */
59    function render($format, &$renderer, $data) {
60        if($format == 'xhtml'){
61            //handle various info stuff
62            switch ($data[0]){
63                case 'version':
64                    $renderer->doc .= getVersion();
65                    break;
66                case 'syntaxmodes':
67                    $renderer->doc .= $this->_syntaxmodes_xhtml();
68                    break;
69                case 'syntaxtypes':
70                    $renderer->doc .= $this->_syntaxtypes_xhtml();
71                    break;
72                case 'syntaxplugins':
73                    $this->_plugins_xhtml('syntax', $renderer);
74                    break;
75                case 'adminplugins':
76                    $this->_plugins_xhtml('admin', $renderer);
77                    break;
78                case 'actionplugins':
79                    $this->_plugins_xhtml('action', $renderer);
80                    break;
81                case 'rendererplugins':
82                    $this->_plugins_xhtml('renderer', $renderer);
83                    break;
84                case 'helperplugins':
85                    $this->_plugins_xhtml('helper', $renderer);
86                    break;
87                case 'helpermethods':
88                    $this->_helpermethods_xhtml($renderer);
89                    break;
90                default:
91                    $renderer->doc .= "no info about ".htmlspecialchars($data[0]);
92            }
93            return true;
94        }
95        return false;
96    }
97
98    /**
99     * list all installed plugins
100     *
101     * uses some of the original renderer methods
102     */
103    function _plugins_xhtml($type, Doku_Renderer &$renderer){
104        global $lang;
105        $renderer->doc .= '<ul>';
106
107        $plugins = plugin_list($type);
108        $plginfo = array();
109
110        // remove subparts
111        foreach($plugins as $p){
112            if (!$po =& plugin_load($type,$p)) continue;
113            list($name,$part) = explode('_',$p,2);
114            $plginfo[$name] = $po->getInfo();
115        }
116
117        // list them
118        foreach($plginfo as $info){
119            $renderer->doc .= '<li><div class="li">';
120            $renderer->externallink($info['url'],$info['name']);
121            $renderer->doc .= ' ';
122            $renderer->doc .= '<em>'.$info['date'].'</em>';
123            $renderer->doc .= ' ';
124            $renderer->doc .= $lang['by'];
125            $renderer->doc .= ' ';
126            $renderer->emaillink($info['email'],$info['author']);
127            $renderer->doc .= '<br />';
128            $renderer->doc .= strtr(hsc($info['desc']),array("\n"=>"<br />"));
129            $renderer->doc .= '</div></li>';
130            unset($po);
131        }
132
133        $renderer->doc .= '</ul>';
134    }
135
136    /**
137     * list all installed plugins
138     *
139     * uses some of the original renderer methods
140     */
141    function _helpermethods_xhtml(Doku_Renderer &$renderer){
142        global $lang;
143
144        $plugins = plugin_list('helper');
145        foreach($plugins as $p){
146            if (!$po =& plugin_load('helper',$p)) continue;
147
148            if (!method_exists($po, 'getMethods')) continue;
149            $methods = $po->getMethods();
150            $info = $po->getInfo();
151
152            $hid = $this->_addToTOC($info['name'], 2, $renderer);
153            $doc = '<h2><a name="'.$hid.'" id="'.$hid.'">'.hsc($info['name']).'</a></h2>';
154            $doc .= '<div class="level2">';
155            $doc .= '<p>'.strtr(hsc($info['desc']), array("\n"=>"<br />")).'</p>';
156            $doc .= '<pre class="code">$'.$p." =& plugin_load('helper', '".$p."');</pre>";
157            $doc .= '</div>';
158            foreach ($methods as $method){
159                $title = '$'.$p.'->'.$method['name'].'()';
160                $hid = $this->_addToTOC($title, 3, $renderer);
161                $doc .= '<h3><a name="'.$hid.'" id="'.$hid.'">'.hsc($title).'</a></h3>';
162                $doc .= '<div class="level3">';
163                $doc .= '<div class="table"><table class="inline"><tbody>';
164                $doc .= '<tr><th>Description</th><td colspan="2">'.$method['desc'].
165                    '</td></tr>';
166                if ($method['params']){
167                    $c = count($method['params']);
168                    $doc .= '<tr><th rowspan="'.$c.'">Parameters</th><td>';
169                    $params = array();
170                    foreach ($method['params'] as $desc => $type){
171                        $params[] = hsc($desc).'</td><td>'.hsc($type);
172                    }
173                    $doc .= join($params, '</td></tr><tr><td>').'</td></tr>';
174                }
175                if ($method['return']){
176                    $doc .= '<tr><th>Return value</th><td>'.hsc(key($method['return'])).
177                        '</td><td>'.hsc(current($method['return'])).'</td></tr>';
178                }
179                $doc .= '</tbody></table></div>';
180                $doc .= '</div>';
181            }
182            unset($po);
183
184            $renderer->doc .= $doc;
185        }
186    }
187
188    /**
189     * lists all known syntax types and their registered modes
190     */
191    function _syntaxtypes_xhtml(){
192        global $PARSER_MODES;
193        $doc  = '';
194
195        $doc .= '<div class="table"><table class="inline"><tbody>';
196        foreach($PARSER_MODES as $mode => $modes){
197            $doc .= '<tr>';
198            $doc .= '<td class="leftalign">';
199            $doc .= $mode;
200            $doc .= '</td>';
201            $doc .= '<td class="leftalign">';
202            $doc .= join(', ',$modes);
203            $doc .= '</td>';
204            $doc .= '</tr>';
205        }
206        $doc .= '</tbody></table></div>';
207        return $doc;
208    }
209
210    /**
211     * lists all known syntax modes and their sorting value
212     */
213    function _syntaxmodes_xhtml(){
214        $modes = p_get_parsermodes();
215        $doc  = '';
216
217        foreach ($modes as $mode){
218            $doc .= $mode['mode'].' ('.$mode['sort'].'), ';
219        }
220        return $doc;
221    }
222
223    /**
224     * Adds a TOC item
225     */
226    function _addToTOC($text, $level, Doku_Renderer_xhtml &$renderer){
227        global $conf;
228
229        if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){
230            $hid  = $renderer->_headerToLink($text, 'true');
231            $renderer->toc[] = array(
232                'hid'   => $hid,
233                'title' => $text,
234                'type'  => 'ul',
235                'level' => $level - $conf['toptoclevel'] + 1
236            );
237        }
238        return $hid;
239    }
240}
241
242//Setup VIM: ex: et ts=4 :
243