From a4f579f5c153021eaa09c05f199fe420a499aa3e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 9 May 2016 17:27:51 +0100 Subject: [PATCH] Bug 16088: Use Koha::Cache to cache the language The goal of this patch is to avoid unecessary flush of the L1 cache on creating a new CGI object each time C4::Languages::getlanguage is called without a CGI object. Test plan: At the OPAC and the intranet interfaces: - Clear the cookies of your browser - Choose a language and browse - Change the language and browse Try to use a language which is not defined: Add &language=es-ES (if es-ES is not translated) to the url, you should not see the Spanish interface. --- C4/Languages.pm | 29 +++++++++++++++++++---------- C4/Templates.pm | 11 +++++------ Koha/Cache.pm | 5 +++++ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/C4/Languages.pm b/C4/Languages.pm index affe96c..1e4628b 100644 --- a/C4/Languages.pm +++ b/C4/Languages.pm @@ -569,6 +569,13 @@ sub accept_language { sub getlanguage { my ($cgi) = @_; + my $cache = Koha::Cache->get_instance(); + my $cache_key = "getlanguage"; + if ( not $cgi or not $cgi->param('language') ) { + my $cached = $cache->get_from_cache($cache_key); + return $cached if $cached; + } + $cgi //= new CGI; my $interface = C4::Context->interface; my $theme = C4::Context->preference( ( $interface eq 'opac' ) ? 'opacthemes' : 'template' ); @@ -584,14 +591,14 @@ sub getlanguage { } # Chose language from the URL - $language = $cgi->param( 'language' ); - if ( defined $language && any { $_ eq $language } @languages) { - return $language; + my $cgi_param_language = $cgi->param( 'language' ); + if ( defined $cgi_param_language && any { $_ eq $cgi_param_language } @languages) { + $language = $cgi_param_language; } # cookie - if ($language = $cgi->cookie('KohaOpacLanguage') ) { - $language =~ s/[^a-zA-Z_-]*//; # sanitize cookie + if (not $language and my $cgi_cookie_language = $cgi->cookie('KohaOpacLanguage') ) { + ( $language = $cgi_cookie_language ) =~ s/[^a-zA-Z_-]*//; # sanitize cookie } # HTTP_ACCEPT_LANGUAGE @@ -601,16 +608,18 @@ sub getlanguage { } # Ignore a lang not selected in sysprefs - if ( $language && any { $_ eq $language } @languages ) { - return $language; + if ( $language && not any { $_ eq $language } @languages ) { + $language = undef; } # Pick the first selected syspref language - $language = shift @languages; - return $language if $language; + $language = shift @languages unless $language; # Fall back to English if necessary - return 'en'; + $language ||= 'en'; + + $cache->set_in_cache( $cache_key, $language ); + return $language; } 1; diff --git a/C4/Templates.pm b/C4/Templates.pm index b3d7edf..00fc976 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -273,12 +273,11 @@ sub themelanguage { sub setlanguagecookie { my ( $query, $language, $uri ) = @_; - my $cookie = $query->cookie( - -name => 'KohaOpacLanguage', - -value => $language, - -HttpOnly => 1, - -expires => '+3y' - ); + my $cookie = getlanguagecookie( $query, $language ); + + # We do not want to set getlanguage in cache, some additional checks are + # done in C4::Languages::getlanguage + Koha::Cache->get_instance()->clear_from_cache( 'getlanguage' ); print $query->redirect( -uri => $uri, -cookie => $cookie diff --git a/Koha/Cache.pm b/Koha/Cache.pm index 6f10b00..a42a32f 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -287,6 +287,11 @@ sub set_in_cache { } } +sub set_in_L1_cache_only { + my ( $self, $key, $value ) = @_; + $L1_cache{ $key } = $value; +} + =head2 get_from_cache my $value = $cache->get_from_cache($key, [ $options ]); -- 2.7.0