Skip to content

Commit

Permalink
Merge pull request #1 from micgro42/extendSyntax
Browse files Browse the repository at this point in the history
Extend syntax
  • Loading branch information
splitbrain committed Feb 19, 2015
2 parents eb73725 + ac48637 commit 5f42574
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ rating Plugin for DokuWiki
Allows rating a page

All documentation for this plugin can be found at
http://www.cosmocode.de
https://www.dokuwiki.org/plugin:rating

If you install this plugin manually, make sure it is installed in
lib/plugins/rating/ - if the folder is called different it
Expand Down
100 changes: 100 additions & 0 deletions _test/date.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
*
*
* @author Michael Große <grosse@cosmocode.de>
*
* @group Michael Große <grosse@cosmocode.de>
* @group plugin_rating
* @group plugins
*/

class best_rating_test extends DokuWikiTest {
protected $pluginsEnabled = array('rating');

function test_vanilla_syntax_parsing() {
$parser_response = p_get_instructions('{{rating}}')[2];
$expected_response = array(
0 => 'plugin',
1 => array(
0 => 'rating',
1 => array(
0 => DOKU_LEXER_SPECIAL,
1 => array(
'lang' => '',
'startdate' => '',
)
),
2 => DOKU_LEXER_SPECIAL,
3 => '{{rating}}',
),
2 => 1,
);
$this->assertEquals($expected_response, $parser_response);
}

function test_date_syntax_parsing() {
$parser_response = p_get_instructions('{{rating|startdate=2015-02-17}}')[2];
$expected_response = array(
0 => 'plugin',
1 => array(
0 => 'rating',
1 => array(
0 => DOKU_LEXER_SPECIAL,
1 => array(
'lang' => '',
'startdate' => '2015-02-17',
)
),
2 => DOKU_LEXER_SPECIAL,
3 => '{{rating|startdate=2015-02-17}}',
),
2 => 1,
);
$this->assertEquals($expected_response, $parser_response);
}

function test_lang_syntax_parsing() {
$parser_response = p_get_instructions('{{rating|lang=en}}')[2];
$expected_response = array(
0 => 'plugin',
1 => array(
0 => 'rating',
1 => array(
0 => DOKU_LEXER_SPECIAL,
1 => array(
'lang' => 'en',
'startdate' => '',
)
),
2 => DOKU_LEXER_SPECIAL,
3 => '{{rating|lang=en}}',
),
2 => 1,
);
$this->assertEquals($expected_response, $parser_response);
}

function test_datelang_syntax_parsing() {
$parser_response = p_get_instructions('{{rating|startdate=2015-02-17,lang=en}}')[2];
$expected_response = array(
0 => 'plugin',
1 => array(
0 => 'rating',
1 => array(
0 => DOKU_LEXER_SPECIAL,
1 => array(
'lang' => 'en',
'startdate' => '2015-02-17',
)
),
2 => DOKU_LEXER_SPECIAL,
3 => '{{rating|startdate=2015-02-17,lang=en}}',
),
2 => 1,
);
$this->assertEquals($expected_response, $parser_response);
}

}
57 changes: 57 additions & 0 deletions _test/general.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* General tests for the top plugin
*
* @group plugin_rating
* @group plugins
*/
class general_plugin_rating_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('rating', $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 . 'rating/conf/default.php and ' . DOKU_PLUGIN . 'rating/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 . 'rating/conf/metadata.php');
}
foreach($meta as $key => $value) {
$this->assertArrayHasKey($key, $conf, 'Key $conf[\'' . $key . '\'] missing in ' . DOKU_PLUGIN . 'rating/conf/default.php');
}
}
}
}
2 changes: 1 addition & 1 deletion db/latest.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1
2
25 changes: 25 additions & 0 deletions db/update0002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TEMPORARY TABLE ratingtemp (
page,
rater,
lang,
date,
value,
PRIMARY KEY(page, rater)
);

INSERT INTO ratingtemp (page, rater, lang, date, value) SELECT page, rater, '', '', value FROM ratings;

DROP TABLE ratings;

CREATE TABLE ratings (
page,
rater,
lang,
date,
value,
PRIMARY KEY(page, rater)
);

INSERT INTO ratings (page, rater, lang, date, value) SELECT page, rater, lang, date, value FROM ratingtemp;

DROP TABLE ratingtemp;
32 changes: 27 additions & 5 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,25 @@ public function remove($page) {
* @param int $num
* @return array
*/
public function best($num = 10) {
public function best($lang, $startdate, $num = 10) {
$sqlite = $this->getDBHelper();
if(!$sqlite) return array();

$sql = "SELECT sum(value) as val, page FROM ratings GROUP BY page ORDER BY sum(value) DESC LIMIT ?";
$res = $sqlite->query($sql, $num);
$sqlbegin = "SELECT sum(value) as val, page, lang FROM ratings ";
$sqlend = "GROUP BY page ORDER BY sum(value) DESC LIMIT ?";
if ($lang === null && $startdate === null){
$sql = $sqlbegin . $sqlend;
$res = $sqlite->query($sql, $num);
} else if ($lang !== null && $startdate === null) {
$sql = $sqlbegin . "WHERE lang = ? " . $sqlend;
$res = $sqlite->query($sql, $lang, $num);
} else if ($lang === null && $startdate !== null){
$sql = $sqlbegin . "WHERE date >= ? " . $sqlend;
$res = $sqlite->query($sql, $startdate, $num);
} else {
$sql = $sqlbegin . "WHERE lang = ? AND date >= ? " . $sqlend;
$res = $sqlite->query($sql, $lang, $startdate, $num);
}
$list = $sqlite->res2arr($res);
$sqlite->res_close($res);
return $list;
Expand All @@ -127,8 +140,17 @@ public function rate($rate, $page) {
$sqlite = $this->getDBHelper();
if(!$sqlite) return;

$sql = "INSERT OR REPLACE INTO ratings (page, rater, value) VALUES (?, ?, ?)";
$sqlite->query($sql, $page, $this->userID(), $rate);
$translation = plugin_load('helper', 'translation');
if (!$translation) {
$lang = '';
} else {
$lang = $translation->getLangPart($page);
}

$date = date('Y-m-d');

$sql = "INSERT OR REPLACE INTO ratings (page, rater, lang, date, value) VALUES (?, ?, ?, ?, ?)";
$sqlite->query($sql, $page, $this->userID(), $lang, $date, $rate);
}
}

Expand Down
4 changes: 2 additions & 2 deletions plugin.info.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
base rating
author Andreas Gohr
email gohr@cosmocode.de
date 2014-10-08
date 2015-02-19
name rating plugin
desc Allows rating a page
url http://www.cosmocode.de
url https://www.dokuwiki.org/plugin:rating
42 changes: 33 additions & 9 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class syntax_plugin_rating extends DokuWiki_Syntax_Plugin {
* What kind of syntax are we?
*/
function getType() {
return 'substition';
return 'protected';
}

/**
Expand All @@ -29,32 +29,56 @@ function getSort() {
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern('\\{\\{rating\\}\\}', $mode, 'plugin_rating');
$this->Lexer->addSpecialPattern('\\{\\{rating(?:.*?)\\}\\}', $mode, 'plugin_rating');
}

/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler) {
return array();
function handle($match, $state, $pos, Doku_Handler $handler) {
if ($state==DOKU_LEXER_SPECIAL) {
$options = array('lang' => null, 'startdate' => null );
$match = rtrim($match,'\}');
$match = substr($match,8);
if ($match != '') {
$match = ltrim($match,'\|');
$match = explode(",", $match);
foreach($match as $option) {
$options[explode('=', $option)[0]] = explode('=', $option)[1];
}
}
return array($state, $options);
} else {
return array($state, '');
}
}

/**
* Create output
*/
function render($format, Doku_Renderer $renderer, $data) {
if($format == 'metadata') return false;
/** @var helper_plugin_rating $hlp */
if($data[0] != DOKU_LEXER_SPECIAL) return false;

$hlp = plugin_load('helper', 'rating');
$list = $hlp->best();
$list = $hlp->best($data[1]['lang'],$data[1]['startdate'], 20);

$renderer->listu_open();
$renderer->listo_open();
$num_items=0;
foreach($list as $item) {
if (auth_aclcheck($item['page'],'',null) < AUTH_READ) continue;
$num_items = $num_items +1;
$renderer->listitem_open(1);
if (strpos($item['page'],':') === false) {
$item['page'] = ':' . $item['page'];
}
$renderer->internallink($item['page']);
$renderer->cdata(' (' . $item['val'] . ')');

$renderer->listitem_close();
if ($num_items >= 10) break;
}
$renderer->listu_close();
$renderer->listo_close();
}

}
}

0 comments on commit 5f42574

Please sign in to comment.