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