diff --git a/README b/README new file mode 100644 index 0000000..8b27b3d --- /dev/null +++ b/README @@ -0,0 +1,27 @@ +sqlquery Plugin for DokuWiki + +Processing query to mysql database and display results as a table. + +All documentation for this plugin can be found at +https://www.dokuwiki.org/plugin:sqlquery + +If you install this plugin manually, make sure it is installed in +lib/plugins/sqlquery/ - if the folder is called different it +will not work! + +Please refer to http://www.dokuwiki.org/plugins for additional info +on how to install plugins in DokuWiki. + +---- +Copyright (C) George Pirogov + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +See the COPYING file in your DokuWiki folder for details diff --git a/conf/default.php b/conf/default.php new file mode 100644 index 0000000..5ba9faa --- /dev/null +++ b/conf/default.php @@ -0,0 +1,13 @@ + + */ + +//$conf['fixme'] = 'FIXME'; + +$conf['Host'] = 'localhost'; +$conf['DB'] = ''; +$conf['user'] = ''; +$conf['password'] = ''; diff --git a/conf/metadata.php b/conf/metadata.php new file mode 100644 index 0000000..2f183c1 --- /dev/null +++ b/conf/metadata.php @@ -0,0 +1,12 @@ + + */ + + +$meta['Host'] = array('string'); +$meta['DB'] = array('string'); +$meta['user'] = array('string'); +$meta['password'] = array('string'); diff --git a/lang/en/lang.php b/lang/en/lang.php new file mode 100644 index 0000000..018f557 --- /dev/null +++ b/lang/en/lang.php @@ -0,0 +1,16 @@ + + */ + +// menu entry for admin plugins +// $lang['menu'] = 'Your menu entry'; + +// custom language strings for the plugin +// $lang['fixme'] = 'FIXME'; + + + +//Setup VIM: ex: et ts=4 : diff --git a/lang/en/settings.php b/lang/en/settings.php new file mode 100644 index 0000000..d42b00c --- /dev/null +++ b/lang/en/settings.php @@ -0,0 +1,13 @@ + + */ + +// keys need to match the config setting name +// $lang['fixme'] = 'FIXME'; + + + +//Setup VIM: ex: et ts=4 : diff --git a/lang/ru/lang.php b/lang/ru/lang.php new file mode 100644 index 0000000..018f557 --- /dev/null +++ b/lang/ru/lang.php @@ -0,0 +1,16 @@ + + */ + +// menu entry for admin plugins +// $lang['menu'] = 'Your menu entry'; + +// custom language strings for the plugin +// $lang['fixme'] = 'FIXME'; + + + +//Setup VIM: ex: et ts=4 : diff --git a/lang/ru/settings.php b/lang/ru/settings.php new file mode 100644 index 0000000..08ffa8e --- /dev/null +++ b/lang/ru/settings.php @@ -0,0 +1,17 @@ + + */ + +// keys need to match the config setting name +// $lang['fixme'] = 'FIXME'; + +$lang['Host'] = 'Адрес MySQL сервера (dns или ip)'; +$lang['DB'] = 'Имя базы данных MySQL'; +$lang['user'] = 'Логин'; +$lang['password'] = 'Пароль'; + + +//Setup VIM: ex: et ts=4 : diff --git a/plugin.info.txt b/plugin.info.txt new file mode 100644 index 0000000..4fee125 --- /dev/null +++ b/plugin.info.txt @@ -0,0 +1,7 @@ +base sqlquery +author George Pirogov +email i1557@yandex.ru +date 2016-11-25 +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 new file mode 100644 index 0000000..f76e7ef --- /dev/null +++ b/syntax.php @@ -0,0 +1,139 @@ + + */ + +// must be run within Dokuwiki +if (!defined('DOKU_INC')) die(); + +class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin { + + public function getType() { + return 'substition'; + } + + public function getSort() { + return 666; + } + + public function connectTo($mode) + { + $this->Lexer->addEntryPattern('', $mode, 'plugin_sqlquery'); + } + + public function postConnect() + { + $this->Lexer->addExitPattern('','plugin_sqlquery'); + } + + /** + * Handle matches of the sqlquery syntax + * + * @param string $match The match of the syntax + * @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; + + } + + $data = array(); + return $data; + } + + /** + * Render xhtml output or metadata + * + * @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 + return true; + } // of render function +} + +// vim:ts=4:sw=4:et: