Skip to content

Commit

Permalink
initial prototype checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed Jan 5, 2021
0 parents commit 2e564e0
Show file tree
Hide file tree
Showing 12 changed files with 693 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Config file for travis-ci.org

language: php
php:
- "7.3"
- "7.2"
- "7.1"
- "7.0"
- "5.6"
env:
- DOKUWIKI=master
- DOKUWIKI=stable
before_install: wget https://raw.github.com/splitbrain/dokuwiki-travis/master/travis.sh
install: sh travis.sh
script: cd _test && ./phpunit.phar --stderr --group plugin_dbquery
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
dbquery Plugin for DokuWiki

Display query results from a database

All documentation for this plugin can be found at
https://www.dokuwiki.org/plugin:dbquery

If you install this plugin manually, make sure it is installed in
lib/plugins/dbquery/ - 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) Andreas Gohr <[email protected]>

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 LICENSING file for details
76 changes: 76 additions & 0 deletions _test/general.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* General tests for the dbquery plugin
*
* @group plugin_dbquery
* @group plugins
*/
class general_plugin_dbquery_test extends DokuWikiTest
{

/**
* Simple test to make sure the plugin.info.txt is in correct format
*/
public function test_plugininfo()
{
$file = __DIR__ . '/../plugin.info.txt';
$this->assertFileExists($file);

$info = confToHash($file);

$this->assertArrayHasKey('base', $info);
$this->assertArrayHasKey('author', $info);
$this->assertArrayHasKey('email', $info);
$this->assertArrayHasKey('date', $info);
$this->assertArrayHasKey('name', $info);
$this->assertArrayHasKey('desc', $info);
$this->assertArrayHasKey('url', $info);

$this->assertEquals('dbquery', $info['base']);
$this->assertRegExp('/^https?:\/\//', $info['url']);
$this->assertTrue(mail_isvalid($info['email']));
$this->assertRegExp('/^\d\d\d\d-\d\d-\d\d$/', $info['date']);
$this->assertTrue(false !== strtotime($info['date']));
}

/**
* Test to ensure that every conf['...'] entry in conf/default.php has a corresponding meta['...'] entry in
* conf/metadata.php.
*/
public function test_plugin_conf()
{
$conf_file = __DIR__ . '/../conf/default.php';
if (file_exists($conf_file)) {
include($conf_file);
}
$meta_file = __DIR__ . '/../conf/metadata.php';
if (file_exists($meta_file)) {
include($meta_file);
}

$this->assertEquals(
gettype($conf),
gettype($meta),
'Both ' . DOKU_PLUGIN . 'dbquery/conf/default.php and ' . DOKU_PLUGIN . 'dbquery/conf/metadata.php have to exist and contain the same keys.'
);

if (gettype($conf) != 'NULL' && gettype($meta) != 'NULL') {
foreach ($conf as $key => $value) {
$this->assertArrayHasKey(
$key,
$meta,
'Key $meta[\'' . $key . '\'] missing in ' . DOKU_PLUGIN . 'dbquery/conf/metadata.php'
);
}

foreach ($meta as $key => $value) {
$this->assertArrayHasKey(
$key,
$conf,
'Key $conf[\'' . $key . '\'] missing in ' . DOKU_PLUGIN . 'dbquery/conf/default.php'
);
}
}

}
}
12 changes: 12 additions & 0 deletions conf/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Default settings for the dbquery plugin
*
* @author Andreas Gohr <[email protected]>
*/

$conf['namespace'] = 'dbquery';
$conf['dsn'] = '';
$conf['user'] = '';
$conf['pass'] = '';

13 changes: 13 additions & 0 deletions conf/metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Options for the dbquery plugin
*
* @author Andreas Gohr <[email protected]>
*/

$meta['namespace'] = ['string'];
$meta['dsn'] = ['string'];
$meta['user'] = ['string'];
$meta['pass'] = ['password'];


