Filename | /home/vagrant/kohaclone/C4/Output.pm |
Statements | Executed 17 statements in 268µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
4 | 1 | 1 | 448µs | 448µs | CORE:print (opcode) | C4::Output::
4 | 2 | 2 | 426µs | 4.72ms | output_with_http_headers | C4::Output::
4 | 1 | 1 | 258µs | 258µs | CORE:subst (opcode) | C4::Output::
1 | 1 | 1 | 28µs | 28µs | BEGIN@38 | C4::Output::
1 | 1 | 1 | 25µs | 47µs | BEGIN@28 | C4::Output::
1 | 1 | 1 | 13µs | 41µs | BEGIN@31 | C4::Output::
1 | 1 | 1 | 13µs | 58µs | BEGIN@36 | C4::Output::
1 | 1 | 1 | 11µs | 14µs | BEGIN@33 | C4::Output::
1 | 1 | 1 | 11µs | 792µs | output_html_with_http_headers | C4::Output::
1 | 1 | 1 | 10µs | 15µs | BEGIN@34 | C4::Output::
0 | 0 | 0 | 0s | 0s | END | C4::Output::
0 | 0 | 0 | 0s | 0s | is_ajax | C4::Output::
0 | 0 | 0 | 0s | 0s | output_ajax_with_http_headers | C4::Output::
0 | 0 | 0 | 0s | 0s | pagination_bar | C4::Output::
0 | 0 | 0 | 0s | 0s | parametrized_url | C4::Output::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package C4::Output; | ||||
2 | |||||
3 | #package to deal with marking up output | ||||
4 | #You will need to edit parts of this pm | ||||
5 | #set the value of path to be where your html lives | ||||
6 | |||||
7 | # Copyright 2000-2002 Katipo Communications | ||||
8 | # | ||||
9 | # This file is part of Koha. | ||||
10 | # | ||||
11 | # Koha is free software; you can redistribute it and/or modify it | ||||
12 | # under the terms of the GNU General Public License as published by | ||||
13 | # the Free Software Foundation; either version 3 of the License, or | ||||
14 | # (at your option) any later version. | ||||
15 | # | ||||
16 | # Koha is distributed in the hope that it will be useful, but | ||||
17 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
19 | # GNU General Public License for more details. | ||||
20 | # | ||||
21 | # You should have received a copy of the GNU General Public License | ||||
22 | # along with Koha; if not, see <http://www.gnu.org/licenses>. | ||||
23 | |||||
24 | |||||
25 | # NOTE: I'm pretty sure this module is deprecated in favor of | ||||
26 | # templates. | ||||
27 | |||||
28 | 2 | 70µs | # spent 47µs (25+22) within C4::Output::BEGIN@28 which was called:
# once (25µs+22µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_errors_404_2epl::BEGIN@23 at line 28 # spent 47µs making 1 call to C4::Output::BEGIN@28
# spent 22µs making 1 call to strict::import | ||
29 | #use warnings; FIXME - Bug 2505 | ||||
30 | |||||
31 | 2 | 70µs | # spent 41µs (13+28) within C4::Output::BEGIN@31 which was called:
# once (13µs+28µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_errors_404_2epl::BEGIN@23 at line 31 # spent 41µs making 1 call to C4::Output::BEGIN@31
# spent 28µs making 1 call to Exporter::import | ||
32 | |||||
33 | 2 | 17µs | # spent 14µs (11+3) within C4::Output::BEGIN@33 which was called:
# once (11µs+3µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_errors_404_2epl::BEGIN@23 at line 33 # spent 14µs making 1 call to C4::Output::BEGIN@33
# spent 3µs making 1 call to C4::Context::import | ||
34 | 2 | 20µs | # spent 15µs (10+5) within C4::Output::BEGIN@34 which was called:
# once (10µs+5µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_errors_404_2epl::BEGIN@23 at line 34 # spent 15µs making 1 call to C4::Output::BEGIN@34
# spent 5µs making 1 call to Class::Accessor::import | ||
35 | |||||
36 | 2 | 102µs | # spent 58µs (13+45) within C4::Output::BEGIN@36 which was called:
# once (13µs+45µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_errors_404_2epl::BEGIN@23 at line 36 # spent 58µs making 1 call to C4::Output::BEGIN@36
# spent 45µs making 1 call to vars::import | ||
37 | |||||
38 | # spent 28µs within C4::Output::BEGIN@38 which was called:
# once (28µs+0s) by CGI::Compile::ROOT::home_vagrant_kohaclone_errors_404_2epl::BEGIN@23 at line 57 | ||||
39 | # set the version for version checking | ||||
40 | $VERSION = 3.07.00.049; | ||||
41 | require Exporter; | ||||
42 | |||||
43 | @ISA = qw(Exporter); | ||||
44 | @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead | ||||
45 | %EXPORT_TAGS = ( all =>[qw(setlanguagecookie pagination_bar parametrized_url | ||||
46 | &output_with_http_headers &output_ajax_with_http_headers &output_html_with_http_headers)], | ||||
47 | ajax =>[qw(&output_with_http_headers &output_ajax_with_http_headers is_ajax)], | ||||
48 | html =>[qw(&output_with_http_headers &output_html_with_http_headers)] | ||||
49 | ); | ||||
50 | push @EXPORT, qw( | ||||
51 | setlanguagecookie getlanguagecookie pagination_bar parametrized_url | ||||
52 | ); | ||||
53 | push @EXPORT, qw( | ||||
54 | &output_html_with_http_headers &output_ajax_with_http_headers &output_with_http_headers | ||||
55 | ); | ||||
56 | |||||
57 | 1 | 28µs | } # spent 28µs making 1 call to C4::Output::BEGIN@38 | ||
58 | |||||
59 | =head1 NAME | ||||
60 | |||||
61 | C4::Output - Functions for managing output, is slowly being deprecated | ||||
62 | |||||
63 | =head1 FUNCTIONS | ||||
64 | |||||
65 | =over 2 | ||||
66 | =cut | ||||
67 | |||||
68 | =item pagination_bar | ||||
69 | |||||
70 | pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name) | ||||
71 | |||||
72 | Build an HTML pagination bar based on the number of page to display, the | ||||
73 | current page and the url to give to each page link. | ||||
74 | |||||
75 | C<$base_url> is the URL for each page link. The | ||||
76 | C<$startfrom_name>=page_number is added at the end of the each URL. | ||||
77 | |||||
78 | C<$nb_pages> is the total number of pages available. | ||||
79 | |||||
80 | C<$current_page> is the current page number. This page number won't become a | ||||
81 | link. | ||||
82 | |||||
83 | This function returns HTML, without any language dependency. | ||||
84 | |||||
85 | =cut | ||||
86 | |||||
87 | sub pagination_bar { | ||||
88 | my $base_url = (@_ ? shift : return); | ||||
89 | my $nb_pages = (@_) ? shift : 1; | ||||
90 | my $current_page = (@_) ? shift : undef; # delay default until later | ||||
91 | my $startfrom_name = (@_) ? shift : 'page'; | ||||
92 | my $additional_parameters = shift || {}; | ||||
93 | |||||
94 | # how many pages to show before and after the current page? | ||||
95 | my $pages_around = 2; | ||||
96 | |||||
97 | my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference | ||||
98 | $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var | ||||
99 | unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) { | ||||
100 | $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1 | ||||
101 | # $debug and # FIXME: use C4::Debug; | ||||
102 | # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3"; | ||||
103 | } | ||||
104 | $base_url =~ s/($delim)+/$1/g; # compress duplicate delims | ||||
105 | $base_url =~ s/$delim;//g; # remove empties | ||||
106 | $base_url =~ s/$delim$//; # remove trailing delim | ||||
107 | |||||
108 | my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '='; | ||||
109 | my $url_suffix; | ||||
110 | while ( my ( $k, $v ) = each %$additional_parameters ) { | ||||
111 | $url_suffix .= '&' . $k . '=' . $v; | ||||
112 | } | ||||
113 | my $pagination_bar = ''; | ||||
114 | |||||
115 | # navigation bar useful only if more than one page to display ! | ||||
116 | if ( $nb_pages > 1 ) { | ||||
117 | |||||
118 | # link to first page? | ||||
119 | if ( $current_page > 1 ) { | ||||
120 | $pagination_bar .= | ||||
121 | "\n" . ' ' | ||||
122 | . '<a href="' | ||||
123 | . $url | ||||
124 | . '1' | ||||
125 | . $url_suffix | ||||
126 | . '"rel="start">' | ||||
127 | . '<<' . '</a>'; | ||||
128 | } | ||||
129 | else { | ||||
130 | $pagination_bar .= | ||||
131 | "\n" . ' <span class="inactive"><<</span>'; | ||||
132 | } | ||||
133 | |||||
134 | # link on previous page ? | ||||
135 | if ( $current_page > 1 ) { | ||||
136 | my $previous = $current_page - 1; | ||||
137 | |||||
138 | $pagination_bar .= | ||||
139 | "\n" . ' ' | ||||
140 | . '<a href="' | ||||
141 | . $url | ||||
142 | . $previous | ||||
143 | . $url_suffix | ||||
144 | . '" rel="prev">' . '<' . '</a>'; | ||||
145 | } | ||||
146 | else { | ||||
147 | $pagination_bar .= | ||||
148 | "\n" . ' <span class="inactive"><</span>'; | ||||
149 | } | ||||
150 | |||||
151 | my $min_to_display = $current_page - $pages_around; | ||||
152 | my $max_to_display = $current_page + $pages_around; | ||||
153 | my $last_displayed_page = undef; | ||||
154 | |||||
155 | for my $page_number ( 1 .. $nb_pages ) { | ||||
156 | if ( | ||||
157 | $page_number == 1 | ||||
158 | or $page_number == $nb_pages | ||||
159 | or ( $page_number >= $min_to_display | ||||
160 | and $page_number <= $max_to_display ) | ||||
161 | ) | ||||
162 | { | ||||
163 | if ( defined $last_displayed_page | ||||
164 | and $last_displayed_page != $page_number - 1 ) | ||||
165 | { | ||||
166 | $pagination_bar .= | ||||
167 | "\n" . ' <span class="inactive">...</span>'; | ||||
168 | } | ||||
169 | |||||
170 | if ( $page_number == $current_page ) { | ||||
171 | $pagination_bar .= | ||||
172 | "\n" . ' ' | ||||
173 | . '<span class="currentPage">' | ||||
174 | . $page_number | ||||
175 | . '</span>'; | ||||
176 | } | ||||
177 | else { | ||||
178 | $pagination_bar .= | ||||
179 | "\n" . ' ' | ||||
180 | . '<a href="' | ||||
181 | . $url | ||||
182 | . $page_number | ||||
183 | . $url_suffix | ||||
184 | . '">' | ||||
185 | . $page_number . '</a>'; | ||||
186 | } | ||||
187 | $last_displayed_page = $page_number; | ||||
188 | } | ||||
189 | } | ||||
190 | |||||
191 | # link on next page? | ||||
192 | if ( $current_page < $nb_pages ) { | ||||
193 | my $next = $current_page + 1; | ||||
194 | |||||
195 | $pagination_bar .= "\n" | ||||
196 | . ' <a href="' | ||||
197 | . $url | ||||
198 | . $next | ||||
199 | . $url_suffix | ||||
200 | . '" rel="next">' . '>' . '</a>'; | ||||
201 | } | ||||
202 | else { | ||||
203 | $pagination_bar .= | ||||
204 | "\n" . ' <span class="inactive">></span>'; | ||||
205 | } | ||||
206 | |||||
207 | # link to last page? | ||||
208 | if ( $current_page != $nb_pages ) { | ||||
209 | $pagination_bar .= "\n" | ||||
210 | . ' <a href="' | ||||
211 | . $url | ||||
212 | . $nb_pages | ||||
213 | . $url_suffix | ||||
214 | . '" rel="last">' | ||||
215 | . '>>' . '</a>'; | ||||
216 | } | ||||
217 | else { | ||||
218 | $pagination_bar .= | ||||
219 | "\n" . ' <span class="inactive">>></span>'; | ||||
220 | } | ||||
221 | } | ||||
222 | |||||
223 | return $pagination_bar; | ||||
224 | } | ||||
225 | |||||
226 | =item output_with_http_headers | ||||
227 | |||||
228 | &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]]) | ||||
229 | |||||
230 | Outputs $data with the appropriate HTTP headers, | ||||
231 | the authentication cookie $cookie and a Content-Type specified in | ||||
232 | $content_type. | ||||
233 | |||||
234 | If applicable, $cookie can be undef, and it will not be sent. | ||||
235 | |||||
236 | $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'. | ||||
237 | |||||
238 | $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'. | ||||
239 | |||||
240 | $extra_options is hashref. If the key 'force_no_caching' is present and has | ||||
241 | a true value, the HTTP headers include directives to force there to be no | ||||
242 | caching whatsoever. | ||||
243 | |||||
244 | =cut | ||||
245 | |||||
246 | # spent 4.72ms (426µs+4.30) within C4::Output::output_with_http_headers which was called 4 times, avg 1.18ms/call:
# 3 times (376µs+3.57ms) by CGI::Compile::ROOT::home_vagrant_kohaclone_errors_404_2epl::__ANON__[/home/vagrant/kohaclone/errors/404.pl:58] at line 38 of errors/404.pl, avg 1.31ms/call
# once (50µs+731µs) by C4::Output::output_html_with_http_headers at line 290 | ||||
247 | 1 | 800ns | my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_; | ||
248 | 1 | 500ns | $status ||= '200 OK'; | ||
249 | |||||
250 | 1 | 800ns | $extra_options //= {}; | ||
251 | |||||
252 | 1 | 8µs | my %content_type_map = ( | ||
253 | 'html' => 'text/html', | ||||
254 | 'js' => 'text/javascript', | ||||
255 | 'json' => 'application/json', | ||||
256 | 'xml' => 'text/xml', | ||||
257 | # NOTE: not using application/atom+xml or application/rss+xml because of | ||||
258 | # Internet Explorer 6; see bug 2078. | ||||
259 | 'rss' => 'text/xml', | ||||
260 | 'atom' => 'text/xml' | ||||
261 | ); | ||||
262 | |||||
263 | 1 | 700ns | die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) ); | ||
264 | 1 | 400ns | my $cache_policy = 'no-cache'; | ||
265 | 1 | 400ns | $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching}; | ||
266 | 1 | 6µs | my $options = { | ||
267 | type => $content_type_map{$content_type}, | ||||
268 | status => $status, | ||||
269 | charset => 'UTF-8', | ||||
270 | Pragma => 'no-cache', | ||||
271 | 'Cache-Control' => $cache_policy, | ||||
272 | }; | ||||
273 | 1 | 400ns | $options->{expires} = 'now' if $extra_options->{force_no_caching}; | ||
274 | |||||
275 | 1 | 9µs | 4 | 301µs | $options->{cookie} = $cookie if $cookie; # spent 301µs making 4 calls to CGI::Cookie::as_string, avg 75µs/call |
276 | 1 | 1µs | if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died | ||
277 | 1 | 2µs | $options->{'Content-Style-Type' } = 'text/css'; | ||
278 | 1 | 1µs | $options->{'Content-Script-Type'} = 'text/javascript'; | ||
279 | } | ||||
280 | |||||
281 | # We can't encode here, that will double encode our templates, and xslt | ||||
282 | # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates | ||||
283 | |||||
284 | 1 | 84µs | 4 | 258µs | $data =~ s/\&\;amp\; /\&\; /g; # spent 258µs making 4 calls to C4::Output::CORE:subst, avg 65µs/call |
285 | 1 | 141µs | 8 | 3.10ms | print $query->header($options), $data; # spent 2.00ms making 3 calls to CGI::header, avg 666µs/call
# spent 654µs making 1 call to CGI::AUTOLOAD
# spent 448µs making 4 calls to C4::Output::CORE:print, avg 112µs/call |
286 | } | ||||
287 | |||||
288 | # spent 792µs (11+781) within C4::Output::output_html_with_http_headers which was called:
# once (11µs+781µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::__ANON__[/home/vagrant/kohaclone/mainpage.pl:107] at line 87 of mainpage.pl | ||||
289 | 1 | 900ns | my ( $query, $cookie, $data, $status, $extra_options ) = @_; | ||
290 | 1 | 10µs | 1 | 781µs | output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options ); # spent 781µs making 1 call to C4::Output::output_with_http_headers |
291 | } | ||||
292 | |||||
293 | |||||
294 | sub output_ajax_with_http_headers { | ||||
295 | my ( $query, $js ) = @_; | ||||
296 | print $query->header( | ||||
297 | -type => 'text/javascript', | ||||
298 | -charset => 'UTF-8', | ||||
299 | -Pragma => 'no-cache', | ||||
300 | -'Cache-Control' => 'no-cache', | ||||
301 | -expires => '-1d', | ||||
302 | ), $js; | ||||
303 | } | ||||
304 | |||||
305 | sub is_ajax { | ||||
306 | my $x_req = $ENV{HTTP_X_REQUESTED_WITH}; | ||||
307 | return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0; | ||||
308 | } | ||||
309 | |||||
310 | sub parametrized_url { | ||||
311 | my $url = shift || ''; # ie page.pl?ln={LANG} | ||||
312 | my $vars = shift || {}; # ie { LANG => en } | ||||
313 | my $ret = $url; | ||||
314 | while ( my ($key,$val) = each %$vars) { | ||||
315 | my $val_url = URI::Escape::uri_escape_utf8($val); | ||||
316 | $ret =~ s/\{$key\}/$val_url/g; | ||||
317 | } | ||||
318 | $ret =~ s/\{[^\{]*\}//g; # remove not defined vars | ||||
319 | return $ret; | ||||
320 | } | ||||
321 | |||||
322 | END { } # module clean-up code here (global destructor) | ||||
323 | |||||
324 | 1; | ||||
325 | __END__ | ||||
# spent 448µs within C4::Output::CORE:print which was called 4 times, avg 112µs/call:
# 4 times (448µs+0s) by C4::Output::output_with_http_headers at line 285, avg 112µs/call | |||||
# spent 258µs within C4::Output::CORE:subst which was called 4 times, avg 65µs/call:
# 4 times (258µs+0s) by C4::Output::output_with_http_headers at line 284, avg 65µs/call |