]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/po.pm
po: Always filter .po files
[git.ikiwiki.info.git] / IkiWiki / Plugin / po.pm
1 #!/usr/bin/perl
2 # .po as a wiki page type
3 # Licensed under GPL v2 or greater
4 # Copyright (C) 2008-2009 intrigeri <intrigeri@boum.org>
5 # inspired by the GPL'd po4a-translate,
6 # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
7 package IkiWiki::Plugin::po;
9 use warnings;
10 use strict;
11 use IkiWiki 3.00;
12 use Encode;
13 eval q{use Locale::Po4a::Common qw(nowrapi18n !/.*/)};
14 if ($@) {
15         print STDERR gettext("warning: Old po4a detected! Recommend upgrade to 0.35.")."\n";
16         eval q{use Locale::Po4a::Common qw(!/.*/)};
17         die $@ if $@;
18 }
19 use Locale::Po4a::Chooser;
20 use Locale::Po4a::Po;
21 use File::Basename;
22 use File::Copy;
23 use File::Spec;
24 use File::Temp;
25 use Memoize;
27 my ($master_language_code, $master_language_name);
28 my %translations;
29 my @origneedsbuild;
30 my %origsubs;
31 my @slavelanguages; # language codes ordered as in config po_slave_languages
32 my %slavelanguages; # language code to name lookup
33 my $language_code_pattern = '[a-zA-Z]+(?:_[a-zA-Z]+)?';
35 memoize("istranslatable");
36 memoize("_istranslation");
37 memoize("percenttranslated");
39 sub import {
40         hook(type => "getsetup", id => "po", call => \&getsetup);
41         hook(type => "checkconfig", id => "po", call => \&checkconfig,
42                 last => 1);
43         hook(type => "needsbuild", id => "po", call => \&needsbuild);
44         hook(type => "scan", id => "po", call => \&scan, last => 1);
45         hook(type => "filter", id => "po", call => \&filter);
46         hook(type => "htmlize", id => "po", call => \&htmlize);
47         hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
48         hook(type => "rename", id => "po", call => \&renamepages, first => 1);
49         hook(type => "delete", id => "po", call => \&mydelete);
50         hook(type => "rendered", id => "po", call => \&rendered);
51         hook(type => "checkcontent", id => "po", call => \&checkcontent);
52         hook(type => "canremove", id => "po", call => \&canremove);
53         hook(type => "canrename", id => "po", call => \&canrename);
54         hook(type => "formbuilder_setup", id => "po", call => \&formbuilder_setup, last => 1);
55         hook(type => "formbuilder", id => "po", call => \&formbuilder);
57         if (! %origsubs) {
58                 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
59                 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
60                 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
61                 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
62                 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
63                 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
64                 $origsubs{'urlto'}=\&IkiWiki::urlto;
65                 inject(name => "IkiWiki::urlto", call => \&myurlto);
66                 $origsubs{'cgiurl'}=\&IkiWiki::cgiurl;
67                 inject(name => "IkiWiki::cgiurl", call => \&mycgiurl);
68                 if (IkiWiki->can('rootpage')) {
69                         $origsubs{'rootpage'}=\&IkiWiki::rootpage;
70                         inject(name => "IkiWiki::rootpage", call => \&myrootpage)
71                                 if defined $origsubs{'rootpage'};
72                 }
73                 $origsubs{'isselflink'}=\&IkiWiki::isselflink;
74                 inject(name => "IkiWiki::isselflink", call => \&myisselflink);
75         }
76 }
79 # ,----
80 # | Table of contents
81 # `----
83 # 1. Hooks
84 # 2. Injected functions
85 # 3. Blackboxes for private data
86 # 4. Helper functions
87 # 5. PageSpecs
90 # ,----
91 # | Hooks
92 # `----
94 sub getsetup () {
95         return
96                 plugin => {
97                         safe => 1,
98                         rebuild => 1, # format plugin
99                         section => "format",
100                 },
101                 po_master_language => {
102                         type => "string",
103                         example => "en|English",
104                         description => "master language (non-PO files)",
105                         safe => 1,
106                         rebuild => 1,
107                 },
108                 po_slave_languages => {
109                         type => "string",
110                         example => [
111                                 'fr|Français',
112                                 'es|Español',
113                                 'de|Deutsch'
114                         ],
115                         description => "slave languages (translated via PO files) format: ll|Langname",
116                         safe => 1,
117                         rebuild => 1,
118                 },
119                 po_translatable_pages => {
120                         type => "pagespec",
121                         example => "* and !*/Discussion",
122                         description => "PageSpec controlling which pages are translatable",
123                         link => "ikiwiki/PageSpec",
124                         safe => 1,
125                         rebuild => 1,
126                 },
127                 po_link_to => {
128                         type => "string",
129                         example => "current",
130                         description => "internal linking behavior (default/current/negotiated)",
131                         safe => 1,
132                         rebuild => 1,
133                 },
136 sub checkconfig () {
137         if (exists $config{po_master_language}) {
138                 if (! ref $config{po_master_language}) {
139                         ($master_language_code, $master_language_name)=
140                                 splitlangpair($config{po_master_language});
141                 }
142                 else {
143                         $master_language_code=$config{po_master_language}{code};
144                         $master_language_name=$config{po_master_language}{name};
145                         $config{po_master_language}=joinlangpair($master_language_code, $master_language_name);
146                 }
147         }
148         if (! defined $master_language_code) {
149                 $master_language_code='en';
150         }
151         if (! defined $master_language_name) {
152                 $master_language_name='English';
153         }
155         if (ref $config{po_slave_languages} eq 'ARRAY') {
156                 foreach my $pair (@{$config{po_slave_languages}}) {
157                         my ($code, $name)=splitlangpair($pair);
158                         if (defined $code && ! exists $slavelanguages{$code}) {
159                                 push @slavelanguages, $code;
160                                 $slavelanguages{$code} = $name;
161                         }
162                 }
163         }
164         elsif (ref $config{po_slave_languages} eq 'HASH') {
165                 %slavelanguages=%{$config{po_slave_languages}};
166                 @slavelanguages = sort {
167                         $config{po_slave_languages}->{$a} cmp $config{po_slave_languages}->{$b};
168                 } keys %slavelanguages;
169                 $config{po_slave_languages}=[
170                         map { joinlangpair($_, $slavelanguages{$_}) } @slavelanguages
171                 ]
172         }
174         delete $slavelanguages{$master_language_code};
176         map {
177                 islanguagecode($_)
178                         or error(sprintf(gettext("%s is not a valid language code"), $_));
179         } ($master_language_code, @slavelanguages);
181         if (! exists $config{po_translatable_pages} ||
182             ! defined $config{po_translatable_pages}) {
183                 $config{po_translatable_pages}="";
184         }
185         if (! exists $config{po_link_to} ||
186             ! defined $config{po_link_to}) {
187                 $config{po_link_to}='default';
188         }
189         elsif ($config{po_link_to} !~ /^(default|current|negotiated)$/) {
190                 warn(sprintf(gettext('%s is not a valid value for po_link_to, falling back to po_link_to=default'),
191                              $config{po_link_to}));
192                 $config{po_link_to}='default';
193         }
194         elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
195                 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
196                 $config{po_link_to}='default';
197         }
199         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
201         # Translated versions of the underlays are added if available.
202         foreach my $underlay ("basewiki",
203                               map { m/^\Q$config{underlaydirbase}\E\/*(.*)/ }
204                                   reverse @{$config{underlaydirs}}) {
205                 next if $underlay=~/^locale\//;
207                 # Underlays containing the po files for slave languages.
208                 foreach my $ll (@slavelanguages) {
209                         add_underlay("po/$ll/$underlay")
210                                 if -d "$config{underlaydirbase}/po/$ll/$underlay";
211                 }
212         
213                 if ($master_language_code ne 'en') {
214                         # Add underlay containing translated source files
215                         # for the master language.
216                         add_underlay("locale/$master_language_code/$underlay")
217                                 if -d "$config{underlaydirbase}/locale/$master_language_code/$underlay";
218                 }
219         }
222 sub needsbuild () {
223         my $needsbuild=shift;
225         # backup @needsbuild content so that change() can know whether
226         # a given master page was rendered because its source file was changed
227         @origneedsbuild=(@$needsbuild);
229         flushmemoizecache();
230         buildtranslationscache();
232         # make existing translations depend on the corresponding master page
233         foreach my $master (keys %translations) {
234                 map add_depends($_, $master), values %{otherlanguages_pages($master)};
235         }
237         return $needsbuild;
240 sub scan (@) {
241         my %params=@_;
242         my $page=$params{page};
243         my $content=$params{content};
244         my $run_by_po=$params{run_by_po};
246         # Massage the recorded state of internal links so that:
247         # - it matches the actually generated links, rather than the links as
248         #   written in the pages' source
249         # - backlinks are consistent in all cases
251         # A second scan pass is made over translation pages, so as an
252         # optimization, we only do so on the second pass in this case,
253         # i.e. when this hook is called by itself.
254         if ($run_by_po && istranslation($page)) {
255                 # replace the occurence of $destpage in $links{$page}
256                 my @orig_links = @{$links{$page}};
257                 $links{$page} = [];
258                 foreach my $destpage (@orig_links) {
259                         if (istranslatedto($destpage, lang($page))) {
260                                 add_link($page, $destpage . '.' . lang($page));
261                         }
262                         else {
263                                 add_link($page, $destpage);
264                         }
265                 }
266         }
267         # No second scan pass is done for a non-translation page, so
268         # links massaging must happen on first pass in this case.
269         elsif (! $run_by_po && ! istranslatable($page) && ! istranslation($page)) {
270                 foreach my $destpage (@{$links{$page}}) {
271                         if (istranslatable($destpage)) {
272                                 # make sure any destpage's translations has
273                                 # $page in its backlinks
274                                 foreach my $link (values %{otherlanguages_pages($destpage)}) {
275                                         add_link($page, $link);
276                                 }
277                         }
278                 }
279         }
281         # Re-run the preprocess hooks in scan mode, then the scan hooks,
282         # over the po-to-markup converted content
283         return if $run_by_po; # avoid looping endlessly
284         return unless istranslation($page);
285         $content = po_to_markup($page, $content);
286         require IkiWiki;
287         IkiWiki::preprocess($page, $page, $content, 1);
288         IkiWiki::run_hooks(scan => sub {
289                 shift->(
290                         page => $page,
291                         content => $content,
292                         run_by_po => 1,
293                 );
294         });
297 # We use filter to convert PO to the master page's format,
298 # since the rest of ikiwiki should not work on PO files.
299 sub filter (@) {
300         my %params = @_;
302         my $page = $params{page};
303         my $destpage = $params{destpage};
304         my $content = $params{content};
305         if (istranslation($page)) {
306                 $content = po_to_markup($page, $content);
307         }
308         return $content;
311 sub htmlize (@) {
312         my %params=@_;
314         my $page = $params{page};
315         my $content = $params{content};
317         # ignore PO files this plugin did not create
318         return $content unless istranslation($page);
320         # force content to be htmlize'd as if it was the same type as the master page
321         return IkiWiki::htmlize($page, $page,
322                 pagetype(srcfile($pagesources{masterpage($page)})),
323                 $content);
326 sub pagetemplate (@) {
327         my %params=@_;
328         my $page=$params{page};
329         my $destpage=$params{destpage};
330         my $template=$params{template};
332         my ($masterpage, $lang) = istranslation($page);
334         if (istranslation($page) && $template->query(name => "percenttranslated")) {
335                 $template->param(percenttranslated => percenttranslated($page));
336         }
337         if ($template->query(name => "istranslation")) {
338                 $template->param(istranslation => scalar istranslation($page));
339         }
340         if ($template->query(name => "istranslatable")) {
341                 $template->param(istranslatable => istranslatable($page));
342         }
343         my $lang_code = istranslation($page) ? lang($page) : $master_language_code;
344         if ($template->query(name => "lang_code")) {
345                 $template->param(lang_code => $lang_code);
346         }
347         if ($template->query(name => "html_lang_code")) {
348                 $template->param(html_lang_code => htmllangcode($lang_code));
349         }
350         if ($template->query(name => "html_lang_dir")) {
351                 $template->param(html_lang_dir => htmllangdir($lang_code));
352         }
353         if ($template->query(name => "lang_name")) {
354                 $template->param(lang_name => languagename($lang_code));
355         }
356         if ($template->query(name => "HOMEPAGEURL")) {
357                 $template->param(homepageurl => homepageurl($page));
358         }
359         if ($template->query(name => "otherlanguages")) {
360                 $template->param(otherlanguages => [otherlanguagesloop($page)]);
361                 map add_depends($page, $_), (values %{otherlanguages_pages($page)});
362         }
363         if ($config{discussion} && istranslation($page)) {
364                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
365                    (length $config{cgiurl} ||
366                     exists $links{$masterpage."/".lc($config{discussionpage})})) {
367                         $template->param('discussionlink' => htmllink(
368                                 $page,
369                                 $destpage,
370                                 $masterpage . '/' . $config{discussionpage},
371                                 noimageinline => 1,
372                                 forcesubpage => 0,
373                                 linktext => $config{discussionpage},
374                 ));
375                 }
376         }
377         # Remove broken parentlink to ./index.html on home page's translations.
378         # It works because this hook has the "last" parameter set, to ensure it
379         # runs after parentlinks' own pagetemplate hook.
380         if ($template->param('parentlinks')
381             && istranslation($page)
382             && $masterpage eq "index") {
383                 $template->param('parentlinks' => []);
384         }
385         if (ishomepage($page) && $template->query(name => "title")
386             && !$template->param("title_overridden")) {
387                 $template->param(title => $config{wikiname});
388         }
391 # Add the renamed page translations to the list of to-be-renamed pages.
392 sub renamepages (@) {
393         my %params = @_;
395         my %torename = %{$params{torename}};
396         my $session = $params{session};
398         # Save the page(s) the user asked to rename, so that our
399         # canrename hook can tell the difference between:
400         #  - a translation being renamed as a consequence of its master page
401         #    being renamed
402         #  - a user trying to directly rename a translation
403         # This is why this hook has to be run first, before the list of pages
404         # to rename is modified by other plugins.
405         my @orig_torename;
406         @orig_torename=@{$session->param("po_orig_torename")}
407                 if defined $session->param("po_orig_torename");
408         push @orig_torename, $torename{src};
409         $session->param(po_orig_torename => \@orig_torename);
410         IkiWiki::cgi_savesession($session);
412         return () unless istranslatable($torename{src});
414         my @ret;
415         my %otherpages=%{otherlanguages_pages($torename{src})};
416         while (my ($lang, $otherpage) = each %otherpages) {
417                 push @ret, {
418                         src => $otherpage,
419                         srcfile => $pagesources{$otherpage},
420                         dest => otherlanguage_page($torename{dest}, $lang),
421                         destfile => $torename{dest}.".".$lang.".po",
422                         required => 0,
423                 };
424         }
425         return @ret;
428 sub mydelete (@) {
429         my @deleted=@_;
431         map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
434 sub rendered (@) {
435         my @rendered=@_;
437         my $updated_po_files=0;
439         # Refresh/create POT and PO files as needed.
440         foreach my $file (grep {istranslatablefile($_)} @rendered) {
441                 my $masterfile=srcfile($file);
442                 my $page=pagename($file);
443                 my $updated_pot_file=0;
445                 # Avoid touching underlay files.
446                 next if $masterfile ne "$config{srcdir}/$file";
448                 # Only refresh POT file if it does not exist, or if
449                 # the source was changed: don't if only the HTML was
450                 # refreshed, e.g. because of a dependency.
451                 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild) ||
452                     ! -e potfile($masterfile)) {
453                         refreshpot($masterfile);
454                         $updated_pot_file=1;
455                 }
456                 my @pofiles;
457                 foreach my $po (pofiles($masterfile)) {
458                         next if ! $updated_pot_file && -e $po;
459                         next if grep { $po=~/\Q$_\E/ } @{$config{underlaydirs}};
460                         push @pofiles, $po;
461                 }
462                 if (@pofiles) {
463                         refreshpofiles($masterfile, @pofiles);
464                         map { s/^\Q$config{srcdir}\E\/*//; IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
465                         $updated_po_files=1;
466                 }
467         }
469         if ($updated_po_files) {
470                 commit_and_refresh(
471                         gettext("updated PO files"));
472         }
475 sub checkcontent (@) {
476         my %params=@_;
478         if (istranslation($params{page})) {
479                 my $res = isvalidpo($params{content});
480                 if ($res) {
481                         return undef;
482                 }
483                 else {
484                         return "$res";
485                 }
486         }
487         return undef;
490 sub canremove (@) {
491         my %params = @_;
493         if (istranslation($params{page})) {
494                 return gettext("Can not remove a translation. If the master page is removed, ".
495                                "however, its translations will be removed as well.");
496         }
497         return undef;
500 sub canrename (@) {
501         my %params = @_;
502         my $session = $params{session};
504         if (istranslation($params{src})) {
505                 my $masterpage = masterpage($params{src});
506                 # Tell the difference between:
507                 #  - a translation being renamed as a consequence of its master page
508                 #    being renamed, which is allowed
509                 #  - a user trying to directly rename a translation, which is forbidden
510                 # by looking for the master page in the list of to-be-renamed pages we
511                 # saved early in the renaming process.
512                 my $orig_torename = $session->param("po_orig_torename");
513                 unless (grep { $_ eq $masterpage } @{$orig_torename}) {
514                         return gettext("Can not rename a translation. If the master page is renamed, ".
515                                        "however, its translations will be renamed as well.");
516                 }
517         }
518         return undef;
521 sub formbuilder_setup (@) {
522         my %params=@_;
523         my $form=$params{form};
524         my $q=$params{cgi};
526         return unless defined $form->field("do");
528         if ($form->field("do") eq "create") {
529                 # Warn the user: new pages must be written in master language.
530                 my $template=template("pocreatepage.tmpl");
531                 $template->param(LANG => $master_language_name);
532                 $form->tmpl_param(message => $template->output);
533         }
534         elsif ($form->field("do") eq "edit") {
535                 # Remove the rename/remove buttons on slave pages.
536                 # This has to be done after the rename/remove plugins have added
537                 # their buttons, which is why this hook must be run last.
538                 # The canrename/canremove hooks already ensure this is forbidden
539                 # at the backend level, so this is only UI sugar.
540                 if (istranslation(scalar $form->field("page"))) {
541                         map {
542                                 for (my $i = 0; $i < @{$params{buttons}}; $i++) {
543                                         if (@{$params{buttons}}[$i] eq $_) {
544                                                 delete  @{$params{buttons}}[$i];
545                                                 last;
546                                         }
547                                 }
548                         } qw(Rename Remove);
549                 }
550         }
553 sub formbuilder (@) {
554         my %params=@_;
555         my $form=$params{form};
556         my $q=$params{cgi};
558         return unless defined $form->field("do");
560         # Do not allow to create pages of type po: they are automatically created.
561         # The main reason to do so is to bypass the "favor the type of linking page
562         # on page creation" logic, which is unsuitable when a broken link is clicked
563         # on a slave (PO) page.
564         # This cannot be done in the formbuilder_setup hook as the list of types is
565         # computed later.
566         if ($form->field("do") eq "create") {
567                 foreach my $field ($form->field) {
568                         next unless "$field" eq "type";
569                         next unless $field->type eq 'select';
570                         my $orig_value = $field->value;
571                         # remove po from the list of types
572                         my @types = grep { $_->[0] ne 'po' } $field->options;
573                         $field->options(\@types) if @types;
574                         # favor the type of linking page's masterpage
575                         if ($orig_value eq 'po') {
576                                 my ($from, $type);
577                                 if (defined $form->field('from')) {
578                                         ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
579                                         $from = masterpage($from);
580                                 }
581                                 if (defined $from && exists $pagesources{$from}) {
582                                         $type=pagetype($pagesources{$from});
583                                 }
584                                 $type=$config{default_pageext} unless defined $type;
585                                 $field->value($type) ;
586                         }
587                 }
588         }
591 # ,----
592 # | Injected functions
593 # `----
595 # Implement po_link_to 'current' and 'negotiated' settings.
596 sub mybestlink ($$) {
597         my $page=shift;
598         my $link=shift;
600         return $origsubs{'bestlink'}->($page, $link)
601                 if defined $config{po_link_to} && $config{po_link_to} eq "default";
603         my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
604         my @caller = caller(1);
605         if (length $res
606             && istranslatedto($res, lang($page))
607             && istranslation($page)
608             &&  !(exists $caller[3] && defined $caller[3]
609                   && ($caller[3] eq "IkiWiki::PageSpec::match_link"))) {
610                 return $res . "." . lang($page);
611         }
612         return $res;
615 sub mybeautify_urlpath ($) {
616         my $url=shift;
618         my $res=$origsubs{'beautify_urlpath'}->($url);
619         if (defined $config{po_link_to} && $config{po_link_to} eq "negotiated") {
620                 $res =~ s!/\Qindex.$master_language_code.$config{htmlext}\E$!/!;
621                 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
622                 map {
623                         $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
624                 } @slavelanguages;
625         }
626         return $res;
629 sub mytargetpage ($$;$) {
630         my $page=shift;
631         my $ext=shift;
632         my $filename=shift;
634         if (istranslation($page) || istranslatable($page)) {
635                 my ($masterpage, $lang) = (masterpage($page), lang($page));
636                 if (defined $filename) {
637                         return $masterpage . "/" . $filename . "." . $lang . "." . $ext;
638                 }
639                 elsif (! $config{usedirs} || $masterpage eq 'index') {
640                         return $masterpage . "." . $lang . "." . $ext;
641                 }
642                 else {
643                         return $masterpage . "/index." . $lang . "." . $ext;
644                 }
645         }
646         return $origsubs{'targetpage'}->($page, $ext, $filename);
649 sub myurlto ($;$$) {
650         my $to=shift;
651         my $from=shift;
652         my $absolute=shift;
654         # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
655         if (! length $to
656             && $config{po_link_to} eq "current"
657             && istranslatable('index')) {
658                 if (defined $from) {
659                         return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
660                 }
661                 else {
662                         return $origsubs{'urlto'}->($to,$from,$absolute);
663                 }
664         }
665         # avoid using our injected beautify_urlpath if run by cgi_editpage,
666         # so that one is redirected to the just-edited page rather than to the
667         # negociated translation; to prevent unnecessary fiddling with caller/inject,
668         # we only do so when our beautify_urlpath would actually do what we want to
669         # avoid, i.e. when po_link_to = negotiated.
670         # also avoid doing so when run by cgi_goto, so that the links on recentchanges
671         # page actually lead to the exact page they pretend to.
672         if ($config{po_link_to} eq "negotiated") {
673                 my @caller = caller(1);
674                 my $use_orig = 0;
675                 $use_orig = 1 if (exists $caller[3] && defined $caller[3]
676                                  && ($caller[3] eq "IkiWiki::cgi_editpage" ||
677                                      $caller[3] eq "IkiWiki::Plugin::goto::cgi_goto")
678                                  );
679                 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
680                         if $use_orig;
681                 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
682                 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
683                         if $use_orig;
684                 return $res;
685         }
686         else {
687                 return $origsubs{'urlto'}->($to,$from,$absolute)
688         }
691 sub mycgiurl (@) {
692         my %params=@_;
694         # slave pages have no subpages
695         if (istranslation($params{'from'})) {
696                 $params{'from'} = masterpage($params{'from'});
697         }
698         return $origsubs{'cgiurl'}->(%params);
701 sub myrootpage (@) {
702         my %params=@_;
704         my $rootpage;
705         if (exists $params{rootpage}) {
706                 $rootpage=$origsubs{'bestlink'}->($params{page}, $params{rootpage});
707                 if (!length $rootpage) {
708                         $rootpage=$params{rootpage};
709                 }
710         }
711         else {
712                 $rootpage=masterpage($params{page});
713         }
714         return $rootpage;
717 sub myisselflink ($$) {
718         my $page=shift;
719         my $link=shift;
721         return 1 if $origsubs{'isselflink'}->($page, $link);
722         if (istranslation($page)) {
723                 return $origsubs{'isselflink'}->(masterpage($page), $link);
724         }
725         return;
728 # ,----
729 # | Helper functions
730 # `----
732 sub maybe_add_leading_slash ($;$) {
733         my $str=shift;
734         my $add=shift;
735         $add=1 unless defined $add;
736         return '/' . $str if $add;
737         return $str;
740 sub istranslatablefile ($) {
741         my $file=shift;
743         return 0 unless defined $file;
744         my $type=pagetype($file);
745         return 0 if ! defined $type || $type eq 'po';
746         return 0 if $file =~ /\.pot$/;
747         return 0 if ! defined $config{po_translatable_pages};
748         return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
749         return;
752 sub istranslatable ($) {
753         my $page=shift;
755         $page=~s#^/##;
756         return 1 if istranslatablefile($pagesources{$page});
757         return;
760 sub istranslatedto ($$) {
761         my $page=shift;
762         my $destlang = shift;
764         $page=~s#^/##;
765         return 0 unless istranslatable($page);
766         exists $pagesources{otherlanguage_page($page, $destlang)};
769 sub _istranslation ($) {
770         my $page=shift;
772         $page='' unless defined $page && length $page;
773         my $hasleadingslash = ($page=~s#^/##);
774         my $file=$pagesources{$page};
775         return 0 unless defined $file
776                          && defined pagetype($file)
777                          && pagetype($file) eq 'po';
778         return 0 if $file =~ /\.pot$/;
780         my ($masterpage, $lang) = ($page =~ /(.*)[.]($language_code_pattern)$/);
781         return 0 unless defined $masterpage && defined $lang
782                          && length $masterpage && length $lang
783                          && defined $pagesources{$masterpage}
784                          && defined $slavelanguages{$lang};
786         return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
787                 if istranslatable($masterpage);
790 sub istranslation ($) {
791         my $page=shift;
793         if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
794                 my $hasleadingslash = ($masterpage=~s#^/##);
795                 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
796                 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
797         }
798         return "";
801 sub masterpage ($) {
802         my $page=shift;
804         if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
805                 return $masterpage;
806         }
807         return $page;
810 sub lang ($) {
811         my $page=shift;
813         if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
814                 return $lang;
815         }
816         return $master_language_code;
819 sub htmllangcode ($) {
820         (my $lang = shift) =~ tr/_/-/;
821         return $lang;
824 sub htmllangdir ($) {
825         my $lang = shift;
826         if ($lang =~ /^(ar|fa|he)/) {
827                 return 'rtl';
828         }
829         return 'ltr';
832 sub islanguagecode ($) {
833         my $code=shift;
835         return $code =~ /^$language_code_pattern$/;
838 sub otherlanguage_page ($$) {
839         my $page=shift;
840         my $code=shift;
842         return masterpage($page) if $code eq $master_language_code;
843         return masterpage($page) . '.' . $code;
846 # Returns the list of other languages codes: the master language comes first,
847 # then the codes are ordered the same way as in po_slave_languages, if it is
848 # an array, or in the language name lexical order, if it is a hash.
849 sub otherlanguages_codes ($) {
850         my $page=shift;
852         my @ret;
853         return \@ret unless istranslation($page) || istranslatable($page);
854         my $curlang=lang($page);
855         foreach my $lang
856                 ($master_language_code, @slavelanguages) {
857                 next if $lang eq $curlang;
858                 if ($lang eq $master_language_code ||
859                     istranslatedto(masterpage($page), $lang)) {
860                         push @ret, $lang;
861                 }
862         }
863         return \@ret;
866 sub otherlanguages_pages ($) {
867         my $page=shift;
869         my %ret;
870         map {
871                 $ret{$_} = otherlanguage_page($page, $_)
872         } @{otherlanguages_codes($page)};
874         return \%ret;
877 sub potfile ($) {
878         my $masterfile=shift;
880         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
881         $dir='' if $dir eq './';
882         return File::Spec->catpath('', $dir, $name . ".pot");
885 sub pofile ($$) {
886         my $masterfile=shift;
887         my $lang=shift;
889         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
890         $dir='' if $dir eq './';
891         return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
894 sub pofiles ($) {
895         my $masterfile=shift;
897         return map pofile($masterfile, $_), @slavelanguages;
900 sub refreshpot ($) {
901         my $masterfile=shift;
903         my $potfile=potfile($masterfile);
904         my $doc=Locale::Po4a::Chooser::new(po4a_type($masterfile),
905                                            po4a_options($masterfile));
906         $doc->{TT}{utf_mode} = 1;
907         $doc->{TT}{file_in_charset} = 'UTF-8';
908         $doc->{TT}{file_out_charset} = 'UTF-8';
909         $doc->read($masterfile);
910         # let's cheat a bit to force porefs option to be passed to
911         # Locale::Po4a::Po; this is undocument use of internal
912         # Locale::Po4a::TransTractor's data, compulsory since this module
913         # prevents us from using the porefs option.
914         $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
915         $doc->{TT}{po_out}->set_charset('UTF-8');
916         # do the actual work
917         $doc->parse;
918         IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
919         $doc->writepo($potfile);
922 sub refreshpofiles ($@) {
923         my $masterfile=shift;
924         my @pofiles=@_;
926         my $potfile=potfile($masterfile);
927         if (! -e $potfile) {
928                 error("po(refreshpofiles) ".sprintf(gettext("POT file (%s) does not exist"), $potfile));
929         }
931         foreach my $pofile (@pofiles) {
932                 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
934                 if (! -e $pofile) {
935                         # If the po file exists in an underlay, copy it
936                         # from there.
937                         my ($pobase)=$pofile=~/^\Q$config{srcdir}\E\/?(.*)$/;
938                         foreach my $dir (@{$config{underlaydirs}}) {
939                                 if (-e "$dir/$pobase") {
940                                         File::Copy::syscopy("$dir/$pobase",$pofile)
941                                                 or error("po(refreshpofiles) ".
942                                                          sprintf(gettext("failed to copy underlay PO file to %s"),
943                                                                  $pofile));
944                                 }
945                         }
946                 }
948                 if (-e $pofile) {
949                         if (! (system("msgmerge", "--previous", "-q", "-U", "--backup=none", $pofile, $potfile) == 0)) {
950                                 print STDERR ("po(refreshpofiles) ". sprintf(gettext("failed to update %s"), $pofile));
951                         }
952                 }
953                 else {
954                         File::Copy::syscopy($potfile,$pofile)
955                                 or error("po(refreshpofiles) ".
956                                          sprintf(gettext("failed to copy the POT file to %s"),
957                                                  $pofile));
958                 }
959         }
962 sub buildtranslationscache() {
963         # use istranslation's side-effect
964         map istranslation($_), (keys %pagesources);
967 sub resettranslationscache() {
968         undef %translations;
971 sub flushmemoizecache() {
972         Memoize::flush_cache("istranslatable");
973         Memoize::flush_cache("_istranslation");
974         Memoize::flush_cache("percenttranslated");
977 sub urlto_with_orig_beautiful_urlpath($$) {
978         my $to=shift;
979         my $from=shift;
981         inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
982         my $res=urlto($to, $from);
983         inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
985         return $res;
988 sub percenttranslated ($) {
989         my $page=shift;
991         $page=~s/^\///;
992         return gettext("N/A") unless istranslation($page);
993         my $file=srcfile($pagesources{$page});
994         my $masterfile = srcfile($pagesources{masterpage($page)});
995         my $doc=Locale::Po4a::Chooser::new(po4a_type($masterfile),
996                                            po4a_options($masterfile));
997         $doc->process(
998                 'po_in_name'    => [ $file ],
999                 'file_in_name'  => [ $masterfile ],
1000                 'file_in_charset'  => 'UTF-8',
1001                 'file_out_charset' => 'UTF-8',
1002         ) or error("po(percenttranslated) ".
1003                    sprintf(gettext("failed to translate %s"), $page));
1004         my ($percent,$hit,$queries) = $doc->stats();
1005         $percent =~ s/\.[0-9]+$//;
1006         return $percent;
1009 sub languagename ($) {
1010         my $code=shift;
1012         return $master_language_name
1013                 if $code eq $master_language_code;
1014         return $slavelanguages{$code}
1015                 if defined $slavelanguages{$code};
1016         return;
1019 sub otherlanguagesloop ($) {
1020         my $page=shift;
1022         my @ret;
1023         if (istranslation($page)) {
1024                 push @ret, {
1025                         url => urlto_with_orig_beautiful_urlpath(masterpage($page), $page),
1026                         code => $master_language_code,
1027                         html_code => htmllangcode($master_language_code),
1028                         html_dir => htmllangdir($master_language_code),
1029                         language => $master_language_name,
1030                         master => 1,
1031                 };
1032         }
1033         foreach my $lang (@{otherlanguages_codes($page)}) {
1034                 next if $lang eq $master_language_code;
1035                 my $otherpage = otherlanguage_page($page, $lang);
1036                 push @ret, {
1037                         url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
1038                         code => $lang,
1039                         html_code => htmllangcode($lang),
1040                         html_dir => htmllangdir($lang),
1041                         language => languagename($lang),
1042                         percent => percenttranslated($otherpage),
1043                 }
1044         }
1045         return @ret;
1048 sub homepageurl (;$) {
1049         my $page=shift;
1051         return urlto('', $page);
1054 sub ishomepage ($) {
1055         my $page = shift;
1057         return 1 if $page eq 'index';
1058         map { return 1 if $page eq 'index.'.$_ } @slavelanguages;
1059         return undef;
1062 sub deletetranslations ($) {
1063         my $deletedmasterfile=shift;
1065         my $deletedmasterpage=pagename($deletedmasterfile);
1066         my @todelete;
1067         map {
1068                 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
1069                 my $absfile = "$config{srcdir}/$file";
1070                 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
1071                         push @todelete, $file;
1072                 }
1073         } @slavelanguages;
1075         map {
1076                 if ($config{rcs}) {
1077                         IkiWiki::rcs_remove($_);
1078                 }
1079                 else {
1080                         IkiWiki::prune("$config{srcdir}/$_", $config{srcdir});
1081                 }
1082         } @todelete;
1084         if (@todelete) {
1085                 commit_and_refresh(
1086                         gettext("removed obsolete PO files"));
1087         }
1090 sub commit_and_refresh ($) {
1091         my $msg = shift;
1093         if ($config{rcs}) {
1094                 IkiWiki::disable_commit_hook();
1095                 IkiWiki::rcs_commit_staged(
1096                         message => $msg,
1097                 );
1098                 IkiWiki::enable_commit_hook();
1099                 IkiWiki::rcs_update();
1100         }
1101         # Reinitialize module's private variables.
1102         resettranslationscache();
1103         flushmemoizecache();
1104         # Trigger a wiki refresh.
1105         require IkiWiki::Render;
1106         # without preliminary saveindex/loadindex, refresh()
1107         # complains about a lot of uninitialized variables
1108         IkiWiki::saveindex();
1109         IkiWiki::loadindex();
1110         IkiWiki::refresh();
1111         IkiWiki::saveindex();
1114 sub po_to_markup ($$) {
1115         my ($page, $content) = (shift, shift);
1117         $content = '' unless defined $content;
1118         $content = decode_utf8(encode_utf8($content));
1119         # CRLF line terminators make poor Locale::Po4a feel bad
1120         $content=~s/\r\n/\n/g;
1122         # There are incompatibilities between some File::Temp versions
1123         # (including 0.18, bundled with Lenny's perl-modules package)
1124         # and others (e.g. 0.20, previously present in the archive as
1125         # a standalone package): under certain circumstances, some
1126         # return a relative filename, whereas others return an absolute one;
1127         # we here use this module in a way that is at least compatible
1128         # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1129         my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
1130                                     DIR => File::Spec->tmpdir,
1131                                     UNLINK => 1)->filename;
1132         my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
1133                                      DIR => File::Spec->tmpdir,
1134                                      UNLINK => 1)->filename;
1136         my $fail = sub ($) {
1137                 my $msg = "po(po_to_markup) - $page : " . shift;
1138                 error($msg, sub { unlink $infile, $outfile});
1139         };
1141         writefile(basename($infile), File::Spec->tmpdir, $content)
1142                 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1144         my $masterfile = srcfile($pagesources{masterpage($page)});
1145         my $doc=Locale::Po4a::Chooser::new(po4a_type($masterfile),
1146                                            po4a_options($masterfile));
1147         $doc->process(
1148                 'po_in_name'    => [ $infile ],
1149                 'file_in_name'  => [ $masterfile ],
1150                 'file_in_charset'  => 'UTF-8',
1151                 'file_out_charset' => 'UTF-8',
1152         ) or return $fail->(gettext("failed to translate"));
1153         $doc->write($outfile)
1154                 or return $fail->(sprintf(gettext("failed to write %s"), $outfile));
1156         $content = readfile($outfile);
1158         # Unlinking should happen automatically, thanks to File::Temp,
1159         # but it does not work here, probably because of the way writefile()
1160         # and Locale::Po4a::write() work.
1161         unlink $infile, $outfile;
1163         return $content;
1166 # returns a SuccessReason or FailReason object
1167 sub isvalidpo ($) {
1168         my $content = shift;
1170         # NB: we don't use po_to_markup here, since Po4a parser does
1171         # not mind invalid PO content
1172         $content = '' unless defined $content;
1173         $content = decode_utf8(encode_utf8($content));
1175         # There are incompatibilities between some File::Temp versions
1176         # (including 0.18, bundled with Lenny's perl-modules package)
1177         # and others (e.g. 0.20, previously present in the archive as
1178         # a standalone package): under certain circumstances, some
1179         # return a relative filename, whereas others return an absolute one;
1180         # we here use this module in a way that is at least compatible
1181         # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1182         my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-isvalidpo.XXXXXXXXXX",
1183                                     DIR => File::Spec->tmpdir,
1184                                     UNLINK => 1)->filename;
1186         my $fail = sub ($) {
1187                 my $msg = '[po/isvalidpo] ' . shift;
1188                 unlink $infile;
1189                 return IkiWiki::FailReason->new("$msg");
1190         };
1192         writefile(basename($infile), File::Spec->tmpdir, $content)
1193                 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1195         my $res = (system("msgfmt", "--check", $infile, "-o", "/dev/null") == 0);
1197         # Unlinking should happen automatically, thanks to File::Temp,
1198         # but it does not work here, probably because of the way writefile()
1199         # and Locale::Po4a::write() work.
1200         unlink $infile;
1202         if ($res) {
1203                 return IkiWiki::SuccessReason->new("valid gettext data");
1204         }
1205         return IkiWiki::FailReason->new(gettext("invalid gettext data, go back ".
1206                                         "to previous page to continue edit"));
1209 sub po4a_type ($) {
1210         my $file = shift;
1212         my $pagetype = pagetype($file);
1213         if ($pagetype eq 'html') {
1214                 return 'xhtml';
1215         }
1216         return 'text';
1219 sub po4a_options($) {
1220         my $file = shift;
1222         my %options;
1223         my $pagetype = pagetype($file);
1225         if ($pagetype eq 'html') {
1226                 # how to disable options is not consistent across po4a modules
1227                 $options{includessi} = '';
1228                 $options{includeexternal} = 0;
1229                 $options{ontagerror} = 'warn';
1230         }
1231         elsif ($pagetype eq 'mdwn') {
1232                 $options{markdown} = 1;
1233         }
1234         else {
1235                 $options{markdown} = 0;
1236         }
1238         return %options;
1241 sub splitlangpair ($) {
1242         my $pair=shift;
1244         my ($code, $name) = ( $pair =~ /^($language_code_pattern)\|(.+)$/ );
1245         if (! defined $code || ! defined $name ||
1246             ! length $code || ! length $name) {
1247                 # not a fatal error to avoid breaking if used with web setup
1248                 warn sprintf(gettext("%s has invalid syntax: must use CODE|NAME"),
1249                         $pair);
1250         }
1252         return $code, $name;
1255 sub joinlangpair ($$) {
1256         my $code=shift;
1257         my $name=shift;
1259         return "$code|$name";
1262 # ,----
1263 # | PageSpecs
1264 # `----
1266 package IkiWiki::PageSpec;
1268 sub match_istranslation ($;@) {
1269         my $page=shift;
1271         if (IkiWiki::Plugin::po::istranslation($page)) {
1272                 return IkiWiki::SuccessReason->new("is a translation page");
1273         }
1274         else {
1275                 return IkiWiki::FailReason->new("is not a translation page");
1276         }
1279 sub match_istranslatable ($;@) {
1280         my $page=shift;
1282         if (IkiWiki::Plugin::po::istranslatable($page)) {
1283                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
1284         }
1285         else {
1286                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
1287         }
1290 sub match_lang ($$;@) {
1291         my $page=shift;
1292         my $wanted=shift;
1294         my $regexp=IkiWiki::glob2re($wanted);
1295         my $lang=IkiWiki::Plugin::po::lang($page);
1296         if ($lang !~ $regexp) {
1297                 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
1298         }
1299         else {
1300                 return IkiWiki::SuccessReason->new("file language is $wanted");
1301         }
1304 sub match_currentlang ($$;@) {
1305         my $page=shift;
1306         shift;
1307         my %params=@_;
1309         return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
1311         my $currentlang=IkiWiki::Plugin::po::lang($params{location});
1312         my $lang=IkiWiki::Plugin::po::lang($page);
1314         if ($lang eq $currentlang) {
1315                 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
1316         }
1317         else {
1318                 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
1319         }
1322 sub match_needstranslation ($$;@) {
1323         my $page=shift;
1324         my $wanted=shift;
1326         if (defined $wanted && $wanted ne "") {
1327                 if ($wanted !~ /^\d+$/) {
1328                         return IkiWiki::FailReason->new("parameter is not an integer");
1329                 }
1330                 elsif ($wanted > 100) {
1331                         return IkiWiki::FailReason->new("parameter is greater than 100");
1332                 }
1333         }
1334         else {
1335                 $wanted=100;
1336         }
1338         my $percenttranslated=IkiWiki::Plugin::po::percenttranslated($page);
1339         if ($percenttranslated eq 'N/A') {
1340                 return IkiWiki::FailReason->new("file is not a translatable page");
1341         }
1342         elsif ($percenttranslated < $wanted) {
1343                 return IkiWiki::SuccessReason->new("file has $percenttranslated translated");
1344         }
1345         else {
1346                 return IkiWiki::FailReason->new("file is translated enough");
1347         }