2 # .po as a wiki page type
3 # Licensed under GPL v2 or greater
4 # Copyright (C) 2008 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);
47 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
48 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
49 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
50 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
51 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
52 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
53 $origsubs{'urlto'}=\&IkiWiki::urlto;
54 inject(name => "IkiWiki::urlto", call => \&myurlto);
55 $origsubs{'nicepagetitle'}=\&IkiWiki::nicepagetitle;
56 inject(name => "IkiWiki::nicepagetitle", call => \&mynicepagetitle);
65 # 2. Injected functions
66 # 3. Blackboxes for private data
81 po_master_language => {
87 description => "master language (non-PO files)",
91 po_slave_languages => {
98 description => "slave languages (PO files)",
102 po_translatable_pages => {
104 example => "!*/Discussion",
105 description => "PageSpec controlling which pages are translatable",
106 link => "ikiwiki/PageSpec",
112 example => "current",
113 description => "internal linking behavior (default/current/negotiated)",
117 po_translation_status_in_links => {
120 description => "display translation status in links to translations",
127 foreach my $field (qw{po_master_language po_slave_languages}) {
128 if (! exists $config{$field} || ! defined $config{$field}) {
129 error(sprintf(gettext("Must specify %s"), $field));
132 if (! (keys %{$config{po_slave_languages}})) {
133 error(gettext("At least one slave language must be defined in po_slave_languages"));
137 or error(sprintf(gettext("%s is not a valid language code"), $_));
138 } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
139 if (! exists $config{po_translatable_pages} ||
140 ! defined $config{po_translatable_pages}) {
141 $config{po_translatable_pages}="";
143 if (! exists $config{po_link_to} ||
144 ! defined $config{po_link_to}) {
145 $config{po_link_to}='default';
148 $config{po_link_to} eq $_
149 } ('default', 'current', 'negotiated')) {
150 warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
151 $config{po_link_to}));
152 $config{po_link_to}='default';
154 elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
155 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
156 $config{po_link_to}='default';
158 if (! exists $config{po_translation_status_in_links} ||
159 ! defined $config{po_translation_status_in_links}) {
160 $config{po_translation_status_in_links}=1;
162 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
166 my $needsbuild=shift;
168 # backup @needsbuild content so that change() can know whether
169 # a given master page was rendered because its source file was changed
170 @origneedsbuild=(@$needsbuild);
173 buildtranslationscache();
175 # make existing translations depend on the corresponding master page
176 foreach my $master (keys %translations) {
177 map add_depends($_, $master), values %{otherlanguages($master)};
181 # Massage the recorded state of internal links so that:
182 # - it matches the actually generated links, rather than the links as written
183 # in the pages' source
184 # - backlinks are consistent in all cases
187 my $page=$params{page};
188 my $content=$params{content};
190 return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
192 if (istranslation($page)) {
193 foreach my $destpage (@{$links{$page}}) {
194 if (istranslatable($destpage)) {
195 # replace one occurence of $destpage in $links{$page}
196 # (we only want to replace the one that was added by
197 # IkiWiki::Plugin::link::scan, other occurences may be
198 # there for other reasons)
199 for (my $i=0; $i<@{$links{$page}}; $i++) {
200 if (@{$links{$page}}[$i] eq $destpage) {
201 @{$links{$page}}[$i] = $destpage . '.' . lang($page);
208 elsif (! istranslatable($page) && ! istranslation($page)) {
209 foreach my $destpage (@{$links{$page}}) {
210 if (istranslatable($destpage)) {
211 # make sure any destpage's translations has
212 # $page in its backlinks
213 push @{$links{$page}},
214 values %{otherlanguages($destpage)};
220 # We use filter to convert PO to the master page's format,
221 # since the rest of ikiwiki should not work on PO files.
225 my $page = $params{page};
226 my $destpage = $params{destpage};
227 my $content = decode_utf8(encode_utf8($params{content}));
229 return $content if ( ! istranslation($page)
230 || alreadyfiltered($page, $destpage) );
232 # CRLF line terminators make poor Locale::Po4a feel bad
233 $content=~s/\r\n/\n/g;
235 # There are incompatibilities between some File::Temp versions
236 # (including 0.18, bundled with Lenny's perl-modules package)
237 # and others (e.g. 0.20, previously present in the archive as
238 # a standalone package): under certain circumstances, some
239 # return a relative filename, whereas others return an absolute one;
240 # we here use this module in a way that is at least compatible
241 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
242 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
243 DIR => File::Spec->tmpdir,
244 UNLINK => 1)->filename;
245 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
246 DIR => File::Spec->tmpdir,
247 UNLINK => 1)->filename;
249 writefile(basename($infile), File::Spec->tmpdir, $content);
251 my $masterfile = srcfile($pagesources{masterpage($page)});
253 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
255 my $doc=Locale::Po4a::Chooser::new('text',%options);
257 'po_in_name' => [ $infile ],
258 'file_in_name' => [ $masterfile ],
259 'file_in_charset' => 'utf-8',
260 'file_out_charset' => 'utf-8',
261 ) or error("[po/filter:$page]: failed to translate");
262 $doc->write($outfile) or error("[po/filter:$page] could not write $outfile");
263 $content = readfile($outfile) or error("[po/filter:$page] could not read $outfile");
265 # Unlinking should happen automatically, thanks to File::Temp,
266 # but it does not work here, probably because of the way writefile()
267 # and Locale::Po4a::write() work.
268 unlink $infile, $outfile;
270 setalreadyfiltered($page, $destpage);
277 my $page = $params{page};
278 my $content = $params{content};
280 # ignore PO files this plugin did not create
281 return $content unless istranslation($page);
283 # force content to be htmlize'd as if it was the same type as the master page
284 return IkiWiki::htmlize($page, $page,
285 pagetype(srcfile($pagesources{masterpage($page)})),
289 sub pagetemplate (@) {
291 my $page=$params{page};
292 my $destpage=$params{destpage};
293 my $template=$params{template};
295 my ($masterpage, $lang) = istranslation($page);
297 if (istranslation($page) && $template->query(name => "percenttranslated")) {
298 $template->param(percenttranslated => percenttranslated($page));
300 if ($template->query(name => "istranslation")) {
301 $template->param(istranslation => scalar istranslation($page));
303 if ($template->query(name => "istranslatable")) {
304 $template->param(istranslatable => istranslatable($page));
306 if ($template->query(name => "HOMEPAGEURL")) {
307 $template->param(homepageurl => homepageurl($page));
309 if ($template->query(name => "otherlanguages")) {
310 $template->param(otherlanguages => [otherlanguagesloop($page)]);
311 map add_depends($page, $_), (values %{otherlanguages($page)});
313 # Rely on IkiWiki::Render's genpage() to decide wether
314 # a discussion link should appear on $page; this is not
315 # totally accurate, though: some broken links may be generated
316 # when cgiurl is disabled.
317 # This compromise avoids some code duplication, and will probably
318 # prevent future breakage when ikiwiki internals change.
319 # Known limitations are preferred to future random bugs.
320 if ($template->param('discussionlink') && istranslation($page)) {
321 $template->param('discussionlink' => htmllink(
324 $masterpage . '/' . gettext("Discussion"),
327 linktext => gettext("Discussion"),
330 # Remove broken parentlink to ./index.html on home page's translations.
331 # It works because this hook has the "last" parameter set, to ensure it
332 # runs after parentlinks' own pagetemplate hook.
333 if ($template->param('parentlinks')
334 && istranslation($page)
335 && $masterpage eq "index") {
336 $template->param('parentlinks' => []);
342 my $page = $params{page};
344 # backlinks involve back-dependencies, so that nicepagetitle effects,
345 # such as translation status displayed in links, are updated
347 map add_depends($page, $_), keys %{$IkiWiki::backlinks{$page}};
350 # Add the renamed page translations to the list of to-be-renamed pages.
351 sub renamepages($$$) {
352 my ($torename, $cgi, $session) = (shift, shift, shift);
354 # copy the initial array, so that we can iterate on it AND
355 # modify it at the same time, without iterating on the items we
356 # pushed on it ourselves
357 my @torename=@{$torename};
359 # Save the page(s) the user asked to rename, so that our
360 # canrename hook can tell the difference between:
361 # - a translation being renamed as a consequence of its master page
363 # - a user trying to directly rename a translation
364 # This is why this hook has to be run first, before @torename is modified
366 $session->param(po_orig_torename => [ @torename ]);
367 IkiWiki::cgi_savesession($session);
369 foreach my $rename (@torename) {
370 next unless istranslatable($rename->{src});
371 my %otherpages=%{otherlanguages($rename->{src})};
372 while (my ($lang, $otherpage) = each %otherpages) {
375 srcfile => $pagesources{$otherpage},
376 dest => otherlanguage($rename->{dest}, $lang),
377 destfile => $rename->{dest}.".".$lang.".po",
387 map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
393 my $updated_po_files=0;
395 # Refresh/create POT and PO files as needed.
396 foreach my $file (grep {istranslatablefile($_)} @rendered) {
397 my $page=pagename($file);
398 my $masterfile=srcfile($file);
399 my $updated_pot_file=0;
400 # Only refresh Pot file if it does not exist, or if
401 # $pagesources{$page} was changed: don't if only the HTML was
402 # refreshed, e.g. because of a dependency.
403 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
404 || ! -e potfile($masterfile)) {
405 refreshpot($masterfile);
410 push @pofiles, $_ if ($updated_pot_file || ! -e $_);
411 } (pofiles($masterfile));
413 refreshpofiles($masterfile, @pofiles);
414 map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
419 if ($updated_po_files) {
421 gettext("updated PO files"),
422 "IkiWiki::Plugin::po::change");
427 my ($page, $content, $cgi, $session) = (shift, shift, shift, shift);
429 debug("po plugin running in cansave")
432 sub canremove ($$$) {
433 my ($page, $cgi, $session) = (shift, shift, shift);
435 if (istranslation($page)) {
436 return gettext("Can not remove a translation. Removing the master page, ".
437 "though, removes its translations as well.");
442 sub canrename ($$@) {
443 my ($cgi, $session) = (shift, shift);
446 if (istranslation($params{src})) {
447 my $masterpage = masterpage($params{src});
448 # Tell the difference between:
449 # - a translation being renamed as a consequence of its master page
450 # being renamed, which is allowed
451 # - a user trying to directly rename a translation, which is forbidden
452 # by looking for the master page in the list of to-be-renamed pages we
453 # saved early in the renaming process.
454 my $orig_torename = $session->param("po_orig_torename");
455 unless (scalar grep { $_->{src} eq $masterpage } @{$orig_torename}) {
456 return gettext("Can not rename a translation. Renaming the master page, ".
457 "though, renames its translations as well.");
463 # As we're previewing or saving a page, the content may have
464 # changed, so tell the next filter() invocation it must not be lazy.
468 unsetalreadyfiltered($params{page}, $params{page});
469 return $params{content};
474 # | Injected functions
477 # Implement po_link_to 'current' and 'negotiated' settings.
478 sub mybestlink ($$) {
482 my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
484 && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
485 && istranslatable($res)
486 && istranslation($page)) {
487 return $res . "." . lang($page);
492 sub mybeautify_urlpath ($) {
495 my $res=$origsubs{'beautify_urlpath'}->($url);
496 if ($config{po_link_to} eq "negotiated") {
497 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
498 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
500 $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
501 } (keys %{$config{po_slave_languages}});
506 sub mytargetpage ($$) {
510 if (istranslation($page) || istranslatable($page)) {
511 my ($masterpage, $lang) = (masterpage($page), lang($page));
512 if (! $config{usedirs} || $masterpage eq 'index') {
513 return $masterpage . "." . $lang . "." . $ext;
516 return $masterpage . "/index." . $lang . "." . $ext;
519 return $origsubs{'targetpage'}->($page, $ext);
527 # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
529 && $config{po_link_to} eq "current"
530 && istranslatable('index')) {
531 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
533 # avoid using our injected beautify_urlpath if run by cgi_editpage,
534 # so that one is redirected to the just-edited page rather than to the
535 # negociated translation; to prevent unnecessary fiddling with caller/inject,
536 # we only do so when our beautify_urlpath would actually do what we want to
537 # avoid, i.e. when po_link_to = negotiated
538 if ($config{po_link_to} eq "negotiated") {
539 my @caller = caller(1);
540 my $run_by_editpage = 0;
541 $run_by_editpage = 1 if (exists $caller[3] && defined $caller[3]
542 && $caller[3] eq "IkiWiki::cgi_editpage");
543 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
545 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
546 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
551 return $origsubs{'urlto'}->($to,$from,$absolute)
555 sub mynicepagetitle ($;$) {
556 my ($page, $unescaped) = (shift, shift);
558 my $res = $origsubs{'nicepagetitle'}->($page, $unescaped);
559 return $res unless istranslation($page);
560 return $res unless $config{po_translation_status_in_links};
561 return $res.' ('.percenttranslated($page).' %)';
565 # | Blackboxes for private data
571 sub alreadyfiltered($$) {
575 return ( exists $filtered{$page}{$destpage}
576 && $filtered{$page}{$destpage} eq 1 );
579 sub setalreadyfiltered($$) {
583 $filtered{$page}{$destpage}=1;
586 sub unsetalreadyfiltered($$) {
590 if (exists $filtered{$page}{$destpage}) {
591 delete $filtered{$page}{$destpage};
595 sub resetalreadyfiltered() {
604 sub maybe_add_leading_slash ($;$) {
607 $add=1 unless defined $add;
608 return '/' . $str if $add;
612 sub istranslatablefile ($) {
615 return 0 unless defined $file;
616 return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
617 return 0 if $file =~ /\.pot$/;
618 return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
622 sub istranslatable ($) {
626 return 1 if istranslatablefile($pagesources{$page});
630 sub _istranslation ($) {
633 my $hasleadingslash = ($page=~s#^/##);
634 my $file=$pagesources{$page};
635 return 0 unless (defined $file
636 && defined pagetype($file)
637 && pagetype($file) eq 'po');
638 return 0 if $file =~ /\.pot$/;
640 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
641 return 0 unless (defined $masterpage && defined $lang
642 && length $masterpage && length $lang
643 && defined $pagesources{$masterpage}
644 && defined $config{po_slave_languages}{$lang});
646 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
647 if istranslatable($masterpage);
650 sub istranslation ($) {
653 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
654 my $hasleadingslash = ($masterpage=~s#^/##);
655 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
656 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
664 if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
673 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
676 return $config{po_master_language}{code};
679 sub islanguagecode ($) {
682 return ($code =~ /^[a-z]{2}$/);
685 sub otherlanguage ($$) {
689 return masterpage($page) if $code eq $config{po_master_language}{code};
690 return masterpage($page) . '.' . $code;
693 sub otherlanguages ($) {
697 return \%ret unless (istranslation($page) || istranslatable($page));
698 my $curlang=lang($page);
700 ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
701 next if $lang eq $curlang;
702 $ret{$lang}=otherlanguage($page, $lang);
708 my $masterfile=shift;
710 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
711 $dir='' if $dir eq './';
712 return File::Spec->catpath('', $dir, $name . ".pot");
716 my $masterfile=shift;
719 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
720 $dir='' if $dir eq './';
721 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
725 my $masterfile=shift;
727 return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
731 my $masterfile=shift;
733 my $potfile=potfile($masterfile);
734 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
735 my $doc=Locale::Po4a::Chooser::new('text',%options);
736 $doc->{TT}{utf_mode} = 1;
737 $doc->{TT}{file_in_charset} = 'utf-8';
738 $doc->{TT}{file_out_charset} = 'utf-8';
739 $doc->read($masterfile);
740 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
741 # this is undocument use of internal Locale::Po4a::TransTractor's data,
742 # compulsory since this module prevents us from using the porefs option.
743 $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
744 $doc->{TT}{po_out}->set_charset('utf-8');
747 IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
748 $doc->writepo($potfile);
751 sub refreshpofiles ($@) {
752 my $masterfile=shift;
755 my $potfile=potfile($masterfile);
756 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
758 foreach my $pofile (@pofiles) {
759 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
761 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
762 or error("[po/refreshpofiles:$pofile] failed to update");
765 File::Copy::syscopy($potfile,$pofile)
766 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
771 sub buildtranslationscache() {
772 # use istranslation's side-effect
773 map istranslation($_), (keys %pagesources);
776 sub resettranslationscache() {
780 sub flushmemoizecache() {
781 Memoize::flush_cache("istranslatable");
782 Memoize::flush_cache("_istranslation");
783 Memoize::flush_cache("percenttranslated");
786 sub urlto_with_orig_beautiful_urlpath($$) {
790 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
791 my $res=urlto($to, $from);
792 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
797 sub percenttranslated ($) {
801 return gettext("N/A") unless istranslation($page);
802 my $file=srcfile($pagesources{$page});
803 my $masterfile = srcfile($pagesources{masterpage($page)});
805 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
807 my $doc=Locale::Po4a::Chooser::new('text',%options);
809 'po_in_name' => [ $file ],
810 'file_in_name' => [ $masterfile ],
811 'file_in_charset' => 'utf-8',
812 'file_out_charset' => 'utf-8',
813 ) or error("[po/percenttranslated:$page]: failed to translate");
814 my ($percent,$hit,$queries) = $doc->stats();
818 sub languagename ($) {
821 return $config{po_master_language}{name}
822 if $code eq $config{po_master_language}{code};
823 return $config{po_slave_languages}{$code}
824 if defined $config{po_slave_languages}{$code};
828 sub otherlanguagesloop ($) {
832 my %otherpages=%{otherlanguages($page)};
833 while (my ($lang, $otherpage) = each %otherpages) {
834 if (istranslation($page) && masterpage($page) eq $otherpage) {
836 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
838 language => languagename($lang),
844 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
846 language => languagename($lang),
847 percent => percenttranslated($otherpage),
852 return -1 if $a->{code} eq $config{po_master_language}{code};
853 return 1 if $b->{code} eq $config{po_master_language}{code};
854 return $a->{language} cmp $b->{language};
858 sub homepageurl (;$) {
861 return urlto('', $page);
864 sub deletetranslations ($) {
865 my $deletedmasterfile=shift;
867 my $deletedmasterpage=pagename($deletedmasterfile);
870 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
871 my $absfile = "$config{srcdir}/$file";
872 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
873 push @todelete, $file;
875 } keys %{$config{po_slave_languages}};
879 IkiWiki::rcs_remove($_);
882 IkiWiki::prune("$config{srcdir}/$_");
886 if (scalar @todelete) {
888 gettext("removed obsolete PO files"),
889 "IkiWiki::Plugin::po::deletetranslations");
893 sub commit_and_refresh ($$) {
894 my ($msg, $author) = (shift, shift);
897 IkiWiki::disable_commit_hook();
898 IkiWiki::rcs_commit_staged($msg, $author, "127.0.0.1");
899 IkiWiki::enable_commit_hook();
900 IkiWiki::rcs_update();
902 # Reinitialize module's private variables.
903 resetalreadyfiltered();
904 resettranslationscache();
906 # Trigger a wiki refresh.
907 require IkiWiki::Render;
908 # without preliminary saveindex/loadindex, refresh()
909 # complains about a lot of uninitialized variables
910 IkiWiki::saveindex();
911 IkiWiki::loadindex();
913 IkiWiki::saveindex();
920 package IkiWiki::PageSpec;
925 sub match_istranslation ($;@) {
928 if (IkiWiki::Plugin::po::istranslation($page)) {
929 return IkiWiki::SuccessReason->new("is a translation page");
932 return IkiWiki::FailReason->new("is not a translation page");
936 sub match_istranslatable ($;@) {
939 if (IkiWiki::Plugin::po::istranslatable($page)) {
940 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
943 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
947 sub match_lang ($$;@) {
951 my $regexp=IkiWiki::glob2re($wanted);
952 my $lang=IkiWiki::Plugin::po::lang($page);
953 if ($lang!~/^$regexp$/i) {
954 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
957 return IkiWiki::SuccessReason->new("file language is $wanted");
961 sub match_currentlang ($$;@) {
966 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
968 my $currentlang=IkiWiki::Plugin::po::lang($params{location});
969 my $lang=IkiWiki::Plugin::po::lang($page);
971 if ($lang eq $currentlang) {
972 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
975 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");