-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
127 lines (108 loc) · 3.44 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\HTTP\DokuHTTPClient;
/**
* DokuWiki Plugin pegel (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
class syntax_plugin_pegel extends SyntaxPlugin
{
public const API_URL = 'https://www.pegelonline.wsv.de/webservices/rest-api/v2';
protected $apiResults = [];
/** @inheritDoc */
public function getType()
{
return 'substition';
}
/** @inheritDoc */
public function getPType()
{
return 'normal';
}
/** @inheritDoc */
public function getSort()
{
return 155;
}
/** @inheritDoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('<pegel .*?>', $mode, 'plugin_pegel');
}
/** @inheritDoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
$match = substr($match, 7, -1);
[$uuid, $type] = sexplode(' ', $match, 2);
$uuid = trim($uuid);
$type = trim($type);
if (!$type) $type = 'v';
return [
'uuid' => $uuid,
'type' => $type,
];
}
/** @inheritDoc */
public function render($mode, Doku_Renderer $renderer, $data)
{
if ($mode === 'metadata') {
return false;
}
// for everything else we simply use cdata()
try {
$pegel = $this->callAPI($data['uuid']);
$station = $pegel[0]['stations'][0];
$current = $station['timeseries'][0];
switch ($data['type']) {
case 'v':
$text = $current['currentMeasurement']['value'] . $current['unit'];
break;
case 'dt':
$text = (new DateTime($current['currentMeasurement']['timestamp']))->format($this->getConf('dt'));
break;
case 't':
$text = (new DateTime($current['currentMeasurement']['timestamp']))->format($this->getConf('t'));
break;
case 'd':
$text = (new DateTime($current['currentMeasurement']['timestamp']))->format($this->getConf('d'));
break;
default:
$text = $station[$data['type']] ?? 'unknown type';
}
} catch (Exception $e) {
$text = 'Error: ' . $e->getMessage();
}
$renderer->cdata($text);
return true;
}
/**
* Fetch the data from the API
*
* @param string $uuid
* @return mixed
* @throws Exception
*/
protected function callAPI($uuid)
{
if (isset($this->apiResults[$uuid])) return $this->apiResults[$uuid]; // only one call per request
$url = self::API_URL . '/waters.json?' . http_build_query(
[
'stations' => $uuid,
'includeTimeseries' => 'true',
'includeCurrentMeasurement' => 'true',
'includeStations' => 'true',
],
'', '&'
);
$http = new DokuHTTPClient();
$ok = $http->sendRequest($url);
if ($ok === false) {
return throw new Exception('API request failed ' . $http->error, $http->status);
}
$data = json_decode($http->resp_body, true);
$this->apiResults[$uuid] = $data;
return $data;
}
}