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;
13 use Locale::Po4a::Chooser;
26 memoize("istranslatable");
27 memoize("_istranslation");
28 memoize("percenttranslated");
31 hook(type => "getsetup", id => "po", call => \&getsetup);
32 hook(type => "checkconfig", id => "po", call => \&checkconfig);
33 hook(type => "needsbuild", id => "po", call => \&needsbuild);
34 hook(type => "scan", id => "po", call => \&scan, last =>1);
35 hook(type => "filter", id => "po", call => \&filter);
36 hook(type => "htmlize", id => "po", call => \&htmlize);
37 hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
38 hook(type => "postscan", id => "po", call => \&postscan);
39 hook(type => "rename", id => "po", call => \&renamepages, first => 1);
40 hook(type => "delete", id => "po", call => \&mydelete);
41 hook(type => "change", id => "po", call => \&change);
42 hook(type => "cansave", id => "po", call => \&cansave);
43 hook(type => "canremove", id => "po", call => \&canremove);
44 hook(type => "canrename", id => "po", call => \&canrename);
45 hook(type => "editcontent", id => "po", call => \&editcontent);
46 hook(type => "formbuilder_setup", id => "po", call => \&formbuilder_setup, last => 1);
47 hook(type => "formbuilder", id => "po", call => \&formbuilder);
49 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
50 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
51 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
52 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
53 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
54 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
55 $origsubs{'urlto'}=\&IkiWiki::urlto;
56 inject(name => "IkiWiki::urlto", call => \&myurlto);
57 $origsubs{'nicepagetitle'}=\&IkiWiki::nicepagetitle;
58 inject(name => "IkiWiki::nicepagetitle", call => \&mynicepagetitle);
67 # 2. Injected functions
68 # 3. Blackboxes for private data
83 po_master_language => {
89 description => "master language (non-PO files)",
93 po_slave_languages => {
100 description => "slave languages (PO files)",
104 po_translatable_pages => {
106 example => "!*/Discussion",
107 description => "PageSpec controlling which pages are translatable",
108 link => "ikiwiki/PageSpec",
114 example => "current",
115 description => "internal linking behavior (default/current/negotiated)",
119 po_translation_status_in_links => {
122 description => "display translation status in links to translations",
129 foreach my $field (qw{po_master_language po_slave_languages}) {
130 if (! exists $config{$field} || ! defined $config{$field}) {
131 error(sprintf(gettext("Must specify %s when using the %s plugin"),
135 if (! (keys %{$config{po_slave_languages}})) {
136 error(gettext("At least one slave language must be defined ".
137 "in po_slave_languages when using the po plugin"));
141 or error(sprintf(gettext("%s is not a valid language code"), $_));
142 } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
143 if (! exists $config{po_translatable_pages} ||
144 ! defined $config{po_translatable_pages}) {
145 $config{po_translatable_pages}="";
147 if (! exists $config{po_link_to} ||
148 ! defined $config{po_link_to}) {
149 $config{po_link_to}='default';
152 $config{po_link_to} eq $_
153 } ('default', 'current', 'negotiated')) {
154 warn(sprintf(gettext('%s is not a valid value for po_link_to, falling back to po_link_to=default'),
155 $config{po_link_to}));
156 $config{po_link_to}='default';
158 elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
159 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
160 $config{po_link_to}='default';
162 if (! exists $config{po_translation_status_in_links} ||
163 ! defined $config{po_translation_status_in_links}) {
164 $config{po_translation_status_in_links}=1;
166 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
170 my $needsbuild=shift;
172 # backup @needsbuild content so that change() can know whether
173 # a given master page was rendered because its source file was changed
174 @origneedsbuild=(@$needsbuild);
177 buildtranslationscache();
179 # make existing translations depend on the corresponding master page
180 foreach my $master (keys %translations) {
181 map add_depends($_, $master), values %{otherlanguages($master)};
185 # Massage the recorded state of internal links so that:
186 # - it matches the actually generated links, rather than the links as written
187 # in the pages' source
188 # - backlinks are consistent in all cases
191 my $page=$params{page};
192 my $content=$params{content};
194 return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
196 if (istranslation($page)) {
197 foreach my $destpage (@{$links{$page}}) {
198 if (istranslatable($destpage)) {
199 # replace one occurence of $destpage in $links{$page}
200 # (we only want to replace the one that was added by
201 # IkiWiki::Plugin::link::scan, other occurences may be
202 # there for other reasons)
203 for (my $i=0; $i<@{$links{$page}}; $i++) {
204 if (@{$links{$page}}[$i] eq $destpage) {
205 @{$links{$page}}[$i] = $destpage . '.' . lang($page);
212 elsif (! istranslatable($page) && ! istranslation($page)) {
213 foreach my $destpage (@{$links{$page}}) {
214 if (istranslatable($destpage)) {
215 # make sure any destpage's translations has
216 # $page in its backlinks
217 push @{$links{$page}},
218 values %{otherlanguages($destpage)};
224 # We use filter to convert PO to the master page's format,
225 # since the rest of ikiwiki should not work on PO files.
229 my $page = $params{page};
230 my $destpage = $params{destpage};
231 my $content = $params{content};
232 if (istranslation($page) && ! alreadyfiltered($page, $destpage)) {
233 $content = po_to_markup($page, $content);
234 setalreadyfiltered($page, $destpage);
242 my $page = $params{page};
243 my $content = $params{content};
245 # ignore PO files this plugin did not create
246 return $content unless istranslation($page);
248 # force content to be htmlize'd as if it was the same type as the master page
249 return IkiWiki::htmlize($page, $page,
250 pagetype(srcfile($pagesources{masterpage($page)})),
254 sub pagetemplate (@) {
256 my $page=$params{page};
257 my $destpage=$params{destpage};
258 my $template=$params{template};
260 my ($masterpage, $lang) = istranslation($page);
262 if (istranslation($page) && $template->query(name => "percenttranslated")) {
263 $template->param(percenttranslated => percenttranslated($page));
265 if ($template->query(name => "istranslation")) {
266 $template->param(istranslation => scalar istranslation($page));
268 if ($template->query(name => "istranslatable")) {
269 $template->param(istranslatable => istranslatable($page));
271 if ($template->query(name => "HOMEPAGEURL")) {
272 $template->param(homepageurl => homepageurl($page));
274 if ($template->query(name => "otherlanguages")) {
275 $template->param(otherlanguages => [otherlanguagesloop($page)]);
276 map add_depends($page, $_), (values %{otherlanguages($page)});
278 # Rely on IkiWiki::Render's genpage() to decide wether
279 # a discussion link should appear on $page; this is not
280 # totally accurate, though: some broken links may be generated
281 # when cgiurl is disabled.
282 # This compromise avoids some code duplication, and will probably
283 # prevent future breakage when ikiwiki internals change.
284 # Known limitations are preferred to future random bugs.
285 if ($template->param('discussionlink') && istranslation($page)) {
286 $template->param('discussionlink' => htmllink(
289 $masterpage . '/' . gettext("Discussion"),
292 linktext => gettext("Discussion"),
295 # Remove broken parentlink to ./index.html on home page's translations.
296 # It works because this hook has the "last" parameter set, to ensure it
297 # runs after parentlinks' own pagetemplate hook.
298 if ($template->param('parentlinks')
299 && istranslation($page)
300 && $masterpage eq "index") {
301 $template->param('parentlinks' => []);
307 my $page = $params{page};
309 # backlinks involve back-dependencies, so that nicepagetitle effects,
310 # such as translation status displayed in links, are updated
312 map add_depends($page, $_), keys %{$IkiWiki::backlinks{$page}};
315 # Add the renamed page translations to the list of to-be-renamed pages.
316 sub renamepages($$$) {
317 my ($torename, $cgi, $session) = (shift, shift, shift);
319 # copy the initial array, so that we can iterate on it AND
320 # modify it at the same time, without iterating on the items we
321 # pushed on it ourselves
322 my @torename=@{$torename};
324 # Save the page(s) the user asked to rename, so that our
325 # canrename hook can tell the difference between:
326 # - a translation being renamed as a consequence of its master page
328 # - a user trying to directly rename a translation
329 # This is why this hook has to be run first, before @torename is modified
331 $session->param(po_orig_torename => [ @torename ]);
332 IkiWiki::cgi_savesession($session);
334 foreach my $rename (@torename) {
335 next unless istranslatable($rename->{src});
336 my %otherpages=%{otherlanguages($rename->{src})};
337 while (my ($lang, $otherpage) = each %otherpages) {
340 srcfile => $pagesources{$otherpage},
341 dest => otherlanguage($rename->{dest}, $lang),
342 destfile => $rename->{dest}.".".$lang.".po",
352 map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
358 my $updated_po_files=0;
360 # Refresh/create POT and PO files as needed.
361 foreach my $file (grep {istranslatablefile($_)} @rendered) {
362 my $page=pagename($file);
363 my $masterfile=srcfile($file);
364 my $updated_pot_file=0;
365 # Only refresh Pot file if it does not exist, or if
366 # $pagesources{$page} was changed: don't if only the HTML was
367 # refreshed, e.g. because of a dependency.
368 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
369 || ! -e potfile($masterfile)) {
370 refreshpot($masterfile);
375 push @pofiles, $_ if ($updated_pot_file || ! -e $_);
376 } (pofiles($masterfile));
378 refreshpofiles($masterfile, @pofiles);
379 map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
384 if ($updated_po_files) {
386 gettext("updated PO files"),
387 "IkiWiki::Plugin::po::change");
392 my ($page, $content, $cgi, $session) = (shift, shift, shift, shift);
394 if (istranslation($page)) {
395 my $res = isvalidpo($content);
406 sub canremove ($$$) {
407 my ($page, $cgi, $session) = (shift, shift, shift);
409 if (istranslation($page)) {
410 return gettext("Can not remove a translation. Removing the master page, ".
411 "though, removes its translations as well.");
416 sub canrename ($$@) {
417 my ($cgi, $session) = (shift, shift);
420 if (istranslation($params{src})) {
421 my $masterpage = masterpage($params{src});
422 # Tell the difference between:
423 # - a translation being renamed as a consequence of its master page
424 # being renamed, which is allowed
425 # - a user trying to directly rename a translation, which is forbidden
426 # by looking for the master page in the list of to-be-renamed pages we
427 # saved early in the renaming process.
428 my $orig_torename = $session->param("po_orig_torename");
429 unless (scalar grep { $_->{src} eq $masterpage } @{$orig_torename}) {
430 return gettext("Can not rename a translation. Renaming the master page, ".
431 "though, renames its translations as well.");
437 # As we're previewing or saving a page, the content may have
438 # changed, so tell the next filter() invocation it must not be lazy.
442 unsetalreadyfiltered($params{page}, $params{page});
443 return $params{content};
446 sub formbuilder_setup (@) {
448 my $form=$params{form};
451 return unless defined $form->field("do");
453 if ($form->field("do") eq "create") {
454 # Warn the user: new pages must be written in master language.
455 my $template=template("pocreatepage.tmpl");
456 $template->param(LANG => $config{po_master_language}{name});
457 $form->tmpl_param(message => $template->output);
459 elsif ($form->field("do") eq "edit") {
460 # Remove the rename/remove buttons on slave pages.
461 # This has to be done after the rename/remove plugins have added
462 # their buttons, which is why this hook must be run last.
463 # The canrename/canremove hooks already ensure this is forbidden
464 # at the backend level, so this is only UI sugar.
465 if (istranslation($form->field("page"))) {
467 for (my $i = 0; $i < @{$params{buttons}}; $i++) {
468 if (@{$params{buttons}}[$i] eq $_) {
469 delete @{$params{buttons}}[$i];
478 # Do not allow to create pages of type po: they are automatically created.
479 # The main reason to do so is to bypass the "favor the type of linking page
480 # on page creation" logic, which is unsuitable when a broken link is clicked
481 # on a slave (PO) page.
482 sub formbuilder (@) {
484 my $form=$params{form};
487 return unless (defined $form->field("do") && $form->field("do") eq "create");
489 for my $field ($form->field) {
490 next unless "$field" eq "type";
491 if ($field->type eq 'select') {
492 # remove po from the types list
493 my @types = grep { $_ ne 'po' } $field->options;
494 $field->options(\@types) if scalar @types;
497 # make sure the default value is not po;
498 # does this case actually happen?
499 debug "po(formbuilder) ".gettext("type field is not select - not implemented yet");
505 # | Injected functions
508 # Implement po_link_to 'current' and 'negotiated' settings.
509 sub mybestlink ($$) {
513 my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
515 && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
516 && istranslatable($res)
517 && istranslation($page)) {
518 return $res . "." . lang($page);
523 sub mybeautify_urlpath ($) {
526 my $res=$origsubs{'beautify_urlpath'}->($url);
527 if ($config{po_link_to} eq "negotiated") {
528 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
529 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
531 $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
532 } (keys %{$config{po_slave_languages}});
537 sub mytargetpage ($$) {
541 if (istranslation($page) || istranslatable($page)) {
542 my ($masterpage, $lang) = (masterpage($page), lang($page));
543 if (! $config{usedirs} || $masterpage eq 'index') {
544 return $masterpage . "." . $lang . "." . $ext;
547 return $masterpage . "/index." . $lang . "." . $ext;
550 return $origsubs{'targetpage'}->($page, $ext);
558 # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
560 && $config{po_link_to} eq "current"
561 && istranslatable('index')) {
562 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
564 # avoid using our injected beautify_urlpath if run by cgi_editpage,
565 # so that one is redirected to the just-edited page rather than to the
566 # negociated translation; to prevent unnecessary fiddling with caller/inject,
567 # we only do so when our beautify_urlpath would actually do what we want to
568 # avoid, i.e. when po_link_to = negotiated
569 if ($config{po_link_to} eq "negotiated") {
570 my @caller = caller(1);
571 my $run_by_editpage = 0;
572 $run_by_editpage = 1 if (exists $caller[3] && defined $caller[3]
573 && $caller[3] eq "IkiWiki::cgi_editpage");
574 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
576 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
577 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
582 return $origsubs{'urlto'}->($to,$from,$absolute)
586 sub mynicepagetitle ($;$) {
587 my ($page, $unescaped) = (shift, shift);
589 my $res = $origsubs{'nicepagetitle'}->($page, $unescaped);
590 return $res unless istranslation($page);
591 return $res unless $config{po_translation_status_in_links};
592 return $res.' ('.percenttranslated($page).' %)';
596 # | Blackboxes for private data
602 sub alreadyfiltered($$) {
606 return ( exists $filtered{$page}{$destpage}
607 && $filtered{$page}{$destpage} eq 1 );
610 sub setalreadyfiltered($$) {
614 $filtered{$page}{$destpage}=1;
617 sub unsetalreadyfiltered($$) {
621 if (exists $filtered{$page}{$destpage}) {
622 delete $filtered{$page}{$destpage};
626 sub resetalreadyfiltered() {
635 sub maybe_add_leading_slash ($;$) {
638 $add=1 unless defined $add;
639 return '/' . $str if $add;
643 sub istranslatablefile ($) {
646 return 0 unless defined $file;
647 return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
648 return 0 if $file =~ /\.pot$/;
649 return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
653 sub istranslatable ($) {
657 return 1 if istranslatablefile($pagesources{$page});
661 sub _istranslation ($) {
664 my $hasleadingslash = ($page=~s#^/##);
665 my $file=$pagesources{$page};
666 return 0 unless (defined $file
667 && defined pagetype($file)
668 && pagetype($file) eq 'po');
669 return 0 if $file =~ /\.pot$/;
671 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
672 return 0 unless (defined $masterpage && defined $lang
673 && length $masterpage && length $lang
674 && defined $pagesources{$masterpage}
675 && defined $config{po_slave_languages}{$lang});
677 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
678 if istranslatable($masterpage);
681 sub istranslation ($) {
684 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
685 my $hasleadingslash = ($masterpage=~s#^/##);
686 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
687 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
695 if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
704 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
707 return $config{po_master_language}{code};
710 sub islanguagecode ($) {
713 return ($code =~ /^[a-z]{2}$/);
716 sub otherlanguage ($$) {
720 return masterpage($page) if $code eq $config{po_master_language}{code};
721 return masterpage($page) . '.' . $code;
724 sub otherlanguages ($) {
728 return \%ret unless (istranslation($page) || istranslatable($page));
729 my $curlang=lang($page);
731 ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
732 next if $lang eq $curlang;
733 $ret{$lang}=otherlanguage($page, $lang);
739 my $masterfile=shift;
741 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
742 $dir='' if $dir eq './';
743 return File::Spec->catpath('', $dir, $name . ".pot");
747 my $masterfile=shift;
750 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
751 $dir='' if $dir eq './';
752 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
756 my $masterfile=shift;
758 return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
762 my $masterfile=shift;
764 my $potfile=potfile($masterfile);
765 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
766 my $doc=Locale::Po4a::Chooser::new('text',%options);
767 $doc->{TT}{utf_mode} = 1;
768 $doc->{TT}{file_in_charset} = 'utf-8';
769 $doc->{TT}{file_out_charset} = 'utf-8';
770 $doc->read($masterfile);
771 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
772 # this is undocument use of internal Locale::Po4a::TransTractor's data,
773 # compulsory since this module prevents us from using the porefs option.
774 $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
775 $doc->{TT}{po_out}->set_charset('utf-8');
778 IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
779 $doc->writepo($potfile);
782 sub refreshpofiles ($@) {
783 my $masterfile=shift;
786 my $potfile=potfile($masterfile);
788 or error("po(refreshpofiles) ".sprintf(gettext("POT file (%s) does not exist"),
791 foreach my $pofile (@pofiles) {
792 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
794 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
795 or error("po(refreshpofiles) ".
796 sprintf(gettext("failed to update %s"),
800 File::Copy::syscopy($potfile,$pofile)
801 or error("po(refreshpofiles) ".
802 sprintf(gettext("failed to copy the POT file to %s"),
808 sub buildtranslationscache() {
809 # use istranslation's side-effect
810 map istranslation($_), (keys %pagesources);
813 sub resettranslationscache() {
817 sub flushmemoizecache() {
818 Memoize::flush_cache("istranslatable");
819 Memoize::flush_cache("_istranslation");
820 Memoize::flush_cache("percenttranslated");
823 sub urlto_with_orig_beautiful_urlpath($$) {
827 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
828 my $res=urlto($to, $from);
829 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
834 sub percenttranslated ($) {
838 return gettext("N/A") unless istranslation($page);
839 my $file=srcfile($pagesources{$page});
840 my $masterfile = srcfile($pagesources{masterpage($page)});
842 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
844 my $doc=Locale::Po4a::Chooser::new('text',%options);
846 'po_in_name' => [ $file ],
847 'file_in_name' => [ $masterfile ],
848 'file_in_charset' => 'utf-8',
849 'file_out_charset' => 'utf-8',
850 ) or error("po(percenttranslated) ".
851 sprintf(gettext("failed to translate %s"), $page));
852 my ($percent,$hit,$queries) = $doc->stats();
853 $percent =~ s/\.[0-9]+$//;
857 sub languagename ($) {
860 return $config{po_master_language}{name}
861 if $code eq $config{po_master_language}{code};
862 return $config{po_slave_languages}{$code}
863 if defined $config{po_slave_languages}{$code};
867 sub otherlanguagesloop ($) {
871 my %otherpages=%{otherlanguages($page)};
872 while (my ($lang, $otherpage) = each %otherpages) {
873 if (istranslation($page) && masterpage($page) eq $otherpage) {
875 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
877 language => languagename($lang),
883 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
885 language => languagename($lang),
886 percent => percenttranslated($otherpage),
891 return -1 if $a->{code} eq $config{po_master_language}{code};
892 return 1 if $b->{code} eq $config{po_master_language}{code};
893 return $a->{language} cmp $b->{language};
897 sub homepageurl (;$) {
900 return urlto('', $page);
903 sub deletetranslations ($) {
904 my $deletedmasterfile=shift;
906 my $deletedmasterpage=pagename($deletedmasterfile);
909 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
910 my $absfile = "$config{srcdir}/$file";
911 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
912 push @todelete, $file;
914 } keys %{$config{po_slave_languages}};
918 IkiWiki::rcs_remove($_);
921 IkiWiki::prune("$config{srcdir}/$_");
925 if (scalar @todelete) {
927 gettext("removed obsolete PO files"),
928 "IkiWiki::Plugin::po::deletetranslations");
932 sub commit_and_refresh ($$) {
933 my ($msg, $author) = (shift, shift);
936 IkiWiki::disable_commit_hook();
937 IkiWiki::rcs_commit_staged($msg, $author, "127.0.0.1");
938 IkiWiki::enable_commit_hook();
939 IkiWiki::rcs_update();
941 # Reinitialize module's private variables.
942 resetalreadyfiltered();
943 resettranslationscache();
945 # Trigger a wiki refresh.
946 require IkiWiki::Render;
947 # without preliminary saveindex/loadindex, refresh()
948 # complains about a lot of uninitialized variables
949 IkiWiki::saveindex();
950 IkiWiki::loadindex();
952 IkiWiki::saveindex();
955 # on success, returns the filtered content.
956 # on error, if $nonfatal, warn and return undef; else, error out.
957 sub po_to_markup ($$;$) {
958 my ($page, $content) = (shift, shift);
959 my $nonfatal = shift;
961 $content = '' unless defined $content;
962 $content = decode_utf8(encode_utf8($content));
963 # CRLF line terminators make poor Locale::Po4a feel bad
964 $content=~s/\r\n/\n/g;
966 # There are incompatibilities between some File::Temp versions
967 # (including 0.18, bundled with Lenny's perl-modules package)
968 # and others (e.g. 0.20, previously present in the archive as
969 # a standalone package): under certain circumstances, some
970 # return a relative filename, whereas others return an absolute one;
971 # we here use this module in a way that is at least compatible
972 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
973 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
974 DIR => File::Spec->tmpdir,
975 UNLINK => 1)->filename;
976 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
977 DIR => File::Spec->tmpdir,
978 UNLINK => 1)->filename;
981 my $msg = "po(po_to_markup) - $page : " . shift;
986 error($msg, sub { unlink $infile, $outfile});
989 writefile(basename($infile), File::Spec->tmpdir, $content)
990 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
992 my $masterfile = srcfile($pagesources{masterpage($page)});
994 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
996 my $doc=Locale::Po4a::Chooser::new('text',%options);
998 'po_in_name' => [ $infile ],
999 'file_in_name' => [ $masterfile ],
1000 'file_in_charset' => 'utf-8',
1001 'file_out_charset' => 'utf-8',
1002 ) or return $fail->(gettext("failed to translate"));
1003 $doc->write($outfile)
1004 or return $fail->(sprintf(gettext("failed to write %s"), $outfile));
1006 $content = readfile($outfile)
1007 or return $fail->(sprintf(gettext("failed to read %s"), $outfile));
1009 # Unlinking should happen automatically, thanks to File::Temp,
1010 # but it does not work here, probably because of the way writefile()
1011 # and Locale::Po4a::write() work.
1012 unlink $infile, $outfile;
1017 # returns a SuccessReason or FailReason object
1019 my $content = shift;
1021 # NB: we don't use po_to_markup here, since Po4a parser does
1022 # not mind invalid PO content
1023 $content = '' unless defined $content;
1024 $content = decode_utf8(encode_utf8($content));
1026 # There are incompatibilities between some File::Temp versions
1027 # (including 0.18, bundled with Lenny's perl-modules package)
1028 # and others (e.g. 0.20, previously present in the archive as
1029 # a standalone package): under certain circumstances, some
1030 # return a relative filename, whereas others return an absolute one;
1031 # we here use this module in a way that is at least compatible
1032 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1033 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-isvalidpo.XXXXXXXXXX",
1034 DIR => File::Spec->tmpdir,
1035 UNLINK => 1)->filename;
1037 my $fail = sub ($) {
1038 my $msg = '[po/isvalidpo] ' . shift;
1040 return IkiWiki::FailReason->new("$msg");
1043 writefile(basename($infile), File::Spec->tmpdir, $content)
1044 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1046 my $res = (system("msgfmt", "--check", $infile, "-o", "/dev/null") == 0);
1048 # Unlinking should happen automatically, thanks to File::Temp,
1049 # but it does not work here, probably because of the way writefile()
1050 # and Locale::Po4a::write() work.
1054 return IkiWiki::SuccessReason->new("valid gettext data");
1056 return IkiWiki::FailReason->new("invalid gettext data");
1063 package IkiWiki::PageSpec;
1068 sub match_istranslation ($;@) {
1071 if (IkiWiki::Plugin::po::istranslation($page)) {
1072 return IkiWiki::SuccessReason->new("is a translation page");
1075 return IkiWiki::FailReason->new("is not a translation page");
1079 sub match_istranslatable ($;@) {
1082 if (IkiWiki::Plugin::po::istranslatable($page)) {
1083 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
1086 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
1090 sub match_lang ($$;@) {
1094 my $regexp=IkiWiki::glob2re($wanted);
1095 my $lang=IkiWiki::Plugin::po::lang($page);
1096 if ($lang!~/^$regexp$/i) {
1097 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
1100 return IkiWiki::SuccessReason->new("file language is $wanted");
1104 sub match_currentlang ($$;@) {
1109 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
1111 my $currentlang=IkiWiki::Plugin::po::lang($params{location});
1112 my $lang=IkiWiki::Plugin::po::lang($page);
1114 if ($lang eq $currentlang) {
1115 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
1118 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");