]> sipb.mit.edu Git - ikiwiki.git/blob - doc/plugins/po.mdwn
po: delete translations of deleted master pages
[ikiwiki.git] / doc / plugins / po.mdwn
1 [[!template id=plugin name=po core=0 author="[[intrigeri]]"]]
2 [[!tag type/format]]
3
4 This plugin adds support for multi-lingual wikis, translated with
5 gettext, using [po4a](http://po4a.alioth.debian.org/).
6
7 It depends on the Perl `Locale::Po4a::Po` library (`apt-get install po4a`).
8
9 [[!toc levels=2]]
10
11 Introduction
12 ============
13
14 A language is chosen as the "master" one, and any other supported
15 language is a "slave" one.
16
17 A page written in the "master" language is a "master" page. It can be
18 of any page type supported by ikiwiki, except `po`. It does not have to be
19 named a special way: migration to this plugin does not imply any page
20 renaming work.
21
22 Example: `bla/page.mdwn` is a "master" Markdown page written in
23 English; if `usedirs` is enabled, it is rendered as
24 `bla/page/index.en.html`, else as `bla/page.en.html`.
25
26 Any translation of a "master" page into a "slave" language is called
27 a "slave" page; it is written in the gettext PO format. `po` is now
28 a page type supported by ikiwiki.
29
30 Example: `bla/page.fr.po` is the PO "message catalog" used to
31 translate `bla/page.mdwn` into French; if `usedirs` is enabled, it is
32 rendered as `bla/page/index.fr.html`, else as `bla/page.fr.html`
33
34
35 Configuration
36 =============
37
38 Supported languages
39 -------------------
40
41 `po_master_language` is used to set the "master" language in
42 `ikiwiki.setup`, such as:
43
44         po_master_language => { 'code' => 'en', 'name' => 'English' }
45
46 `po_slave_languages` is used to set the list of supported "slave"
47 languages, such as:
48
49         po_slave_languages => { 'fr' => 'Français',
50                                 'es' => 'Castellano',
51                                 'de' => 'Deutsch',
52         }
53
54 Decide which pages are translatable
55 -----------------------------------
56
57 The `po_translatable_pages` setting configures what pages are
58 translatable. It is a [[ikiwiki/PageSpec]], so you have lots of
59 control over what kind of pages are translatable.
60
61 The `.po` files are not considered as being translatable, so you don't need to
62 worry about excluding them explicitly from this [[ikiwiki/PageSpec]].
63
64 Internal links
65 --------------
66
67 The `po_link_to` option in `ikiwiki.setup` is used to decide how
68 internal links should be generated, depending on web server features
69 and site-specific preferences.
70
71 ### Default linking behavior
72
73 If `po_link_to` is unset, or set to `default`, ikiwiki's default
74 linking behavior is preserved: `\[[destpage]]` links to the master
75 language's page.
76
77 ### Link to current language
78
79 If `po_link_to` is set to `current`, `\[[destpage]]` links to the
80 `destpage`'s version written in the current page's language, if
81 available, *i.e.*:
82
83 - `foo/destpage/index.LL.html` if `usedirs` is enabled
84 - `foo/destpage.LL.html` if `usedirs` is disabled
85
86 ### Link to negotiated language
87
88 If `po_link_to` is set to `negotiated`, `\[[page]]` links to the
89 negotiated preferred language, *i.e.* `foo/page/`.
90
91 (In)compatibility notes:
92
93 - if `usedirs` is disabled, it does not make sense to set `po_link_to`
94   to `negotiated`; this option combination is neither implemented
95   nor allowed.
96 - if the web server does not support Content Negotiation, setting
97   `po_link_to` to `negotiated` will produce a unusable website.
98
99
100 Server support
101 ==============
102
103 Apache
104 ------
105
106 Using Apache `mod_negotiation` makes it really easy to have Apache
107 serve any page in the client's preferred language, if available.
108 This is the default Debian Apache configuration.
109
110 When `usedirs` is enabled, one has to set `DirectoryIndex index` for
111 the wiki context.
112
113 Setting `DefaultLanguage LL` (replace `LL` with your default MIME
114 language code) for the wiki context can help to ensure
115 `bla/page/index.en.html` is served as `Content-Language: LL`.
116
117 lighttpd
118 --------
119
120 lighttpd unfortunately does not support content negotiation.
121
122 **FIXME**: does `mod_magnet` provide the functionality needed to
123  emulate this?
124
125
126 Usage
127 =====
128
129 Templates
130 ---------
131
132 When `po_link_to` is not set to `negotiated`, one should replace some
133 occurrences of `BASEURL` with `HOMEPAGEURL` to get correct links to
134 the wiki homepage.
135
136 The `ISTRANSLATION` and `ISTRANSLATABLE` variables can be used to
137 display things only on translatable or translation pages.
138
139 ### Display page's versions in other languages
140
141 The `OTHERLANGUAGES` loop provides ways to display other languages'
142 versions of the same page, and the translations' status.
143
144 One typically adds the following code to `templates/page.tmpl`:
145
146         <TMPL_IF NAME="OTHERLANGUAGES">
147         <div id="otherlanguages">
148           <ul>
149           <TMPL_LOOP NAME="OTHERLANGUAGES">
150             <li>
151               <a href="<TMPL_VAR NAME="URL">"><TMPL_VAR NAME="LANGUAGE"></a>
152               <TMPL_UNLESS NAME="MASTER">
153                 (<TMPL_VAR NAME="PERCENT">&nbsp;%)
154               </TMPL_UNLESS>
155             </li>
156           </TMPL_LOOP>
157           </ul>
158         </div>
159         </TMPL_IF>
160
161 The following variables are available inside the loop (for every page in):
162
163 - `URL` - url to the page
164 - `CODE` - two-letters language code
165 - `LANGUAGE` - language name (as defined in `po_slave_languages`)
166 - `MASTER` - is true (1) if, and only if the page is a "master" page
167 - `PERCENT` - for "slave" pages, is set to the translation completeness, in percents
168
169 ### Display the current translation status
170
171 The `PERCENTTRANSLATED` variable is set to the translation
172 completeness, expressed in percent, on "slave" pages.
173
174 One can use it this way:
175
176         <TMPL_IF NAME="ISTRANSLATION">
177         <div id="percenttranslated">
178           <TMPL_VAR NAME="PERCENTTRANSLATED">
179         </div>
180         </TMPL_IF>
181
182 Additional PageSpec tests
183 -------------------------
184
185 This plugin enhances the regular [[ikiwiki/PageSpec]] syntax with some
186 additional tests that are documented [[here|ikiwiki/pagespec/po]].
187
188 Automatic PO file update
189 ------------------------
190
191 Committing changes to a "master" page:
192
193 1. updates the POT file and the PO files for the "slave" languages;
194    the updated PO files are then put under version control;
195 2. triggers a refresh of the corresponding HTML slave pages.
196
197 Also, when the plugin has just been enabled, or when a page has just
198 been declared as being translatable, the needed POT and PO files are
199 created, and the PO files are checked into version control.
200
201 Discussion pages and other sub-pages
202 ------------------------------------
203
204 Discussion should happen in the language in which the pages are
205 written for real, *i.e.* the "master" one. If discussion pages are
206 enabled, "slave" pages therefore link to the "master" page's
207 discussion page.
208
209 Likewise, "slave" pages are not supposed to have sub-pages;
210 [[WikiLinks|wikilink]] that appear on a "slave" page therefore link to
211 the master page's sub-pages.
212
213 Translating
214 -----------
215
216 One can edit the PO files using ikiwiki's CGI (a message-by-message
217 interface could also be implemented at some point).
218
219 If [[tips/untrusted_git_push]] is setup, one can edit the PO files in one's
220 preferred `$EDITOR`, without needing to be online.
221
222 TODO
223 ====
224
225 Security checks
226 ---------------
227
228 ### Security history
229
230 The only past security issues I could find in GNU gettext and po4a
231 are:
232
233 - [CVE-2004-0966](http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2004-0966),
234   *i.e.* [Debian bug #278283](http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=278283):
235   the autopoint and gettextize scripts in the GNU gettext package
236   1.14 and later versions, as used in Trustix Secure Linux 1.5
237   through 2.1 and other operating systems, allows local users to
238   overwrite files via a symlink attack on temporary files.
239 - [CVE-2007-4462](http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-4462):
240   `lib/Locale/Po4a/Po.pm` in po4a before 0.32 allows local users to
241   overwrite arbitrary files via a symlink attack on the
242   gettextization.failed.po temporary file.
243
244 **FIXME**: check whether this plugin would have been a possible attack
245 vector to exploit these vulnerabilities.
246
247 Depending on my mood, the lack of found security issues can either
248 indicate that there are none, or reveal that no-one ever bothered to
249 find (and publish) them.
250
251 ### PO file features
252
253 Can any sort of directives be put in po files that will cause mischief
254 (ie, include other files, run commands, crash gettext, whatever)?
255
256 > No [documented](http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files)
257 > directive is supposed to do so. [[--intrigeri]]
258
259 ### Running po4a on untrusted content
260
261 Are there any security issues on running po4a on untrusted content?
262
263 To say the least, this issue is not well covered, at least publicly:
264
265 - the documentation does not talk about it;
266 - grep'ing the source code for `security` or `trust` gives no answer.
267
268 On the other hand, a po4a developer answered my questions in
269 a convincing manner, stating that processing untrusted content was not
270 an initial goal, and analysing in detail the possible issues.
271
272 #### Already checked
273
274 - the core (`Po.pm`, `Transtractor.pm`) should be safe
275 - po4a source code was fully checked for other potential symlink
276   attacks, after discovery of one such issue
277 - the only external program run by the core is `diff`, in `Po.pm` (in
278   parts of its code we don't use)
279 - `Locale::gettext`: only used to display translated error messages
280 - Nicolas François "hopes" `DynaLoader` is safe, and has "no reason to
281   think that `Encode` is not safe"
282 - Nicolas François has "no reason to think that `Encode::Guess` is not
283   safe". The po plugin nevertheless avoids using it by defining the
284   input charset (`file_in_charset`) before asking `Transtractor` to
285   read any file. NB: this hack depends on po4a internals to stay
286   the same.
287
288 #### To be checked
289
290 ##### Locale::Po4a modules
291
292 The modules we want to use have to be checked, as not all are safe
293 (e.g. the LaTeX module's behaviour is changed by commands included in
294 the content); they may use regexps generated from the content.
295
296 `Chooser.pm` only loads the plugin we tell it too: currently, this
297 means the `Text` module only.
298
299 `Text` module (I checked the CVS version):
300
301 - it does not run any external program
302 - only `do_paragraph()` builds regexp's that expand untrusted
303   variables; they seem safe to me, but someone more expert than me
304   will need to check. Joey?
305
306   > Freaky code, but seems ok due to use of `quotementa`.
307
308 ##### Text::WrapI18N
309
310 `Text::WrapI18N` can cause DoS (see the
311 [Debian bug #470250](http://bugs.debian.org/470250)), but it is
312 optional and we do not need the features it provides.
313
314 It is loaded if available by `Locale::Po4a::Common`; looking at the
315 code, I'm not sure we can prevent this at all, but maybe some symbol
316 table manipulation tricks could work; overriding
317 `Locale::Po4a::Common::wrapi18n` may be easier. I'm no expert at all
318 in this field. Joey? [[--intrigeri]]
319
320 > Update: Nicolas François suggests we add an option to po4a to
321 > disable it. It would do the trick, but only for people running
322 > a brand new po4a (probably too late for Lenny). Anyway, this option
323 > would have to take effect in a `BEGIN` / `eval` that I'm not
324 > familiar with. I can learn and do it, in case no Perl wizard
325 > volunteers to provide the po4a patch. [[--intrigeri]]
326
327 >> That doesn't really need to be in a BEGIN. This patch moves it to
328 >> `import`, and makes this disable wrap18n:
329 >> `use Locale::Po4a::Common q{nowrapi18n}` --[[Joey]]
330
331 <pre>
332 --- /usr/share/perl5/Locale/Po4a/Common.pm      2008-07-21 14:54:52.000000000 -0400
333 +++ Common.pm   2008-11-11 18:27:34.000000000 -0500
334 @@ -30,8 +30,16 @@
335  use strict;
336  use warnings;
337  
338 -BEGIN {
339 -    if (eval { require Text::WrapI18N }) {
340 +sub import {
341 +    my $class=shift;
342 +    my $wrapi18n=1;
343 +    if ($_[0] eq 'nowrapi18n') {
344 +       shift;
345 +       $wrapi18n=0;
346 +    }
347 +    $class->export_to_level(1, $class, @_);
348 +
349 +    if ($wrapi18n && eval { require Text::WrapI18N }) {
350      
351          # Don't bother determining the wrap column if we cannot wrap.
352          my $col=$ENV{COLUMNS};
353 </pre>
354
355 ##### Term::ReadKey
356
357 `Term::ReadKey` is not a hard dependency in our case, *i.e.* po4a
358 works nicely without it. But the po4a Debian package recommends
359 `libterm-readkey-perl`, so it will probably be installed on most
360 systems using the po plugin.
361
362 If `$ENV{COLUMNS}` is not set, `Locale::Po4a::Common` uses
363 `Term::ReadKey::GetTerminalSize()` to get the terminal size. How safe
364 is this?
365
366 Part of `Term::ReadKey` is written in C. Depending on the runtime
367 platform, this function use ioctl, environment, or C library function
368 calls, and may end up running the `resize` command (without
369 arguments).
370
371 IMHO, using Term::ReadKey has too far reaching implications for us to
372 be able to guarantee anything wrt. security. Since it is anyway of no
373 use in our case, I suggest we define `ENV{COLUMNS}` before loading
374 `Locale::Po4a::Common`, just to be on the safe side. Joey?
375 [[--intrigeri]]
376
377 > Update: adding an option to disable `Text::WrapI18N`, as Nicolas
378 > François suggested, would as a bonus disable `Term::ReadKey`
379 > as well. [[--intrigeri]]
380
381 ### msgmerge
382
383 `refreshpofiles()` runs this external program. A po4a developer
384 answered he does "not expect any security issues from it".
385
386 ### Fuzzing input
387
388 I was not able to find any public information about gettext or po4a
389 having been tested with a fuzzing program, such as `zzuf` or `fusil`.
390 Moreover, some gettext parsers seem to be quite
391 [easy to crash](http://fusil.hachoir.org/trac/browser/trunk/fuzzers/fusil-gettext),
392 so it might be useful to bang msgmerge/po4a's heads against such
393 a program in order to easily detect some of the most obvious DoS.
394 [[--intrigeri]]
395
396 > po4a was not fuzzy-tested, but according to one of its developers,
397 > "it would be really appreciated". [[--intrigeri]]
398
399 Test conditions:
400
401 - a 21M file containing 100 concatenated copies of all the files in my
402   `/usr/share/common-licenses/`; I had no existing PO file or
403   translated versions at hand, which renders these tests
404   quite incomplete.
405 - po4a was the Debian 0.34-2 package; the same tests were also run
406   after replacing the `Text` module with the CVS one (the core was not
407   changed in CVS since 0.34-2 was released), without any significant
408   difference in the results.
409 - Perl 5.10.0-16
410
411 #### po4a-gettextize
412
413 `po4a-gettextize` uses more or less the same po4a features as our
414 `refreshpot` function.
415
416 Without specifying an input charset, zzuf'ed `po4a-gettextize` quickly
417 errors out, complaining it was not able to detect the input charset;
418 it leaves no incomplete file on disk.
419
420 So I had to pretend the input was in UTF-8, as does the po plugin.
421
422 Two ways of crashing were revealed by this command-line:
423
424         zzuf -vc -s 0:100 -r 0.1:0.5 \
425           po4a-gettextize -f text -o markdown -M utf-8 -L utf-8 \
426             -m LICENSES >/dev/null
427
428 They are:
429
430         Malformed UTF-8 character (UTF-16 surrogate 0xdcc9) in substitution iterator at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
431         Malformed UTF-8 character (fatal) at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
432
433 and
434
435         Malformed UTF-8 character (UTF-16 surrogate 0xdcec) in substitution (s///) at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
436         Malformed UTF-8 character (fatal) at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
437
438 Perl seems to exit cleanly, and an incomplete PO file is written on
439 disk. I not sure whether if this is a bug in Perl or in `Po.pm`.
440
441 > It's fairly standard perl behavior when fed malformed utf-8. As long as it doesn't
442 > crash ikiwiki, it's probably acceptable. Ikiwiki can do some similar things itself when fed malformed utf-8 (doesn't crash tho) --[[Joey]]
443
444 #### po4a-translate
445
446 `po4a-translate` uses more or less the same po4a features as our
447 `filter` function.
448
449 Without specifying an input charset, same behaviour as
450 `po4a-gettextize`, so let's specify UTF-8 as input charset as of now.
451
452         zzuf -cv \
453           po4a-translate -d -f text -o markdown -M utf-8 -L utf-8 \
454             -k 0 -m LICENSES -p LICENSES.fr.po -l test.fr
455
456 ... prints tons of occurences of the following error, but a complete
457 translated document is written (obviously with some weird chars
458 inside):
459
460         Use of uninitialized value in string ne at /usr/share/perl5/Locale/Po4a/TransTractor.pm line 854.
461         Use of uninitialized value in string ne at /usr/share/perl5/Locale/Po4a/TransTractor.pm line 840.
462         Use of uninitialized value in pattern match (m//) at /usr/share/perl5/Locale/Po4a/Po.pm line 1002.
463
464 While:
465
466         zzuf -cv -s 0:10 -r 0.001:0.3 \
467           po4a-translate -d -f text -o markdown -M utf-8 -L utf-8 \
468             -k 0 -m LICENSES -p LICENSES.fr.po -l test.fr
469
470 ... seems to lose the fight, at the `readpo(LICENSES.fr.po)` step,
471 against some kind of infinite loop, deadlock, or any similar beast.
472 It does not seem to eat memory, though.
473
474 Whatever format module is used does not change anything. This is thus
475 probably a bug in po4a's core or in a lib it depends on.
476
477 The sub `read`, in `TransTractor.pm`, seems to be a good debugging
478 starting point.
479
480 #### msgmerge
481
482 `msgmerge` is run in our `refreshpofiles` function. I did not manage
483 to crash it with `zzuf`.
484
485 gettext/po4a rough corners
486 --------------------------
487
488 - fix infinite loop when synchronizing two ikiwiki (when checkouts
489   live in different directories): say bla.fr.po has been updated in
490   repo2; pulling repo2 from repo1 seems to trigger a PO update, that
491   changes bla.fr.po in repo1; then pushing repo1 to repo2 triggers
492   a PO update, that changes bla.fr.po in repo2; etc.; quickly fixed in
493   `629968fc89bced6727981c0a1138072631751fee`, by disabling references
494   in Pot files. Using `Locale::Po4a::write_if_needed` might be
495   a cleaner solution. (warning: this function runs the external
496   `diff` program, have to check security)
497 - new translations created in the web interface must get proper
498   charset/encoding gettext metadata, else the next automatic PO update
499   removes any non-ascii chars; possible solution: put such metadata
500   into the Pot file, and let it propagate; should be fixed in
501   `773de05a7a1ee68d2bed173367cf5e716884945a`, time will tell.
502
503 Better links
504 ------------
505
506 ### Page title in links
507
508 To use the page titles set with the [[meta|plugins/meta]] plugin when
509 rendering links would be very much nicer, than the current
510 "filename.LL" format. This is actually a duplicate for
511 [[bugs/pagetitle_function_does_not_respect_meta_titles]].
512
513 Going to work on this in my `meta` branch.
514
515 ### Translation status in links
516
517 See [[contrib/po]].
518
519 ### Backlinks
520
521 They are not updated when the source page changes (e.g. meta title).
522
523 ### Redirect after edit
524
525 After editing master page, when using `po_link_to=negotiated`, one is
526 redirected to the page in her preferred language. Would be slightly
527 better to be redirected, in any case, to the just saved page.
528
529 Page formats
530 ------------
531
532 Markdown is well supported, great, but what about others?
533
534 The [[po|plugins/po]] uses `Locale::Po4a::Text` for every page format;
535 this can be expected to work out of the box with most other wiki-like
536 formats supported by ikiwiki. Some of their ad-hoc syntax might be
537 parsed in a strange way, but the worst problems I can imagine would be
538 wrapping issues; e.g. there is code in po4a dedicated to prevent
539 re-wrapping the underlined Markdown headers.
540
541 While it would be easy to better support formats such as [[html]] or
542 LaTeX, by using for each one the dedicated po4a module, this can be
543 problematic from a security point of view.
544
545 **TODO**: test the more popular formats and write proper documentation
546 about it.
547
548 Translation quality assurance
549 -----------------------------
550
551 Modifying a PO file via the CGI must be forbidden if the new version
552 is not a valid PO file. As a bonus, check that it provides a more
553 complete translation than the existing one.
554
555 A new `cansave` type of hook would be needed to implement this.
556
557 Note: committing to the underlying repository is a way to bypass
558 this check.
559
560 Creating new pages on the web
561 -----------------------------
562
563 See [[contrib/po|contrib/po]].
564
565 Renaming pages
566 --------------
567
568 - Renaming a translation should be forbidden.
569
570 Robustness tests
571 ----------------
572
573 ### Disabling the plugin
574
575 - enabling the plugin with `po_translatable_pages` set
576 - enabling the plugin without `po_translatable_pages` set: **OK**
577 - disabling the plugin: **OK**
578
579 ### Changing the plugin config
580
581 - adding existing pages to `po_translatable_pages`: **OK**
582 - removing existing pages from `po_translatable_pages`: **OK**
583 - adding a language to `po_slave_languages`: **OK**
584 - removing a language from `po_slave_languages`: **OK**
585 - changing `po_master_language`: **OK**
586 - replacing `po_master_language` with a language previously part of
587   `po_slave_languages`: needs two rebuilds, but **OK** (this is quite
588   a perverse test actually)
589
590 ### Creating pages
591
592 - creating a master page via RCS: **OK**
593 - creating a master page via CGI: **OK**
594
595 ### Deleting pages
596
597 - removing a master page via RCS: **OK**
598 - removing a translation via RCS: **OK**
599 - removing a master page via CGI: **OK**
600 - removing a translation via CGI: **OK**
601
602 ### Renaming pages
603
604 - renaming a master page via RCS: **OK** (but the old translations
605   are lost, because not all RCS track file renaming)
606 - renaming a master page and its translations via RCS: **OK**
607 - renaming a master page via CGI: **OK**
608 - renaming a translation via RCS
609 - renaming a translation via CGI
610
611 ### Misc
612
613 - general test with `usedirs` disabled: **OK**
614 - general test with `indexpages` enabled
615 - general test with `po_link_to=default`
616
617 Documentation
618 -------------
619
620 Maybe write separate documentation depending on the people it targets:
621 translators, wiki administrators, hackers. This plugin may be complex
622 enough to deserve this.