xref: /dokuwiki/lib/plugins/info/syntax.php (revision e9ac59e98127e23270b4aa2272acc7f32cac8e5a)
1<?php
2
3/**
4 * Info Plugin: Displays information about various DokuWiki internals
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 * @author     Esther Brunner <wikidesign@gmail.com>
9 */
10class syntax_plugin_info extends DokuWiki_Syntax_Plugin
11{
12
13    /**
14     * What kind of syntax are we?
15     */
16    public function getType()
17    {
18        return 'substition';
19    }
20
21    /**
22     * What about paragraphs?
23     */
24    public function getPType()
25    {
26        return 'block';
27    }
28
29    /**
30     * Where to sort in?
31     */
32    public function getSort()
33    {
34        return 155;
35    }
36
37    /**
38     * Connect pattern to lexer
39     */
40    public function connectTo($mode)
41    {
42        $this->Lexer->addSpecialPattern('~~INFO:\w+~~', $mode, 'plugin_info');
43    }
44
45    /**
46     * Handle the match
47     *
48     * @param string $match The text matched by the patterns
49     * @param int $state The lexer state for the match
50     * @param int $pos The character position of the matched text
51     * @param Doku_Handler $handler The Doku_Handler object
52     * @return  array Return an array with all data you want to use in render
53     */
54    public function handle($match, $state, $pos, Doku_Handler $handler)
55    {
56        $match = substr($match, 7, -2); //strip ~~INFO: from start and ~~ from end
57        return array(strtolower($match));
58    }
59
60    /**
61     * Create output
62     *
63     * @param string $format string     output format being rendered
64     * @param Doku_Renderer $renderer the current renderer object
65     * @param array $data data created by handler()
66     * @return  boolean                 rendered correctly?
67     */
68    public function render($format, Doku_Renderer $renderer, $data)
69    {
70        if ($format == 'xhtml') {
71            /** @var Doku_Renderer_xhtml $renderer */
72            //handle various info stuff
73            switch ($data[0]) {
74                case 'syntaxmodes':
75                    $renderer->doc .= $this->renderSyntaxModes();
76                    break;
77                case 'syntaxtypes':
78                    $renderer->doc .= $this->renderSyntaxTypes();
79                    break;
80                case 'syntaxplugins':
81                    $this->renderPlugins('syntax', $renderer);
82                    break;
83                case 'adminplugins':
84                    $this->renderPlugins('admin', $renderer);
85                    break;
86                case 'actionplugins':
87                    $this->renderPlugins('action', $renderer);
88                    break;
89                case 'rendererplugins':
90                    $this->renderPlugins('renderer', $renderer);
91                    break;
92                case 'helperplugins':
93                    $this->renderPlugins('helper', $renderer);
94                    break;
95                case 'authplugins':
96                    $this->renderPlugins('auth', $renderer);
97                    break;
98                case 'remoteplugins':
99                    $this->renderPlugins('remote', $renderer);
100                    break;
101                case 'helpermethods':
102                    $this->renderHelperMethods($renderer);
103                    break;
104                case 'hooks':
105                    $this->renderHooks($renderer);
106                    break;
107                case 'datetime':
108                    $renderer->doc .= date('r');
109                    break;
110                default:
111                    $renderer->doc .= "no info about " . htmlspecialchars($data[0]);
112            }
113            return true;
114        }
115        return false;
116    }
117
118    /**
119     * list all installed plugins
120     *
121     * uses some of the original renderer methods
122     *
123     * @param string $type
124     * @param Doku_Renderer_xhtml $renderer
125     */
126    protected function renderPlugins($type, Doku_Renderer_xhtml $renderer)
127    {
128        global $lang;
129        $renderer->doc .= '<ul>';
130
131        $plugins = plugin_list($type);
132        $plginfo = array();
133
134        // remove subparts
135        foreach ($plugins as $p) {
136            if (!$po = plugin_load($type, $p)) continue;
137            list($name,/* $part */) = explode('_', $p, 2);
138            $plginfo[$name] = $po->getInfo();
139        }
140
141        // list them
142        foreach ($plginfo as $info) {
143            $renderer->doc .= '<li><div class="li">';
144            $renderer->externallink($info['url'], $info['name']);
145            $renderer->doc .= ' ';
146            $renderer->doc .= '<em>'.$info['date'].'</em>';
147            $renderer->doc .= ' ';
148            $renderer->doc .= $lang['by'];
149            $renderer->doc .= ' ';
150            $renderer->emaillink($info['email'], $info['author']);
151            $renderer->doc .= '<br />';
152            $renderer->doc .= strtr(hsc($info['desc']), array("\n"=>"<br />"));
153            $renderer->doc .= '</div></li>';
154            unset($po);
155        }
156
157        $renderer->doc .= '</ul>';
158    }
159
160    /**
161     * list all installed plugins
162     *
163     * uses some of the original renderer methods
164     *
165     * @param Doku_Renderer_xhtml $renderer
166     */
167    protected function renderHelperMethods(Doku_Renderer_xhtml $renderer)
168    {
169        $plugins = plugin_list('helper');
170        foreach ($plugins as $p) {
171            if (!$po = plugin_load('helper', $p)) continue;
172
173            if (!method_exists($po, 'getMethods')) continue;
174            $methods = $po->getMethods();
175            $info = $po->getInfo();
176
177            $hid = $this->addToToc($info['name'], 2, $renderer);
178            $doc = '<h2><a name="' . $hid . '" id="' . $hid . '">' . hsc($info['name']) . '</a></h2>';
179            $doc .= '<div class="level2">';
180            $doc .= '<p>' . strtr(hsc($info['desc']), array("\n" => "<br />")) . '</p>';
181            $doc .= '<pre class="code">$' . $p . " = plugin_load('helper', '" . $p . "');</pre>";
182            $doc .= '</div>';
183            foreach ($methods as $method) {
184                $title = '$' . $p . '->' . $method['name'] . '()';
185                $hid = $this->addToToc($title, 3, $renderer);
186                $doc .= '<h3><a name="' . $hid . '" id="' . $hid . '">' . hsc($title) . '</a></h3>';
187                $doc .= '<div class="level3">';
188                $doc .= '<div class="table"><table class="inline"><tbody>';
189                $doc .= '<tr><th>Description</th><td colspan="2">' . $method['desc'] .
190                    '</td></tr>';
191                if ($method['params']) {
192                    $c = count($method['params']);
193                    $doc .= '<tr><th rowspan="' . $c . '">Parameters</th><td>';
194                    $params = array();
195                    foreach ($method['params'] as $desc => $type) {
196                        $params[] = hsc($desc) . '</td><td>' . hsc($type);
197                    }
198                    $doc .= join('</td></tr><tr><td>', $params) . '</td></tr>';
199                }
200                if ($method['return']) {
201                    $doc .= '<tr><th>Return value</th><td>' . hsc(key($method['return'])) .
202                        '</td><td>' . hsc(current($method['return'])) . '</td></tr>';
203                }
204                $doc .= '</tbody></table></div>';
205                $doc .= '</div>';
206            }
207            unset($po);
208
209            $renderer->doc .= $doc;
210        }
211    }
212
213    /**
214     * lists all known syntax types and their registered modes
215     *
216     * @return string
217     */
218    protected function renderSyntaxTypes()
219    {
220        global $PARSER_MODES;
221        $doc = '';
222
223        $doc .= '<div class="table"><table class="inline"><tbody>';
224        foreach ($PARSER_MODES as $mode => $modes) {
225            $doc .= '<tr>';
226            $doc .= '<td class="leftalign">';
227            $doc .= $mode;
228            $doc .= '</td>';
229            $doc .= '<td class="leftalign">';
230            $doc .= join(', ', $modes);
231            $doc .= '</td>';
232            $doc .= '</tr>';
233        }
234        $doc .= '</tbody></table></div>';
235        return $doc;
236    }
237
238    /**
239     * lists all known syntax modes and their sorting value
240     *
241     * @return string
242     */
243    protected function renderSyntaxModes()
244    {
245        $modes = p_get_parsermodes();
246
247        $compactmodes = array();
248        foreach ($modes as $mode) {
249            $compactmodes[$mode['sort']][] = $mode['mode'];
250        }
251        $doc = '';
252        $doc .= '<div class="table"><table class="inline"><tbody>';
253
254        foreach ($compactmodes as $sort => $modes) {
255            $rowspan = '';
256            if (count($modes) > 1) {
257                $rowspan = ' rowspan="' . count($modes) . '"';
258            }
259
260            foreach ($modes as $index => $mode) {
261                $doc .= '<tr>';
262                $doc .= '<td class="leftalign">';
263                $doc .= $mode;
264                $doc .= '</td>';
265
266                if ($index === 0) {
267                    $doc .= '<td class="rightalign" ' . $rowspan . '>';
268                    $doc .= $sort;
269                    $doc .= '</td>';
270                }
271                $doc .= '</tr>';
272            }
273        }
274
275        $doc .= '</tbody></table></div>';
276        return $doc;
277    }
278
279    /**
280     * Render all currently registered event handlers
281     *
282     * @param Doku_Renderer $renderer
283     */
284    protected function renderHooks(Doku_Renderer $renderer)
285    {
286        global $EVENT_HANDLER;
287
288        $list = $EVENT_HANDLER->getEventHandlers();
289        ksort($list);
290
291        $renderer->listu_open();
292        foreach ($list as $event => $handlers) {
293            $renderer->listitem_open(1);
294            $renderer->listcontent_open();
295            $renderer->cdata($event);
296            $renderer->listcontent_close();
297
298            $renderer->listo_open();
299            foreach ($handlers as $sequence) {
300                foreach ($sequence as $handler) {
301                    $renderer->listitem_open(2);
302                    $renderer->listcontent_open();
303                    $renderer->cdata(get_class($handler[0]) . '::' . $handler[1] . '()');
304                    $renderer->listcontent_close();
305                    $renderer->listitem_close();
306                }
307            }
308            $renderer->listo_close();
309            $renderer->listitem_close();
310        }
311        $renderer->listu_close();
312    }
313
314    /**
315     * Adds a TOC item
316     *
317     * @param string $text
318     * @param int $level
319     * @param Doku_Renderer_xhtml $renderer
320     * @return string
321     */
322    protected function addToToc($text, $level, Doku_Renderer_xhtml $renderer)
323    {
324        global $conf;
325
326        $hid = '';
327        if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])) {
328            $hid = $renderer->_headerToLink($text, true);
329            $renderer->toc[] = array(
330                'hid' => $hid,
331                'title' => $text,
332                'type' => 'ul',
333                'level' => $level - $conf['toptoclevel'] + 1,
334            );
335        }
336        return $hid;
337    }
338}
339