-
Notifications
You must be signed in to change notification settings - Fork 2
/
syntax.php
300 lines (254 loc) · 7.9 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* Eventum Plugin: Evetnum SCM addons
*
* interpret eventum issue tags in DokuWiki
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Elan Ruusamäe <glen@pld-linux.org>
*/
if (file_exists($autoload = __DIR__ . '/vendor/autoload.php')) {
require_once $autoload;
} else {
// try from include path
require_once 'XML/RPC.php';
require_once 'class.Eventum_RPC.php';
}
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_eventum extends DokuWiki_Syntax_Plugin
{
/*
* keys that we use for caching, to avoid running out of memory when serializing
*/
private static $cache_keys = array(
'iss_summary' => 'summary',
'sta_title' => 'status',
'sta_is_closed' => 'is_closed',
);
/**
* What kind of syntax are we?
*/
public function getType()
{
return 'substition';
}
/**
* Where to sort in?
*/
public function getSort()
{
return 290;
}
/**
* Connect pattern to lexer
*/
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('\[\[issue>.+?\]\]', $mode, 'plugin_eventum');
}
/**
* Handle the match
*/
public function handle($match, $state, $pos, Doku_Handler $handler)
{
$raw = $match = substr($match, 8, -2);
// extract title
list($match, $title) = explode('|', $match, 2);
// extract id
list($id, $attrs) = explode('&', $match, 2);
$data = array('raw' => $raw, 'id' => $id, 'attrs' => $attrs);
// set title only when specified to be able to make difference between empty and unset
if ($title !== null) {
$data['title'] = trim($title);
}
return $data;
}
private function cache($id, $data = null)
{
global $conf;
$cachefile = $conf['cachedir'] . '/' . $this->getPluginName() . '.cache';
// mode: get but no cachefile
if ($data === null && !file_exists($cachefile)) {
return null;
}
// read cache
$cache = array();
if (file_exists($cachefile)) {
$cache = unserialize(file_get_contents($cachefile));
}
// expire as long as page edit time to make page edit smooth but still
// have almost accurate data.
$mtime = time() - $conf['locktime'];
foreach ($cache as $i => $ent) {
if ($ent['mtime'] < $mtime) {
unset($cache[$i]);
}
}
// mode get:
if ($data === null) {
return isset($cache[$id]) ? $cache[$id] : null;
}
// mode: set
$cache[$id] = $data;
file_put_contents($cachefile, serialize($cache));
return true;
}
/*
* combine keys from $data array to new one.
* rename keys to be named as $value says.
*/
private function filter_keys($keys, $data)
{
$res = array();
foreach ($keys as $key => $value) {
// remap old key to new one
if (isset($data[$key])) {
$res[$value] = $data[$key];
continue;
}
// use already existing new name
if (isset($data[$value])) {
$res[$value] = $data[$value];
continue;
}
}
return $res;
}
/**
* Query data from Eventum server
*/
private function query($id)
{
$cache = $this->cache($id);
if ($cache !== null) {
return $cache;
}
static $client = null;
static $eventum_url, $rpc_url;
if (!$client) {
// setup RPC object
$rpc_url = $this->getConf('url') . '/rpc/xmlrpc.php';
$client = new Eventum_RPC($rpc_url);
$client->setCredentials($this->getConf('username'), $this->getConf('password'));
//$client->setDebug(1);
// Issue link
$eventum_url = $this->getConf('url') . '/view.php?id=';
}
$data['url'] = $eventum_url . $id;
try {
$data['details'] = $this->filter_keys(self::$cache_keys, $client->getSimpleIssueDetails((int )$id));
} catch (Eventum_RPC_Exception $e) {
$data['error'] = $e->getMessage();
global $conf;
if ($conf['allowdebug']) {
$data['rpcurl'] = $rpc_url;
}
}
$data['id'] = $id;
$data['mtime'] = time();
$this->cache($id, $data);
return $data;
}
/**
* Create output
*/
public function render($format, Doku_Renderer $renderer, $data)
{
// fetch extra data from eventum
$data += $this->query($data['id']);
// link title
$link = sprintf($this->getLang('issue'), $data['id']);
if ($data['error']) {
if ($format === 'xhtml') {
$renderer->doc .= $link;
$renderer->doc .= ': <i style="color:red">' . $data['error'] . '</i>';
if (isset($data['rpcurl'])) {
$renderer->doc .= " <tt>RPC URL: {$data['rpcurl']}</tt>";
}
} else {
$renderer->cdata($data['error']);
}
return true;
}
if (!isset($data['title'])) {
$data['title'] = $data['details']['summary'];
}
if ($format === 'xhtml' || $format === 'odt') {
$html = '';
$html .= $this->link($format, $data['url'], $link, $data['details']['summary']);
if ($data['title']) {
$html .= ': ' . hsc($data['title']);
}
if ($data['details']['status']) {
$html .= ' ' . $this->emphasis($format, '(' . $data['details']['status'] . ')');
}
if ($data['details']['is_closed']) {
$html = $this->strike($format, $html);
}
$renderer->doc .= $this->html($format, $html);
}
return true;
}
/** odt/html export helpers, partly ripped from odt plugin */
private function xmlEntities($value)
{
return str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $value);
}
private function strike($format, $text)
{
$doc = '';
if ($format === 'xhtml') {
$doc .= '<strike>';
$doc .= $text;
$doc .= '</strike>';
} elseif ($format === 'odt') {
$doc .= '<text:span text:style-name="del">';
$doc .= $text;
$doc .= '</text:span>';
}
return $doc;
}
private function emphasis($format, $text)
{
$doc = '';
if ($format === 'xhtml') {
$doc .= '<i>';
$doc .= $text;
$doc .= '</i>';
} elseif ($format === 'odt') {
$doc .= '<text:span text:style-name="Emphasis">';
$doc .= $text;
$doc .= '</text:span>';
}
return $doc;
}
private function html($format, $text)
{
$doc = '';
if ($format === 'xhtml') {
$doc .= $text;
} elseif ($format === 'odt') {
$doc .= '<text:span>';
$doc .= $text;
$doc .= '</text:span>';
}
return $doc;
}
private function link($format, $url, $name, $title)
{
$doc = '';
if ($format === 'xhtml') {
$doc .= '<a class="iw_eventum" href="' . $url . '" target="_blank" title="' . $title . '">' . hsc($name) . '</a>';
} elseif ($format === 'odt') {
$url = $this->xmlEntities($url);
$doc .= '<text:a xlink:type="simple" xlink:href="' . $url . '">';
$doc .= $name; // we get the name already XML encoded
$doc .= '</text:a>';
}
return $doc;
}
}
//Setup VIM: ex: et ts=4 enc=utf-8 :