xref: /plugin/dbquery/renderer.php (revision 6adcc9ad882f6bbeb8ab7f02858dfa793487e1a2)
12e564e06SAndreas Gohr<?php
22e564e06SAndreas Gohr
32e564e06SAndreas Gohr/**
42e564e06SAndreas Gohr * DokuWiki Plugin dbquery (Renderer Component)
52e564e06SAndreas Gohr *
6*6adcc9adSAndreas Gohr * Extracts code blocks from pages and returns them as JSON
72e564e06SAndreas Gohr *
82e564e06SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
92e564e06SAndreas Gohr * @author  Andreas Gohr <dokuwiki@cosmocode.de>
102e564e06SAndreas Gohr */
112e564e06SAndreas Gohrclass renderer_plugin_dbquery extends \Doku_Renderer
122e564e06SAndreas Gohr{
13*6adcc9adSAndreas Gohr
14*6adcc9adSAndreas Gohr    protected $codeBlocks = [];
15*6adcc9adSAndreas Gohr    protected $lastHeader = '';
162e564e06SAndreas Gohr
172e564e06SAndreas Gohr    /** @inheritDoc */
182e564e06SAndreas Gohr    public function getFormat()
192e564e06SAndreas Gohr    {
202e564e06SAndreas Gohr        return 'dbquery';
212e564e06SAndreas Gohr    }
222e564e06SAndreas Gohr
232e564e06SAndreas Gohr    /** @inheritDoc */
24*6adcc9adSAndreas Gohr    public function header($text, $level, $pos)
25*6adcc9adSAndreas Gohr    {
26*6adcc9adSAndreas Gohr        $this->lastHeader = $text;
27*6adcc9adSAndreas Gohr    }
28*6adcc9adSAndreas Gohr
29*6adcc9adSAndreas Gohr    /** @inheritDoc */
302e564e06SAndreas Gohr    public function code($text, $lang = null, $file = null)
312e564e06SAndreas Gohr    {
32*6adcc9adSAndreas Gohr        if (!isset($this->codeBlocks['_'])) {
33*6adcc9adSAndreas Gohr            // first code block is always the SQL query
34*6adcc9adSAndreas Gohr            $this->codeBlocks['_'] = trim($text);
35*6adcc9adSAndreas Gohr        } else {
36*6adcc9adSAndreas Gohr            // all other code blocks are treated as HTML named by their header
37*6adcc9adSAndreas Gohr            $this->codeBlocks[$this->lastHeader] = trim($text);
38*6adcc9adSAndreas Gohr        }
39*6adcc9adSAndreas Gohr    }
40*6adcc9adSAndreas Gohr
41*6adcc9adSAndreas Gohr    /** @inheritDoc */
42*6adcc9adSAndreas Gohr    public function document_end()
43*6adcc9adSAndreas Gohr    {
44*6adcc9adSAndreas Gohr        $this->doc = json_encode($this->codeBlocks);
452e564e06SAndreas Gohr    }
462e564e06SAndreas Gohr
472e564e06SAndreas Gohr}
48