/[webpac2]/Webpacus/lib/Webpacus/Model/WebPAC.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /Webpacus/lib/Webpacus/Model/WebPAC.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 413 - (hide annotations)
Mon Feb 20 21:47:54 2006 UTC (18 years, 2 months ago) by dpavlin
File size: 18358 byte(s)
escape HTML entities

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

  ViewVC Help
Powered by ViewVC 1.1.26