Compare commits

..

2 Commits

Author SHA1 Message Date
digitalduke 500e0103a8
Change LICENSE 6 years ago
digitalduke 7a069cc899
Delete LICENSE 6 years ago
  1. 9
      README.md
  2. 1
      conf/default.php
  3. 2
      conf/metadata.php
  4. 16
      lang/de/lang.php
  5. 17
      lang/de/settings.php
  6. 7
      lang/en/settings.php
  7. 2
      lang/ru/lang.php
  8. 7
      lang/ru/settings.php
  9. 4
      plugin.info.txt
  10. 163
      syntax.php

@ -1,13 +1,4 @@
# sqlquery # sqlquery
A dokuwiki plugin for processing query to MySQL databases and display results as a table. A dokuwiki plugin for processing query to MySQL databases and display results as a table.
This is a improved version which cann connect to different hosts
and databases. The defaults are set in the plugin configuration.
Different types, hosts and databases can be set inside the tag:
```
<sql type=mysql host=myhost db=mydb>
SELECT foo FROM bar ORDER BY baz
<sql>
```
More information at https://www.dokuwiki.org/plugin:sqlquery More information at https://www.dokuwiki.org/plugin:sqlquery

@ -7,7 +7,6 @@
//$conf['fixme'] = 'FIXME'; //$conf['fixme'] = 'FIXME';
$conf['type'] = 'mysql';
$conf['Host'] = 'localhost'; $conf['Host'] = 'localhost';
$conf['DB'] = ''; $conf['DB'] = '';
$conf['user'] = ''; $conf['user'] = '';

@ -5,7 +5,7 @@
* @author George Pirogov <i1557@yandex.ru> * @author George Pirogov <i1557@yandex.ru>
*/ */
$meta['type'] = array('multichoice', '_choices' => array('mysql', 'dblib'));
$meta['Host'] = array('string'); $meta['Host'] = array('string');
$meta['DB'] = array('string'); $meta['DB'] = array('string');
$meta['user'] = array('string'); $meta['user'] = array('string');

@ -1,16 +0,0 @@
<?php
/**
* German language file for sqlquery plugin
*
* @author Thomas Hooge <thomas@hoogi.de>
*/
// 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 :

@ -1,17 +0,0 @@
<?php
/**
* German language file for sqlquery plugin
*
* @author Thomas Hooge <thomas@hoogi.de>
*/
// keys need to match the config setting name
$lang['type'] = 'DSN-Prefix (Datenbanktyp)';
$lang['Host'] = 'Standard MySQL-Serveradresse (DNS or IP)';
$lang['DB'] = 'Standard MySQL-Datenbankname';
$lang['user'] = 'Datenbankbenutzername';
$lang['password'] = 'Datenbankkennwort';
//Setup VIM: ex: et ts=4 :

@ -1,6 +1,6 @@
<?php <?php
/** /**
* English language file for sqlquery plugin * english language file for sqlquery plugin
* *
* @author George Pirogov <i1557@yandex.ru> * @author George Pirogov <i1557@yandex.ru>
*/ */
@ -8,11 +8,6 @@
// keys need to match the config setting name // keys need to match the config setting name
// $lang['fixme'] = 'FIXME'; // $lang['fixme'] = 'FIXME';
$lang['type'] = 'DSN prefix (database type)';
$lang['Host'] = 'Default MySQL server address (DNS or IP)';
$lang['DB'] = 'MySQL database name';
$lang['user'] = 'Database username';
$lang['password'] = 'Database password';
//Setup VIM: ex: et ts=4 : //Setup VIM: ex: et ts=4 :

@ -1,6 +1,6 @@
<?php <?php
/** /**
* Russian language file for sqlquery plugin * English language file for sqlquery plugin
* *
* @author George Pirogov <i1557@yandex.ru> * @author George Pirogov <i1557@yandex.ru>
*/ */

@ -1,6 +1,6 @@
<?php <?php
/** /**
* Russian language file for sqlquery plugin * english language file for sqlquery plugin
* *
* @author George Pirogov <i1557@yandex.ru> * @author George Pirogov <i1557@yandex.ru>
*/ */
@ -8,9 +8,8 @@
// keys need to match the config setting name // keys need to match the config setting name
// $lang['fixme'] = 'FIXME'; // $lang['fixme'] = 'FIXME';
$lang['type'] = 'Префикс DSN (тип базы данных)'; $lang['Host'] = 'Адрес MySQL сервера (dns или ip)';
$lang['Host'] = 'Адрес MySQL сервера по умолчанию (DNS или IP)'; $lang['DB'] = 'Имя базы данных MySQL';
$lang['DB'] = 'Имя базы данных MySQL по умолчанию';
$lang['user'] = 'Логин'; $lang['user'] = 'Логин';
$lang['password'] = 'Пароль'; $lang['password'] = 'Пароль';

@ -1,7 +1,7 @@
base sqlquery base sqlquery
author George Pirogov author George Pirogov
email i1557@yandex.ru email i1557@yandex.ru
date 2018-12-12 date 2016-11-25
name SQL query plugin (multidb) name SQL query plugin
desc Processing query to mysql database and display results as a table. desc Processing query to mysql database and display results as a table.
url https://www.dokuwiki.org/plugin:sqlquery url https://www.dokuwiki.org/plugin:sqlquery

@ -4,26 +4,29 @@
* *
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author George Pirogov <i1557@yandex.ru> * @author George Pirogov <i1557@yandex.ru>
* @author Thomas Hooge <hooge@rowa-group.com>
*
*/ */
// must be run within Dokuwiki // must be run within Dokuwiki
if (!defined('DOKU_INC')) die(); 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 { class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin {
public function getType() { return 'substition'; } public function getType() {
public function getSort() { return 666; } return 'substition';
public function getPType() { return 'block'; } }
public function getSort() {
return 666;
}
public function connectTo($mode) public function connectTo($mode)
{ {
$this->Lexer->addSpecialPattern('<sql\b(?:\s+(?:host|db|type)=[\w\-\.$]+?)*\s*>(?:.*?</sql>)', $mode, 'plugin_sqlquery'); $this->Lexer->addEntryPattern('<sql>', $mode, 'plugin_sqlquery');
}
public function postConnect()
{
$this->Lexer->addExitPattern('</sql>','plugin_sqlquery');
} }
/** /**
@ -33,36 +36,29 @@ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin {
* @param int $state The state of the handler * @param int $state The state of the handler
* @param int $pos The position in the document * @param int $pos The position in the document
* @param Doku_Handler $handler The handler * @param Doku_Handler $handler The handler
*
* @return array Data for the renderer * @return array Data for the renderer
*/ */
public function handle($match, $state, $pos, Doku_Handler $handler) { public function handle($match, $state, $pos, Doku_Handler $handler)
$data = array('state' => $state); {
if ($state == DOKU_LEXER_SPECIAL) { switch ( $state )
# get type (DSN prefix) {
if (preg_match('/<sql\b.*type=(mysql|dblib)/', $match, $result)) { case DOKU_LEXER_ENTER:
$data['type'] = $result[1]; $data = array();
} else { return $data;
$data['type'] = $this->getConf('type'); break;
}
# get host case DOKU_LEXER_UNMATCHED:
if (preg_match('/<sql\b.*host=([\w\-\.$]+)/', $match, $result)) { return array('sqlquery' => $match);
$data['host'] = $result[1]; break;
} else {
$data['host'] = $this->getConf('Host'); case DOKU_LEXER_EXIT:
} $data = array();
# get database return $data;
if (preg_match('/<sql\b.*db=([\w\-\.$]+)/', $match, $result)) { break;
$data['db'] = $result[1];
} else {
$data['db'] = $this->getConf('DB');
}
# get query
$data['match'] = $match;
if (preg_match('%<sql.*?>(.*)</sql>%s', $match, $result)) {
$data['query'] = trim($result[1]);
}
} }
$data = array();
return $data; return $data;
} }
@ -72,73 +68,72 @@ class syntax_plugin_sqlquery extends DokuWiki_Syntax_Plugin {
* @param string $mode Renderer mode (supported modes: xhtml) * @param string $mode Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $renderer The renderer * @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function * @param array $data The data from the handler() function
*
* @return bool If rendering was successful. * @return bool If rendering was successful.
*/ */
public function render($mode, Doku_Renderer $renderer, $data) public function render($mode, Doku_Renderer $renderer, $data)
{ {
if ( $mode != 'xhtml' ) return false; if ( $mode != 'xhtml' ) return false;
if (empty($data['query'])) return true;
// get configuration if ( !empty( $data['sqlquery'] ) )
{
// получаем параметры конфигурации
$host = $this->getConf('Host');
$DB = $this->getConf('DB');
$user = $this->getConf('user'); $user = $this->getConf('user');
$password = $this->getConf('password'); $password = $this->getConf('password');
// connect to database // получаем запрос
$dsn = "{$data['type']}:host={$data['host']};dbname={$data[db]};charset=UTF-8;"; $querystring = $data['sqlquery'];
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
$renderer->doc .= "<pre>Unable to connect ro database:" . $e->getMessage() . "</pre>\n";
return true;
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM);
// run query
try {
$result = $dbh->query($data['query']);
} catch (PDOException $e) {
$renderer->doc .= "<pre>Error in query:" . $e->getMessage() . "</pre>\n";
return true;
}
// get the number of fields in the table // подключаемся к базе
$fieldcount = $result->columnCount(); $link = mysqli_connect($host, $user, $password, $DB);
mysqli_set_charset($link, "utf8");
// build a table // подключились
$renderer->doc .= '<table id="sqlquerytable" class="inline">' . "\n"; if ( $link )
{
$result = mysqli_query($link, $querystring);
if ( $result )
{
// получаем кол-во полей в таблице
$fieldcount = mysqli_num_fields($result);
// build the header section of the table // строим таблицу
$renderer->doc .= "<thead><tr>"; $renderer->doc .= "<table id=\"sqlquerytable\" class=\"inline\">";
for ($i = 0; $i < $fieldcount; $i++) { // строим заголовок
$meta = $result->getColumnMeta($i); $renderer->doc .= "<thead><tr>";
while ($fieldinfo = mysqli_fetch_field($result))
{
$renderer->doc .= "<th>"; $renderer->doc .= "<th>";
$renderer->doc .= $meta['name']; $renderer->doc .= $fieldinfo->name;
$renderer->doc .= "</th>"; $renderer->doc .= "</th>";
} }
$renderer->doc .= "</tr></thead>\n"; $renderer->doc .= "</tr></thead>";
// build the contents of the table // строим содержимое таблицы
$renderer->doc .= "<tbody>\n"; $renderer->doc .= "<tbody>";
foreach ($result as $row) { while ($row = mysqli_fetch_row($result))
{
$renderer->doc .= "<tr>"; $renderer->doc .= "<tr>";
for ( $i = 0; $i < $fieldcount; $i++ ) {
// строим строку
for ( $i = 0; $i < $fieldcount; $i++ )
{
$renderer->doc .= "<td>"; $renderer->doc .= "<td>";
$renderer->doc .= htmlentities($row[$i]); $renderer->doc .= $row[$i];
$renderer->doc .= "</td>"; $renderer->doc .= "</td>";
} }
$renderer->doc .= "</tr>\n"; $renderer->doc .= "</tr>";
} } // of while fetch_row
// закрываем таблицу
// finish the table $renderer->doc .= "</tbody></table>";
$renderer->doc .= "</tbody>\n</table>\n"; } // of mysqli_query
mysqli_close($link);
// Close connection, there is no close() method with PDO :-( } // of mysqli link
$result = null; } // of sqlquery not empty
$dbh = null;
return true; return true;
} // of render function
} }
}
// vim:ts=4:sw=4:et: