Fix plugin for DokuWiki version Hogfather & more features

- Fix method render()
- Fix indentation to 2 spaces
- Fix use of `lastchange` to be compatible
  with space API version >=13
- Add error handling if API path is not provided
- Add error handling if API path couldn't be fetched
- Add new translation keys
- Add feature to set API path inside the shortcode
  i.e. [wiyh 'api path here']
- Update README
This commit is contained in:
Nik 2021-02-17 10:43:51 +01:00
parent 01c885d48f
commit 223f37300a
4 changed files with 60 additions and 42 deletions

View File

@ -4,7 +4,10 @@ This plugin uses the [SpaceAPI](http://spaceapi.net/) of your hackerspace to dis
## Install ## Install
The best way is to put [wiyh] into the page :sidebar, but any other place is fine too. The best way is to put `[wiyh 'hackerspace API path/url here']` into the page :sidebar, but any other place is fine too.
Else you can edit the `dokuwiki/lib/plugins/whoisinyourhackspace/conf/default.php` and put the API path in
`$conf['api_path'] = '';` and then just put `[wiyh]` in any page.
If you install this plugin manually, make sure it is installed in If you install this plugin manually, make sure it is installed in
lib/plugins/whoisinyourhackspace/ - if the folder is called different it lib/plugins/whoisinyourhackspace/ - if the folder is called different it

View File

@ -10,6 +10,8 @@ $lang['wiyh_heading'] = 'Raumstatus';
$lang['wiyh_open'] = 'ist geöffnet.'; $lang['wiyh_open'] = 'ist geöffnet.';
$lang['wiyh_closed'] = 'ist geschlossen.'; $lang['wiyh_closed'] = 'ist geschlossen.';
$lang['wiyh_stats'] = 'Raumöffnungsstatistiken'; $lang['wiyh_stats'] = 'Raumöffnungsstatistiken';
$lang['wiyh_no_api_path'] = 'Es wurde kein API-Pfad angegeben.';
$lang['wiyh_fetch_error'] = 'Der API-Pfad konnte nicht abgerufen werden.';

View File

@ -10,7 +10,8 @@ $lang['wiyh_heading'] = 'State';
$lang['wiyh_open'] = 'is open.'; $lang['wiyh_open'] = 'is open.';
$lang['wiyh_closed'] = 'is closed.'; $lang['wiyh_closed'] = 'is closed.';
$lang['wiyh_stats'] = 'Stats'; $lang['wiyh_stats'] = 'Stats';
$lang['wiyh_no_api_path'] = 'No API path was given.';
$lang['wiyh_fetch_error'] = 'Could not fetch API path.';
//Setup VIM: ex: et ts=4 : //Setup VIM: ex: et ts=4 :

View File

@ -16,19 +16,6 @@ if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'syntax.php'; require_once DOKU_PLUGIN.'syntax.php';
class syntax_plugin_whoisinyourhackspace extends DokuWiki_Syntax_Plugin { class syntax_plugin_whoisinyourhackspace extends DokuWiki_Syntax_Plugin {
/**
* Check if a given option has been given, and remove it from the initial string
* @param string $match The string match by the plugin
* @param string $pattern The pattern which activate the option
* @param $varAffected The variable which will memorise the option
* @param $valIfFound the value affected to the previous variable if the option is found
*/
private function _checkOption(&$match, $pattern, &$varAffected, $valIfFound){
if ( preg_match($pattern, $match, $found) ){
$varAffected = $valIfFound;
$match = str_replace($found[0], '', $match);
}
} // _checkOption
public function getType() { public function getType() {
return 'substition'; return 'substition';
@ -42,44 +29,69 @@ class syntax_plugin_whoisinyourhackspace extends DokuWiki_Syntax_Plugin {
return 0; return 0;
} }
public function connectTo($mode) { public function connectTo($mode) {
$this->Lexer->addSpecialPattern('\[wiyh\]',$mode,'plugin_whoisinyourhackspace'); $this->Lexer->addSpecialPattern("(?:\[wiyh '.*?'\]|\[wiyh\])", $mode,'plugin_whoisinyourhackspace');
} }
public function render($mode, &$renderer, $data) { public function handle($match, $state, $pos, Doku_Handler $handler)
{
if (DOKU_LEXER_SPECIAL !== $state) {
return [];
}
$matches = [];
if (false !== preg_match("/wiyh '(.*?)'/", $match, $matches)) {
return [$state, $match, $matches[1] ?? null];
}
return [$state, $match];
}
public function render($format, Doku_Renderer $renderer, $data) {
global $conf; global $conf;
if($mode != 'xhtml') return false; [$state, $match, $url] = $data;
$api_path = $this->getConf('api_path'); if($format != 'xhtml') return false;
$file = file_get_contents($api_path); $api_path = $url ?: $this->getConf('api_path');
if (empty ($api_path)) {
$renderer->doc .= "<div>{$this->getLang('wiyh_no_api_path')}</div>";
return true;
}
if (false === $file = file_get_contents($api_path)) {
$renderer->doc .= "<div>{$this->getLang('wiyh_fetch_error')}</div>";
return true;
}
$api = json_decode($file); $api = json_decode($file);
$content = ''; $content = '';
$content .= '<div class="leinelab-state">'; $content .= '<div class="hackerspace-room-state">';
$content .= "<h3>" . $this->getLang('wiyh_heading') . "</h3>"; $content .= "<h3>" . $this->getLang('wiyh_heading') . "</h3>";
if ($api->state->open) { if ($api->state->open) {
$content .= "<img class=\"icon\" src=\"{$api->state->icon->open}\" alt=\"{$api->space} ist besetzt.\" title=\"{$api->space} ist besetzt.\" />"; $content .= $api->state->icon->open ? "<img class=\"icon\" src=\"{$api->state->icon->open}\" alt=\"{$api->space} ist besetzt.\" title=\"{$api->space} ist besetzt.\" />" : '';
$content .= "<p class=\"text\">{$api->space} " . $this->getLang('wiyh_open') . "</p>"; $content .= "<p class=\"text\">{$api->space} " . $this->getLang('wiyh_open') . "</p>";
} else { } else {
$content .= "<img class=\"icon\" src=\"{$api->state->icon->closed}\" alt=\"{$api->space} ist geschlossen.\" title=\"{$api->space} ist geschlossen.\" />"; $content .= $api->state->icon->open ? "<img class=\"icon\" src=\"{$api->state->icon->closed}\" alt=\"{$api->space} ist geschlossen.\" title=\"{$api->space} ist geschlossen.\" />" : '';
$content .= "<p class=\"text\">{$api->space} " . $this->getLang('wiyh_closed') . "</p>"; $content .= "<p class=\"text\">{$api->space} " . $this->getLang('wiyh_closed') . "</p>";
} }
$date = new DateTime(); $date = new DateTime();
$date->setTimeStamp($api->lastchange); $date->setTimeStamp($api->state->lastchange);
$content .= "<p class=\"text\"> seit " . $date->format('d.m.Y H:i') . " Uhr</p>"; $content .= "<p class=\"text\"> seit " . $date->format('d.m.Y H:i') . " Uhr</p>";
$content .= '<hr />'; $content .= '<hr />';
$content .= sprintf('<p><a href="http://spaceapi-stats.n39.eu/#%s">'.$this->getLang('wiyh_stats').'</a></p>',strtolower($api->space));
$content .= '</div>'; $content .= '</div>';
$renderer->doc .= $content; $renderer->doc .= $content;
return true; return true;
} }
} }