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; |
10 |
use WebPAC::Output::TT 0.05; |
use Search::Estraier 0.04; |
|
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 |
|
|
16 |
=head1 NAME |
=head1 NAME |
17 |
|
|
40 |
user: 'admin' |
user: 'admin' |
41 |
passwd: 'admin' |
passwd: 'admin' |
42 |
hits_on_page: 100 |
hits_on_page: 100 |
43 |
|
hits_for_pager: 1000 |
44 |
|
|
45 |
webpac: |
webpac: |
46 |
db_path: '/data/webpac2/db' |
db_path: '/data/webpac2/db' |
48 |
template: 'html_ffzg_results_short.tt' |
template: 'html_ffzg_results_short.tt' |
49 |
# encoding comming from webpac |
# encoding comming from webpac |
50 |
webpac_encoding: 'iso-8859-2' |
webpac_encoding: 'iso-8859-2' |
|
# encoding expected by Catalyst |
|
|
out_encoding: 'UTF-8' |
|
51 |
|
|
52 |
=cut |
=cut |
53 |
|
|
65 |
|
|
66 |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
67 |
|
|
68 |
$log->debug("using config:" . Dumper($est_cfg) ); |
$log->dumper($est_cfg, 'est_cfg'); |
69 |
|
|
70 |
if (! $est_cfg->{database}) { |
if (! $est_cfg->{database}) { |
71 |
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
73 |
$est_cfg->{database} = $defaultnode; |
$est_cfg->{database} = $defaultnode; |
74 |
} |
} |
75 |
|
|
76 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
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 |
# save config parametars in object |
89 |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding defaultdepth/) { |
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} || |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
94 |
$c->config->{webpac}->{$f}; |
$c->config->{webpac}->{$f}; |
95 |
$log->debug("self->{$f} = " . $self->{$f}); |
$log->debug("self->{$f} = " . $self->{$f}); |
105 |
database => $est_cfg->{database}, |
database => $est_cfg->{database}, |
106 |
); |
); |
107 |
|
|
|
$self->{out} = new WebPAC::Output::TT( |
|
|
include_path => $template_path, |
|
|
filters => { foo => sub { shift } }, |
|
|
); |
|
|
|
|
108 |
# default template from config.yaml |
# default template from config.yaml |
109 |
$self->{template} ||= $c->config->{webpac}->{template}; |
$self->{template} ||= $c->config->{webpac}->{template}; |
110 |
|
|
|
$self->{iconv} = new Text::Iconv( |
|
|
$c->config->{webpac}->{webpac_encoding}, |
|
|
$c->config->{webpac}->{out_encoding} |
|
|
); |
|
|
|
|
111 |
$log->debug("converting encoding from webpac_encoding '" . |
$log->debug("converting encoding from webpac_encoding '" . |
112 |
$c->config->{webpac}->{webpac_encoding} . |
$c->config->{webpac}->{webpac_encoding} . |
|
"' to '" . |
|
|
$c->config->{webpac}->{out_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; |
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 |
|
|
157 |
|
$self->{log}->fatal("setup_site can't find site or defaultnode") unless ($site); |
158 |
|
|
159 |
|
my $url = $self->{masterurl} . '/node/' . $site; |
160 |
|
$self->{est_node}->set_url( $url ); |
161 |
|
$self->{log}->debug("setup_site $site using $url"); |
162 |
|
} |
163 |
|
|
164 |
=head2 search |
=head2 search |
165 |
|
|
181 |
sub search { |
sub search { |
182 |
my $self = shift; |
my $self = shift; |
183 |
|
|
184 |
|
my $search_start_t = time(); |
185 |
|
|
186 |
my $args = {@_}; |
my $args = {@_}; |
187 |
|
|
188 |
my $log = $self->{log}; |
my $log = $self->{log}; |
189 |
|
|
190 |
$log->debug("args: " . Dumper( $args )); |
$log->dumper($args, 'args'); |
191 |
|
|
192 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
193 |
|
|
|
$log->debug("search model query: '$query'"); |
|
|
if ($args->{add_attr}) { |
|
|
$log->debug(" + add_attr: " . |
|
|
join("','", @{ $args->{add_attr} }) |
|
|
); |
|
|
} |
|
|
|
|
194 |
my $template_filename = $args->{template} || $self->{template}; |
my $template_filename = $args->{template} || $self->{template}; |
195 |
|
|
196 |
$args->{max} ||= $self->{'hits_on_page'}; |
$args->{max} ||= $self->{'hits_for_pager'}; |
197 |
if (! $args->{max}) { |
if (! $args->{max}) { |
198 |
$args->{max} = 10; |
$args->{max} = 100; |
199 |
$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}"); |
200 |
} |
} |
201 |
|
|
202 |
my $times; # store some times for benchmarking |
my $times; # store some times for benchmarking |
209 |
$args->{depth} = $default; |
$args->{depth} = $default; |
210 |
$log->warn("using default search depth $default"); |
$log->warn("using default search depth $default"); |
211 |
} |
} |
212 |
|
$args->{depth} ||= 0; |
213 |
|
|
214 |
my @results = $self->{est}->search( %{ $args } ); |
$log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]'); |
215 |
|
|
216 |
$times->{est} += time() - $t; |
# |
217 |
|
# construct condition for Hyper Estraier |
218 |
|
# |
219 |
|
my $cond = Search::Estraier::Condition->new(); |
220 |
|
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
221 |
|
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
222 |
|
map { |
223 |
|
$cond->add_attr( $_ ); |
224 |
|
$log->debug(" + $_"); |
225 |
|
} @{ $args->{add_attr} }; |
226 |
|
}; |
227 |
|
|
228 |
|
$cond->set_phrase( $query ) if ($query); |
229 |
|
$cond->set_options( $args->{options} ) if ($args->{options}); |
230 |
|
$cond->set_order( $args->{order} ) if ($args->{order}); |
231 |
|
|
232 |
|
my $max = $args->{max} || 7; |
233 |
|
my $page = $args->{page} || 1; |
234 |
|
if ($page < 1) { |
235 |
|
$log->warn("page number $page < 1"); |
236 |
|
$page = 1; |
237 |
|
} |
238 |
|
|
239 |
my $hits = $#results + 1; |
$cond->set_max( $page * $max ); |
240 |
|
|
241 |
$log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) ); |
my $result = $self->{est_node}->search($cond, $args->{depth}); |
242 |
|
if (! $result) { |
243 |
|
$self->{log}->fatal("search didn't return result"); |
244 |
|
return; |
245 |
|
} |
246 |
|
my $hits = $result->doc_num; |
247 |
|
|
248 |
|
$times->{est} += time() - $t; |
249 |
|
|
250 |
# just return results? |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
251 |
return @results unless ($args->{'template'}); |
|
252 |
|
$self->{hints} = $result->{hints}; |
253 |
|
$log->dumper($self->{hints}, 'original hints' ); |
254 |
|
|
255 |
# |
# |
256 |
# construct HTML results |
# fetch results |
257 |
# |
# |
258 |
|
|
259 |
my @html_results; |
my @results; |
260 |
|
|
261 |
for my $i ( 0 .. $#results ) { |
for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) { |
262 |
|
|
263 |
my ($database, $prefix, $id); |
$t = time(); |
264 |
if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)#(\d+)$!) { |
|
265 |
($database, $prefix,$id) = ($1,$2,$3); |
#$log->debug("get_doc($i)"); |
266 |
} else { |
my $doc = $result->get_doc( $i ); |
267 |
$log->warn("can't decode prefix#id from " . $results[$i]->{'@uri'}); |
if (! $doc) { |
268 |
|
$log->warn("can't find result $i"); |
269 |
next; |
next; |
270 |
} |
} |
271 |
|
|
272 |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
my $hash; |
273 |
|
|
274 |
$t = time(); |
foreach my $attr (@{ $args->{get_attr} }) { |
275 |
|
my $val = $doc->attr( $attr ); |
276 |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
#$log->debug("attr $attr = ", $val || 'undef'); |
277 |
if (! $ds) { |
$hash->{$attr} = $val if (defined($val)); |
|
$log->error("can't load_ds( ${database}/${prefix}#${id} )"); |
|
|
next; |
|
278 |
} |
} |
279 |
|
|
280 |
$times->{db} += time() - $t; |
$times->{hash} += time() - $t; |
281 |
|
|
282 |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
next unless ($hash); |
283 |
|
|
284 |
$t = time(); |
if (! $args->{'template'}) { |
285 |
|
push @results, $hash; |
286 |
|
} else { |
287 |
|
my ($database, $prefix, $id); |
288 |
|
|
289 |
my $html = $self->{out}->apply( |
if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { |
290 |
template => $template_filename, |
($database, $prefix,$id) = ($1,$2,$3); |
291 |
data => $ds, |
} else { |
292 |
record_uri => "${database}/${prefix}/${id}", |
$log->warn("can't decode database/prefix/id from " . $hash->{'@uri'}); |
293 |
); |
next; |
294 |
|
} |
295 |
|
|
296 |
$times->{out} += time() - $t; |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
297 |
|
|
298 |
$t = time(); |
$t = time(); |
299 |
|
|
300 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
301 |
|
if (! $ds) { |
302 |
|
$log->error("can't load_ds( ${database}/${prefix}/${id} )"); |
303 |
|
next; |
304 |
|
} |
305 |
|
|
306 |
$times->{iconv} += time() - $t; |
$times->{db} += time() - $t; |
307 |
|
|
308 |
push @html_results, $html; |
$t = time(); |
309 |
|
|
310 |
} |
my $html = $self->apply( |
311 |
|
template => $template_filename, |
312 |
|
data => $ds, |
313 |
|
record_uri => "${database}/${prefix}/${id}", |
314 |
|
config => $self->{databases}->{$database}, |
315 |
|
); |
316 |
|
|
317 |
|
$times->{apply} += time() - $t; |
318 |
|
|
319 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
$t = time(); |
320 |
|
|
321 |
|
$html = decode($self->{webpac_encoding}, $html); |
322 |
|
|
323 |
|
$times->{decode} += time() - $t; |
324 |
|
|
325 |
|
push @results, $html; |
326 |
|
} |
327 |
|
|
328 |
|
} |
329 |
|
|
330 |
$log->debug( sprintf( |
$log->debug( sprintf( |
331 |
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
"duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", |
332 |
$times->{db}, $times->{out}, $times->{iconv}, |
$times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, |
333 |
) ); |
) ); |
334 |
|
|
335 |
return \@html_results; |
return \@results; |
336 |
} |
} |
337 |
|
|
338 |
|
=head2 hints |
339 |
|
|
340 |
|
my $hints = $m->hints; |
341 |
|
|
342 |
|
Return various useful hints about result |
343 |
|
|
344 |
|
=cut |
345 |
|
|
346 |
|
sub hints { |
347 |
|
my $self = shift; |
348 |
|
|
349 |
|
unless ($self->{hints}) { |
350 |
|
$self->{log}->fatal("no hints found!"); |
351 |
|
return; |
352 |
|
} |
353 |
|
|
354 |
|
my $hints; |
355 |
|
|
356 |
|
while (my ($key,$val) = each %{ $self->{hints} }) { |
357 |
|
|
358 |
|
if ($key =~ m/^(?:HITS*|TIME|DOCNUM|WORDNUM)$/) { |
359 |
|
$hints->{ lc($key) } = $val; |
360 |
|
} elsif ($key =~ m/^HINT#/) { |
361 |
|
my ($word,$count) = split(/\t/,$val,2); |
362 |
|
$hints->{words}->{$word} = $count; |
363 |
|
} elsif ($key =~ m/^LINK#/) { |
364 |
|
my ($url,undef,undef,undef,undef,undef,$results) = split(/\t/,$val,7); |
365 |
|
if ($url =~ m#/node/(.+)$#) { |
366 |
|
$hints->{node}->{$1} = $results; |
367 |
|
} |
368 |
|
} |
369 |
|
} |
370 |
|
|
371 |
|
$self->{log}->dumper($hints, 'model hints' ); |
372 |
|
|
373 |
|
return $hints; |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
=head2 record |
=head2 record |
378 |
|
|
379 |
my $html = $m->record( |
my $html = $m->record( |
391 |
|
|
392 |
my $args = {@_}; |
my $args = {@_}; |
393 |
my $log = $self->{log}; |
my $log = $self->{log}; |
394 |
$log->debug("args: " . Dumper( $args )); |
$log->dumper( $args, 'args' ); |
395 |
|
|
396 |
foreach my $f (qw/record_uri template/) { |
foreach my $f (qw/record_uri template/) { |
397 |
$log->fatal("need $f") unless ($args->{$f}); |
$log->fatal("need $f") unless ($args->{$f}); |
412 |
return; |
return; |
413 |
} |
} |
414 |
|
|
415 |
my $html = $self->{out}->apply( |
my $html = $self->apply( |
416 |
template => $args->{template}, |
template => $args->{template}, |
417 |
data => $ds, |
data => $ds, |
418 |
record_uri => $args->{record_uri}, |
record_uri => $args->{record_uri}, |
419 |
|
config => $self->{databases}->{$database}, |
420 |
); |
); |
421 |
|
|
422 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
423 |
|
|
424 |
return $html; |
return $html; |
425 |
} |
} |
426 |
|
|
427 |
|
|
428 |
|
=head2 list_nodes |
429 |
|
|
430 |
|
my @nodes = $m->list_nodes( 'site' ); |
431 |
|
|
432 |
|
Return all databases which have records for selected site. Returned array of |
433 |
|
hashes has elements C<name> and C<label>. |
434 |
|
|
435 |
|
=cut |
436 |
|
|
437 |
|
sub list_nodes { |
438 |
|
my $self = shift; |
439 |
|
|
440 |
|
my $site = shift; |
441 |
|
|
442 |
|
$self->{log}->debug("list_nodes use site $site"); |
443 |
|
|
444 |
|
$self->setup_site( $site ); |
445 |
|
|
446 |
|
my @nodes; |
447 |
|
|
448 |
|
if ($self->{est_node}->doc_num > 0) { |
449 |
|
push @nodes, { |
450 |
|
name => $self->{est_node}->name, |
451 |
|
label => $self->{est_node}->label, |
452 |
|
doc_num => $self->{est_node}->doc_num, |
453 |
|
} |
454 |
|
} |
455 |
|
|
456 |
|
# refresh set info |
457 |
|
$self->{est_node}->_set_info; |
458 |
|
|
459 |
|
my $links = $self->{est_node}->links || return @nodes; |
460 |
|
|
461 |
|
$self->{log}->dumper( $links, 'links' ); |
462 |
|
|
463 |
|
foreach my $link (@{ $links }) { |
464 |
|
my ($url, $label, $credit) = split(/\t/, $link, 3); |
465 |
|
if ($url =~ m#/node/(.+)$#) { |
466 |
|
my $node = $1; |
467 |
|
$self->setup_site( $node ); |
468 |
|
$self->{est_node}->_set_info; |
469 |
|
$label = decode('UTF-8', $label); |
470 |
|
push @nodes, { |
471 |
|
name => $node, |
472 |
|
label => $label, |
473 |
|
doc_num => $self->{est_node}->doc_num, |
474 |
|
} |
475 |
|
} else { |
476 |
|
$self->{log}->warn("can't find node name in link $link"); |
477 |
|
} |
478 |
|
} |
479 |
|
|
480 |
|
$self->setup_site( $site ); |
481 |
|
$self->{est_node}->_set_info; |
482 |
|
|
483 |
|
$self->{log}->dumper( \@nodes, 'nodes' ); |
484 |
|
|
485 |
|
return @nodes; |
486 |
|
} |
487 |
|
|
488 |
|
=cut |
489 |
|
|
490 |
|
|
491 |
=head2 save_html |
=head2 save_html |
492 |
|
|
493 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
494 |
|
|
495 |
It will use C<Text::Iconv> to convert content encoding back to |
It will use C<Encode> to convert content encoding back to |
496 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
497 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
498 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
503 |
sub save_html { |
sub save_html { |
504 |
my ($self, $path, $content) = @_; |
my ($self, $path, $content) = @_; |
505 |
|
|
506 |
|
# FIXME Should this be UTF-8 or someting? |
507 |
|
my $js_encoding = $self->{webpac_encoding}; |
508 |
|
$js_encoding = 'UTF-16'; |
509 |
|
|
510 |
sub _conv_js { |
sub _conv_js { |
511 |
my $t = shift || return; |
return '0x' . $_[1]; |
512 |
return $self->{iconv}->convert(chr(hex($t))); |
return encode($_[0], chr(hex($_[1]))); |
513 |
} |
} |
514 |
$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; |
515 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
516 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
517 |
|
$content =~ s/\n\r/\n/gs; |
518 |
|
|
519 |
my ($from, $to) = ( |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
520 |
$self->{out_encoding}, |
$self->{log}->debug("convert encoding to $disk_encoding"); |
521 |
$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?"; |
|
522 |
|
|
523 |
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 $!"; |
524 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
540 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
541 |
|
|
542 |
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); |
|
543 |
|
|
544 |
return $content; |
return decode($self->{webpac_encoding}, $content); |
545 |
} |
} |
546 |
|
|
547 |
|
|
548 |
|
=head2 apply |
549 |
|
|
550 |
|
Create output from in-memory data structure using Template Toolkit template. |
551 |
|
|
552 |
|
my $text = $tt->apply( |
553 |
|
template => 'text.tt', |
554 |
|
data => $ds, |
555 |
|
record_uri => 'database/prefix/mfn', |
556 |
|
); |
557 |
|
|
558 |
|
It also has follwing template toolikit filter routies defined: |
559 |
|
|
560 |
|
=cut |
561 |
|
|
562 |
|
# Escape <, >, & and ", and to produce valid XML |
563 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
564 |
|
my $escape_re = join '|' => keys %escape; |
565 |
|
|
566 |
|
sub apply { |
567 |
|
my $self = shift; |
568 |
|
|
569 |
|
my $args = {@_}; |
570 |
|
|
571 |
|
my $log = $self->{log} || die "no log?"; |
572 |
|
|
573 |
|
foreach my $a (qw/template data/) { |
574 |
|
$log->fatal("need $a") unless ($args->{$a}); |
575 |
|
} |
576 |
|
|
577 |
|
=head3 tt_filter_type |
578 |
|
|
579 |
|
filter to return values of specified from $ds, usage from TT template is in form |
580 |
|
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
581 |
|
|
582 |
|
[% d('Title') %] |
583 |
|
[% d('Author',', ' %] |
584 |
|
|
585 |
|
=cut |
586 |
|
|
587 |
|
sub tt_filter_type { |
588 |
|
my ($data,$type) = @_; |
589 |
|
|
590 |
|
die "no data?" unless ($data); |
591 |
|
$type ||= 'display'; |
592 |
|
|
593 |
|
my $default_delimiter = { |
594 |
|
'display' => '¶<br/>', |
595 |
|
'index' => '\n', |
596 |
|
}; |
597 |
|
|
598 |
|
return sub { |
599 |
|
|
600 |
|
my ($name,$join) = @_; |
601 |
|
|
602 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
603 |
|
# Hm? Should we die here? |
604 |
|
return unless ($name); |
605 |
|
|
606 |
|
my $item = $data->{'data'}->{$name} || return; |
607 |
|
|
608 |
|
my $v = $item->{$type} || return; |
609 |
|
|
610 |
|
if (ref($v) eq 'ARRAY') { |
611 |
|
if ($#{$v} == 0) { |
612 |
|
$v = $v->[0]; |
613 |
|
$v =~ s/($escape_re)/$escape{$1}/g; |
614 |
|
} else { |
615 |
|
$join = $default_delimiter->{$type} unless defined($join); |
616 |
|
$v = join($join, map { |
617 |
|
s/($escape_re)/$escape{$1}/g; |
618 |
|
} @{$v}); |
619 |
|
} |
620 |
|
} else { |
621 |
|
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
622 |
|
} |
623 |
|
|
624 |
|
return $v; |
625 |
|
} |
626 |
|
} |
627 |
|
|
628 |
|
$args->{'d'} = tt_filter_type($args, 'display'); |
629 |
|
$args->{'display'} = tt_filter_type($args, 'display'); |
630 |
|
|
631 |
|
=head3 tt_filter_search |
632 |
|
|
633 |
|
filter to return links to search, usage in TT: |
634 |
|
|
635 |
|
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
636 |
|
|
637 |
|
=cut |
638 |
|
|
639 |
|
sub tt_filter_search { |
640 |
|
|
641 |
|
my ($data) = @_; |
642 |
|
|
643 |
|
die "no data?" unless ($data); |
644 |
|
|
645 |
|
return sub { |
646 |
|
|
647 |
|
my ($display,$search,$delimiter,$template) = @_; |
648 |
|
|
649 |
|
# default delimiter |
650 |
|
$delimiter ||= '¶<br/>', |
651 |
|
|
652 |
|
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
653 |
|
# Hm? Should we die here? |
654 |
|
return unless ($display); |
655 |
|
|
656 |
|
my $item = $data->{'data'}->{$display} || return; |
657 |
|
|
658 |
|
return unless($item->{'display'}); |
659 |
|
if (! $item->{'search'}) { |
660 |
|
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
661 |
|
return; |
662 |
|
} |
663 |
|
|
664 |
|
my @warn; |
665 |
|
foreach my $type (qw/display search/) { |
666 |
|
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
667 |
|
} |
668 |
|
|
669 |
|
if (@warn) { |
670 |
|
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
671 |
|
return; |
672 |
|
} |
673 |
|
my @html; |
674 |
|
|
675 |
|
my $d_el = $#{ $item->{'display'} }; |
676 |
|
my $s_el = $#{ $item->{'search'} }; |
677 |
|
|
678 |
|
# easy, both fields have same number of elements or there is just |
679 |
|
# one search and multiple display |
680 |
|
if ( $d_el == $s_el || $s_el == 0 ) { |
681 |
|
|
682 |
|
foreach my $i ( 0 .. $d_el ) { |
683 |
|
|
684 |
|
my $s; |
685 |
|
if ($s_el > 0) { |
686 |
|
$s = $item->{'search'}->[$i] or warn "can't find value $i for type search in field $search"; |
687 |
|
} else { |
688 |
|
$s = $item->{'search'}->[0]; |
689 |
|
} |
690 |
|
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
691 |
|
$s = __quotemeta( $s ); |
692 |
|
|
693 |
|
my $d = $item->{'display'}->[$i] or warn "can't find value $i for type display in field $display"; |
694 |
|
|
695 |
|
my $template_arg = ''; |
696 |
|
$template_arg = qq{,'$template'} if ($template); |
697 |
|
|
698 |
|
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>}; |
699 |
|
} |
700 |
|
|
701 |
|
return join($delimiter, @html); |
702 |
|
} else { |
703 |
|
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>}; |
704 |
|
my $v = $item->{'display'}; |
705 |
|
|
706 |
|
if ($#{$v} == 0) { |
707 |
|
$html .= $v->[0]; |
708 |
|
} else { |
709 |
|
$html .= join($delimiter, @{$v}); |
710 |
|
} |
711 |
|
return $html; |
712 |
|
} |
713 |
|
} |
714 |
|
} |
715 |
|
|
716 |
|
$args->{'search'} = tt_filter_search($args); |
717 |
|
|
718 |
|
=head3 load_rec |
719 |
|
|
720 |
|
Used mostly for onClick events like this: |
721 |
|
|
722 |
|
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
723 |
|
|
724 |
|
It will automatically do sanity checking and create correct JavaScript code. |
725 |
|
|
726 |
|
=cut |
727 |
|
|
728 |
|
$args->{'load_rec'} = sub { |
729 |
|
my @errors; |
730 |
|
|
731 |
|
my $record_uri = shift or push @errors, "record_uri missing"; |
732 |
|
my $template = shift or push @errors, "template missing"; |
733 |
|
|
734 |
|
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
735 |
|
push @errors, "invalid format of record_uri: $record_uri"; |
736 |
|
} |
737 |
|
|
738 |
|
if (@errors) { |
739 |
|
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
740 |
|
} else { |
741 |
|
return "load_rec('$record_uri','$template'); return false;"; |
742 |
|
} |
743 |
|
}; |
744 |
|
|
745 |
|
=head3 load_template |
746 |
|
|
747 |
|
Used to re-submit search request and load results in different template |
748 |
|
|
749 |
|
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
750 |
|
|
751 |
|
=cut |
752 |
|
|
753 |
|
$args->{'load_template'} = sub { |
754 |
|
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
755 |
|
return "load_template($template); return false;"; |
756 |
|
}; |
757 |
|
|
758 |
|
my $out; |
759 |
|
|
760 |
|
$self->{'tt'}->process( |
761 |
|
$args->{'template'}, |
762 |
|
$args, |
763 |
|
\$out |
764 |
|
) || $log->error( "apply can't process template: ", $self->{'tt'}->error() ); |
765 |
|
|
766 |
|
return $out; |
767 |
|
} |
768 |
|
|
769 |
|
|
770 |
|
=head2 __quotemeta |
771 |
|
|
772 |
|
Helper to quote JavaScript-friendly characters |
773 |
|
|
774 |
|
=cut |
775 |
|
|
776 |
|
sub __quotemeta { |
777 |
|
local $_ = shift; |
778 |
|
$_ = decode('iso-8859-2', $_); |
779 |
|
|
780 |
|
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
781 |
|
{ |
782 |
|
use bytes; |
783 |
|
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
784 |
|
} |
785 |
|
|
786 |
|
s/\\x09/\\t/g; |
787 |
|
s/\\x0A/\\n/g; |
788 |
|
s/\\x0D/\\r/g; |
789 |
|
s/"/\\"/g; |
790 |
|
s/\\x5C/\\\\/g; |
791 |
|
|
792 |
|
return $_; |
793 |
|
} |
794 |
|
|
795 |
|
|
796 |
|
|
797 |
=head1 AUTHOR |
=head1 AUTHOR |
798 |
|
|
799 |
Dobrica Pavlinusic |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
800 |
|
|
801 |
=head1 LICENSE |
=head1 LICENSE |
802 |
|
|