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