xref: /plugin/dbquery/renderer.php (revision c64c174801842a2c4a9122988403542bc5de02db)
12e564e06SAndreas Gohr<?php
22e564e06SAndreas Gohr
32e564e06SAndreas Gohr/**
42e564e06SAndreas Gohr * DokuWiki Plugin dbquery (Renderer Component)
52e564e06SAndreas Gohr *
66adcc9adSAndreas 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{
136adcc9adSAndreas Gohr
146adcc9adSAndreas Gohr    protected $codeBlocks = [];
156adcc9adSAndreas 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 */
246adcc9adSAndreas Gohr    public function header($text, $level, $pos)
256adcc9adSAndreas Gohr    {
266adcc9adSAndreas Gohr        $this->lastHeader = $text;
276adcc9adSAndreas Gohr    }
286adcc9adSAndreas Gohr
296adcc9adSAndreas Gohr    /** @inheritDoc */
302e564e06SAndreas Gohr    public function code($text, $lang = null, $file = null)
312e564e06SAndreas Gohr    {
326adcc9adSAndreas Gohr        if (!isset($this->codeBlocks['_'])) {
336adcc9adSAndreas Gohr            // first code block is always the SQL query
346adcc9adSAndreas Gohr            $this->codeBlocks['_'] = trim($text);
356adcc9adSAndreas Gohr        } else {
366adcc9adSAndreas Gohr            // all other code blocks are treated as HTML named by their header
376adcc9adSAndreas Gohr            $this->codeBlocks[$this->lastHeader] = trim($text);
386adcc9adSAndreas Gohr        }
396adcc9adSAndreas Gohr    }
406adcc9adSAndreas Gohr
41*c64c1748SAndreas Gohr    /** @inheritdoc */
42*c64c1748SAndreas Gohr    public function document_start()
43*c64c1748SAndreas Gohr    {
44*c64c1748SAndreas Gohr        parent::document_start();
45*c64c1748SAndreas Gohr        $this->info['dbquery']['transpose'] = false;
46*c64c1748SAndreas Gohr    }
47*c64c1748SAndreas Gohr
486adcc9adSAndreas Gohr    /** @inheritDoc */
496adcc9adSAndreas Gohr    public function document_end()
506adcc9adSAndreas Gohr    {
51*c64c1748SAndreas Gohr        $this->doc = json_encode([
52*c64c1748SAndreas Gohr            'codeblocks' => $this->codeBlocks,
53*c64c1748SAndreas Gohr            'macros' => $this->info['dbquery'],
54*c64c1748SAndreas Gohr        ]);
552e564e06SAndreas Gohr    }
562e564e06SAndreas Gohr
572e564e06SAndreas Gohr}
58