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