51 changes: 51 additions & 0 deletions helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* DokuWiki Plugin dbquery (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
class helper_plugin_dbquery extends dokuwiki\Extension\Plugin
{

/**
* @param string $name Page name of the query
* @throws \Exception
*/
public function loadQueryFromPage($name)
{

$name = cleanID($name);
$id = $this->getConf('namespace') . ':' . $name;
if (!page_exists($id)) throw new \Exception("No query named '$name' found");

$doc = p_cached_output(wikiFN($id), 'dbquery');
// FIXME handle additional stuff later

return trim($doc);
}

/**
* Opens a database connection, executes the query and returns the result
*
* @param string $query
* @param string[] $params
* @return array
* @throws \PDOException
* @todo should we keep the DB connection around for subsequent queries?
* @todo should we allow SELECT queries only for additional security?
*/
public function executeQuery($query, $params)
{
$pdo = new PDO($this->getConf('dsn'), $this->getConf('user'), $this->getConf('pass'));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sth = $pdo->prepare($query);
$sth->execute($params);
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$sth->closeCursor();

return $data;
}
}
13 changes: 13 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* English language file for dbquery plugin
*
* @author Andreas Gohr <[email protected]>
*/

// menu entry for admin plugins
// $lang['menu'] = 'Your menu entry';

// custom language strings for the plugin
// $lang['fixme'] = 'FIXME';

10 changes: 10 additions & 0 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* english language file for dbquery plugin
*
* @author Andreas Gohr <[email protected]>
*/

// keys need to match the config setting name
// $lang['fixme'] = 'FIXME';

7 changes: 7 additions & 0 deletions plugin.info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
base dbquery
author Andreas Gohr
email [email protected]
date 2021-01-05
name dbquery plugin
desc Display query results from a database
url https://www.dokuwiki.org/plugin:dbquery
31 changes: 31 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* DokuWiki Plugin dbquery (Renderer Component)
*
* Extracts code blocks from pages
*
* @todo this needs to be extended to get all the HTML blocks from sub sections
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
class renderer_plugin_dbquery extends \Doku_Renderer
{
/** @var bool remember if the first code block has been found already */
protected $codeFound = false;

/** @inheritDoc */
public function getFormat()
{
return 'dbquery';
}

/** @inheritDoc */
public function code($text, $lang = null, $file = null)
{
if ($this->codeFound) return;
$this->codeFound = true;
$this->doc = $text;
}

}
99 changes: 99 additions & 0 deletions syntax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* DokuWiki Plugin dbquery (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
class syntax_plugin_dbquery extends DokuWiki_Syntax_Plugin
{
/** @inheritDoc */
public function getType()
{
return 'substition';
}

/** @inheritDoc */
public function getPType()
{
return 'block';
}

/** @inheritDoc */
public function getSort()
{
return 135;
}

/** @inheritDoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('QUERY:\w+', $mode, 'plugin_dbquery');
}

/** @inheritDoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
return ['name' => substr($match, 6)];
}

/** @inheritDoc */
public function render($mode, Doku_Renderer $renderer, $data)
{
if ($mode !== 'xhtml') {
return false;
}

/** @var helper_plugin_dbquery $hlp */
$hlp = plugin_load('helper', 'dbquery');
try {
$query = $hlp->loadQueryFromPage($data['name']);
$result = $hlp->executeQuery($query, []);
} catch (\Exception $e) {
msg(hsc($e->getMessage()), -1);
return true;
}

$this->renderResultTable($result, $renderer);

return true;
}

/**
* Render the given result as a table
*
* @param string[][] $result
* @param Doku_Renderer $R
*/
public function renderResultTable($result, Doku_Renderer $R)
{
global $lang;

if (!count($result)) {
$R->cdata($lang['nothingfound']);
return;
}

$R->table_open();
$R->tablerow_open();
foreach (array_keys($result[0]) as $header) {
$R->tableheader_open();
$R->cdata($header);
$R->tableheader_close();
}
$R->tablerow_close();

foreach ($result as $row) {
$R->tablerow_open();
foreach ($row as $cell) {
$R->tablecell_open();
$R->cdata($cell);
$R->tablecell_close();
}
$R->tablerow_close();
}
$R->table_close();
}
}

0 comments on commit 2e564e0

Please sign in to comment.