xref: /plugin/dbquery/renderer.php (revision 491a7dffb517303e7339f19414486f18ef755a48)
12e564e06SAndreas Gohr<?php
22e564e06SAndreas Gohr
3*491a7dffSAndreas Gohr// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
4*491a7dffSAndreas Gohr
52e564e06SAndreas Gohr/**
62e564e06SAndreas Gohr * DokuWiki Plugin dbquery (Renderer Component)
72e564e06SAndreas Gohr *
86adcc9adSAndreas Gohr * Extracts code blocks from pages and returns them as JSON
92e564e06SAndreas Gohr *
102e564e06SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
112e564e06SAndreas Gohr * @author  Andreas Gohr <dokuwiki@cosmocode.de>
122e564e06SAndreas Gohr */
132e564e06SAndreas Gohrclass renderer_plugin_dbquery extends \Doku_Renderer
142e564e06SAndreas Gohr{
156adcc9adSAndreas Gohr    protected $codeBlocks = [];
166adcc9adSAndreas Gohr    protected $lastHeader = '';
172e564e06SAndreas Gohr
182e564e06SAndreas Gohr    /** @inheritDoc */
192e564e06SAndreas Gohr    public function getFormat()
202e564e06SAndreas Gohr    {
212e564e06SAndreas Gohr        return 'dbquery';
222e564e06SAndreas Gohr    }
232e564e06SAndreas Gohr
242e564e06SAndreas Gohr    /** @inheritDoc */
256adcc9adSAndreas Gohr    public function header($text, $level, $pos)
266adcc9adSAndreas Gohr    {
276adcc9adSAndreas Gohr        $this->lastHeader = $text;
286adcc9adSAndreas Gohr    }
296adcc9adSAndreas Gohr
306adcc9adSAndreas Gohr    /** @inheritDoc */
312e564e06SAndreas Gohr    public function code($text, $lang = null, $file = null)
322e564e06SAndreas Gohr    {
336adcc9adSAndreas Gohr        if (!isset($this->codeBlocks['_'])) {
346adcc9adSAndreas Gohr            // first code block is always the SQL query
356adcc9adSAndreas Gohr            $this->codeBlocks['_'] = trim($text);
366adcc9adSAndreas Gohr        } else {
376adcc9adSAndreas Gohr            // all other code blocks are treated as HTML named by their header
386adcc9adSAndreas Gohr            $this->codeBlocks[$this->lastHeader] = trim($text);
396adcc9adSAndreas Gohr        }
406adcc9adSAndreas Gohr    }
416adcc9adSAndreas Gohr
42c64c1748SAndreas Gohr    /** @inheritdoc */
43c64c1748SAndreas Gohr    public function document_start()
44c64c1748SAndreas Gohr    {
45c64c1748SAndreas Gohr        parent::document_start();
46c64c1748SAndreas Gohr        $this->info['dbquery']['transpose'] = false;
47c64c1748SAndreas Gohr    }
48c64c1748SAndreas Gohr
496adcc9adSAndreas Gohr    /** @inheritDoc */
506adcc9adSAndreas Gohr    public function document_end()
516adcc9adSAndreas Gohr    {
52c64c1748SAndreas Gohr        $this->doc = json_encode([
53c64c1748SAndreas Gohr            'codeblocks' => $this->codeBlocks,
54c64c1748SAndreas Gohr            'macros' => $this->info['dbquery'],
55c64c1748SAndreas Gohr        ]);
562e564e06SAndreas Gohr    }
572e564e06SAndreas Gohr}
58