← Index
NYTProf Performance Profile   « line view »
For svc/members/upsert
  Run on Tue Jan 13 11:50:22 2015
Reported on Tue Jan 13 12:09:51 2015

Filename/mnt/catalyst/koha/C4/Templates.pm
StatementsExecuted 24 statements in 4.47ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1114.41ms5.06msC4::Templates::::BEGIN@35C4::Templates::BEGIN@35
1111.51ms34.6msC4::Templates::::BEGIN@33C4::Templates::BEGIN@33
111435µs448µsC4::Templates::::BEGIN@3C4::Templates::BEGIN@3
11114µs40µsC4::Templates::::BEGIN@7C4::Templates::BEGIN@7
11110µs30µsC4::Templates::::BEGIN@6C4::Templates::BEGIN@6
11110µs77µsC4::Templates::::BEGIN@32C4::Templates::BEGIN@32
11110µs203µsC4::Templates::::BEGIN@34C4::Templates::BEGIN@34
1119µs11µsC4::Templates::::BEGIN@37C4::Templates::BEGIN@37
1119µs17µsC4::Templates::::BEGIN@4C4::Templates::BEGIN@4
1118µs44µsC4::Templates::::BEGIN@5C4::Templates::BEGIN@5
0000s0sC4::Templates::::__ANON__[:361]C4::Templates::__ANON__[:361]
0000s0sC4::Templates::::__ANON__[:378]C4::Templates::__ANON__[:378]
0000s0sC4::Templates::::_current_languageC4::Templates::_current_language
0000s0sC4::Templates::::_get_template_fileC4::Templates::_get_template_file
0000s0sC4::Templates::::getlanguageC4::Templates::getlanguage
0000s0sC4::Templates::::getlanguagecookieC4::Templates::getlanguagecookie
0000s0sC4::Templates::::gettemplateC4::Templates::gettemplate
0000s0sC4::Templates::::newC4::Templates::new
0000s0sC4::Templates::::outputC4::Templates::output
0000s0sC4::Templates::::paramC4::Templates::param
0000s0sC4::Templates::::setlanguagecookieC4::Templates::setlanguagecookie
0000s0sC4::Templates::::themelanguageC4::Templates::themelanguage
0000s0sC4::Templates::::utf8_arrayrefC4::Templates::utf8_arrayref
0000s0sC4::Templates::::utf8_hashrefC4::Templates::utf8_hashref
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package C4::Templates;
2
3229µs2462µs
# spent 448µs (435+14) within C4::Templates::BEGIN@3 which was called: # once (435µs+14µs) by C4::Auth::BEGIN@29 at line 3
use strict;
# spent 448µs making 1 call to C4::Templates::BEGIN@3 # spent 14µs making 1 call to strict::import
4223µs225µs
# spent 17µs (9+8) within C4::Templates::BEGIN@4 which was called: # once (9µs+8µs) by C4::Auth::BEGIN@29 at line 4
use warnings;
# spent 17µs making 1 call to C4::Templates::BEGIN@4 # spent 8µs making 1 call to warnings::import
5224µs280µs
# spent 44µs (8+36) within C4::Templates::BEGIN@5 which was called: # once (8µs+36µs) by C4::Auth::BEGIN@29 at line 5
use Carp;
# spent 44µs making 1 call to C4::Templates::BEGIN@5 # spent 36µs making 1 call to Exporter::import
6226µs250µs
# spent 30µs (10+20) within C4::Templates::BEGIN@6 which was called: # once (10µs+20µs) by C4::Auth::BEGIN@29 at line 6
use CGI;
# spent 30µs making 1 call to C4::Templates::BEGIN@6 # spent 20µs making 1 call to CGI::import
7243µs267µs
# spent 40µs (14+26) within C4::Templates::BEGIN@7 which was called: # once (14µs+26µs) by C4::Auth::BEGIN@29 at line 7
use List::MoreUtils qw/any/;
# spent 40µs making 1 call to C4::Templates::BEGIN@7 # spent 26µs making 1 call to Exporter::import
8
9# Copyright 2009 Chris Cormack and The Koha Dev Team
10#
11# This file is part of Koha.
12#
13# Koha is free software; you can redistribute it and/or modify it under the
14# terms of the GNU General Public License as published by the Free Software
15# Foundation; either version 2 of the License, or (at your option) any later
16# version.
17#
18# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License along with
23# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24# Suite 330, Boston, MA 02111-1307 USA
25
26=head1 NAME
27
28 Koha::Templates - Object for manipulating templates for use with Koha
29
30=cut
31
32226µs2145µs
# spent 77µs (10+68) within C4::Templates::BEGIN@32 which was called: # once (10µs+68µs) by C4::Auth::BEGIN@29 at line 32
use base qw(Class::Accessor);
# spent 77µs making 1 call to C4::Templates::BEGIN@32 # spent 68µs making 1 call to base::import
332665µs134.6ms
# spent 34.6ms (1.51+33.1) within C4::Templates::BEGIN@33 which was called: # once (1.51ms+33.1ms) by C4::Auth::BEGIN@29 at line 33
use Template;
# spent 34.6ms making 1 call to C4::Templates::BEGIN@33
34231µs2396µs
# spent 203µs (10+193) within C4::Templates::BEGIN@34 which was called: # once (10µs+193µs) by C4::Auth::BEGIN@29 at line 34
use Template::Constants qw( :debug );
# spent 203µs making 1 call to C4::Templates::BEGIN@34 # spent 193µs making 1 call to Exporter::import
3522.15ms25.12ms
# spent 5.06ms (4.41+649µs) within C4::Templates::BEGIN@35 which was called: # once (4.41ms+649µs) by C4::Auth::BEGIN@29 at line 35
use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
# spent 5.06ms making 1 call to C4::Templates::BEGIN@35 # spent 59µs making 1 call to Exporter::import
36
3721.43ms214µs
# spent 11µs (9+2) within C4::Templates::BEGIN@37 which was called: # once (9µs+2µs) by C4::Auth::BEGIN@29 at line 37
use C4::Context;
# spent 11µs making 1 call to C4::Templates::BEGIN@37 # spent 2µs making 1 call to C4::Context::import
38
3919µs1198µs__PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
# spent 198µs making 1 call to Class::Accessor::mk_accessors
40
- -
43sub new {
44 my $class = shift;
45 my $interface = shift;
46 my $filename = shift;
47 my $tmplbase = shift;
48 my $query = @_? shift: undef;
49 my $htdocs;
50 if ( $interface ne "intranet" ) {
51 $htdocs = C4::Context->config('opachtdocs');
52 }
53 else {
54 $htdocs = C4::Context->config('intrahtdocs');
55 }
56 my ($theme, $lang, $activethemes)= themelanguage( $htdocs, $tmplbase, $interface, $query);
57 my @includes;
58 foreach (@$activethemes) {
59 push @includes, "$htdocs/$_/$lang/includes";
60 push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
61 }
62 my $template = Template->new(
63 { EVAL_PERL => 1,
64 ABSOLUTE => 1,
65 PLUGIN_BASE => 'Koha::Template::Plugin',
66 COMPILE_EXT => C4::Context->config('template_cache_dir')?'.ttc':'',
67 COMPILE_DIR => C4::Context->config('template_cache_dir')?C4::Context->config('template_cache_dir'):'',,
68 INCLUDE_PATH => \@includes,
69 FILTERS => {},
70 }
71 ) or die Template->error();
72 my $self = {
73 TEMPLATE => $template,
74 VARS => {},
75 };
76 bless $self, $class;
77 $self->theme($theme);
78 $self->lang($lang);
79 $self->activethemes($activethemes);
80 $self->preferredtheme($activethemes->[0]);
81 $self->filename($filename);
82 $self->htdocs($htdocs);
83 $self->interface($interface);
84 $self->{VARS}->{"test"} = "value";
85 return $self;
86
87}
88
89sub output {
90 my $self = shift;
91 my $vars = shift;
92
93# my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
94 my $template = $self->{TEMPLATE};
95 if ( $self->interface eq 'intranet' ) {
96 $vars->{themelang} = '/intranet-tmpl';
97 }
98 else {
99 $vars->{themelang} = '/opac-tmpl';
100 }
101 $vars->{lang} = $self->lang;
102 $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang;
103 $vars->{yuipath} =
104 ( C4::Context->preference("yuipath") eq "local"
105 ? ( $self->interface eq 'intranet' ? $vars->{themelang} . "/lib/yui" : "/opac-tmpl/lib/yui" )
106 : C4::Context->preference("yuipath") );
107 $vars->{interface} =
108 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
109 $vars->{theme} = $self->theme;
110 $vars->{opaccolorstylesheet} =
111 C4::Context->preference('opaccolorstylesheet');
112 $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
113 $vars->{opaclayoutstylesheet} =
114 C4::Context->preference('opaclayoutstylesheet');
115
116 # add variables set via param to $vars for processing
117 # and clean any utf8 mess
118 for my $k ( keys %{ $self->{VARS} } ) {
119 $vars->{$k} = $self->{VARS}->{$k};
120 if (ref($vars->{$k}) eq 'ARRAY'){
121 utf8_arrayref($vars->{$k});
122 }
123 elsif (ref($vars->{$k}) eq 'HASH'){
124 utf8_hashref($vars->{$k});
125 }
126 else {
127 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
128 }
129 }
130 my $data;
131# binmode( STDOUT, ":utf8" );
132 $template->process( $self->filename, $vars, \$data )
133 || die "Template process failed: ", $template->error();
134 return $data;
135}
136
137sub utf8_arrayref {
138 my $arrayref = shift;
139 foreach my $element (@$arrayref){
140 if (ref($element) eq 'ARRAY'){
141 utf8_arrayref($element);
142 next;
143 }
144 if (ref($element) eq 'HASH'){
145 utf8_hashref($element);
146 next;
147 }
148 utf8::encode($element) if utf8::is_utf8($element);
149 }
150}
151
152sub utf8_hashref {
153 my $hashref = shift;
154 for my $key (keys %{$hashref}){
155 if (ref($hashref->{$key}) eq 'ARRAY'){
156 utf8_arrayref($hashref->{$key});
157 next;
158 }
159 if (ref($hashref->{$key}) eq 'HASH'){
160 utf8_hashref($hashref->{$key});
161 next;
162 }
163 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
164 }
165}
166
167
168# FIXME - this is a horrible hack to cache
169# the current known-good language, temporarily
170# put in place to resolve bug 4403. It is
171# used only by C4::XSLT::XSLTParse4Display;
172# the language is set via the usual call
173# to themelanguage.
1741200nsmy $_current_language = 'en';
175
176sub _current_language {
177 return $_current_language;
178}
179
180
181# wrapper method to allow easier transition from HTML template pro to Template Toolkit
182sub param {
183 my $self = shift;
184 while (@_) {
185 my $key = shift;
186 my $val = shift;
187 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
188 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
189 if ( $key ) {
190 $self->{VARS}->{$key} = $val;
191 } else {
192 warn "Problem = a value of $val has been passed to param without key";
193 }
194 }
195}
196
197
198=head1 NAME
199
200C4::Templates - Functions for managing templates
201
202=head1 FUNCTIONS
203
204=cut
205
206# FIXME: this is a quick fix to stop rc1 installing broken
207# Still trying to figure out the correct fix.
20814µs110µsmy $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
# spent 10µs making 1 call to C4::Context::config
209
210#---------------------------------------------------------------------------------------------------------
211# FIXME - POD
212
213sub _get_template_file {
214 my ($tmplbase, $interface, $query) = @_;
215
216 my $is_intranet = $interface eq 'intranet';
217 my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
218 my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
219
220 # if the template doesn't exist, load the English one as a last resort
221 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
222 unless (-f $filename) {
223 $lang = 'en';
224 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
225 }
226 return ($htdocs, $theme, $lang, $filename);
227}
228
229
230sub gettemplate {
231 my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
232 ($query) or warn "no query in gettemplate";
233 my $path = C4::Context->preference('intranet_includes') || 'includes';
234 $tmplbase =~ s/\.tmpl$/.tt/;
235 my ($htdocs, $theme, $lang, $filename)
236 = _get_template_file($tmplbase, $interface, $query);
237 $filename = $tmplbase if ( $is_plugin );
238 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
239
240# NOTE: Commenting these out rather than deleting them so that those who need
241# to know how we previously shimmed these directories will be able to understand.
242# my $is_intranet = $interface eq 'intranet';
243# my $themelang =
244# ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
245# "/$theme/$lang";
246# $template->param(
247# themelang => $themelang,
248# yuipath => C4::Context->preference("yuipath") eq "local"
249# ? "$themelang/lib/yui"
250# : C4::Context->preference("yuipath"),
251# interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
252# theme => $theme,
253# lang => $lang
254# );
255
256 # Bidirectionality, must be sent even if is the only language
257 my $current_lang = regex_lang_subtags($lang);
258 my $bidi;
259 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
260 $template->param(
261 bidi => $bidi,
262 );
263 # Languages
264 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
265 my $num_languages_enabled = 0;
266 foreach my $lang (@$languages_loop) {
267 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
268 $num_languages_enabled++ if $sublang->{enabled};
269 }
270 }
271 my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well
272 $template->param(
273 languages_loop => $languages_loop,
274 one_language_enabled => $one_language_enabled,
275 ) unless $one_language_enabled;
276
277 return $template;
278}
279
280
281#---------------------------------------------------------------------------------------------------------
282# FIXME - POD
283sub themelanguage {
284 my ($htdocs, $tmpl, $interface, $query) = @_;
285 ($query) or warn "no query in themelanguage";
286
287 # Select a language based on cookie, syspref available languages & browser
288 my $lang = getlanguage($query, $interface);
289
290 # Select theme
291 my $is_intranet = $interface eq 'intranet';
292 my @themes = split(" ", C4::Context->preference(
293 $is_intranet ? "template" : "opacthemes" ));
294 push @themes, 'prog';
295
296 # Try to find first theme for the selected language
297 for my $theme (@themes) {
298 if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
299 $_current_language = $lang;
300 return ($theme, $lang, \@themes)
301 }
302 }
303 # Otherwise, return prog theme in English 'en'
304 return ('prog', 'en', \@themes);
305}
306
307
308sub setlanguagecookie {
309 my ( $query, $language, $uri ) = @_;
310 my $cookie = $query->cookie(
311 -name => 'KohaOpacLanguage',
312 -value => $language,
313 -HttpOnly => 1,
314 -expires => '+3y'
315 );
316 print $query->redirect(
317 -uri => $uri,
318 -cookie => $cookie
319 );
320}
321
322=head2 getlanguagecookie
323
324 my $cookie = getlanguagecookie($query,$language);
325
326Returns a cookie object containing the calculated language to be used.
327
328=cut
329
330sub getlanguagecookie {
331 my ( $query, $language ) = @_;
332 my $cookie = $query->cookie(
333 -name => 'KohaOpacLanguage',
334 -value => $language,
335 -HttpOnly => 1,
336 -expires => '+3y'
337 );
338
339 return $cookie;
340}
341
342=head2 getlanguage
343
344 Select a language based on the URL parameter 'language', a cookie,
345 syspref available languages & browser
346
347=cut
348
349sub getlanguage {
350 my ($query, $interface) = @_;
351
352 my $preference_to_check =
353 $interface eq 'intranet' ? 'language' : 'opaclanguages';
354 # Get the available/valid languages list
355 my @languages = split /,/, C4::Context->preference($preference_to_check);
356
357 my $lang;
358
359 # Chose language from the URL
360 $lang = $query->param( 'language' );
361 if ( defined $lang && any { $_ eq $lang } @languages) {
362 return $lang;
363 }
364
365 # cookie
366 if ($query and $query->cookie('KohaOpacLanguage') ) {
367 $lang = $query->cookie('KohaOpacLanguage');
368 $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie
369 }
370
371 # HTTP_ACCEPT_LANGUAGE
372 if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE} ) {
373 $lang = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE},
374 getTranslatedLanguages( $interface, 'prog' ) );
375 }
376
377 # Ignore a lang not selected in sysprefs
378 if ( $lang && any { $_ eq $lang } @languages ) {
379 return $lang;
380 }
381
382 # Pick the first selected syspref language
383 $lang = shift @languages;
384 return $lang if $lang;
385
386 # Fall back to English if necessary
387 return 'en';
388}
389
39014µs1;