-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
425 lines (338 loc) · 11.2 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
/**
* DokuWiki Plugin dokutranslate (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Martin Doucha <[email protected]>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'syntax.php';
# Nesting counter, patterns disabled when non-zero
$DOKUTRANSLATE_NEST = 0;
# Generate edit button for paragraph
function parEditButton($parId) {
global $ID;
global $INFO;
$ret = '';
$params = array(
'do' => 'edit',
'rev' => $INFO['lastmod'],
'parid' => $parId,
);
$ret .= '<div class="secedit editbutton_par' . strval($parId) . '">';
$ret .= html_btn('secedit', $ID, '', $params, 'post');
$ret .= '</div>';
return $ret;
}
function startEditForm(&$renderer, $erase = true) {
global $DOKUTRANSLATE_EDITFORM;
global $DOKUTRANSLATE_NEST;
global $ACT;
global $TEXT;
# Insert saved edit form
$renderer->doc .= '<div class="preview" id="scroll__here">';
$renderer->doc .= $DOKUTRANSLATE_EDITFORM;
# Render preview from submitted text (the saved page may look different
# if dokutranslate markup is present in the text)
if ($ACT == 'preview') {
$renderer->doc .= p_locale_xhtml('preview');
$DOKUTRANSLATE_NEST++;
$previewIns = p_get_instructions($TEXT);
$DOKUTRANSLATE_NEST--;
$renderer->nest($previewIns);
}
$renderer->doc .= '</div>';
if ($erase) {
# Insert erasure start marker
$renderer->doc .= '<!-- DOKUTRANSLATE ERASE START -->';
}
}
function endEditForm(&$renderer) {
# Insert erasure end marker
$renderer->doc .= '<!-- DOKUTRANSLATE ERASE STOP -->';
}
function loadTranslationMeta($id) {
global $REV;
# Loading meta for current version is simple
if (empty($REV)) {
return unserialize(io_readFile(metaFN($id, '.translate'), false));
}
# Old revision, do it the hard way...
$ret = array();
$meta = unserialize(io_readFile(metaFN($id, '.translateHistory'), false));
$oldrev = intval($REV);
for ($i = 0; $i < count($meta[$oldrev]); $i++) {
$tmp = empty($meta[$oldrev][$i]['changed']) ? $oldrev : $meta[$oldrev][$i]['changed'];
$ret[$i] = $meta[$tmp][$i];
$ret[$i]['changed'] = $tmp;
}
return $ret;
}
function parReviewClass($meta, $parid) {
static $classes = array('mistrans', 'reph', 'incaccept', 'accept');
# No reviews, no class
if (empty($meta[$parid]['reviews'])) {
return '';
}
# Start with max possible value of $clsid
$clsid = count($classes) - 1;
# Find the worst review
foreach ($meta[$parid]['reviews'] as $line) {
$tmp = $line['quality'];
if ($tmp >= count($classes) - 2 && !$line['incomplete']) {
$tmp++;
}
$clsid = $tmp < $clsid ? $tmp : $clsid;
}
return empty($classes[$clsid]) ? '' : $classes[$clsid];
}
class syntax_plugin_dokutranslate extends DokuWiki_Syntax_Plugin {
private $origIns = NULL;
private $meta = NULL;
private $parCounter = 0;
public function getType() {
return 'container';
}
public function getPType() {
return 'stack';
}
public function getSort() {
return 100;
}
public function getAllowedTypes() {
return array('container', 'baseonly', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
}
public function isSingleton() {
return true;
}
public function connectTo($mode) {
global $DOKUTRANSLATE_NEST;
global $ID;
# Disable patterns when the page is not being translated or
# we're building instructions for original page
if (!@file_exists(metaFN($ID, '.translate')) || $DOKUTRANSLATE_NEST > 0) {
return;
}
$this->Lexer->addEntryPattern('~~DOKUTRANSLATE_START~~(?=.*~~DOKUTRANSLATE_END~~)',$mode,'plugin_dokutranslate');
$this->Lexer->addSpecialPattern('~~DOKUTRANSLATE_PARAGRAPH~~','plugin_dokutranslate','plugin_dokutranslate');
}
public function postConnect() {
global $DOKUTRANSLATE_NEST;
global $ID;
# Disable patterns when the page is not being translated or
# we're building instructions for original page
if (!@file_exists(metaFN($ID, '.translate')) || $DOKUTRANSLATE_NEST > 0) {
return;
}
$this->Lexer->addExitPattern('~~DOKUTRANSLATE_END~~','plugin_dokutranslate');
}
public function handle($match, $state, $pos, Doku_Handler $handler){
switch ($state) {
case DOKU_LEXER_ENTER:
case DOKU_LEXER_EXIT:
case DOKU_LEXER_SPECIAL:
return array($state, $pos, $pos + strlen($match));
}
return array($state, $match);
}
public function render($mode, Doku_Renderer $renderer, $data) {
global $DOKUTRANSLATE_NEST;
global $ID;
global $ACT;
global $TEXT;
global $REV;
# No metadata rendering
if($mode == 'metadata') {
return false;
}
# Allow exporting the page
if (substr($ACT, 0, 7) == 'export_') {
# Ignore plugin-specific markup, just let text through
if ($data[0] != DOKU_LEXER_UNMATCHED) {
return true;
}
$renderer->cdata($data[1]);
return true;
# Not exporting, allow only XHTML
} else if ($mode != 'xhtml') {
return false;
}
# Load instructions for original text on first call
if (is_null($this->origIns)) {
$DOKUTRANSLATE_NEST++;
$this->origIns = getCleanInstructions(dataPath($ID) . '/orig.txt');
$this->meta = loadTranslationMeta($ID);
$this->parCounter = 0;
$DOKUTRANSLATE_NEST--;
}
$parid = getParID();
$edithere = (in_array($ACT, array('edit', 'preview')) && $parid == $this->parCounter);
switch ($data[0]) {
# Open the table
case DOKU_LEXER_ENTER:
$renderer->doc .= '<table width="100%" class="dokutranslate"><tbody><tr>';
$cls = parReviewClass($this->meta, $this->parCounter);
# Start the cell with proper review class
if (empty($cls)) {
$renderer->doc .= '<td width="50%">';
} else {
$renderer->doc .= '<td width="50%" class="' . $cls . '">';
}
# Paragraph anchor (yes, empty named anchor is valid)
$renderer->doc .= "<a name=\"_par$this->parCounter\"></a>\n";
# Insert edit form if we're editing the first paragraph
if ($edithere) {
startEditForm($renderer);
}
break;
# Dump original text and close the row
case DOKU_LEXER_SPECIAL:
# Generate edit button
if ($ACT == 'show') {
if (empty($REV)) {
$renderer->doc .= parEditButton($this->parCounter);
}
$renderer->doc .= $this->_renderReviews($ID, $this->meta, $this->parCounter);
# Finish erasure if we're editing this paragraph
} else if ($edithere) {
endEditForm($renderer);
$renderer->doc .= $this->_renderReviews($ID, $this->meta, $this->parCounter);
}
$renderer->doc .= "</td>\n";
if (needsReview($ID, $this->meta, $this->parCounter) || $edithere) {
$renderer->doc .= '<td class="reviewme">';
} else {
$renderer->doc .= '<td>';
}
# If this condition fails, somebody's been messing
# with the data
if (current($this->origIns) !== FALSE) {
$renderer->nest(current($this->origIns));
next($this->origIns);
}
$renderer->doc .= "</td></tr>\n<tr>";
$this->parCounter++;
$cls = parReviewClass($this->meta, $this->parCounter);
# Start the cell with proper review class
if (empty($cls)) {
$renderer->doc .= '<td width="50%">';
} else {
$renderer->doc .= '<td width="50%" class="' . $cls . '">';
}
# Paragraph anchor (yes, empty named anchor is valid)
$renderer->doc .= "<a name=\"_par$this->parCounter\"></a>\n";
# Insert edit form if we're editing this paragraph
if (in_array($ACT, array('edit', 'preview')) && getParID() == $this->parCounter) {
startEditForm($renderer);
}
break;
# Dump the rest of the original text and close the table
case DOKU_LEXER_EXIT:
# Generate edit button
if ($ACT == 'show') {
if (empty($REV)) {
$renderer->doc .= parEditButton($this->parCounter);
}
$renderer->doc .= $this->_renderReviews($ID, $this->meta, $this->parCounter);
# Finish erasure if we're editing the last paragraph
} else if (in_array($ACT, array('edit', 'preview'))) {
$parid = getParID();
if ($parid == $this->parCounter) {
endEditForm($renderer);
$renderer->doc .= $this->_renderReviews($ID, $this->meta, $this->parCounter);
# Invalid paragraph ID, show form here
} else if ($parid > $this->parCounter) {
startEditForm($renderer, true);
}
}
$renderer->doc .= "</td>\n";
if (needsReview($ID, $this->meta, $this->parCounter) || $edithere) {
$renderer->doc .= '<td class="reviewme">';
} else {
$renderer->doc .= '<td>';
}
# Loop to make sure all remaining text gets dumped
# (external edit safety)
while (current($this->origIns) !== FALSE) {
$renderer->nest(current($this->origIns));
next($this->origIns);
}
$renderer->doc .= '</td></tr></tbody></table>';
break;
# Just sanitize and dump the text
default:
$renderer->cdata($data[1]);
break;
}
return true;
}
function _renderReviews($id, $meta, $parid) {
# Check for permission to write reviews
$mod = canReview($id, $meta, $parid);
# No reviews and no moderator privileges => no review block
if (!$mod && empty($meta[$parid]['reviews'])) {
return '';
}
$ret = "<div class=\"dokutranslate_review\">\n";
$ret .= '<h5>' . $this->getLang('review_header') . "</h5>\n";
$ret .= "<table>\n";
$listbox = array(
array('0', $this->getLang('trans_wrong')),
array('1', $this->getLang('trans_rephrase')),
array('2', $this->getLang('trans_accepted'))
);
# Prepare review form for current user
if ($mod) {
if (isset($meta[$parid]['reviews'][$_SERVER['REMOTE_USER']])) {
$myReview = $meta[$parid]['reviews'][$_SERVER['REMOTE_USER']];
} else {
$myReview = array('message' => '', 'quality' => 0, 'incomplete' => false);
}
$form = new Doku_Form(array());
$form->addHidden('parid', strval($parid));
$form->addHidden('do', 'dokutranslate_review');
$form->addElement(form_makeTextField('review', $myReview['message'], $this->getLang('trans_message'), '', 'nowrap', array('size' => '50')));
$form->addElement(form_makeMenuField('quality', $listbox, strval($myReview['quality']), $this->getLang('trans_quality'), '', 'nowrap'));
$args = array();
if ($myReview['incomplete']) {
$args['checked'] = 'checked';
}
$form->addElement(form_makeCheckboxField('incomplete', '1', $this->getLang('trans_incomplete'), '', 'nowrap', $args));
$form->addElement(form_makeButton('submit', '', $this->getLang('add_review')));
}
# Display all reviews for this paragraph
while (list($key, $value) = each($meta[$parid]['reviews'])) {
$ret .= '<tr><td>' . hsc($key) . '</td><td>';
# Moderators can modify their own review
if ($mod && $key == $_SERVER['REMOTE_USER']) {
$ret .= $form->getForm();
} else {
$ret .= '(' . $listbox[$value['quality']][1];
if ($value['incomplete']) {
$ret .= ', ' . $this->getLang('rend_incomplete');
}
$ret .= ') ';
$ret .= hsc($value['message']);
}
$ret .= "</td></tr>\n";
}
# Current user is a moderator who didn't write a review yet,
# display the review form at the end
if ($mod && !isset($meta[$parid]['reviews'][$_SERVER['REMOTE_USER']])) {
if (empty($meta[$parid]['reviews'])) {
$ret .= '<tr><td>';
} else {
$ret .= '<tr><td colspan="2">';
}
$ret .= $form->getForm();
$ret .= "</td></tr>\n";
}
$ret .= "</table></div>\n";
return $ret;
}
}
// vim:ts=4:sw=4:et: