po: abstract %filtered implementation details
[ikiwiki] / 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 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;
8
9 use warnings;
10 use strict;
11 use IkiWiki 2.00;
12 use Encode;
13 use Locale::Po4a::Chooser;
14 use Locale::Po4a::Po;
15 use File::Basename;
16 use File::Copy;
17 use File::Spec;
18 use File::Temp;
19 use Memoize;
20 use UNIVERSAL;
21
22 my %translations;
23 my @origneedsbuild;
24 our %filtered;
25
26 memoize("_istranslation");
27 memoize("percenttranslated");
28 # FIXME: memoizing istranslatable() makes some test cases fail once every
29 # two tries; this may be related to the artificial way the testsuite is
30 # run, or not.
31 # memoize("istranslatable");
32
33 # backup references to subs that will be overriden
34 my %origsubs;
35
36 sub import { #{{{
37         hook(type => "getsetup", id => "po", call => \&getsetup);
38         hook(type => "checkconfig", id => "po", call => \&checkconfig);
39         hook(type => "needsbuild", id => "po", call => \&needsbuild);
40         hook(type => "scan", id => "po", call => \&scan, last =>1);
41         hook(type => "filter", id => "po", call => \&filter);
42         hook(type => "htmlize", id => "po", call => \&htmlize);
43         hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
44         hook(type => "change", id => "po", call => \&change);
45         hook(type => "editcontent", id => "po", call => \&editcontent);
46
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 } #}}}
56
57 sub getsetup () { #{{{
58         return
59                 plugin => {
60                         safe => 0,
61                         rebuild => 1, # format plugin & changes html filenames
62                 },
63                 po_master_language => {
64                         type => "string",
65                         example => {
66                                 'code' => 'en',
67                                 'name' => 'English'
68                         },
69                         description => "master language (non-PO files)",
70                         safe => 1,
71                         rebuild => 1,
72                 },
73                 po_slave_languages => {
74                         type => "string",
75                         example => {
76                                 'fr' => 'Français',
77                                 'es' => 'Castellano',
78                                 'de' => 'Deutsch'
79                         },
80                         description => "slave languages (PO files)",
81                         safe => 1,
82                         rebuild => 1,
83                 },
84                 po_translatable_pages => {
85                         type => "pagespec",
86                         example => "!*/Discussion",
87                         description => "PageSpec controlling which pages are translatable",
88                         link => "ikiwiki/PageSpec",
89                         safe => 1,
90                         rebuild => 1,
91                 },
92                 po_link_to => {
93                         type => "string",
94                         example => "current",
95                         description => "internal linking behavior (default/current/negotiated)",
96                         safe => 1,
97                         rebuild => 1,
98                 },
99 } #}}}
100
101 sub islanguagecode ($) { #{{{
102     my $code=shift;
103     return ($code =~ /^[a-z]{2}$/);
104 } #}}}
105
106 sub checkconfig () { #{{{
107         foreach my $field (qw{po_master_language po_slave_languages}) {
108                 if (! exists $config{$field} || ! defined $config{$field}) {
109                         error(sprintf(gettext("Must specify %s"), $field));
110                 }
111         }
112         if (! (keys %{$config{po_slave_languages}})) {
113                 error(gettext("At least one slave language must be defined in po_slave_languages"));
114         }
115         map {
116                 islanguagecode($_)
117                         or error(sprintf(gettext("%s is not a valid language code"), $_));
118         } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
119         if (! exists $config{po_translatable_pages} ||
120             ! defined $config{po_translatable_pages}) {
121                 $config{po_translatable_pages}="";
122         }
123         if (! exists $config{po_link_to} ||
124             ! defined $config{po_link_to}) {
125                 $config{po_link_to}='default';
126         }
127         elsif (! grep {
128                         $config{po_link_to} eq $_
129                 } ('default', 'current', 'negotiated')) {
130                 warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
131                                 $config{po_link_to}));
132                 $config{po_link_to}='default';
133         }
134         elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
135                 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
136                 $config{po_link_to}='default';
137         }
138         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
139 } #}}}
140
141 sub potfile ($) { #{{{
142         my $masterfile=shift;
143
144         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
145         $dir='' if $dir eq './';
146         return File::Spec->catpath('', $dir, $name . ".pot");
147 } #}}}
148
149 sub pofile ($$) { #{{{
150         my $masterfile=shift;
151         my $lang=shift;
152
153         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
154         $dir='' if $dir eq './';
155         return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
156 } #}}}
157
158 sub pofiles ($) { #{{{
159         my $masterfile=shift;
160         return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
161 } #}}}
162
163 sub refreshpot ($) { #{{{
164         my $masterfile=shift;
165
166         my $potfile=potfile($masterfile);
167         my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
168         my $doc=Locale::Po4a::Chooser::new('text',%options);
169         $doc->{TT}{utf_mode} = 1;
170         $doc->{TT}{file_in_charset} = 'utf-8';
171         $doc->{TT}{file_out_charset} = 'utf-8';
172         $doc->read($masterfile);
173         # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
174         # this is undocument use of internal Locale::Po4a::TransTractor's data,
175         # compulsory since this module prevents us from using the porefs option.
176         my %po_options = ('porefs' => 'none');
177         $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
178         $doc->{TT}{po_out}->set_charset('utf-8');
179         # do the actual work
180         $doc->parse;
181         IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
182         $doc->writepo($potfile);
183 } #}}}
184
185 sub refreshpofiles ($@) { #{{{
186         my $masterfile=shift;
187         my @pofiles=@_;
188
189         my $potfile=potfile($masterfile);
190         error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
191
192         foreach my $pofile (@pofiles) {
193                 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
194                 if (-e $pofile) {
195                         system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
196                                 or error("[po/refreshpofiles:$pofile] failed to update");
197                 }
198                 else {
199                         File::Copy::syscopy($potfile,$pofile)
200                                 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
201                 }
202         }
203 } #}}}
204
205 sub needsbuild () { #{{{
206         my $needsbuild=shift;
207
208         # backup @needsbuild content so that change() can know whether
209         # a given master page was rendered because its source file was changed
210         @origneedsbuild=(@$needsbuild);
211
212         # build %translations, using istranslation's side-effect
213         map istranslation($_), (keys %pagesources);
214
215         # make existing translations depend on the corresponding master page
216         foreach my $master (keys %translations) {
217                 foreach my $slave (values %{$translations{$master}}) {
218                         add_depends($slave, $master);
219                 }
220         }
221 } #}}}
222
223 sub scan (@) { #{{{
224         my %params=@_;
225         my $page=$params{page};
226         my $content=$params{content};
227
228         return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
229
230         if (istranslation($page)) {
231                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
232                 foreach my $destpage (@{$links{$page}}) {
233                         if (istranslatable($destpage)) {
234                                 # replace one occurence of $destpage in $links{$page}
235                                 # (we only want to replace the one that was added by
236                                 # IkiWiki::Plugin::link::scan, other occurences may be
237                                 # there for other reasons)
238                                 for (my $i=0; $i<@{$links{$page}}; $i++) {
239                                         if (@{$links{$page}}[$i] eq $destpage) {
240                                                 @{$links{$page}}[$i] = $destpage . '.' . $curlang;
241                                                 last;
242                                         }
243                                 }
244                         }
245                 }
246         }
247         elsif (! istranslatable($page) && ! istranslation($page)) {
248                 foreach my $destpage (@{$links{$page}}) {
249                         if (istranslatable($destpage)) {
250                                 map {
251                                         push @{$links{$page}}, $destpage . '.' . $_;
252                                 } (keys %{$config{po_slave_languages}});
253                         }
254                 }
255         }
256 } #}}}
257
258 sub mytargetpage ($$) { #{{{
259         my $page=shift;
260         my $ext=shift;
261
262         if (istranslation($page)) {
263                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
264                 if (! $config{usedirs} || $masterpage eq 'index') {
265                         return $masterpage . "." . $lang . "." . $ext;
266                 }
267                 else {
268                         return $masterpage . "/index." . $lang . "." . $ext;
269                 }
270         }
271         elsif (istranslatable($page)) {
272                 if (! $config{usedirs} || $page eq 'index') {
273                         return $page . "." . $config{po_master_language}{code} . "." . $ext;
274                 }
275                 else {
276                         return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
277                 }
278         }
279         return $origsubs{'targetpage'}->($page, $ext);
280 } #}}}
281
282 sub mybeautify_urlpath ($) { #{{{
283         my $url=shift;
284
285         my $res=$origsubs{'beautify_urlpath'}->($url);
286         if ($config{po_link_to} eq "negotiated") {
287                 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
288         }
289         return $res;
290 } #}}}
291
292 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
293         my $to=shift;
294         my $from=shift;
295
296         inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
297         my $res=urlto($to, $from);
298         inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
299
300         return $res;
301 } #}}}
302
303 sub myurlto ($$;$) { #{{{
304         my $to=shift;
305         my $from=shift;
306         my $absolute=shift;
307
308         # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
309         if (! length $to
310             && $config{po_link_to} eq "current"
311             && istranslation($from)
312             && istranslatable('index')) {
313                 my ($masterpage, $curlang) = ($from =~ /(.*)[.]([a-z]{2})$/);
314                 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . $curlang . ".$config{htmlext}");
315         }
316         return $origsubs{'urlto'}->($to,$from,$absolute);
317 } #}}}
318
319 sub mybestlink ($$) { #{{{
320         my $page=shift;
321         my $link=shift;
322
323         my $res=$origsubs{'bestlink'}->($page, $link);
324         if (length $res) {
325                 if ($config{po_link_to} eq "current"
326                     && istranslatable($res)
327                     && istranslation($page)) {
328                         my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
329                         return $res . "." . $curlang;
330                 }
331                 else {
332                         return $res;
333                 }
334         }
335         return "";
336 } #}}}
337
338 sub alreadyfiltered($$) { #{{{
339         my $page=shift;
340         my $destpage=shift;
341
342         return ( exists $filtered{$page}{$destpage}
343                  && $filtered{$page}{$destpage} eq 1 );
344 } #}}}
345 sub setalreadyfiltered($$) { #{{{
346         my $page=shift;
347         my $destpage=shift;
348
349         $filtered{$page}{$destpage}=1;
350 } #}}}
351 sub unsetalreadyfiltered($$) { #{{{
352         my $page=shift;
353         my $destpage=shift;
354
355         if (exists $filtered{$page}{$destpage}) {
356                 delete $filtered{$page}{$destpage};
357         }
358 } #}}}
359 sub resetalreadyfiltered() { #{{{
360         undef %filtered;
361 } #}}}
362
363 # We use filter to convert PO to the master page's format,
364 # since the rest of ikiwiki should not work on PO files.
365 sub filter (@) { #{{{
366         my %params = @_;
367
368         my $page = $params{page};
369         my $destpage = $params{destpage};
370         my $content = decode_utf8(encode_utf8($params{content}));
371
372         return $content if ( ! istranslation($page)
373                              || alreadyfiltered($page, $destpage) );
374
375         # CRLF line terminators make poor Locale::Po4a feel bad
376         $content=~s/\r\n/\n/g;
377
378         # Implementation notes
379         #
380         # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
381         #    to learn how to disguise a variable as a file.
382         # 2. There are incompatibilities between some File::Temp versions
383         #    (including 0.18, bundled with Lenny's perl-modules package)
384         #    and others (e.g. 0.20, previously present in the archive as
385         #    a standalone package): under certain circumstances, some
386         #    return a relative filename, whereas others return an absolute one;
387         #    we here use this module in a way that is at least compatible
388         #    with 0.18 and 0.20. Beware, hit'n'run refactorers!
389         my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
390                                     DIR => File::Spec->tmpdir,
391                                     UNLINK => 1)->filename;
392         my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
393                                      DIR => File::Spec->tmpdir,
394                                      UNLINK => 1)->filename;
395
396         writefile(basename($infile), File::Spec->tmpdir, $content);
397
398         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
399         my $masterfile = srcfile($pagesources{$masterpage});
400         my (@pos,@masters);
401         push @pos,$infile;
402         push @masters,$masterfile;
403         my %options = (
404                 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
405         );
406         my $doc=Locale::Po4a::Chooser::new('text',%options);
407         $doc->process(
408                 'po_in_name'    => \@pos,
409                 'file_in_name'  => \@masters,
410                 'file_in_charset'  => 'utf-8',
411                 'file_out_charset' => 'utf-8',
412         ) or error("[po/filter:$page]: failed to translate");
413         $doc->write($outfile) or error("[po/filter:$page] could not write $outfile");
414         $content = readfile($outfile) or error("[po/filter:$page] could not read $outfile");
415
416         # Unlinking should happen automatically, thanks to File::Temp,
417         # but it does not work here, probably because of the way writefile()
418         # and Locale::Po4a::write() work.
419         unlink $infile, $outfile;
420
421         setalreadyfiltered($page, $destpage);
422         return $content;
423 } #}}}
424
425 sub htmlize (@) { #{{{
426         my %params=@_;
427
428         my $page = $params{page};
429         my $content = $params{content};
430         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
431         my $masterfile = srcfile($pagesources{$masterpage});
432
433         # force content to be htmlize'd as if it was the same type as the master page
434         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
435 } #}}}
436
437 sub percenttranslated ($) { #{{{
438         my $page=shift;
439
440         return gettext("N/A") unless (istranslation($page));
441         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
442         my $file=srcfile($pagesources{$page});
443         my $masterfile = srcfile($pagesources{$masterpage});
444         my (@pos,@masters);
445         push @pos,$file;
446         push @masters,$masterfile;
447         my %options = (
448                 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
449         );
450         my $doc=Locale::Po4a::Chooser::new('text',%options);
451         $doc->process(
452                 'po_in_name'    => \@pos,
453                 'file_in_name'  => \@masters,
454                 'file_in_charset'  => 'utf-8',
455                 'file_out_charset' => 'utf-8',
456         ) or error("[po/percenttranslated:$page]: failed to translate");
457         my ($percent,$hit,$queries) = $doc->stats();
458         return $percent;
459 } #}}}
460
461 sub otherlanguages ($) { #{{{
462         my $page=shift;
463
464         my @ret;
465         if (istranslatable($page)) {
466                 foreach my $lang (sort keys %{$translations{$page}}) {
467                         my $translation = $translations{$page}{$lang};
468                         push @ret, {
469                                 url => urlto($translation, $page),
470                                 code => $lang,
471                                 language => $config{po_slave_languages}{$lang},
472                                 percent => percenttranslated($translation),
473                         };
474                 }
475         }
476         elsif (istranslation($page)) {
477                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
478                 push @ret, {
479                         url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
480                         code => $config{po_master_language}{code},
481                         language => $config{po_master_language}{name},
482                         master => 1,
483                 };
484                 foreach my $lang (sort keys %{$translations{$masterpage}}) {
485                         push @ret, {
486                                 url => urlto($translations{$masterpage}{$lang}, $page),
487                                 code => $lang,
488                                 language => $config{po_slave_languages}{$lang},
489                                 percent => percenttranslated($translations{$masterpage}{$lang}),
490                         } unless ($lang eq $curlang);
491                 }
492         }
493         return @ret;
494 } #}}}
495
496 sub pagetemplate (@) { #{{{
497         my %params=@_;
498         my $page=$params{page};
499         my $destpage=$params{destpage};
500         my $template=$params{template};
501
502         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
503
504         if (istranslation($page) && $template->query(name => "percenttranslated")) {
505                 $template->param(percenttranslated => percenttranslated($page));
506         }
507         if ($template->query(name => "istranslation")) {
508                 $template->param(istranslation => istranslation($page));
509         }
510         if ($template->query(name => "istranslatable")) {
511                 $template->param(istranslatable => istranslatable($page));
512         }
513         if ($template->query(name => "otherlanguages")) {
514                 $template->param(otherlanguages => [otherlanguages($page)]);
515                 if (istranslatable($page)) {
516                         foreach my $translation (values %{$translations{$page}}) {
517                                 add_depends($page, $translation);
518                         }
519                 }
520                 elsif (istranslation($page)) {
521                         add_depends($page, $masterpage);
522                         foreach my $translation (values %{$translations{$masterpage}}) {
523                                 add_depends($page, $translation) unless $page eq $translation;
524                         }
525                 }
526         }
527         # Rely on IkiWiki::Render's genpage() to decide wether
528         # a discussion link should appear on $page; this is not
529         # totally accurate, though: some broken links may be generated
530         # when cgiurl is disabled.
531         # This compromise avoids some code duplication, and will probably
532         # prevent future breakage when ikiwiki internals change.
533         # Known limitations are preferred to future random bugs.
534         if ($template->param('discussionlink') && istranslation($page)) {
535                 $template->param('discussionlink' => htmllink(
536                                                         $page,
537                                                         $destpage,
538                                                         $masterpage . '/' . gettext("Discussion"),
539                                                         noimageinline => 1,
540                                                         forcesubpage => 0,
541                                                         linktext => gettext("Discussion"),
542                                                         ));
543         }
544         # Remove broken parentlink to ./index.html on home page's translations.
545         # It works because this hook has the "last" parameter set, to ensure it
546         # runs after parentlinks' own pagetemplate hook.
547         if ($template->param('parentlinks')
548             && istranslation($page)
549             && $masterpage eq "index") {
550                 $template->param('parentlinks' => []);
551         }
552 } # }}}
553
554 sub change(@) { #{{{
555         my @rendered=@_;
556
557         my $updated_po_files=0;
558
559         # Refresh/create POT and PO files as needed.
560         foreach my $page (map pagename($_), @rendered) {
561                 next unless istranslatable($page);
562                 my $file=srcfile($pagesources{$page});
563                 my $updated_pot_file=0;
564                 # Only refresh Pot file if it does not exist, or if
565                 # $pagesources{$page} was changed: don't if only the HTML was
566                 # refreshed, e.g. because of a dependency.
567                 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
568                     || ! -e potfile($file)) {
569                         refreshpot($file);
570                         $updated_pot_file=1;
571                 }
572                 my @pofiles;
573                 foreach my $lang (keys %{$config{po_slave_languages}}) {
574                         my $pofile=pofile($file, $lang);
575                         if ($updated_pot_file || ! -e $pofile) {
576                                 push @pofiles, $pofile;
577                         }
578                 }
579                 if (@pofiles) {
580                         refreshpofiles($file, @pofiles);
581                         map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
582                         $updated_po_files=1;
583                 }
584         }
585
586         if ($updated_po_files) {
587                 # Check staged changes in.
588                 if ($config{rcs}) {
589                         IkiWiki::disable_commit_hook();
590                         IkiWiki::rcs_commit_staged(gettext("updated PO files"),
591                                 "IkiWiki::Plugin::po::change", "127.0.0.1");
592                         IkiWiki::enable_commit_hook();
593                         IkiWiki::rcs_update();
594                 }
595                 # Reinitialize module's private variables.
596                 resetalreadyfiltered();
597                 undef %translations;
598                 # Trigger a wiki refresh.
599                 require IkiWiki::Render;
600                 IkiWiki::refresh();
601                 IkiWiki::saveindex();
602         }
603 } #}}}
604
605 sub editcontent () { #{{{
606         my %params=@_;
607         # as we're previewing or saving a page, the content may have
608         # changed, so tell the next filter() invocation it must not be lazy
609         unsetalreadyfiltered($params{page}, $params{page});
610         return $params{content};
611 } #}}}
612
613 sub istranslatable ($) { #{{{
614         my $page=shift;
615
616         my $file=$pagesources{$page};
617
618         if (! defined $file
619             || (defined pagetype($file) && pagetype($file) eq 'po')
620             || $file =~ /\.pot$/) {
621                 return 0;
622         }
623         return pagespec_match($page, $config{po_translatable_pages});
624 } #}}}
625
626 sub _istranslation ($) { #{{{
627         my $page=shift;
628
629         my $file=$pagesources{$page};
630         if (! defined $file) {
631                 return IkiWiki::FailReason->new("no file specified");
632         }
633
634         if (! defined $file
635             || ! defined pagetype($file)
636             || ! pagetype($file) eq 'po'
637             || $file =~ /\.pot$/) {
638                 return 0;
639         }
640
641         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
642         if (! defined $masterpage || ! defined $lang
643             || ! (length($masterpage) > 0) || ! (length($lang) > 0)
644             || ! defined $pagesources{$masterpage}
645             || ! defined $config{po_slave_languages}{$lang}) {
646                 return 0;
647         }
648
649         return istranslatable($masterpage);
650 } #}}}
651
652 sub istranslation ($) { #{{{
653         my $page=shift;
654
655         if (_istranslation($page)) {
656                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
657                 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
658                 return 1;
659         }
660         return 0;
661 } #}}}
662
663 package IkiWiki::PageSpec;
664 use warnings;
665 use strict;
666 use IkiWiki 2.00;
667
668 sub match_istranslation ($;@) { #{{{
669         my $page=shift;
670
671         if (IkiWiki::Plugin::po::istranslation($page)) {
672                 return IkiWiki::SuccessReason->new("is a translation page");
673         }
674         else {
675                 return IkiWiki::FailReason->new("is not a translation page");
676         }
677 } #}}}
678
679 sub match_istranslatable ($;@) { #{{{
680         my $page=shift;
681
682         if (IkiWiki::Plugin::po::istranslatable($page)) {
683                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
684         }
685         else {
686                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
687         }
688 } #}}}
689
690 sub match_lang ($$;@) { #{{{
691         my $page=shift;
692         my $wanted=shift;
693
694         my $regexp=IkiWiki::glob2re($wanted);
695         my $lang;
696         my $masterpage;
697
698         if (IkiWiki::Plugin::po::istranslation($page)) {
699                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
700         }
701         else {
702                 $lang = $config{po_master_language}{code};
703         }
704
705         if ($lang!~/^$regexp$/i) {
706                 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
707         }
708         else {
709                 return IkiWiki::SuccessReason->new("file language is $wanted");
710         }
711 } #}}}
712
713 sub match_currentlang ($$;@) { #{{{
714         my $page=shift;
715
716         shift;
717         my %params=@_;
718         my ($currentmasterpage, $currentlang, $masterpage, $lang);
719
720         return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
721
722         if (IkiWiki::Plugin::po::istranslation($params{location})) {
723                 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
724         }
725         else {
726                 $currentlang = $config{po_master_language}{code};
727         }
728
729         if (IkiWiki::Plugin::po::istranslation($page)) {
730                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
731         }
732         else {
733                 $lang = $config{po_master_language}{code};
734         }
735
736         if ($lang eq $currentlang) {
737                 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
738         }
739         else {
740                 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
741         }
742 } #}}}
743
744 1