/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.92  
changed lines
  Added in v.425

  ViewVC Help
Powered by ViewVC 1.1.26