po plugin: memoize istranslatable and _istranslation

Both functions are called very often, and:
- istranslatable has no side effect
- _istranslation is the helper function, without any side effect, for the
  istranslation function

Signed-off-by: intrigeri <intrigeri@boum.org>
master
intrigeri 2008-10-11 03:41:12 +02:00
parent 1e874b3f0a
commit 08df9dcef7
1 changed files with 16 additions and 10 deletions

View File

@ -10,11 +10,15 @@ use IkiWiki 2.00;
use Encode; use Encode;
use Locale::Po4a::Chooser; use Locale::Po4a::Chooser;
use File::Temp; use File::Temp;
use Memoize;
my %translations;
memoize("istranslatable");
memoize("_istranslation");
sub import { sub import {
hook(type => "getsetup", id => "po", call => \&getsetup); hook(type => "getsetup", id => "po", call => \&getsetup);
hook(type => "checkconfig", id => "po", call => \&checkconfig); hook(type => "checkconfig", id => "po", call => \&checkconfig);
hook(type => "scan", id => "po", call => \&scan);
hook(type => "targetpage", id => "po", call => \&targetpage); hook(type => "targetpage", id => "po", call => \&targetpage);
hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath); hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink); hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
@ -85,14 +89,6 @@ sub checkconfig () { #{{{
push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/; push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
} #}}} } #}}}
sub scan (@) { #{{{
my %params=@_;
my $page=$params{page};
# FIXME: cache (or memoize) the list of translatable/translation pages,
# and/or istranslation/istranslated results
} #}}}
sub targetpage (@) { #{{{ sub targetpage (@) { #{{{
my %params = @_; my %params = @_;
my $page=$params{page}; my $page=$params{page};
@ -203,7 +199,7 @@ sub istranslatable ($) { #{{{
return pagespec_match($page, $config{po_translatable_pages}); return pagespec_match($page, $config{po_translatable_pages});
} #}}} } #}}}
sub istranslation ($) { #{{{ sub _istranslation ($) { #{{{
my $page=shift; my $page=shift;
my $file=$pagesources{$page}; my $file=$pagesources{$page};
if (! defined $file) { if (! defined $file) {
@ -228,6 +224,16 @@ sub istranslation ($) { #{{{
return istranslatable($masterpage); return istranslatable($masterpage);
} #}}} } #}}}
sub istranslation ($) { #{{{
my $page=shift;
if (_istranslation($page)) {
my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
$translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
return 1;
}
return 0;
} #}}}
package IkiWiki::PageSpec; package IkiWiki::PageSpec;
use warnings; use warnings;