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