From 3944eabdcde209a363b968e14f2591102ecc42f5 Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Mon, 10 Dec 2018 14:20:10 +0100 Subject: [PATCH] Reworked to support multiple hosts and databases --- plugin.info.txt | 2 +- syntax.php | 185 +++++++++++++++++++++++------------------------- 2 files changed, 90 insertions(+), 97 deletions(-) diff --git a/plugin.info.txt b/plugin.info.txt index 4fee125..f89c1e5 100644 --- a/plugin.info.txt +++ b/plugin.info.txt @@ -1,7 +1,7 @@ base sqlquery author George Pirogov email i1557@yandex.ru -date 2016-11-25 +date 2018-12-10 name SQL query plugin desc Processing query to mysql database and display results as a table. url https://www.dokuwiki.org/plugin:sqlquery diff --git a/syntax.php b/syntax.php index f76e7ef..9ddbb5a 100644 --- a/syntax.php +++ b/syntax.php @@ -4,29 +4,26 @@ * * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html * @author George Pirogov + * @author Thomas Hooge + * */ // must be run within Dokuwiki if (!defined('DOKU_INC')) die(); +/** + * All DokuWiki plugins to extend the parser/rendering mechanism + * need to inherit from this class + */ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin { - public function getType() { - return 'substition'; - } - - public function getSort() { - return 666; - } + public function getType() { return 'substition'; } + public function getSort() { return 666; } + public function getPType() { return 'block'; } public function connectTo($mode) { - $this->Lexer->addEntryPattern('', $mode, 'plugin_sqlquery'); - } - - public function postConnect() - { - $this->Lexer->addExitPattern('','plugin_sqlquery'); + $this->Lexer->addSpecialPattern('(?:.*?)', $mode, 'plugin_sqlquery'); } /** @@ -36,29 +33,30 @@ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin { * @param int $state The state of the handler * @param int $pos The position in the document * @param Doku_Handler $handler The handler + * * @return array Data for the renderer */ - public function handle($match, $state, $pos, Doku_Handler $handler) - { - switch ( $state ) - { - case DOKU_LEXER_ENTER: - $data = array(); - return $data; - break; - - case DOKU_LEXER_UNMATCHED: - return array('sqlquery' => $match); - break; - - case DOKU_LEXER_EXIT: - $data = array(); - return $data; - break; - + public function handle($match, $state, $pos, Doku_Handler $handler) { + $data = array('state' => $state); + if ($state == DOKU_LEXER_SPECIAL) { + # get host + if (preg_match('/getConf('host'); + } + # get database + if (preg_match('/getConf('db'); + } + # get query + $data['match'] = $match; + if (preg_match('%(.*)%s', $match, $result)) { + $data['query'] = trim($result[1]); + } } - - $data = array(); return $data; } @@ -68,72 +66,67 @@ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin { * @param string $mode Renderer mode (supported modes: xhtml) * @param Doku_Renderer $renderer The renderer * @param array $data The data from the handler() function + * * @return bool If rendering was successful. */ public function render($mode, Doku_Renderer $renderer, $data) { - if ( $mode != 'xhtml' ) return false; - - if ( !empty( $data['sqlquery'] ) ) - { - // получаем параметры конфигурации - $host = $this->getConf('Host'); - $DB = $this->getConf('DB'); - $user = $this->getConf('user'); - $password = $this->getConf('password'); - - // получаем запрос - $querystring = $data['sqlquery']; - - // подключаемся к базе - $link = mysqli_connect($host, $user, $password, $DB); - mysqli_set_charset($link, "utf8"); - - // подключились - if ( $link ) - { - $result = mysqli_query($link, $querystring); - if ( $result ) - { - // получаем кол-во полей в таблице - $fieldcount = mysqli_num_fields($result); - - // строим таблицу - $renderer->doc .= ""; - - // строим заголовок - $renderer->doc .= ""; - while ($fieldinfo = mysqli_fetch_field($result)) - { - $renderer->doc .= ""; - } - $renderer->doc .= ""; - - // строим содержимое таблицы - $renderer->doc .= ""; - while ($row = mysqli_fetch_row($result)) - { - $renderer->doc .= ""; - - // строим строку - for ( $i = 0; $i < $fieldcount; $i++ ) - { - $renderer->doc .= ""; - } - $renderer->doc .= ""; - } // of while fetch_row - // закрываем таблицу - $renderer->doc .= "
"; - $renderer->doc .= $fieldinfo->name; - $renderer->doc .= "
"; - $renderer->doc .= $row[$i]; - $renderer->doc .= "
"; - } // of mysqli_query - mysqli_close($link); - } // of mysqli link - } // of sqlquery not empty + if ($mode != 'xhtml') return false; + if (empty($data['query'])) return true; + + // get configuration + $user = $this->getConf('user'); + $password = $this->getConf('password'); + + // connect to database + $link = mysqli_connect($data['host'], $user, $password, $data['db']); + if (!$link) { + $renderer->doc .= "
" . mysqli_connect_error() . "
"; + return true; + } + mysqli_set_charset($link, "utf8"); + + // run query + $result = mysqli_query($link, $data['query']); + if ($result) { + + // get the number of fields in the table + $fieldcount = mysqli_num_fields($result); + + // build a table + $renderer->doc .= '' . "\n"; + + // build the header section of the table + $renderer->doc .= ""; + while ($fieldinfo = mysqli_fetch_field($result)) { + $renderer->doc .= ""; + } + $renderer->doc .= "\n"; + + // build the contents of the table + $renderer->doc .= "\n"; + while ($row = mysqli_fetch_row($result)) { + $renderer->doc .= ""; + for ( $i = 0; $i < $fieldcount; $i++ ) { + $renderer->doc .= ""; + } + $renderer->doc .= "\n"; + } + + // finish the table + $renderer->doc .= "
"; + $renderer->doc .= $fieldinfo->name; + $renderer->doc .= "
"; + $renderer->doc .= $row[$i]; + $renderer->doc .= "
\n"; + + } else { + // error in query + $renderer->doc .= "
" . mysqli_error($link) . "
"; + } + + mysqli_close($link); + return true; - } // of render function + } } - -// vim:ts=4:sw=4:et: