]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/plugins/po.mdwn
2f359bb8389e00ca318978c69e445e37e288e4a4
[git.ikiwiki.info.git] / doc / plugins / po.mdwn
1 [[!template id=plugin name=po core=0 author="[[intrigeri]]"]]
2 [[!tag type/format]]
4 This plugin adds support for multi-lingual wikis, translated with
5 gettext, using [po4a](http://po4a.alioth.debian.org/).
7 It depends on the Perl `Locale::Po4a::Po` library (`apt-get install po4a`).
9 [[!toc levels=2]]
11 Introduction
12 ============
14 A language is chosen as the "master" one, and any other supported
15 language is a "slave" one.
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.
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`.
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.
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`
35 Configuration
36 =============
38 Supported languages
39 -------------------
41 `po_master_language` is used to set the "master" language in
42 `ikiwiki.setup`, such as:
44         po_master_language => { 'code' => 'en', 'name' => 'English' }
46 `po_slave_languages` is used to set the list of supported "slave"
47 languages, such as:
49         po_slave_languages => { 'fr' => 'Français',
50                                 'es' => 'Castellano',
51                                 'de' => 'Deutsch',
52         }
54 Decide which pages are translatable
55 -----------------------------------
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.
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]].
64 Internal links
65 --------------
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.
71 ### Default linking behavior
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.
77 ### Link to current language
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.*:
83 - `foo/destpage/index.LL.html` if `usedirs` is enabled
84 - `foo/destpage.LL.html` if `usedirs` is disabled
86 ### Link to negotiated language
88 If `po_link_to` is set to `negotiated`, `\[[page]]` links to the
89 negotiated preferred language, *i.e.* `foo/page/`.
91 (In)compatibility notes:
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.
100 Server support
101 ==============
103 Apache
104 ------
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.
110 When `usedirs` is enabled, one has to set `DirectoryIndex index` for
111 the wiki context.
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`.
117 lighttpd
118 --------
120 lighttpd unfortunately does not support content negotiation.
122 **FIXME**: does `mod_magnet` provide the functionality needed to
123  emulate this?
126 Usage
127 =====
129 Templates
130 ---------
132 The `ISTRANSLATION` and `ISTRANSLATABLE` variables can be used to
133 display things only on translatable or translation pages.
135 ### Display page's versions in other languages
137 The `OTHERLANGUAGES` loop provides ways to display other languages'
138 versions of the same page, and the translations' status.
140 One typically adds the following code to `templates/page.tmpl`:
142         <TMPL_IF NAME="OTHERLANGUAGES">
143         <div id="otherlanguages">
144           <ul>
145           <TMPL_LOOP NAME="OTHERLANGUAGES">
146             <li>
147               <a href="<TMPL_VAR NAME="URL">"><TMPL_VAR NAME="LANGUAGE"></a>
148               <TMPL_UNLESS NAME="MASTER">
149                 (<TMPL_VAR NAME="PERCENT">&nbsp;%)
150               </TMPL_UNLESS>
151             </li>
152           </TMPL_LOOP>
153           </ul>
154         </div>
155         </TMPL_IF>
157 The following variables are available inside the loop (for every page in):
159 - `URL` - url to the page
160 - `CODE` - two-letters language code
161 - `LANGUAGE` - language name (as defined in `po_slave_languages`)
162 - `MASTER` - is true (1) if, and only if the page is a "master" page
163 - `PERCENT` - for "slave" pages, is set to the translation completeness, in percents
165 ### Display the current translation status
167 The `PERCENTTRANSLATED` variable is set to the translation
168 completeness, expressed in percent, on "slave" pages.
170 One can use it this way:
172         <TMPL_IF NAME="ISTRANSLATION">
173         <div id="percenttranslated">
174           <TMPL_VAR NAME="PERCENTTRANSLATED">
175         </div>
176         </TMPL_IF>
178 Additional PageSpec tests
179 -------------------------
181 This plugin enhances the regular [[ikiwiki/PageSpec]] syntax with some
182 additional tests that are documented [[here|ikiwiki/pagespec/po]].
184 Automatic PO file update
185 ------------------------
187 Committing changes to a "master" page:
189 1. updates the POT file and the PO files for the "slave" languages;
190    the updated PO files are then put under version control;
191 2. triggers a refresh of the corresponding HTML slave pages.
193 Also, when the plugin has just been enabled, or when a page has just
194 been declared as being translatable, the needed POT and PO files are
195 created, and the PO files are checked into version control.
197 Discussion pages
198 ----------------
200 Discussion should happen in the language in which the pages are
201 written for real, *i.e.* the "master" one. If discussion pages are
202 enabled, "slave" pages therefore link to the "master" page's
203 discussion page.
205 Translating
206 -----------
208 One can edit the PO files using ikiwiki's CGI (a message-by-message
209 interface could also be implemented at some point).
211 If [[tips/untrusted_git_push]] is setup, one can edit the PO files in one's
212 preferred `$EDITOR`, without needing to be online.
214 TODO
215 ====
217 Security checks
218 ---------------
220 ### Security history
222 The only past security issues I could find in GNU gettext and po4a
223 are:
225 - [CVE-2004-0966](http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2004-0966),
226   *i.e.* [Debian bug #278283](http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=278283):
227   the autopoint and gettextize scripts in the GNU gettext package
228   1.14 and later versions, as used in Trustix Secure Linux 1.5
229   through 2.1 and other operating systems, allows local users to
230   overwrite files via a symlink attack on temporary files.
231 - [CVE-2007-4462](http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-4462):
232   `lib/Locale/Po4a/Po.pm` in po4a before 0.32 allows local users to
233   overwrite arbitrary files via a symlink attack on the
234   gettextization.failed.po temporary file.
236 **FIXME**: check whether this plugin would have been a possible attack
237 vector to exploit these vulnerabilities.
239 Depending on my mood, the lack of found security issues can either
240 indicate that there are none, or reveal that no-one ever bothered to
241 find (and publish) them.
243 ### PO file features
245 Can any sort of directives be put in po files that will cause mischief
246 (ie, include other files, run commands, crash gettext, whatever)?
248 > No [documented](http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files)
249 > directive is supposed to do so. [[--intrigeri]]
251 ### Running po4a on untrusted content
253 Are there any security issues on running po4a on untrusted content?
255 To say the least, this issue is not well covered, at least publicly:
257 - the documentation does not talk about it;
258 - grep'ing the source code for `security` or `trust` gives no answer.
260 On the other hand, a po4a developer answered my questions in
261 a convincing manner, stating that processing untrusted content was not
262 an initial goal, and analysing in detail the possible issues.
264 #### Already checked
266 - the core (`Po.pm`, `Transtractor.pm`) should be safe
267 - po4a source code was fully checked for other potential symlink
268   attacks, after discovery of one such issue
269 - the only external program run by the core is `diff`, in `Po.pm` (in
270   parts of its code we don't use)
271 - `Locale::gettext`: only used to display translated error messages
272 - Nicolas François "hopes" `DynaLoader` is safe, and has "no reason to
273   think that `Encode` is not safe"
274 - Nicolas François has "no reason to think that `Encode::Guess` is not
275   safe". The po plugin nevertheless avoids using it by defining the
276   input charset (`file_in_charset`) before asking `Transtractor` to
277   read any file. NB: this hack depends on po4a internals to stay
278   the same.
280 #### To be checked
282 ##### Locale::Po4a modules
284 The modules we want to use have to be checked, as not all are safe
285 (e.g. the LaTeX module's behaviour is changed by commands included in
286 the content); they may use regexps generated from the content.
288 `Chooser.pm` only loads the plugin we tell it too: currently, this
289 means the `Text` module only.
291 `Text` module (I checked the CVS version):
293 - it does not run any external program
294 - only `do_paragraph()` builds regexp's that expand untrusted
295   variables; they seem safe to me, but someone more expert than me
296   will need to check. Joey?
298 ##### Text::WrapI18N
300 `Text::WrapI18N` can cause DoS (see the
301 [Debian bug #470250](http://bugs.debian.org/470250)), but it is
302 optional and we do not need the features it provides.
304 It is loaded if available by `Locale::Po4a::Common`; looking at the
305 code, I'm not sure we can prevent this at all, but maybe some symbol
306 table manipulation tricks could work; overriding
307 `Locale::Po4a::Common::wrapi18n` may be easier. I'm no expert at all
308 in this field. Joey? [[--intrigeri]]
310 > Update: Nicolas François suggests we add an option to po4a to
311 > disable it. It would do the trick, but only for people running
312 > a brand new po4a (probably too late for Lenny). Anyway, this option
313 > would have to take effect in a `BEGIN` / `eval` that I'm not
314 > familiar with. I can learn and do it, in case no Perl wizard
315 > volunteers to provide the po4a patch. [[--intrigeri]]
317 ##### Term::ReadKey
319 `Term::ReadKey` is not a hard dependency in our case, *i.e.* po4a
320 works nicely without it. But the po4a Debian package recommends
321 `libterm-readkey-perl`, so it will probably be installed on most
322 systems using the po plugin.
324 If `$ENV{COLUMNS}` is not set, `Locale::Po4a::Common` uses
325 `Term::ReadKey::GetTerminalSize()` to get the terminal size. How safe
326 is this?
328 Part of `Term::ReadKey` is written in C. Depending on the runtime
329 platform, this function use ioctl, environment, or C library function
330 calls, and may end up running the `resize` command (without
331 arguments).
333 IMHO, using Term::ReadKey has too far reaching implications for us to
334 be able to guarantee anything wrt. security. Since it is anyway of no
335 use in our case, I suggest we define `ENV{COLUMNS}` before loading
336 `Locale::Po4a::Common`, just to be on the safe side. Joey?
337 [[--intrigeri]]
339 > Update: adding an option to disable `Text::WrapI18N`, as Nicolas
340 > François suggested, would as a bonus disable `Term::ReadKey`
341 > as well. [[--intrigeri]]
343 ### msgmerge
345 `refreshpofiles()` runs this external program. A po4a developer
346 answered he does "not expect any security issues from it".
348 ### Fuzzing input
350 I was not able to find any public information about gettext or po4a
351 having been tested with a fuzzing program, such as `zzuf` or `fusil`.
352 Moreover, some gettext parsers seem to be quite
353 [easy to crash](http://fusil.hachoir.org/trac/browser/trunk/fuzzers/fusil-gettext),
354 so it might be useful to bang msgmerge/po4a's heads against such
355 a program in order to easily detect some of the most obvious DoS.
356 [[--intrigeri]]
358 > po4a was not fuzzy-tested, but according to one of its developers,
359 > "it would be really appreciated". [[--intrigeri]]
361 Test conditions:
363 - a 21M file containing 100 concatenated copies of all the files in my
364   `/usr/share/common-licenses/`; I had no existing PO file or
365   translated versions at hand, which renders these tests
366   quite incomplete.
367 - po4a was the Debian 0.34-2 package; the same tests were also run
368   after replacing the `Text` module with the CVS one (the core was not
369   changed in CVS since 0.34-2 was released), without any significant
370   difference in the results.
371 - Perl 5.10.0-16
373 #### po4a-gettextize
375 `po4a-gettextize` uses more or less the same po4a features as our
376 `refreshpot` function.
378 Without specifying an input charset, zzuf'ed `po4a-gettextize` quickly
379 errors out, complaining it was not able to detect the input charset;
380 it leaves no incomplete file on disk.
382 So I had to pretend the input was in UTF-8, as does the po plugin.
384 Two ways of crashing were revealed by this command-line:
386         zzuf -vc -s 0:100 -r 0.1:0.5 \
387           po4a-gettextize -f text -o markdown -M utf-8 -L utf-8 \
388             -m LICENSES >/dev/null
390 They are:
392         Malformed UTF-8 character (UTF-16 surrogate 0xdcc9) in substitution iterator at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
393         Malformed UTF-8 character (fatal) at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
395 and
397         Malformed UTF-8 character (UTF-16 surrogate 0xdcec) in substitution (s///) at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
398         Malformed UTF-8 character (fatal) at /usr/share/perl5/Locale/Po4a/Po.pm line 1443.
400 Perl seems to exit cleanly, and an incomplete PO file is written on
401 disk. I not sure whether if this is a bug in Perl or in `Po.pm`.
403 #### po4a-translate
405 `po4a-translate` uses more or less the same po4a features as our
406 `filter` function.
408 Without specifying an input charset, same behaviour as
409 `po4a-gettextize`, so let's specify UTF-8 as input charset as of now.
411         zzuf -cv \
412           po4a-translate -d -f text -o markdown -M utf-8 -L utf-8 \
413             -k 0 -m LICENSES -p LICENSES.fr.po -l test.fr
415 ... prints tons of occurences of the following error, but a complete
416 translated document is written (obviously with some weird chars
417 inside):
419         Use of uninitialized value in string ne at /usr/share/perl5/Locale/Po4a/TransTractor.pm line 854.
420         Use of uninitialized value in string ne at /usr/share/perl5/Locale/Po4a/TransTractor.pm line 840.
421         Use of uninitialized value in pattern match (m//) at /usr/share/perl5/Locale/Po4a/Po.pm line 1002.
423 While:
425         zzuf -cv -s 0:10 -r 0.001:0.3 \
426           po4a-translate -d -f text -o markdown -M utf-8 -L utf-8 \
427             -k 0 -m LICENSES -p LICENSES.fr.po -l test.fr
429 ... seems to lose the fight, at the `readpo(LICENSES.fr.po)` step,
430 against some kind of infinite loop, deadlock, or any similar beast.
431 It does not seem to eat memory, though.
433 Whatever format module is used does not change anything. This is thus
434 probably a bug in po4a's core or in a lib it depends on.
436 The sub `read`, in `TransTractor.pm`, seems to be a good debugging
437 starting point.
439 #### msgmerge
441 `msgmerge` is run in our `refreshpofiles` function. I did not manage
442 to crash it with `zzuf`.
444 gettext/po4a rough corners
445 --------------------------
447 - fix infinite loop when synchronizing two ikiwiki (when checkouts
448   live in different directories): say bla.fr.po has been updated in
449   repo2; pulling repo2 from repo1 seems to trigger a PO update, that
450   changes bla.fr.po in repo1; then pushing repo1 to repo2 triggers
451   a PO update, that changes bla.fr.po in repo2; etc.; quickly fixed in
452   `629968fc89bced6727981c0a1138072631751fee`, by disabling references
453   in Pot files. Using `Locale::Po4a::write_if_needed` might be
454   a cleaner solution. (warning: this function runs the external
455   `diff` program, have to check security)
456 - new translations created in the web interface must get proper
457   charset/encoding gettext metadata, else the next automatic PO update
458   removes any non-ascii chars; possible solution: put such metadata
459   into the Pot file, and let it propagate; should be fixed in
460   `773de05a7a1ee68d2bed173367cf5e716884945a`, time will tell.
462 When committing a translatable page to the repository, msgmerge
463 happens and then:
465         Use of uninitialized value $page in pattern match (m//) at /usr/local/share/perl/5.10.0/IkiWiki.pm line 624.
466         Use of uninitialized value $p in hash element at /usr/local/share/perl/5.10.0/IkiWiki/Render.pm line 423.
467         Use of uninitialized value $file in string eq at /usr/local/share/perl/5.10.0/IkiWiki/Render.pm line 444.
468         Use of uninitialized value $page in pattern match (m//) at /usr/local/share/perl/5.10.0/IkiWiki.pm line 624.
470 And then tons of:
471         Use of uninitialized value $page in pattern match (m//) at /usr/local/share/perl/5.10.0/IkiWiki.pm line 1860.
472         Use of uninitialized value $page in concatenation (.) or string at /usr/local/share/perl/5.10.0/IkiWiki.pm line 1869.
474 Page titles in links
475 --------------------
477 To use the page titles set with the [meta](plugins/meta) plugin when
478 rendering links would be very much nicer, than the current
479 "filename.LL" format. This is actually a duplicate for
480 [[bugs/pagetitle_function_does_not_respect_meta_titles]].
482 Page formats
483 ------------
485 Markdown is well supported, great, but what about others?
487 The [po](plugins/po) uses `Locale::Po4a::Text` for every page format;
488 this can be expected to work out of the box with most other wiki-like
489 formats supported by ikiwiki. Some of their ad-hoc syntax might be
490 parsed in a strange way, but the worst problems I can imagine would be
491 wrapping issues; e.g. there is code in po4a dedicated to prevent
492 re-wrapping the underlined Markdown headers.
494 While it would be easy to better support formats such as [[html]] or
495 LaTeX, by using for each one the dedicated po4a module, this can be
496 problematic from a security point of view.
498 **TODO**: test the more popular formats and write proper documentation
499 about it.
501 Translation quality assurance
502 -----------------------------
504 Modifying a PO file via the CGI must be forbidden if the new version
505 is not a valid PO file. As a bonus, check that it provides a more
506 complete translation than the existing one.
508 A new `cansave` type of hook would be needed to implement this.
510 Note: committing to the underlying repository is a way to bypass
511 this check.
513 Broken links
514 ------------
516 See [[contrib/po]].
518 Documentation
519 -------------
521 Maybe write separate documentation depending on the people it targets:
522 translators, wiki administrators, hackers. This plugin is maybe
523 complex enough to deserve this.