1 |
package Webpacus::Model::WebPAC; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use lib '/data/webpac2/lib'; |
6 |
use base qw/ |
7 |
Catalyst::Model |
8 |
/; |
9 |
use WebPAC::Store 0.08; |
10 |
use Search::Estraier 0.04; |
11 |
use File::Slurp; |
12 |
use Time::HiRes qw/time/; |
13 |
use Encode qw/encode decode from_to/; |
14 |
use Template; |
15 |
|
16 |
=head1 NAME |
17 |
|
18 |
Webpacus::Model::WebPAC - Catalyst Model |
19 |
|
20 |
=head1 SYNOPSIS |
21 |
|
22 |
See L<Webpacus> and L<WebPAC>. |
23 |
|
24 |
=head1 DESCRIPTION |
25 |
|
26 |
Catalyst Model for access to WebPAC data. |
27 |
|
28 |
=head2 new |
29 |
|
30 |
Configuration for hyperestraier in C<config.yaml> like this: |
31 |
|
32 |
--- #YAML:1.0 |
33 |
# DO NOT USE TABS FOR INDENTATION OR label/value SEPARATION!!! |
34 |
|
35 |
# configuration for hyper estraier full text search engine |
36 |
hyperestraier: |
37 |
masterurl: 'http://localhost:1978/node/webpac2' |
38 |
defaultnode: 'webpac2' |
39 |
defaultdepth: 1 |
40 |
user: 'admin' |
41 |
passwd: 'admin' |
42 |
hits_on_page: 100 |
43 |
hits_for_pager: 1000 |
44 |
|
45 |
webpac: |
46 |
db_path: '/data/webpac2/db' |
47 |
template_path: '/data/webpac2/conf/output/tt' |
48 |
template: 'html_ffzg_results_short.tt' |
49 |
# encoding comming from webpac |
50 |
webpac_encoding: 'iso-8859-2' |
51 |
|
52 |
=cut |
53 |
|
54 |
sub new { |
55 |
my ( $self, $c, $config ) = @_; |
56 |
|
57 |
$self = $self->NEXT::new($c, $config); |
58 |
$self->config($config); |
59 |
|
60 |
my $log = $c->log; |
61 |
$self->{log} = $log; |
62 |
|
63 |
my $est_cfg = $c->config->{hyperestraier}; |
64 |
$est_cfg->{'log'} = $log; |
65 |
|
66 |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
67 |
|
68 |
$log->dumper($est_cfg, 'est_cfg'); |
69 |
|
70 |
if (! $est_cfg->{database}) { |
71 |
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
72 |
$log->info("using default node $defaultnode"); |
73 |
$est_cfg->{database} = $defaultnode; |
74 |
} |
75 |
|
76 |
my $url = $est_cfg->{masterurl} . '/node/' . $est_cfg->{database}; |
77 |
|
78 |
$log->info("opening Hyper Estraier index $url as $est_cfg->{'user'}"); |
79 |
|
80 |
$self->{est_node} = Search::Estraier::Node->new( |
81 |
url => $url, |
82 |
user => $est_cfg->{user}, |
83 |
passwd => $est_cfg->{passwd}, |
84 |
); |
85 |
|
86 |
$log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node}); |
87 |
|
88 |
# save config parametars in object |
89 |
foreach my $f (qw/ |
90 |
db_path template_path hits_on_page webpac_encoding defaultdepth |
91 |
masterurl defaultnode |
92 |
/) { |
93 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
94 |
$c->config->{webpac}->{$f}; |
95 |
$log->debug("self->{$f} = " . $self->{$f}); |
96 |
} |
97 |
my $db_path = $self->{db_path}; |
98 |
my $template_path = $self->{template_path}; |
99 |
|
100 |
$log->debug("using db path '$db_path', template path '$template_path'"); |
101 |
|
102 |
$self->{db} = new WebPAC::Store( |
103 |
path => $db_path, |
104 |
read_only => 1, |
105 |
database => $est_cfg->{database}, |
106 |
); |
107 |
|
108 |
# default template from config.yaml |
109 |
$self->{template} ||= $c->config->{webpac}->{template}; |
110 |
|
111 |
$log->debug("converting encoding from webpac_encoding '" . |
112 |
$c->config->{webpac}->{webpac_encoding} . |
113 |
"'" |
114 |
); |
115 |
|
116 |
$self->{databases} = $c->config->{databases} || $log->fatal("can't find databases in config"); |
117 |
|
118 |
# create Template toolkit instance |
119 |
$self->{'tt'} = Template->new( |
120 |
INCLUDE_PATH => $template_path, |
121 |
FILTERS => { |
122 |
dump_html => sub { |
123 |
return unless (@_); |
124 |
my $out; |
125 |
my $i = 1; |
126 |
foreach my $v (@_) { |
127 |
$out .= qq{<div id="dump_$i">} . |
128 |
Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) . |
129 |
qq{</div>}; |
130 |
$i++; |
131 |
} |
132 |
$out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out); |
133 |
return $out; |
134 |
} |
135 |
}, |
136 |
EVAL_PERL => 1, |
137 |
); |
138 |
|
139 |
return $self; |
140 |
|
141 |
} |
142 |
|
143 |
=head2 setup_site |
144 |
|
145 |
$self->setup_site('site_name'); |
146 |
|
147 |
Change node URL and database name according to site name (if available) or fallback |
148 |
to C<defaultnode> from configuration. |
149 |
|
150 |
=cut |
151 |
|
152 |
sub setup_site { |
153 |
my $self = shift; |
154 |
|
155 |
my $site = shift || $self->{defaultnode}; |
156 |
if (! $site) { |
157 |
$self->{log}->warn("not changing site from " . $self->{est_node}->{url}); |
158 |
return; |
159 |
} |
160 |
|
161 |
$self->{log}->fatal("setup_site can't find site or defaultnode") unless ($site); |
162 |
|
163 |
my $url = $self->{masterurl} . '/node/' . $site; |
164 |
$self->{est_node}->set_url( $url ); |
165 |
$self->{log}->debug("setup_site '$site' using $url"); |
166 |
} |
167 |
|
168 |
=head2 search |
169 |
|
170 |
my $m->search( |
171 |
phrase => 'query phrase', |
172 |
add_attr => \@add_attr |
173 |
get_attr => [ '@uri' ], |
174 |
max => 42, |
175 |
template => 'result_template.tt', |
176 |
depth => 1, |
177 |
); |
178 |
|
179 |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
180 |
C<template> which will (if specified) return results in HTML using |
181 |
selected template. |
182 |
|
183 |
=cut |
184 |
|
185 |
sub search { |
186 |
my $self = shift; |
187 |
|
188 |
my $search_start_t = time(); |
189 |
|
190 |
my $args = {@_}; |
191 |
|
192 |
my $log = $self->{log}; |
193 |
|
194 |
$log->dumper($args, 'args'); |
195 |
|
196 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
197 |
|
198 |
my $template_filename = $args->{template} || $self->{template}; |
199 |
|
200 |
$args->{hits_on_page} ||= $self->{'hits_for_pager'}; |
201 |
if (! $args->{hits_on_page}) { |
202 |
$args->{hits_on_page} = 100; |
203 |
$log->warn("max not set when calling model. Using default of $args->{hits_on_page}"); |
204 |
} |
205 |
|
206 |
my $times; # store some times for benchmarking |
207 |
|
208 |
my $t = time(); |
209 |
|
210 |
# transfer depth of search |
211 |
if (! $args->{depth}) { |
212 |
my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration"); |
213 |
$args->{depth} = $default; |
214 |
$log->warn("using default search depth $default"); |
215 |
} |
216 |
$args->{depth} ||= 0; |
217 |
|
218 |
$log->debug("searching " . $self->{est_node}->{url} . " hits on page: $args->{hits_on_page} depth: $args->{depth} phrase: " . ($query || '[none]') ); |
219 |
|
220 |
# |
221 |
# construct condition for Hyper Estraier |
222 |
# |
223 |
my $cond = Search::Estraier::Condition->new(); |
224 |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
225 |
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
226 |
map { |
227 |
$cond->add_attr( $_ ); |
228 |
$log->debug(" + $_"); |
229 |
} @{ $args->{add_attr} }; |
230 |
}; |
231 |
|
232 |
$cond->set_phrase( $query ) if ($query); |
233 |
$cond->set_options( $args->{options} ) if ($args->{options}); |
234 |
$cond->set_order( $args->{order} ) if ($args->{order}); |
235 |
|
236 |
my $hits_on_page = $args->{hits_on_page} || 7; |
237 |
my $page = $args->{page} || 1; |
238 |
if ($page < 1) { |
239 |
$log->warn("page number $page < 1"); |
240 |
$page = 1; |
241 |
} |
242 |
|
243 |
$cond->set_max( my $max = $page * $hits_on_page ); |
244 |
$cond->set_skip( my $skip = ( $page - 1 ) * $hits_on_page ); |
245 |
|
246 |
$log->debug("search max: $max, skip: $skip"); |
247 |
|
248 |
my $result = $self->{est_node}->search($cond, $args->{depth}); |
249 |
if (! $result) { |
250 |
$self->{log}->fatal("search didn't return result"); |
251 |
return; |
252 |
} |
253 |
my $hits = $result->doc_num; |
254 |
|
255 |
$times->{est} += time() - $t; |
256 |
|
257 |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
258 |
|
259 |
$self->{hints} = $result->{hints}; |
260 |
#$log->dumper($self->{hints}, 'original hints' ); |
261 |
|
262 |
# |
263 |
# fetch results |
264 |
# |
265 |
|
266 |
my @results; |
267 |
|
268 |
|
269 |
|
270 |
for my $i ( 0 .. ( $hits < $max ? ($hits-1) : ($max-1) ) ) { |
271 |
|
272 |
$t = time(); |
273 |
|
274 |
#$log->debug("get_doc($i)"); |
275 |
my $doc = $result->get_doc( $i ); |
276 |
if (! $doc) { |
277 |
$log->warn("can't find result $i"); |
278 |
next; |
279 |
} |
280 |
|
281 |
my $hash; |
282 |
|
283 |
foreach my $attr (@{ $args->{get_attr} }) { |
284 |
my $val = $doc->attr( $attr ); |
285 |
#$log->debug("attr $attr = ", $val || 'undef'); |
286 |
$hash->{$attr} = $val if (defined($val)); |
287 |
} |
288 |
|
289 |
$times->{hash} += time() - $t; |
290 |
|
291 |
next unless ($hash); |
292 |
|
293 |
if (! $args->{'template'}) { |
294 |
push @results, $hash; |
295 |
} else { |
296 |
my ($database, $prefix, $id); |
297 |
|
298 |
if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { |
299 |
($database, $prefix,$id) = ($1,$2,$3); |
300 |
} else { |
301 |
$log->warn("can't decode database/prefix/id from " . $hash->{'@uri'}); |
302 |
next; |
303 |
} |
304 |
|
305 |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
306 |
|
307 |
$t = time(); |
308 |
|
309 |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
310 |
if (! $ds) { |
311 |
$log->error("can't load_ds( ${database}/${prefix}/${id} )"); |
312 |
next; |
313 |
} |
314 |
|
315 |
$times->{db} += time() - $t; |
316 |
|
317 |
$t = time(); |
318 |
|
319 |
my $html = $self->apply( |
320 |
template => $template_filename, |
321 |
data => $ds, |
322 |
record_uri => "${database}/${prefix}/${id}", |
323 |
config => $self->{databases}->{$database}, |
324 |
); |
325 |
|
326 |
$times->{apply} += time() - $t; |
327 |
|
328 |
$t = time(); |
329 |
|
330 |
$html = decode($self->{webpac_encoding}, $html); |
331 |
|
332 |
$times->{decode} += time() - $t; |
333 |
|
334 |
push @results, $html; |
335 |
} |
336 |
|
337 |
} |
338 |
|
339 |
$log->debug( sprintf( |
340 |
"duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", |
341 |
$times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, |
342 |
) ); |
343 |
|
344 |
return \@results; |
345 |
} |
346 |
|
347 |
=head2 hints |
348 |
|
349 |
my $hints = $m->hints; |
350 |
|
351 |
Return various useful hints about result |
352 |
|
353 |
=cut |
354 |
|
355 |
sub hints { |
356 |
my $self = shift; |
357 |
|
358 |
unless ($self->{hints}) { |
359 |
$self->{log}->fatal("no hints found!"); |
360 |
return; |
361 |
} |
362 |
|
363 |
my $hints; |
364 |
|
365 |
while (my ($key,$val) = each %{ $self->{hints} }) { |
366 |
|
367 |
#$self->{log}->debug("current hint $key = $val"); |
368 |
|
369 |
if ($key =~ m/^(?:HITS*|TIME|DOCNUM|WORDNUM)$/) { |
370 |
$hints->{ lc($key) } = $val; |
371 |
} elsif ($key =~ m/^HINT#/) { |
372 |
my ($word,$count) = split(/\t/,$val,2); |
373 |
$hints->{words}->{$word} = $count; |
374 |
} elsif ($key =~ m/^LINK#/) { |
375 |
my ($url,undef,undef,undef,undef,undef,$results) = split(/\t/,$val,7); |
376 |
if ($url =~ m#/node/(.+)$#) { |
377 |
$hints->{node}->{$1} = $results; |
378 |
} else { |
379 |
$self->{log}->debug("url $url doesn't have /node/ in it!"); |
380 |
} |
381 |
} else { |
382 |
$self->{log}->debug("unknown hint $key = $val"); |
383 |
} |
384 |
|
385 |
} |
386 |
|
387 |
$self->{log}->dumper($hints, 'model hints' ); |
388 |
|
389 |
return $hints; |
390 |
} |
391 |
|
392 |
|
393 |
=head2 record |
394 |
|
395 |
my $html = $m->record( |
396 |
mfn => 42, |
397 |
template => 'foo.tt', |
398 |
); |
399 |
|
400 |
This will load one record, convert it to html using C<template> and return |
401 |
it. |
402 |
|
403 |
=cut |
404 |
|
405 |
sub record { |
406 |
my $self = shift; |
407 |
|
408 |
my $args = {@_}; |
409 |
my $log = $self->{log}; |
410 |
$log->dumper( $args, 'args' ); |
411 |
|
412 |
foreach my $f (qw/record_uri template/) { |
413 |
$log->fatal("need $f") unless ($args->{$f}); |
414 |
} |
415 |
|
416 |
my ($database, $prefix, $id); |
417 |
|
418 |
if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) { |
419 |
($database, $prefix, $id) = ($1,$2,$3); |
420 |
} else { |
421 |
$log->error("can't parse $args->{record_uri} into prefix, database and uri"); |
422 |
return; |
423 |
} |
424 |
|
425 |
my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database ); |
426 |
if (! $ds) { |
427 |
$log->error("can't load_ds( $database/$prefix/$id )"); |
428 |
return; |
429 |
} |
430 |
|
431 |
my $html = $self->apply( |
432 |
template => $args->{template}, |
433 |
data => $ds, |
434 |
record_uri => $args->{record_uri}, |
435 |
config => $self->{databases}->{$database}, |
436 |
); |
437 |
|
438 |
$html = decode($self->{webpac_encoding}, $html); |
439 |
|
440 |
return $html; |
441 |
} |
442 |
|
443 |
|
444 |
=head2 list_nodes |
445 |
|
446 |
my @nodes = $m->list_nodes( 'site' ); |
447 |
|
448 |
Return all databases which have records for selected site. Returned array of |
449 |
hashes has elements C<name> and C<label>. |
450 |
|
451 |
This function does cacheing inside C<< $self->{nodes_in_site} >>, but you |
452 |
probably didn't want to know that. |
453 |
|
454 |
=cut |
455 |
|
456 |
sub list_nodes { |
457 |
my $self = shift; |
458 |
|
459 |
my $site = shift || $self->{defaultnode}; |
460 |
|
461 |
# cache? |
462 |
if ($self->{nodes_in_site}->{$site} && ref($self->{nodes_in_site}->{$site}) eq 'ARRAY') { |
463 |
$self->{log}->debug("list_nodes for site $site and returns from cache"); |
464 |
return @{ $self->{nodes_in_site}->{$site} }; |
465 |
}; |
466 |
|
467 |
$self->{log}->debug("list_nodes for site $site"); |
468 |
|
469 |
$self->setup_site( $site ); |
470 |
|
471 |
my @nodes; |
472 |
|
473 |
if ($self->{est_node}->doc_num > 0) { |
474 |
push @nodes, { |
475 |
name => $self->{est_node}->name, |
476 |
label => $self->{est_node}->label, |
477 |
doc_num => $self->{est_node}->doc_num, |
478 |
} |
479 |
} |
480 |
|
481 |
# refresh set info |
482 |
$self->{est_node}->_set_info; |
483 |
|
484 |
my $links = $self->{est_node}->links || return @nodes; |
485 |
|
486 |
$self->{log}->dumper( $links, 'links' ); |
487 |
|
488 |
foreach my $link (@{ $links }) { |
489 |
my ($url, $label, $credit) = split(/\t/, $link, 3); |
490 |
if ($url =~ m#/node/(.+)$#) { |
491 |
my $node = $1; |
492 |
$self->setup_site( $node ); |
493 |
$self->{est_node}->_set_info; |
494 |
$label = decode('UTF-8', $label); |
495 |
push @nodes, { |
496 |
name => $node, |
497 |
label => $label, |
498 |
doc_num => $self->{est_node}->doc_num, |
499 |
} |
500 |
} else { |
501 |
$self->{log}->warn("can't find node name in link $link"); |
502 |
} |
503 |
} |
504 |
|
505 |
$self->setup_site( $site ); |
506 |
$self->{est_node}->_set_info; |
507 |
|
508 |
$self->{nodes_in_site}->{$site} = \@nodes; |
509 |
|
510 |
return @nodes; |
511 |
} |
512 |
|
513 |
=head2 save_html |
514 |
|
515 |
$m->save_html( '/full/path/to/file', $content ); |
516 |
|
517 |
It will use C<Encode> to convert content encoding back to |
518 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
519 |
strip extra newlines at beginning and end, and save to |
520 |
C</full/path/to/file.new> and if that succeeds, just rename |
521 |
it over original file which should be atomic on filesystem level. |
522 |
|
523 |
=cut |
524 |
|
525 |
sub save_html { |
526 |
my ($self, $path, $content) = @_; |
527 |
|
528 |
# FIXME Should this be UTF-8 or someting? |
529 |
my $js_encoding = $self->{webpac_encoding}; |
530 |
$js_encoding = 'UTF-16'; |
531 |
|
532 |
sub _conv_js { |
533 |
return '0x' . $_[1]; |
534 |
return encode($_[0], chr(hex($_[1]))); |
535 |
} |
536 |
#$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex; |
537 |
$content =~ s/^[\n\r]+//s; |
538 |
$content =~ s/[\n\r]+$/\n/s; |
539 |
$content =~ s/\n\r/\n/gs; |
540 |
|
541 |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
542 |
$self->{log}->debug("convert encoding to $disk_encoding"); |
543 |
from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content"); |
544 |
|
545 |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
546 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
547 |
} |
548 |
|
549 |
=head2 load_html |
550 |
|
551 |
my $html = $m->load_html('/full/path/to/file'); |
552 |
|
553 |
This will convert file from Webpac encoding to Catalyst and |
554 |
convert that data to escaped HTML (for sending into |
555 |
C<< <textarea/> >> tags in html. |
556 |
|
557 |
=cut |
558 |
|
559 |
sub load_html { |
560 |
my ($self, $path) = @_; |
561 |
|
562 |
die "no path?" unless ($path); |
563 |
|
564 |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
565 |
|
566 |
return decode($self->{webpac_encoding}, $content); |
567 |
} |
568 |
|
569 |
|
570 |
=head2 apply |
571 |
|
572 |
Create output from in-memory data structure using Template Toolkit template. |
573 |
|
574 |
my $text = $tt->apply( |
575 |
template => 'text.tt', |
576 |
data => $ds, |
577 |
record_uri => 'database/prefix/mfn', |
578 |
); |
579 |
|
580 |
It also has follwing template toolikit filter routies defined: |
581 |
|
582 |
=cut |
583 |
|
584 |
# Escape <, >, & and ", and to produce valid XML |
585 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
586 |
my $escape_re = join '|' => keys %escape; |
587 |
|
588 |
sub apply { |
589 |
my $self = shift; |
590 |
|
591 |
my $args = {@_}; |
592 |
|
593 |
my $log = $self->{log} || die "no log?"; |
594 |
|
595 |
foreach my $a (qw/template data/) { |
596 |
$log->fatal("need $a") unless ($args->{$a}); |
597 |
} |
598 |
|
599 |
=head3 tt_filter_type |
600 |
|
601 |
filter to return values of specified from $ds, usage from TT template is in form |
602 |
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
603 |
|
604 |
[% d('Title') %] |
605 |
[% d('Author',', ' %] |
606 |
|
607 |
=cut |
608 |
|
609 |
sub tt_filter_type { |
610 |
my ($data,$type) = @_; |
611 |
|
612 |
die "no data?" unless ($data); |
613 |
$type ||= 'display'; |
614 |
|
615 |
my $default_delimiter = { |
616 |
'display' => '¶<br/>', |
617 |
'index' => '\n', |
618 |
}; |
619 |
|
620 |
return sub { |
621 |
|
622 |
my ($name,$join) = @_; |
623 |
|
624 |
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
625 |
# Hm? Should we die here? |
626 |
return unless ($name); |
627 |
|
628 |
my $item = $data->{'data'}->{$name} || return; |
629 |
|
630 |
my $v = $item->{$type} || return; |
631 |
|
632 |
if (ref($v) eq 'ARRAY') { |
633 |
if ($#{$v} == 0) { |
634 |
$v = $v->[0]; |
635 |
$v =~ s/($escape_re)/$escape{$1}/g; |
636 |
} else { |
637 |
$join = $default_delimiter->{$type} unless defined($join); |
638 |
$v = join($join, map { |
639 |
s/($escape_re)/$escape{$1}/g; |
640 |
} @{$v}); |
641 |
} |
642 |
} else { |
643 |
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
644 |
} |
645 |
|
646 |
return $v; |
647 |
} |
648 |
} |
649 |
|
650 |
$args->{'d'} = tt_filter_type($args, 'display'); |
651 |
$args->{'display'} = tt_filter_type($args, 'display'); |
652 |
|
653 |
=head3 tt_filter_search |
654 |
|
655 |
filter to return links to search, usage in TT: |
656 |
|
657 |
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
658 |
|
659 |
=cut |
660 |
|
661 |
sub tt_filter_search { |
662 |
|
663 |
my ($data) = @_; |
664 |
|
665 |
die "no data?" unless ($data); |
666 |
|
667 |
return sub { |
668 |
|
669 |
my ($display,$search,$delimiter,$template) = @_; |
670 |
|
671 |
# default delimiter |
672 |
$delimiter ||= '¶<br/>', |
673 |
|
674 |
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
675 |
# Hm? Should we die here? |
676 |
return unless ($display); |
677 |
|
678 |
my $item = $data->{'data'}->{$display} || return; |
679 |
|
680 |
return unless($item->{'display'}); |
681 |
if (! $item->{'search'}) { |
682 |
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
683 |
return; |
684 |
} |
685 |
|
686 |
my @warn; |
687 |
foreach my $type (qw/display search/) { |
688 |
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
689 |
} |
690 |
|
691 |
if (@warn) { |
692 |
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
693 |
return; |
694 |
} |
695 |
my @html; |
696 |
|
697 |
my $d_el = $#{ $item->{'display'} }; |
698 |
my $s_el = $#{ $item->{'search'} }; |
699 |
|
700 |
# easy, both fields have same number of elements or there is just |
701 |
# one search and multiple display |
702 |
if ( $d_el == $s_el || $s_el == 0 ) { |
703 |
|
704 |
foreach my $i ( 0 .. $d_el ) { |
705 |
|
706 |
my $s; |
707 |
if ($s_el > 0) { |
708 |
$s = $item->{'search'}->[$i] or warn "can't find value $i for type search in field $search"; |
709 |
} else { |
710 |
$s = $item->{'search'}->[0]; |
711 |
} |
712 |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
713 |
$s = __quotemeta( $s ); |
714 |
|
715 |
my $d = $item->{'display'}->[$i] or warn "can't find value $i for type display in field $display"; |
716 |
|
717 |
my $template_arg = ''; |
718 |
$template_arg = qq{,'$template'} if ($template); |
719 |
|
720 |
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>}; |
721 |
} |
722 |
|
723 |
return join($delimiter, @html); |
724 |
} else { |
725 |
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>}; |
726 |
my $v = $item->{'display'}; |
727 |
|
728 |
if ($#{$v} == 0) { |
729 |
$html .= $v->[0]; |
730 |
} else { |
731 |
$html .= join($delimiter, @{$v}); |
732 |
} |
733 |
return $html; |
734 |
} |
735 |
} |
736 |
} |
737 |
|
738 |
$args->{'search'} = tt_filter_search($args); |
739 |
|
740 |
=head3 load_rec |
741 |
|
742 |
Used mostly for onClick events like this: |
743 |
|
744 |
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
745 |
|
746 |
It will automatically do sanity checking and create correct JavaScript code. |
747 |
|
748 |
=cut |
749 |
|
750 |
$args->{'load_rec'} = sub { |
751 |
my @errors; |
752 |
|
753 |
my $record_uri = shift or push @errors, "record_uri missing"; |
754 |
my $template = shift or push @errors, "template missing"; |
755 |
|
756 |
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
757 |
push @errors, "invalid format of record_uri: $record_uri"; |
758 |
} |
759 |
|
760 |
if (@errors) { |
761 |
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
762 |
} else { |
763 |
return "load_rec('$record_uri','$template'); return false;"; |
764 |
} |
765 |
}; |
766 |
|
767 |
=head3 load_template |
768 |
|
769 |
Used to re-submit search request and load results in different template |
770 |
|
771 |
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
772 |
|
773 |
=cut |
774 |
|
775 |
$args->{'load_template'} = sub { |
776 |
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
777 |
return "load_template($template); return false;"; |
778 |
}; |
779 |
|
780 |
my $out; |
781 |
|
782 |
$self->{'tt'}->process( |
783 |
$args->{'template'}, |
784 |
$args, |
785 |
\$out |
786 |
) || $log->error( "apply can't process template: ", $self->{'tt'}->error() ); |
787 |
|
788 |
return $out; |
789 |
} |
790 |
|
791 |
|
792 |
=head2 __quotemeta |
793 |
|
794 |
Helper to quote JavaScript-friendly characters |
795 |
|
796 |
=cut |
797 |
|
798 |
sub __quotemeta { |
799 |
local $_ = shift; |
800 |
$_ = decode('iso-8859-2', $_); |
801 |
|
802 |
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
803 |
{ |
804 |
use bytes; |
805 |
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
806 |
} |
807 |
|
808 |
s/\\x09/\\t/g; |
809 |
s/\\x0A/\\n/g; |
810 |
s/\\x0D/\\r/g; |
811 |
s/"/\\"/g; |
812 |
s/\\x5C/\\\\/g; |
813 |
|
814 |
return $_; |
815 |
} |
816 |
|
817 |
|
818 |
|
819 |
=head1 AUTHOR |
820 |
|
821 |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
822 |
|
823 |
=head1 LICENSE |
824 |
|
825 |
This library is free software, you can redistribute it and/or modify |
826 |
it under the same terms as Perl itself. |
827 |
|
828 |
=cut |
829 |
|
830 |
1; |