1<?php
2/**
3 * DokuWiki Plugin sqljson (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  George Pirogov <i1557@yandex.ru>
7 * @author  Marie Mandrela <marie.h.mandrela@gmail.com>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class syntax_plugin_sqljson extends DokuWiki_Syntax_Plugin {
14
15    public function getType() {
16        return 'substition';
17    }
18
19    public function getSort() {
20        return 666;
21    }
22
23    public function connectTo($mode)
24    {
25        $this->Lexer->addEntryPattern('<sqljson(?=.*?>)', $mode, 'plugin_sqljson');
26    }
27
28    public function postConnect()
29    {
30        $this->Lexer->addExitPattern('</sqljson>','plugin_sqljson');
31    }
32
33    /**
34     * Handle matches of the sqljson syntax
35     *
36     * @param string          $match   The match of the syntax
37     * @param int             $state   The state of the handler
38     * @param int             $pos     The position in the document
39     * @param Doku_Handler    $handler The handler
40     * @return array Data for the renderer
41     */
42    public function handle($match, $state, $pos, Doku_Handler $handler)
43    {
44        switch ($state) {
45            case DOKU_LEXER_ENTER:
46            $data = array();
47            return $data;
48            break;
49
50            case DOKU_LEXER_UNMATCHED:
51            // will include everything from <sqljson ... to ... </sqljson >
52            // e.g. ... [name] > [sqljson]
53            list($attr, $content) = preg_split('/>/u',$match,2);
54            return array('sqljson' => $content, 'variable' => trim($attr));
55            break;
56
57            case DOKU_LEXER_EXIT:
58            $data = array();
59            return $data;
60            break;
61		}
62
63        $data = array();
64        return $data;
65    }
66
67    /**
68     * Render xhtml output or metadata
69     *
70     * @param string         $mode      Renderer mode (supported modes: xhtml)
71     * @param Doku_Renderer  $renderer  The renderer
72     * @param array          $data      The data from the handler() function
73     * @return bool If rendering was successful.
74     */
75    public function render($mode, Doku_Renderer $renderer, $data)
76    {
77        if ( $mode != 'xhtml' ) return false;
78
79        if ( !empty( $data['sqljson'] ) )
80        {
81            // get the configuration parameters
82            $host     = $this->getConf('Host');
83            $DB       = $this->getConf('DB');
84            $user     = $this->getConf('user');
85            $password = $this->getConf('password');
86
87            // get a query
88            $querystring = $data['sqljson'];
89
90            // connect to the database
91            $link = mysqli_connect($host, $user, $password, $DB);
92            mysqli_set_charset($link, "utf8");
93
94            // connected
95            if ( $link )
96            {
97                $result = mysqli_query($link, $querystring);
98                if ( $result )
99                {
100                    // get the number of fields in the table
101                    $fieldcount = mysqli_num_fields($result);
102                    // get the number of rows in the table
103                    $rowcount = mysqli_num_rows($result);
104
105                    // open script tag
106                    $renderer->doc .= "<script>";
107
108                    // open json
109                    if (strlen($data['variable']) > 0)
110                    {
111                        $renderer->doc .= "var ";
112                        $renderer->doc .= $data['variable'];
113                        $renderer->doc .= " = [";
114                    }
115                    else
116                    {
117                        $renderer->doc .= "var data = [";
118                    }
119
120                    $header = array();
121                    while ($fieldinfo = mysqli_fetch_field($result))
122                    {
123                        array_push($header, $fieldinfo->name);
124                    }
125
126                    // build the json entries
127                    $j = 0;
128                    while ($row = mysqli_fetch_row($result))
129                    {
130                          $renderer->doc .= "{ ";
131
132                          // construct a row
133                          for ( $i = 0; $i < $fieldcount; $i++ )
134                          {
135                              if ( $i > 0 )
136                              {
137                                  $renderer->doc .= " , ";
138                              }
139
140                              $renderer->doc .= "\"";
141                              $renderer->doc .= $header[$i];
142                              $renderer->doc .= "\":\"";
143                              $renderer->doc .= $row[$i];
144                              $renderer->doc .= "\"";
145                          }
146
147                          $renderer->doc .= " }";
148                          if ( $j < $rowcount - 1 )
149                          {
150                              $renderer->doc .= ",";
151                          }
152                          $j++;
153                    }
154                    // close json
155                    $renderer->doc .= "];";
156
157                    // close script tag
158                    $renderer->doc .= "</script>";
159                }
160                mysqli_close($link);
161            }
162        }
163        return true;
164    }
165}
166
167// vim:ts=4:sw=4:et:
168