xref: /dokuwiki/lib/plugins/info/syntax.php (revision a8422a927454df870e8dd61ab6fcd6e2d1391aa6)
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'   => '2006-12-09',
29            'name'   => 'Info Plugin',
30            'desc'   => 'Displays information about various DokuWiki internals',
31            'url'    => 'http://wiki.splitbrain.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        foreach($plugins as $p){
126            if (!$po =& plugin_load($type,$p)) continue;
127            $info = $po->getInfo();
128
129            $renderer->doc .= '<li><div class="li">';
130            $renderer->externallink($info['url'],$info['name']);
131            $renderer->doc .= ' ';
132            $renderer->doc .= '<em>'.$info['date'].'</em>';
133            $renderer->doc .= ' ';
134            $renderer->doc .= $lang['by'];
135            $renderer->doc .= ' ';
136            $renderer->emaillink($info['email'],$info['author']);
137            $renderer->doc .= '<br />';
138            $renderer->doc .= strtr(hsc($info['desc']),array("\n"=>"<br />"));
139            $renderer->doc .= '</div></li>';
140            unset($po);
141        }
142
143        $renderer->doc .= '</ul>';
144    }
145
146    /**
147     * list all installed plugins
148     *
149     * uses some of the original renderer methods
150     */
151    function _helpermethods_xhtml(&$renderer){
152        global $lang;
153
154        $plugins = plugin_list('helper');
155        foreach($plugins as $p){
156            if (!$po =& plugin_load('helper',$p)) continue;
157
158            if (!method_exists($po, 'getMethods')) continue;
159            $methods = $po->getMethods();
160            $info = $po->getInfo();
161
162            $hid = $this->_addToTOC($info['name'], 2, $renderer);
163            $doc = '<h2><a name="'.$hid.'" id="'.$hid.'">'.hsc($info['name']).'</a></h2>';
164            $doc .= '<div class="level2">';
165            $doc .= '<p>'.strtr(hsc($info['desc']), array("\n"=>"<br />")).'</p>';
166            $doc .= '<pre class="code">$'.$p." =& plugin_load('helper', '".$p."');</pre>";
167            $doc .= '</div>';
168            foreach ($methods as $method){
169                $title = '$'.$p.'->'.$method['name'].'()';
170                $hid = $this->_addToTOC($title, 3, $renderer);
171                $doc .= '<h3><a name="'.$hid.'" id="'.$hid.'">'.hsc($title).'</a></h3>';
172                $doc .= '<div class="level3">';
173                $doc .= '<table class="inline"><tbody>';
174                $doc .= '<tr><th>Description</th><td colspan="2">'.$method['desc'].
175                    '</td></tr>';
176                if ($method['params']){
177                    $c = count($method['params']);
178                    $doc .= '<tr><th rowspan="'.$c.'">Parameters</th><td>';
179                    $params = array();
180                    foreach ($method['params'] as $desc => $type){
181                        $params[] = hsc($desc).'</td><td>'.hsc($type);
182                    }
183                    $doc .= join($params, '</td></tr><tr><td>').'</td></tr>';
184                }
185                if ($method['return']){
186                    $doc .= '<tr><th>Return value</th><td>'.hsc(key($method['return'])).
187                        '</td><td>'.hsc(current($method['return'])).'</td></tr>';
188                }
189                $doc .= '</tbody></table>';
190                $doc .= '</div>';
191            }
192            unset($po);
193
194            $renderer->doc .= $doc;
195        }
196    }
197
198    /**
199     * lists all known syntax types and their registered modes
200     */
201    function _syntaxtypes_xhtml(){
202        global $PARSER_MODES;
203        $doc  = '';
204
205        $doc .= '<table class="inline"><tbody>';
206        foreach($PARSER_MODES as $mode => $modes){
207            $doc .= '<tr>';
208            $doc .= '<td class="leftalign">';
209            $doc .= $mode;
210            $doc .= '</td>';
211            $doc .= '<td class="leftalign">';
212            $doc .= join(', ',$modes);
213            $doc .= '</td>';
214            $doc .= '</tr>';
215        }
216        $doc .= '</tbody></table>';
217        return $doc;
218    }
219
220    /**
221     * lists all known syntax modes and their sorting value
222     */
223    function _syntaxmodes_xhtml(){
224        $modes = p_get_parsermodes();
225        $doc  = '';
226
227        foreach ($modes as $mode){
228            $doc .= $mode['mode'].' ('.$mode['sort'].'), ';
229        }
230        return $doc;
231    }
232
233    /**
234     * Adds a TOC item
235     */
236    function _addToTOC($text, $level, &$renderer){
237        global $conf;
238
239        if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){
240            $hid  = $renderer->_headerToLink($text, 'true');
241            $renderer->toc[] = array(
242                'hid'   => $hid,
243                'title' => $text,
244                'type'  => 'ul',
245                'level' => $level - $conf['toptoclevel'] + 1
246            );
247        }
248        return $hid;
249    }
250}
251
252//Setup VIM: ex: et ts=4 enc=utf-8 :
253