1<?php
2/**
3 * DokuWiki Plugin sqlquery (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  George Pirogov <i1557@yandex.ru>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin {
13
14    public function getType() {
15        return 'substition';
16    }
17
18    public function getSort() {
19        return 666;
20    }
21
22    public function connectTo($mode)
23    {
24        $this->Lexer->addEntryPattern('<sql>', $mode, 'plugin_sqlquery');
25    }
26
27    public function postConnect()
28    {
29        $this->Lexer->addExitPattern('</sql>','plugin_sqlquery');
30    }
31
32    /**
33     * Handle matches of the sqlquery syntax
34     *
35     * @param string          $match   The match of the syntax
36     * @param int             $state   The state of the handler
37     * @param int             $pos     The position in the document
38     * @param Doku_Handler    $handler The handler
39     * @return array Data for the renderer
40     */
41    public function handle($match, $state, $pos, Doku_Handler $handler)
42    {
43        switch ( $state )
44        {
45              case DOKU_LEXER_ENTER:
46              $data = array();
47              return $data;
48              break;
49
50              case DOKU_LEXER_UNMATCHED:
51        			return array('sqlquery' => $match);
52              break;
53
54              case DOKU_LEXER_EXIT:
55              $data = array();
56              return $data;
57              break;
58
59        }
60
61        $data = array();
62        return $data;
63    }
64
65    /**
66     * Render xhtml output or metadata
67     *
68     * @param string         $mode      Renderer mode (supported modes: xhtml)
69     * @param Doku_Renderer  $renderer  The renderer
70     * @param array          $data      The data from the handler() function
71     * @return bool If rendering was successful.
72     */
73    public function render($mode, Doku_Renderer $renderer, $data)
74    {
75        if ( $mode != 'xhtml' ) return false;
76
77        if ( !empty( $data['sqlquery'] ) )
78        {
79            // получаем параметры конфигурации
80            $host     = $this->getConf('Host');
81            $DB       = $this->getConf('DB');
82            $user     = $this->getConf('user');
83            $password = $this->getConf('password');
84
85            // получаем запрос
86            $querystring = $data['sqlquery'];
87
88            // подключаемся к базе
89            $link = mysqli_connect($host, $user, $password, $DB);
90            mysqli_set_charset($link, "utf8");
91
92            // подключились
93            if ( $link )
94            {
95                $result = mysqli_query($link, $querystring);
96                if ( $result )
97                {
98                    // получаем кол-во полей в таблице
99                    $fieldcount = mysqli_num_fields($result);
100
101                    // строим таблицу
102                    $renderer->doc .= "<table id=\"sqlquerytable\" class=\"inline\">";
103
104                    // строим заголовок
105                    $renderer->doc .= "<thead><tr>";
106                    while ($fieldinfo = mysqli_fetch_field($result))
107                    {
108                        $renderer->doc .= "<th>";
109                        $renderer->doc .= $fieldinfo->name;
110                        $renderer->doc .= "</th>";
111                    }
112                    $renderer->doc .= "</tr></thead>";
113
114                    // строим содержимое таблицы
115                    $renderer->doc .= "<tbody>";
116                    while ($row = mysqli_fetch_row($result))
117                    {
118                          $renderer->doc .= "<tr>";
119
120                          // строим строку
121                          for ( $i = 0; $i < $fieldcount; $i++ )
122                          {
123                              $renderer->doc .= "<td>";
124                              $renderer->doc .= $row[$i];
125                              $renderer->doc .= "</td>";
126                          }
127                          $renderer->doc .= "</tr>";
128                    } // of while fetch_row
129                    // закрываем таблицу
130                    $renderer->doc .= "</tbody></table>";
131                } // of mysqli_query
132                mysqli_close($link);
133            } // of mysqli link
134        } // of sqlquery not empty
135        return true;
136    } // of render function
137}
138
139// vim:ts=4:sw=4:et:
140