]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/po.pm
disable istranslatable memoization
[git.ikiwiki.info.git] / IkiWiki / Plugin / po.pm
1 #!/usr/bin/perl
2 # .po as a wiki page type
3 # inspired by the GPL'd po4a-translate,
4 # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
5 package IkiWiki::Plugin::po;
7 use warnings;
8 use strict;
9 use IkiWiki 2.00;
10 use Encode;
11 use Locale::Po4a::Chooser;
12 use Locale::Po4a::Po;
13 use File::Basename;
14 use File::Copy;
15 use File::Spec;
16 use File::Temp;
17 use Memoize;
19 my %translations;
20 our %filtered;
21 ## FIXME: makes some test cases cry once every two tries; this may be
22 ## related to the artificial way the testsuite is run, or not.
23 # memoize("istranslatable");
24 memoize("_istranslation");
25 memoize("percenttranslated");
27 sub import {
28         hook(type => "getsetup", id => "po", call => \&getsetup);
29         hook(type => "checkconfig", id => "po", call => \&checkconfig);
30         hook(type => "needsbuild", id => "po", call => \&needsbuild);
31         hook(type => "targetpage", id => "po", call => \&targetpage);
32         hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
33         hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
34         hook(type => "filter", id => "po", call => \&filter);
35         hook(type => "htmlize", id => "po", call => \&htmlize);
36         hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
37 }
39 sub getsetup () { #{{{
40         return
41                 plugin => {
42                         safe => 0,
43                         rebuild => 1, # format plugin
44                 },
45                 po_master_language => {
46                         type => "string",
47                         example => {
48                                 'code' => 'en',
49                                 'name' => 'English'
50                         },
51                         description => "master language (non-PO files)",
52                         safe => 0,
53                         rebuild => 1,
54                 },
55                 po_slave_languages => {
56                         type => "string",
57                         example => {
58                                 'fr' => 'Français',
59                                 'es' => 'Castellano',
60                                 'de' => 'Deutsch'
61                         },
62                         description => "slave languages (PO files)",
63                         safe => 0,
64                         rebuild => 1,
65                 },
66                 po_translatable_pages => {
67                         type => "pagespec",
68                         example => "!*/Discussion",
69                         description => "PageSpec controlling which pages are translatable",
70                         link => "ikiwiki/PageSpec",
71                         safe => 0,
72                         rebuild => 1,
73                 },
74                 po_link_to => {
75                         type => "string",
76                         example => "current",
77                         description => "internal linking behavior (default/current/negotiated)",
78                         safe => 0,
79                         rebuild => 1,
80                 },
81 } #}}}
83 sub checkconfig () { #{{{
84         foreach my $field (qw{po_master_language po_slave_languages}) {
85                 if (! exists $config{$field} || ! defined $config{$field}) {
86                         error(sprintf(gettext("Must specify %s"), $field));
87                 }
88         }
89         if (! exists $config{po_link_to} ||
90             ! defined $config{po_link_to}) {
91             $config{po_link_to}="default";
92         }
93         if (! exists $config{po_translatable_pages} ||
94             ! defined $config{po_translatable_pages}) {
95             $config{po_translatable_pages}="";
96         }
97         if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
98                 error(gettext("po_link_to=negotiated requires usedirs to be set"));
99         }
100         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
101 } #}}}
103 sub potfile ($) { #{{{
104         my $masterfile=shift;
105         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
106         return File::Spec->catfile($dir, $name . ".pot");
107 } #}}}
109 sub pofile ($$) { #{{{
110         my $masterfile=shift;
111         my $lang=shift;
112         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
113         return File::Spec->catfile($dir, $name . "." . $lang . ".po");
114 } #}}}
116 sub refreshpot ($) { #{{{
117         my $masterfile=shift;
118         my $potfile=potfile($masterfile);
119         my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
120         my $doc=Locale::Po4a::Chooser::new('text',%options);
121         $doc->read($masterfile);
122         $doc->{TT}{utf_mode} = 1;
123         $doc->{TT}{file_in_charset} = 'utf-8';
124         $doc->{TT}{file_out_charset} = 'utf-8';
125         # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
126         # this is undocument use of internal Locale::Po4a::TransTractor's data,
127         # compulsory since this module prevents us from using the porefs option.
128         my %po_options = ('porefs' => 'none');
129         $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
130         # do the actual work
131         $doc->parse;
132         $doc->writepo($potfile);
133 } #}}}
135 sub refreshpofiles ($@) { #{{{
136         my $masterfile=shift;
137         my @pofiles=@_;
139         my $potfile=potfile($masterfile);
140         error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
142         foreach my $pofile (@pofiles) {
143                 if (-e $pofile) {
144                         my $cmd = "msgmerge -U --backup=none $pofile $potfile";
145                         system ($cmd) == 0
146                                 or error("[po/refreshpofiles:$pofile] failed to update");
147                 }
148                 else {
149                         File::Copy::syscopy($potfile,$pofile)
150                                 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
151                 }
152         }
153 } #}}}
155 sub needsbuild () { #{{{
156         my $needsbuild=shift;
158         # build %translations, using istranslation's side-effect
159         foreach my $page (keys %pagesources) {
160                 istranslation($page);
161         }
163         # refresh/create POT and PO files as needed
164         my $updated_po_files=0;
165         foreach my $page (keys %pagesources) {
166                 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
167                 if (istranslatable($page)) {
168                         my $file=srcfile($pagesources{$page});
169                         if ($pageneedsbuild || ! -e potfile($file)) {
170                                 refreshpot($file);
171                         }
172                         my @pofiles;
173                         foreach my $lang (keys %{$config{po_slave_languages}}) {
174                                 my $pofile=pofile($file, $lang);
175                                 if ($pageneedsbuild || ! -e $pofile) {
176                                         push @pofiles, $pofile;
177                                 }
178                         }
179                         if (@pofiles) {
180                                 refreshpofiles($file, @pofiles) ;
181                                 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
182                                 $updated_po_files = 1;
183                         }
184                 }
185         }
187         # check staged changes in and trigger a wiki refresh.
188         if ($updated_po_files) {
189                 if ($config{rcs}) {
190                         IkiWiki::disable_commit_hook();
191                         IkiWiki::rcs_commit_staged(gettext("updated PO files"),
192                                 "refreshpofiles", "127.0.0.1");
193                         IkiWiki::enable_commit_hook();
194                         IkiWiki::rcs_update();
195                 }
196                 IkiWiki::refresh();
197                 IkiWiki::saveindex();
198                 # refresh module's private variables
199                 %filtered=undef;
200                 %translations=undef;
201                 foreach my $page (keys %pagesources) {
202                         istranslation($page);
203                 }
204         }
207         # make existing translations depend on the corresponding master page
208         foreach my $master (keys %translations) {
209                 foreach my $slave (values %{$translations{$master}}) {
210                         add_depends($slave, $master);
211                 }
212         }
213 } #}}}
215 sub targetpage (@) { #{{{
216         my %params = @_;
217         my $page=$params{page};
218         my $ext=$params{ext};
220         if (istranslation($page)) {
221                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
222                 if (! $config{usedirs} || $page eq 'index') {
223                         return $masterpage . "." . $lang . "." . $ext;
224                 }
225                 else {
226                         return $masterpage . "/index." . $lang . "." . $ext;
227                 }
228         }
229         elsif (istranslatable($page)) {
230                 if (! $config{usedirs} || $page eq 'index') {
231                         return $page . "." . $config{po_master_language}{code} . "." . $ext;
232                 }
233                 else {
234                         return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
235                 }
236         }
237         return;
238 } #}}}
240 sub tweakurlpath ($) { #{{{
241         my %params = @_;
242         my $url=$params{url};
243         if ($config{po_link_to} eq "negotiated") {
244                 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
245         }
246         return $url;
247 } #}}}
249 sub tweakbestlink ($$) { #{{{
250         my %params = @_;
251         my $page=$params{page};
252         my $link=$params{link};
253         if ($config{po_link_to} eq "current"
254             && istranslatable($link)
255             && istranslation($page)) {
256                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
257                 return $link . "." . $curlang;
258         }
259         return $link;
260 } #}}}
262 # We use filter to convert PO to the master page's type,
263 # since other plugins should not work on PO files
264 sub filter (@) { #{{{
265         my %params = @_;
266         my $page = $params{page};
267         my $destpage = $params{destpage};
268         my $content = decode_utf8(encode_utf8($params{content}));
270         # decide if this is a PO file that should be converted into a translated document,
271         # and perform various sanity checks
272         if (! istranslation($page) || $filtered{$page}{$destpage}) {
273                 return $content;
274         }
276         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
277         my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
278         my $masterfile = srcfile($pagesources{$masterpage});
279         my (@pos,@masters);
280         push @pos,$file;
281         push @masters,$masterfile;
282         my %options = (
283                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
284                         );
285         my $doc=Locale::Po4a::Chooser::new('text',%options);
286         $doc->process(
287                 'po_in_name'    => \@pos,
288                 'file_in_name'  => \@masters,
289                 'file_in_charset'  => 'utf-8',
290                 'file_out_charset' => 'utf-8',
291         ) or error("[po/filter:$file]: failed to translate");
292         my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
293         my $tmpout = $tmpfh->filename;
294         $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
295         $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
296         $filtered{$page}{$destpage}=1;
297         return $content;
298 } #}}}
300 sub htmlize (@) { #{{{
301         my %params=@_;
302         my $page = $params{page};
303         my $content = $params{content};
304         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
305         my $masterfile = srcfile($pagesources{$masterpage});
307         # force content to be htmlize'd as if it was the same type as the master page
308         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
309 } #}}}
311 sub percenttranslated ($) { #{{{
312         my $page=shift;
313         return "N/A" unless (istranslation($page));
314         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
315         my $file=srcfile($pagesources{$page});
316         my $masterfile = srcfile($pagesources{$masterpage});
317         my (@pos,@masters);
318         push @pos,$file;
319         push @masters,$masterfile;
320         my %options = (
321                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
322                         );
323         my $doc=Locale::Po4a::Chooser::new('text',%options);
324         $doc->process(
325                 'po_in_name'    => \@pos,
326                 'file_in_name'  => \@masters,
327                 'file_in_charset'  => 'utf-8',
328                 'file_out_charset' => 'utf-8',
329         ) or error("[po/percenttranslated:$file]: failed to translate");
330         my ($percent,$hit,$queries) = $doc->stats();
331         return $percent;
332 } #}}}
334 sub otherlanguages ($) { #{{{
335         my $page=shift;
336         my @ret;
337         if (istranslatable($page)) {
338                 foreach my $lang (sort keys %{$translations{$page}}) {
339                         my $translation = $translations{$page}{$lang};
340                         push @ret, {
341                                 url => urlto($translation, $page),
342                                 code => $lang,
343                                 language => $config{po_slave_languages}{$lang},
344                                 percent => percenttranslated($translation),
345                         };
346                 }
347         }
348         elsif (istranslation($page)) {
349                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
350                 push @ret, {
351                         url => urlto($masterpage, $page),
352                         code => $config{po_master_language}{code},
353                         language => $config{po_master_language}{name},
354                         master => 1,
355                 };
356                 foreach my $lang (sort keys %{$translations{$masterpage}}) {
357                         push @ret, {
358                                 url => urlto($translations{$masterpage}{$lang}, $page),
359                                 code => $lang,
360                                 language => $config{po_slave_languages}{$lang},
361                                 percent => percenttranslated($translations{$masterpage}{$lang}),
362                         } unless ($lang eq $curlang);
363                 }
364         }
365         return @ret;
366 } #}}}
368 sub pagetemplate (@) { #{{{
369         my %params=@_;
370         my $page=$params{page};
371         my $template=$params{template};
373         if (istranslation($page) && $template->query(name => "percenttranslated")) {
374                 $template->param(percenttranslated => percenttranslated($page));
375         }
376         if ($template->query(name => "istranslation")) {
377                 $template->param(istranslation => istranslation($page));
378         }
379         if ($template->query(name => "istranslatable")) {
380                 $template->param(istranslatable => istranslatable($page));
381         }
382         if ($template->query(name => "otherlanguages")) {
383                 $template->param(otherlanguages => [otherlanguages($page)]);
384                 if (istranslatable($page)) {
385                         foreach my $translation (values %{$translations{$page}}) {
386                                 add_depends($page, $translation);
387                         }
388                 }
389                 elsif (istranslation($page)) {
390                         my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
391                         add_depends($page, $masterpage);
392                         foreach my $translation (values %{$translations{$masterpage}}) {
393                                 add_depends($page, $translation);
394                         }
395                 }
396         }
397 } # }}}
399 sub istranslatable ($) { #{{{
400         my $page=shift;
401         my $file=$pagesources{$page};
403         if (! defined $file
404             || (defined pagetype($file) && pagetype($file) eq 'po')
405             || $file =~ /\.pot$/) {
406                 return 0;
407         }
408         return pagespec_match($page, $config{po_translatable_pages});
409 } #}}}
411 sub _istranslation ($) { #{{{
412         my $page=shift;
413         my $file=$pagesources{$page};
414         if (! defined $file) {
415                 return IkiWiki::FailReason->new("no file specified");
416         }
418         if (! defined $file
419             || ! defined pagetype($file)
420             || ! pagetype($file) eq 'po'
421             || $file =~ /\.pot$/) {
422                 return 0;
423         }
425         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
426         if (! defined $masterpage || ! defined $lang
427             || ! (length($masterpage) > 0) || ! (length($lang) > 0)
428             || ! defined $pagesources{$masterpage}
429             || ! defined $config{po_slave_languages}{$lang}) {
430                 return 0;
431         }
433         return istranslatable($masterpage);
434 } #}}}
436 sub istranslation ($) { #{{{
437         my $page=shift;
438         if (_istranslation($page)) {
439                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
440                 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
441                 return 1;
442         }
443         return 0;
444 } #}}}
446 package IkiWiki::PageSpec;
447 use warnings;
448 use strict;
449 use IkiWiki 2.00;
451 sub match_istranslation ($;@) { #{{{
452         my $page=shift;
453         if (IkiWiki::Plugin::po::istranslation($page)) {
454                 return IkiWiki::SuccessReason->new("is a translation page");
455         }
456         else {
457                 return IkiWiki::FailReason->new("is not a translation page");
458         }
459 } #}}}
461 sub match_istranslatable ($;@) { #{{{
462         my $page=shift;
463         if (IkiWiki::Plugin::po::istranslatable($page)) {
464                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
465         }
466         else {
467                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
468         }
469 } #}}}
471 sub match_lang ($$;@) { #{{{
472         my $page=shift;
473         my $wanted=shift;
474         my $regexp=IkiWiki::glob2re($wanted);
475         my $lang;
476         my $masterpage;
478         if (IkiWiki::Plugin::po::istranslation($page)) {
479                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
480         }
481         else {
482                 $lang = $config{po_master_language}{code};
483         }
485         if ($lang!~/^$regexp$/i) {
486                 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
487         }
488         else {
489                 return IkiWiki::SuccessReason->new("file language is $wanted");
490         }
491 } #}}}
493 sub match_currentlang ($$;@) { #{{{
494         my $page=shift;
495         shift;
496         my %params=@_;
497         my ($currentmasterpage, $currentlang, $masterpage, $lang);
499         return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
501         if (IkiWiki::Plugin::po::istranslation($params{location})) {
502                 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
503         }
504         else {
505                 $currentlang = $config{po_master_language}{code};
506         }
508         if (IkiWiki::Plugin::po::istranslation($page)) {
509                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
510         }
511         else {
512                 $lang = $config{po_master_language}{code};
513         }
515         if ($lang eq $currentlang) {
516                 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
517         }
518         else {
519                 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
520         }
521 } #}}}