1<?php
2
3// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
4
5/**
6 * DokuWiki Plugin dbquery (Renderer Component)
7 *
8 * Extracts code blocks from pages and returns them as JSON
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
12 */
13class renderer_plugin_dbquery extends \Doku_Renderer
14{
15    protected $codeBlocks = [];
16    protected $lastHeader = '';
17
18    /** @inheritDoc */
19    public function getFormat()
20    {
21        return 'dbquery';
22    }
23
24    /** @inheritDoc */
25    public function header($text, $level, $pos)
26    {
27        $this->lastHeader = $text;
28    }
29
30    /** @inheritDoc */
31    public function code($text, $lang = null, $file = null)
32    {
33        if (!isset($this->codeBlocks['_'])) {
34            // first code block is always the SQL query
35            $this->codeBlocks['_'] = trim($text);
36        } else {
37            // all other code blocks are treated as HTML named by their header
38            $this->codeBlocks[$this->lastHeader] = trim($text);
39        }
40    }
41
42    /** @inheritdoc */
43    public function document_start()
44    {
45        parent::document_start();
46        $this->info['dbquery']['transpose'] = false;
47    }
48
49    /** @inheritDoc */
50    public function document_end()
51    {
52        $this->doc = json_encode([
53            'codeblocks' => $this->codeBlocks,
54            'macros' => $this->info['dbquery'],
55        ]);
56    }
57}
58