6 |
use base qw/ |
use base qw/ |
7 |
Catalyst::Model |
Catalyst::Model |
8 |
/; |
/; |
9 |
use Data::Dumper; |
use WebPAC::Store 0.08; |
10 |
use WebPAC::Store 0.03; |
use Search::Estraier 0.04; |
|
use WebPAC::Output::TT 0.02; |
|
|
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 |
|
|
41 |
user: 'admin' |
user: 'admin' |
42 |
passwd: 'admin' |
passwd: 'admin' |
43 |
hits_on_page: 100 |
hits_on_page: 100 |
44 |
|
hits_for_pager: 1000 |
45 |
|
|
46 |
webpac: |
webpac: |
47 |
db_path: '/data/webpac2/db' |
db_path: '/data/webpac2/db' |
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 |
|
|
64 |
my $est_cfg = $c->config->{hyperestraier}; |
my $est_cfg = $c->config->{hyperestraier}; |
65 |
$est_cfg->{'log'} = $log; |
$est_cfg->{'log'} = $log; |
66 |
|
|
67 |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding}; |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
68 |
|
|
69 |
$log->debug("using config:" . Dumper($est_cfg) ); |
$log->debug("using config:" . Dumper($est_cfg) ); |
70 |
|
|
74 |
$est_cfg->{database} = $defaultnode; |
$est_cfg->{database} = $defaultnode; |
75 |
} |
} |
76 |
|
|
77 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
my $url = $est_cfg->{masterurl} . '/node/' . $est_cfg->{database}; |
78 |
|
|
79 |
|
$log->info("opening Hyper Estraier index $url as $est_cfg->{'user'}"); |
80 |
|
|
81 |
|
$self->{est_node} = Search::Estraier::Node->new( |
82 |
|
url => $url, |
83 |
|
user => $est_cfg->{user}, |
84 |
|
passwd => $est_cfg->{passwd}, |
85 |
|
); |
86 |
|
|
87 |
|
$log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node}); |
88 |
|
|
89 |
# save config parametars in object |
# save config parametars in object |
90 |
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/) { |
91 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
92 |
$c->config->{webpac}->{$f}; |
$c->config->{webpac}->{$f}; |
93 |
$log->debug("self->{$f} = " . $self->{$f}); |
$log->debug("self->{$f} = " . $self->{$f}); |
103 |
database => $est_cfg->{database}, |
database => $est_cfg->{database}, |
104 |
); |
); |
105 |
|
|
|
$self->{out} = new WebPAC::Output::TT( |
|
|
include_path => $template_path, |
|
|
filters => { foo => sub { shift } }, |
|
|
); |
|
|
|
|
106 |
# default template from config.yaml |
# default template from config.yaml |
107 |
$self->{template} ||= $c->config->{webpac}->{template}; |
$self->{template} ||= $c->config->{webpac}->{template}; |
108 |
|
|
|
$self->{iconv} = new Text::Iconv( |
|
|
$c->config->{webpac}->{webpac_encoding}, |
|
|
$c->config->{webpac}->{out_encoding} |
|
|
); |
|
|
|
|
109 |
$log->debug("converting encoding from webpac_encoding '" . |
$log->debug("converting encoding from webpac_encoding '" . |
110 |
$c->config->{webpac}->{webpac_encoding} . |
$c->config->{webpac}->{webpac_encoding} . |
|
"' to '" . |
|
|
$c->config->{webpac}->{out_encoding} . |
|
111 |
"'" |
"'" |
112 |
); |
); |
113 |
|
|
114 |
|
$self->{databases} = $c->config->{databases} || $log->fatal("can't find databases in config"); |
115 |
|
|
116 |
|
# create Template toolkit instance |
117 |
|
$self->{'tt'} = Template->new( |
118 |
|
INCLUDE_PATH => $template_path, |
119 |
|
FILTERS => { |
120 |
|
dump_html => sub { |
121 |
|
return unless (@_); |
122 |
|
my $out; |
123 |
|
my $i = 1; |
124 |
|
foreach my $v (@_) { |
125 |
|
$out .= qq{<div id="dump_$i">} . |
126 |
|
Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) . |
127 |
|
qq{</div>}; |
128 |
|
$i++; |
129 |
|
} |
130 |
|
$out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out); |
131 |
|
return $out; |
132 |
|
} |
133 |
|
}, |
134 |
|
EVAL_PERL => 1, |
135 |
|
); |
136 |
|
|
137 |
return $self; |
return $self; |
138 |
|
|
159 |
sub search { |
sub search { |
160 |
my $self = shift; |
my $self = shift; |
161 |
|
|
162 |
|
my $search_start_t = time(); |
163 |
|
|
164 |
my $args = {@_}; |
my $args = {@_}; |
165 |
|
|
166 |
my $log = $self->{log}; |
my $log = $self->{log}; |
167 |
|
|
168 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("search args: " . Dumper( $args )); |
169 |
|
|
170 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
171 |
|
|
|
$log->debug("search model query: '$query'"); |
|
|
if ($args->{add_attr}) { |
|
|
$log->debug(" + add_attr: " . |
|
|
join("','", @{ $args->{add_attr} }) |
|
|
); |
|
|
} |
|
|
|
|
172 |
my $template_filename = $args->{template} || $self->{template}; |
my $template_filename = $args->{template} || $self->{template}; |
173 |
|
|
174 |
$args->{max} ||= $self->{'hits_on_page'}; |
$args->{max} ||= $self->{'hits_for_pager'}; |
175 |
if (! $args->{max}) { |
if (! $args->{max}) { |
176 |
$args->{max} = 10; |
$args->{max} = 100; |
177 |
$log->warn("max not set when calling model. Using default of 10"); |
$log->warn("max not set when calling model. Using default of $args->{max}"); |
178 |
} |
} |
179 |
|
|
180 |
my $times; # store some times for benchmarking |
my $times; # store some times for benchmarking |
187 |
$args->{depth} = $default; |
$args->{depth} = $default; |
188 |
$log->warn("using default search depth $default"); |
$log->warn("using default search depth $default"); |
189 |
} |
} |
190 |
|
$args->{depth} ||= 0; |
191 |
|
|
192 |
my @results = $self->{est}->search( %{ $args } ); |
$log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]'); |
193 |
|
|
194 |
|
# |
195 |
|
# construct condition for Hyper Estraier |
196 |
|
# |
197 |
|
my $cond = Search::Estraier::Condition->new(); |
198 |
|
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
199 |
|
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
200 |
|
map { |
201 |
|
$cond->add_attr( $_ ); |
202 |
|
$log->debug(" + $_"); |
203 |
|
} @{ $args->{add_attr} }; |
204 |
|
}; |
205 |
|
|
206 |
|
$cond->set_phrase( $query ) if ($query); |
207 |
|
$cond->set_options( $args->{options} ) if ($args->{options}); |
208 |
|
$cond->set_order( $args->{order} ) if ($args->{order}); |
209 |
|
|
210 |
|
my $max = $args->{max} || 7; |
211 |
|
my $page = $args->{page} || 1; |
212 |
|
if ($page < 1) { |
213 |
|
$log->warn("page number $page < 1"); |
214 |
|
$page = 1; |
215 |
|
} |
216 |
|
|
217 |
$times->{est} += time() - $t; |
$times->{est} += time() - $t; |
218 |
|
|
219 |
my $hits = $#results + 1; |
$cond->set_max( $page * $max ); |
220 |
|
|
221 |
$log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) ); |
my $result = $self->{est_node}->search($cond, $args->{depth}); |
222 |
|
my $hits = $result->doc_num; |
223 |
|
|
224 |
# just return results? |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
|
return @results unless ($args->{'template'}); |
|
225 |
|
|
226 |
# |
# |
227 |
# construct HTML results |
# fetch results |
228 |
# |
# |
229 |
|
|
230 |
my @html_results; |
my @results; |
231 |
|
|
232 |
for my $i ( 0 .. $#results ) { |
for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) { |
233 |
|
|
234 |
my ($id, $prefix); |
$t = time(); |
235 |
if ( $results[$i]->{'@uri'} =~ m!/([^/]+)#(\d+)$!) { |
|
236 |
($prefix,$id) = ($1,$2); |
#$log->debug("get_doc($i)"); |
237 |
} else { |
my $doc = $result->get_doc( $i ); |
238 |
$log->warn("can't decode prefix#id from " . $results[$i]->{'@uri'}); |
if (! $doc) { |
239 |
|
$log->warn("can't find result $i"); |
240 |
next; |
next; |
241 |
} |
} |
242 |
|
|
243 |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
my $hash; |
244 |
|
|
245 |
$t = time(); |
foreach my $attr (@{ $args->{get_attr} }) { |
246 |
|
my $val = $doc->attr( $attr ); |
247 |
|
#$log->debug("attr $attr = ", $val || 'undef'); |
248 |
|
$hash->{$attr} = $val if (defined($val)); |
249 |
|
} |
250 |
|
|
251 |
my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix ) || $log->error("can't load_ds( id => $id, prefix => '$prefix' )") && next; |
$times->{hash} += time() - $t; |
252 |
|
|
253 |
$times->{db} += time() - $t; |
next unless ($hash); |
254 |
|
|
255 |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
if (! $args->{'template'}) { |
256 |
|
push @results, $hash; |
257 |
|
} else { |
258 |
|
my ($database, $prefix, $id); |
259 |
|
|
260 |
$t = time(); |
if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { |
261 |
|
($database, $prefix,$id) = ($1,$2,$3); |
262 |
|
} else { |
263 |
|
$log->warn("can't decode database/prefix/id from " . $hash->{'@uri'}); |
264 |
|
next; |
265 |
|
} |
266 |
|
|
267 |
my $html = $self->{out}->apply( |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
|
template => $template_filename, |
|
|
data => $ds, |
|
|
); |
|
268 |
|
|
269 |
$times->{out} += time() - $t; |
$t = time(); |
270 |
|
|
271 |
$t = time(); |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
272 |
|
if (! $ds) { |
273 |
|
$log->error("can't load_ds( ${database}/${prefix}/${id} )"); |
274 |
|
next; |
275 |
|
} |
276 |
|
|
277 |
|
$times->{db} += time() - $t; |
278 |
|
|
279 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
280 |
|
|
281 |
$times->{iconv} += time() - $t; |
$t = time(); |
282 |
|
|
283 |
push @html_results, $html; |
my $html = $self->apply( |
284 |
|
template => $template_filename, |
285 |
|
data => $ds, |
286 |
|
record_uri => "${database}/${prefix}/${id}", |
287 |
|
config => $self->{databases}->{$database}, |
288 |
|
); |
289 |
|
|
290 |
|
$times->{apply} += time() - $t; |
291 |
|
|
292 |
|
$t = time(); |
293 |
|
|
294 |
|
$html = decode($self->{webpac_encoding}, $html); |
295 |
|
|
296 |
|
$times->{decode} += time() - $t; |
297 |
|
|
298 |
|
push @results, $html; |
299 |
|
} |
300 |
|
|
301 |
} |
} |
302 |
|
|
303 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
#$log->debug( '@results = ' . Dumper( \@results ) ); |
304 |
|
|
305 |
$log->debug( sprintf( |
$log->debug( sprintf( |
306 |
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
"duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", |
307 |
$times->{db}, $times->{out}, $times->{iconv}, |
$times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, |
308 |
) ); |
) ); |
309 |
|
|
310 |
return \@html_results; |
return \@results; |
311 |
} |
} |
312 |
|
|
313 |
=head2 record |
=head2 record |
327 |
|
|
328 |
my $args = {@_}; |
my $args = {@_}; |
329 |
my $log = $self->{log}; |
my $log = $self->{log}; |
330 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("record args: " . Dumper( $args )); |
331 |
|
|
332 |
foreach my $f (qw/mfn template prefix/) { |
foreach my $f (qw/record_uri template/) { |
333 |
$log->die("need $f") unless ($args->{$f}); |
$log->fatal("need $f") unless ($args->{$f}); |
334 |
} |
} |
335 |
|
|
336 |
my $mfn = $args->{mfn}; |
my ($database, $prefix, $id); |
337 |
|
|
338 |
|
if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) { |
339 |
|
($database, $prefix, $id) = ($1,$2,$3); |
340 |
|
} else { |
341 |
|
$log->error("can't parse $args->{record_uri} into prefix, database and uri"); |
342 |
|
return; |
343 |
|
} |
344 |
|
|
345 |
my $ds = $self->{db}->load_ds( id => $mfn, prefix => 'FIXME' ) || $log->error("can't load_ds( $mfn )") && next; |
my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database ); |
346 |
|
if (! $ds) { |
347 |
|
$log->error("can't load_ds( $database/$prefix/$id )"); |
348 |
|
return; |
349 |
|
} |
350 |
|
|
351 |
my $html = $self->{out}->apply( |
my $html = $self->apply( |
352 |
template => $args->{template}, |
template => $args->{template}, |
353 |
data => $ds, |
data => $ds, |
354 |
|
record_uri => $args->{record_uri}, |
355 |
|
config => $self->{databases}->{$database}, |
356 |
); |
); |
357 |
|
|
358 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
359 |
|
|
360 |
return $html; |
return $html; |
361 |
} |
} |
362 |
|
|
363 |
|
|
364 |
=head2 save_html |
=head2 save_html |
365 |
|
|
366 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
367 |
|
|
368 |
It will use C<Text::Iconv> to convert content encoding back to |
It will use C<Encode> to convert content encoding back to |
369 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
370 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
371 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
376 |
sub save_html { |
sub save_html { |
377 |
my ($self, $path, $content) = @_; |
my ($self, $path, $content) = @_; |
378 |
|
|
379 |
|
# FIXME Should this be UTF-8 or someting? |
380 |
|
my $js_encoding = $self->{webpac_encoding}; |
381 |
|
$js_encoding = 'UTF-16'; |
382 |
|
|
383 |
sub _conv_js { |
sub _conv_js { |
384 |
my $t = shift || return; |
return '0x' . $_[1]; |
385 |
return $self->{iconv}->convert(chr(hex($t))); |
return encode($_[0], chr(hex($_[1]))); |
386 |
} |
} |
387 |
$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; |
388 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
389 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
390 |
|
$content =~ s/\n\r/\n/gs; |
391 |
|
|
392 |
my ($from, $to) = ( |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
393 |
$self->{out_encoding}, |
$self->{log}->debug("convert encoding to $disk_encoding"); |
394 |
$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?"; |
|
395 |
|
|
396 |
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 $!"; |
397 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
413 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
414 |
|
|
415 |
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); |
|
416 |
|
|
417 |
return $content; |
return decode($self->{webpac_encoding}, $content); |
418 |
} |
} |
419 |
|
|
420 |
|
|
421 |
|
=head2 apply |
422 |
|
|
423 |
|
Create output from in-memory data structure using Template Toolkit template. |
424 |
|
|
425 |
|
my $text = $tt->apply( |
426 |
|
template => 'text.tt', |
427 |
|
data => $ds, |
428 |
|
record_uri => 'database/prefix/mfn', |
429 |
|
); |
430 |
|
|
431 |
|
It also has follwing template toolikit filter routies defined: |
432 |
|
|
433 |
|
=cut |
434 |
|
|
435 |
|
sub apply { |
436 |
|
my $self = shift; |
437 |
|
|
438 |
|
my $args = {@_}; |
439 |
|
|
440 |
|
my $log = $self->{log} || die "no log?"; |
441 |
|
|
442 |
|
foreach my $a (qw/template data/) { |
443 |
|
$log->fatal("need $a") unless ($args->{$a}); |
444 |
|
} |
445 |
|
|
446 |
|
=head3 tt_filter_type |
447 |
|
|
448 |
|
filter to return values of specified from $ds, usage from TT template is in form |
449 |
|
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
450 |
|
|
451 |
|
[% d('Title') %] |
452 |
|
[% d('Author',', ' %] |
453 |
|
|
454 |
|
=cut |
455 |
|
|
456 |
|
sub tt_filter_type { |
457 |
|
my ($data,$type) = @_; |
458 |
|
|
459 |
|
die "no data?" unless ($data); |
460 |
|
$type ||= 'display'; |
461 |
|
|
462 |
|
my $default_delimiter = { |
463 |
|
'display' => '¶<br/>', |
464 |
|
'index' => '\n', |
465 |
|
}; |
466 |
|
|
467 |
|
return sub { |
468 |
|
|
469 |
|
my ($name,$join) = @_; |
470 |
|
|
471 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
472 |
|
# Hm? Should we die here? |
473 |
|
return unless ($name); |
474 |
|
|
475 |
|
my $item = $data->{'data'}->{$name} || return; |
476 |
|
|
477 |
|
my $v = $item->{$type} || return; |
478 |
|
|
479 |
|
if (ref($v) eq 'ARRAY') { |
480 |
|
if ($#{$v} == 0) { |
481 |
|
$v = $v->[0]; |
482 |
|
} else { |
483 |
|
$join = $default_delimiter->{$type} unless defined($join); |
484 |
|
$v = join($join, @{$v}); |
485 |
|
} |
486 |
|
} else { |
487 |
|
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
488 |
|
} |
489 |
|
|
490 |
|
return $v; |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
|
$args->{'d'} = tt_filter_type($args, 'display'); |
495 |
|
$args->{'display'} = tt_filter_type($args, 'display'); |
496 |
|
|
497 |
|
=head3 tt_filter_search |
498 |
|
|
499 |
|
filter to return links to search, usage in TT: |
500 |
|
|
501 |
|
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
502 |
|
|
503 |
|
=cut |
504 |
|
|
505 |
|
sub tt_filter_search { |
506 |
|
|
507 |
|
my ($data) = @_; |
508 |
|
|
509 |
|
die "no data?" unless ($data); |
510 |
|
|
511 |
|
return sub { |
512 |
|
|
513 |
|
my ($display,$search,$delimiter,$template) = @_; |
514 |
|
|
515 |
|
# default delimiter |
516 |
|
$delimiter ||= '¶<br/>', |
517 |
|
|
518 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
519 |
|
# Hm? Should we die here? |
520 |
|
return unless ($display); |
521 |
|
|
522 |
|
my $item = $data->{'data'}->{$display} || return; |
523 |
|
|
524 |
|
return unless($item->{'display'}); |
525 |
|
if (! $item->{'search'}) { |
526 |
|
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
527 |
|
return; |
528 |
|
} |
529 |
|
|
530 |
|
my @warn; |
531 |
|
foreach my $type (qw/display search/) { |
532 |
|
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
533 |
|
} |
534 |
|
|
535 |
|
if (@warn) { |
536 |
|
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
537 |
|
return; |
538 |
|
} |
539 |
|
my @html; |
540 |
|
|
541 |
|
my $d_el = $#{ $item->{'display'} }; |
542 |
|
my $s_el = $#{ $item->{'search'} }; |
543 |
|
|
544 |
|
# easy, both fields have same number of elements or there is just |
545 |
|
# one search and multiple display |
546 |
|
if ( $d_el == $s_el || $s_el == 0 ) { |
547 |
|
|
548 |
|
foreach my $i ( 0 .. $d_el ) { |
549 |
|
|
550 |
|
my $s; |
551 |
|
if ($s_el > 0) { |
552 |
|
$s = $item->{'search'}->[$i] or warn "can't find value $i for type search in field $search"; |
553 |
|
} else { |
554 |
|
$s = $item->{'search'}->[0]; |
555 |
|
} |
556 |
|
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
557 |
|
$s = __quotemeta( $s ); |
558 |
|
|
559 |
|
my $d = $item->{'display'}->[$i] or warn "can't find value $i for type display in field $display"; |
560 |
|
|
561 |
|
my $template_arg = ''; |
562 |
|
$template_arg = qq{,'$template'} if ($template); |
563 |
|
|
564 |
|
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>}; |
565 |
|
} |
566 |
|
|
567 |
|
return join($delimiter, @html); |
568 |
|
} else { |
569 |
|
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>}; |
570 |
|
my $v = $item->{'display'}; |
571 |
|
|
572 |
|
if ($#{$v} == 0) { |
573 |
|
$html .= $v->[0]; |
574 |
|
} else { |
575 |
|
$html .= join($delimiter, @{$v}); |
576 |
|
} |
577 |
|
return $html; |
578 |
|
} |
579 |
|
} |
580 |
|
} |
581 |
|
|
582 |
|
$args->{'search'} = tt_filter_search($args); |
583 |
|
|
584 |
|
=head3 load_rec |
585 |
|
|
586 |
|
Used mostly for onClick events like this: |
587 |
|
|
588 |
|
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
589 |
|
|
590 |
|
It will automatically do sanity checking and create correct JavaScript code. |
591 |
|
|
592 |
|
=cut |
593 |
|
|
594 |
|
$args->{'load_rec'} = sub { |
595 |
|
my @errors; |
596 |
|
|
597 |
|
my $record_uri = shift or push @errors, "record_uri missing"; |
598 |
|
my $template = shift or push @errors, "template missing"; |
599 |
|
|
600 |
|
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
601 |
|
push @errors, "invalid format of record_uri: $record_uri"; |
602 |
|
} |
603 |
|
|
604 |
|
if (@errors) { |
605 |
|
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
606 |
|
} else { |
607 |
|
return "load_rec('$record_uri','$template'); return false;"; |
608 |
|
} |
609 |
|
}; |
610 |
|
|
611 |
|
=head3 load_template |
612 |
|
|
613 |
|
Used to re-submit search request and load results in different template |
614 |
|
|
615 |
|
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
616 |
|
|
617 |
|
=cut |
618 |
|
|
619 |
|
$args->{'load_template'} = sub { |
620 |
|
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
621 |
|
return "load_template($template); return false;"; |
622 |
|
}; |
623 |
|
|
624 |
|
my $out; |
625 |
|
|
626 |
|
$self->{'tt'}->process( |
627 |
|
$args->{'template'}, |
628 |
|
$args, |
629 |
|
\$out |
630 |
|
) || $log->error( "apply can't process template: ", $self->{'tt'}->error() ); |
631 |
|
|
632 |
|
return $out; |
633 |
|
} |
634 |
|
|
635 |
|
|
636 |
|
=head2 __quotemeta |
637 |
|
|
638 |
|
Helper to quote JavaScript-friendly characters |
639 |
|
|
640 |
|
=cut |
641 |
|
|
642 |
|
sub __quotemeta { |
643 |
|
local $_ = shift; |
644 |
|
$_ = decode('iso-8859-2', $_); |
645 |
|
|
646 |
|
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
647 |
|
{ |
648 |
|
use bytes; |
649 |
|
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
650 |
|
} |
651 |
|
|
652 |
|
s/\\x09/\\t/g; |
653 |
|
s/\\x0A/\\n/g; |
654 |
|
s/\\x0D/\\r/g; |
655 |
|
s/"/\\"/g; |
656 |
|
s/\\x5C/\\\\/g; |
657 |
|
|
658 |
|
return $_; |
659 |
|
} |
660 |
|
|
661 |
|
|
662 |
|
|
663 |
=head1 AUTHOR |
=head1 AUTHOR |
664 |
|
|
665 |
Dobrica Pavlinusic |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
666 |
|
|
667 |
=head1 LICENSE |
=head1 LICENSE |
668 |
|
|