6 |
use base qw/ |
use base qw/ |
7 |
Catalyst::Model |
Catalyst::Model |
8 |
/; |
/; |
|
use Data::Dumper; |
|
9 |
use WebPAC::Store 0.08; |
use WebPAC::Store 0.08; |
|
use WebPAC::Output::TT 0.06; |
|
10 |
use WebPAC::Search::Estraier 0.05; |
use WebPAC::Search::Estraier 0.05; |
11 |
use File::Slurp; |
use File::Slurp; |
12 |
use Time::HiRes; |
use Time::HiRes qw/time/; |
13 |
|
use Encode qw/encode decode from_to/; |
14 |
|
use Template; |
15 |
|
use Data::Dumper; |
16 |
|
|
17 |
=head1 NAME |
=head1 NAME |
18 |
|
|
49 |
template: 'html_ffzg_results_short.tt' |
template: 'html_ffzg_results_short.tt' |
50 |
# encoding comming from webpac |
# encoding comming from webpac |
51 |
webpac_encoding: 'iso-8859-2' |
webpac_encoding: 'iso-8859-2' |
|
# encoding expected by Catalyst |
|
|
out_encoding: 'UTF-8' |
|
52 |
|
|
53 |
=cut |
=cut |
54 |
|
|
77 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
78 |
|
|
79 |
# save config parametars in object |
# save config parametars in object |
80 |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding defaultdepth/) { |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) { |
81 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
82 |
$c->config->{webpac}->{$f}; |
$c->config->{webpac}->{$f}; |
83 |
$log->debug("self->{$f} = " . $self->{$f}); |
$log->debug("self->{$f} = " . $self->{$f}); |
93 |
database => $est_cfg->{database}, |
database => $est_cfg->{database}, |
94 |
); |
); |
95 |
|
|
|
$self->{out} = new WebPAC::Output::TT( |
|
|
include_path => $template_path, |
|
|
filters => { foo => sub { shift } }, |
|
|
); |
|
|
|
|
96 |
# default template from config.yaml |
# default template from config.yaml |
97 |
$self->{template} ||= $c->config->{webpac}->{template}; |
$self->{template} ||= $c->config->{webpac}->{template}; |
98 |
|
|
|
$self->{iconv} = new Text::Iconv( |
|
|
$c->config->{webpac}->{webpac_encoding}, |
|
|
$c->config->{webpac}->{out_encoding} |
|
|
); |
|
|
|
|
99 |
$log->debug("converting encoding from webpac_encoding '" . |
$log->debug("converting encoding from webpac_encoding '" . |
100 |
$c->config->{webpac}->{webpac_encoding} . |
$c->config->{webpac}->{webpac_encoding} . |
|
"' to '" . |
|
|
$c->config->{webpac}->{out_encoding} . |
|
101 |
"'" |
"'" |
102 |
); |
); |
103 |
|
|
104 |
$self->{databases} = $c->config->{databases} || $log->error("can't find databases in config"); |
$self->{databases} = $c->config->{databases} || $log->error("can't find databases in config"); |
105 |
|
|
106 |
|
# create Template toolkit instance |
107 |
|
$self->{'tt'} = Template->new( |
108 |
|
INCLUDE_PATH => $template_path, |
109 |
|
FILTERS => { |
110 |
|
dump_html => sub { |
111 |
|
return unless (@_); |
112 |
|
my $out; |
113 |
|
my $i = 1; |
114 |
|
foreach my $v (@_) { |
115 |
|
$out .= qq{<div id="dump_$i">} . |
116 |
|
Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) . |
117 |
|
qq{</div>}; |
118 |
|
$i++; |
119 |
|
} |
120 |
|
$out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out); |
121 |
|
return $out; |
122 |
|
} |
123 |
|
}, |
124 |
|
EVAL_PERL => 1, |
125 |
|
); |
126 |
|
|
127 |
return $self; |
return $self; |
128 |
|
|
129 |
} |
} |
149 |
sub search { |
sub search { |
150 |
my $self = shift; |
my $self = shift; |
151 |
|
|
152 |
|
my $search_start_t = time(); |
153 |
|
|
154 |
my $args = {@_}; |
my $args = {@_}; |
155 |
|
|
156 |
my $log = $self->{log}; |
my $log = $self->{log}; |
157 |
|
|
158 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("search args: " . Dumper( $args )); |
159 |
|
|
160 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
161 |
|
|
191 |
|
|
192 |
my $hits = $#results + 1; |
my $hits = $#results + 1; |
193 |
|
|
194 |
$log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) ); |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
195 |
|
|
196 |
# just return results? |
# just return results? |
197 |
return @results unless ($args->{'template'}); |
return @results unless ($args->{'template'}); |
228 |
|
|
229 |
$t = time(); |
$t = time(); |
230 |
|
|
231 |
my $html = $self->{out}->apply( |
my $html = $self->apply( |
232 |
template => $template_filename, |
template => $template_filename, |
233 |
data => $ds, |
data => $ds, |
234 |
record_uri => "${database}/${prefix}/${id}", |
record_uri => "${database}/${prefix}/${id}", |
239 |
|
|
240 |
$t = time(); |
$t = time(); |
241 |
|
|
242 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
|
|
|
|
$times->{iconv} += time() - $t; |
|
243 |
|
|
244 |
push @html_results, $html; |
push @html_results, $html; |
245 |
|
|
248 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
249 |
|
|
250 |
$log->debug( sprintf( |
$log->debug( sprintf( |
251 |
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
"duration breakdown: store %.6fs, apply %.6fs, total: %.6fs", |
252 |
$times->{db}, $times->{out}, $times->{iconv}, |
$times->{db}, $times->{out}, time() - $search_start_t, |
253 |
) ); |
) ); |
254 |
|
|
255 |
return \@html_results; |
return \@html_results; |
272 |
|
|
273 |
my $args = {@_}; |
my $args = {@_}; |
274 |
my $log = $self->{log}; |
my $log = $self->{log}; |
275 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("record args: " . Dumper( $args )); |
276 |
|
|
277 |
foreach my $f (qw/record_uri template/) { |
foreach my $f (qw/record_uri template/) { |
278 |
$log->fatal("need $f") unless ($args->{$f}); |
$log->fatal("need $f") unless ($args->{$f}); |
293 |
return; |
return; |
294 |
} |
} |
295 |
|
|
296 |
my $html = $self->{out}->apply( |
my $html = $self->apply( |
297 |
template => $args->{template}, |
template => $args->{template}, |
298 |
data => $ds, |
data => $ds, |
299 |
record_uri => $args->{record_uri}, |
record_uri => $args->{record_uri}, |
300 |
config => $self->{databases}->{$database}, |
config => $self->{databases}->{$database}, |
301 |
); |
); |
302 |
|
|
303 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
304 |
|
|
305 |
return $html; |
return $html; |
306 |
} |
} |
310 |
|
|
311 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
312 |
|
|
313 |
It will use C<Text::Iconv> to convert content encoding back to |
It will use C<Encode> to convert content encoding back to |
314 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
315 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
316 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
321 |
sub save_html { |
sub save_html { |
322 |
my ($self, $path, $content) = @_; |
my ($self, $path, $content) = @_; |
323 |
|
|
324 |
|
# FIXME Should this be UTF-8 or someting? |
325 |
|
my $js_encoding = $self->{webpac_encoding}; |
326 |
|
$js_encoding = 'UTF-16'; |
327 |
|
|
328 |
sub _conv_js { |
sub _conv_js { |
329 |
my $t = shift || return; |
return '0x' . $_[1]; |
330 |
return $self->{iconv}->convert(chr(hex($t))); |
return encode($_[0], chr(hex($_[1]))); |
331 |
} |
} |
332 |
$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex; |
#$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex; |
333 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
334 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
335 |
$content =~ s/\n\r/\n/gs; |
$content =~ s/\n\r/\n/gs; |
336 |
|
|
337 |
my ($from, $to) = ( |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
338 |
$self->{out_encoding}, |
$self->{log}->debug("convert encoding to $disk_encoding"); |
339 |
$self->{webpac_encoding}, |
from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content"); |
|
); |
|
|
|
|
|
$self->{log}->debug("using iconv to convert from $from to $to encoding"); |
|
|
|
|
|
my $iconv_on_save = new Text::Iconv($from, $to) |
|
|
|| $self->{log}->fatal("can't create iconv for saving"); |
|
|
|
|
|
$content = $iconv_on_save->convert( $content ) || die "no content?"; |
|
340 |
|
|
341 |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
342 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
358 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
359 |
|
|
360 |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
|
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
|
|
$content = $self->{iconv}->convert($content); |
|
361 |
|
|
362 |
return $content; |
return decode($self->{webpac_encoding}, $content); |
363 |
} |
} |
364 |
|
|
365 |
|
|
366 |
|
=head2 apply |
367 |
|
|
368 |
|
Create output from in-memory data structure using Template Toolkit template. |
369 |
|
|
370 |
|
my $text = $tt->apply( |
371 |
|
template => 'text.tt', |
372 |
|
data => $ds, |
373 |
|
record_uri => 'database/prefix/mfn', |
374 |
|
); |
375 |
|
|
376 |
|
It also has follwing template toolikit filter routies defined: |
377 |
|
|
378 |
|
=cut |
379 |
|
|
380 |
|
sub apply { |
381 |
|
my $self = shift; |
382 |
|
|
383 |
|
my $args = {@_}; |
384 |
|
|
385 |
|
my $log = $self->{log} || die "no log?"; |
386 |
|
|
387 |
|
foreach my $a (qw/template data/) { |
388 |
|
$log->logconfess("need $a") unless ($args->{$a}); |
389 |
|
} |
390 |
|
|
391 |
|
=head3 tt_filter_type |
392 |
|
|
393 |
|
filter to return values of specified from $ds, usage from TT template is in form |
394 |
|
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
395 |
|
|
396 |
|
[% d('Title') %] |
397 |
|
[% d('Author',', ' %] |
398 |
|
|
399 |
|
=cut |
400 |
|
|
401 |
|
sub tt_filter_type { |
402 |
|
my ($data,$type) = @_; |
403 |
|
|
404 |
|
die "no data?" unless ($data); |
405 |
|
$type ||= 'display'; |
406 |
|
|
407 |
|
my $default_delimiter = { |
408 |
|
'display' => '¶<br/>', |
409 |
|
'index' => '\n', |
410 |
|
}; |
411 |
|
|
412 |
|
return sub { |
413 |
|
|
414 |
|
my ($name,$join) = @_; |
415 |
|
|
416 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
417 |
|
# Hm? Should we die here? |
418 |
|
return unless ($name); |
419 |
|
|
420 |
|
my $item = $data->{'data'}->{$name} || return; |
421 |
|
|
422 |
|
my $v = $item->{$type} || return; |
423 |
|
|
424 |
|
if (ref($v) eq 'ARRAY') { |
425 |
|
if ($#{$v} == 0) { |
426 |
|
$v = $v->[0]; |
427 |
|
} else { |
428 |
|
$join = $default_delimiter->{$type} unless defined($join); |
429 |
|
$v = join($join, @{$v}); |
430 |
|
} |
431 |
|
} else { |
432 |
|
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
433 |
|
} |
434 |
|
|
435 |
|
return $v; |
436 |
|
} |
437 |
|
} |
438 |
|
|
439 |
|
$args->{'d'} = tt_filter_type($args, 'display'); |
440 |
|
$args->{'display'} = tt_filter_type($args, 'display'); |
441 |
|
|
442 |
|
=head3 tt_filter_search |
443 |
|
|
444 |
|
filter to return links to search, usage in TT: |
445 |
|
|
446 |
|
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
447 |
|
|
448 |
|
=cut |
449 |
|
|
450 |
|
sub tt_filter_search { |
451 |
|
|
452 |
|
my ($data) = @_; |
453 |
|
|
454 |
|
die "no data?" unless ($data); |
455 |
|
|
456 |
|
return sub { |
457 |
|
|
458 |
|
my ($display,$search,$delimiter,$template) = @_; |
459 |
|
|
460 |
|
# default delimiter |
461 |
|
$delimiter ||= '¶<br/>', |
462 |
|
|
463 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
464 |
|
# Hm? Should we die here? |
465 |
|
return unless ($display); |
466 |
|
|
467 |
|
my $item = $data->{'data'}->{$display} || return; |
468 |
|
|
469 |
|
return unless($item->{'display'}); |
470 |
|
if (! $item->{'search'}) { |
471 |
|
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
472 |
|
return; |
473 |
|
} |
474 |
|
|
475 |
|
my @warn; |
476 |
|
foreach my $type (qw/display search/) { |
477 |
|
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
478 |
|
} |
479 |
|
|
480 |
|
if (@warn) { |
481 |
|
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
482 |
|
return; |
483 |
|
} |
484 |
|
my @html; |
485 |
|
|
486 |
|
my $d_el = $#{ $item->{'display'} }; |
487 |
|
my $s_el = $#{ $item->{'search'} }; |
488 |
|
|
489 |
|
# easy, both fields have same number of elements or there is just |
490 |
|
# one search and multiple display |
491 |
|
if ( $d_el == $s_el || $s_el == 0 ) { |
492 |
|
|
493 |
|
foreach my $i ( 0 .. $d_el ) { |
494 |
|
|
495 |
|
my $s; |
496 |
|
if ($s_el > 0) { |
497 |
|
$s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search"; |
498 |
|
} else { |
499 |
|
$s = $item->{'search'}->[0]; |
500 |
|
} |
501 |
|
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
502 |
|
$s = __quotemeta( $s ); |
503 |
|
|
504 |
|
my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; |
505 |
|
|
506 |
|
my $template_arg = ''; |
507 |
|
$template_arg = qq{,'$template'} if ($template); |
508 |
|
|
509 |
|
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>}; |
510 |
|
} |
511 |
|
|
512 |
|
return join($delimiter, @html); |
513 |
|
} else { |
514 |
|
my $html = qq{<div class="notice">WARNING: we should really support if there is $d_el display elements and $s_el search elements, but currently there is no nice way to do so, so we will just display values</div>}; |
515 |
|
my $v = $item->{'display'}; |
516 |
|
|
517 |
|
if ($#{$v} == 0) { |
518 |
|
$html .= $v->[0]; |
519 |
|
} else { |
520 |
|
$html .= join($delimiter, @{$v}); |
521 |
|
} |
522 |
|
return $html; |
523 |
|
} |
524 |
|
} |
525 |
|
} |
526 |
|
|
527 |
|
$args->{'search'} = tt_filter_search($args); |
528 |
|
|
529 |
|
=head3 load_rec |
530 |
|
|
531 |
|
Used mostly for onClick events like this: |
532 |
|
|
533 |
|
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
534 |
|
|
535 |
|
It will automatically do sanity checking and create correct JavaScript code. |
536 |
|
|
537 |
|
=cut |
538 |
|
|
539 |
|
$args->{'load_rec'} = sub { |
540 |
|
my @errors; |
541 |
|
|
542 |
|
my $record_uri = shift or push @errors, "record_uri missing"; |
543 |
|
my $template = shift or push @errors, "template missing"; |
544 |
|
|
545 |
|
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
546 |
|
push @errors, "invalid format of record_uri: $record_uri"; |
547 |
|
} |
548 |
|
|
549 |
|
if (@errors) { |
550 |
|
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
551 |
|
} else { |
552 |
|
return "load_rec('$record_uri','$template'); return false;"; |
553 |
|
} |
554 |
|
}; |
555 |
|
|
556 |
|
=head3 load_template |
557 |
|
|
558 |
|
Used to re-submit search request and load results in different template |
559 |
|
|
560 |
|
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
561 |
|
|
562 |
|
=cut |
563 |
|
|
564 |
|
$args->{'load_template'} = sub { |
565 |
|
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
566 |
|
return "load_template($template); return false;"; |
567 |
|
}; |
568 |
|
|
569 |
|
my $out; |
570 |
|
|
571 |
|
$self->{'tt'}->process( |
572 |
|
$args->{'template'}, |
573 |
|
$args, |
574 |
|
\$out |
575 |
|
) || $log->error( "apply can't process template: ", $self->{'tt'}->error() ); |
576 |
|
|
577 |
|
return $out; |
578 |
|
} |
579 |
|
|
580 |
|
|
581 |
|
=head2 __quotemeta |
582 |
|
|
583 |
|
Helper to quote JavaScript-friendly characters |
584 |
|
|
585 |
|
=cut |
586 |
|
|
587 |
|
sub __quotemeta { |
588 |
|
local $_ = shift; |
589 |
|
$_ = decode('iso-8859-2', $_); |
590 |
|
|
591 |
|
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
592 |
|
{ |
593 |
|
use bytes; |
594 |
|
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
595 |
|
} |
596 |
|
|
597 |
|
s/\\x09/\\t/g; |
598 |
|
s/\\x0A/\\n/g; |
599 |
|
s/\\x0D/\\r/g; |
600 |
|
s/"/\\"/g; |
601 |
|
s/\\x5C/\\\\/g; |
602 |
|
|
603 |
|
return $_; |
604 |
|
} |
605 |
|
|
606 |
|
|
607 |
|
|
608 |
=head1 AUTHOR |
=head1 AUTHOR |
609 |
|
|
610 |
Dobrica Pavlinusic |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
611 |
|
|
612 |
=head1 LICENSE |
=head1 LICENSE |
613 |
|
|