/[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 351 by dpavlin, Sat Jan 7 18:18:07 2006 UTC revision 379 by dpavlin, Sun Jan 22 00:44:32 2006 UTC
# Line 6  use lib '/data/webpac2/lib'; Line 6  use lib '/data/webpac2/lib';
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;
 use WebPAC::Output::TT 0.07;  
10  use WebPAC::Search::Estraier 0.05;  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/;  use Encode qw/encode decode from_to/;
14  use Data::HTMLDumper;  use Template;
15    use Data::Dumper;
16    
17  =head1 NAME  =head1 NAME
18    
# Line 94  sub new { Line 93  sub new {
93                  database => $est_cfg->{database},                  database => $est_cfg->{database},
94          );          );
95    
         $self->{out} = new WebPAC::Output::TT(  
                 include_path => $template_path,  
                 filters => {  
                         dump_html => sub {  
                                 my $t = shift || return;  
                                 #return Data::HTMLDumper->Dumper( $t );  
                                 return Data::HTMLDumper->Dump([$t],[qw/dump/]);  
                         }  
                 },  
         );  
   
96          # default template from config.yaml          # default template from config.yaml
97          $self->{template} ||= $c->config->{webpac}->{template};          $self->{template} ||= $c->config->{webpac}->{template};
98    
# Line 115  sub new { Line 103  sub new {
103    
104          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");
105    
106            # create Template toolkit instance
107            $self->{'tt'} = Template->new(
108                    INCLUDE_PATH => $template_path,
109                    FILTERS => {
110                            dump_html => sub {
111                                    return unless (@_);
112                                    my $out;
113                                    my $i = 1;
114                                    foreach my $v (@_) {
115                                            $out .= qq{<div id="dump_$i">} .
116                                                    Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) .
117                                                    qq{</div>};
118                                            $i++;
119                                    }
120                                    $out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out);
121                                    return $out;
122                            }
123                    },
124                    EVAL_PERL => 1,
125            );
126    
127          return $self;          return $self;
128    
129  }  }
# Line 140  selected template. Line 149  selected template.
149  sub search {  sub search {
150          my $self = shift;          my $self = shift;
151    
152            my $search_start_t = time();
153    
154          my $args = {@_};          my $args = {@_};
155    
156          my $log = $self->{log};          my $log = $self->{log};
# Line 180  sub search { Line 191  sub search {
191    
192          my $hits = $#results + 1;          my $hits = $#results + 1;
193    
194          $log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) );          $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );
195    
196          # just return results?          # just return results?
197          return @results unless ($args->{'template'});          return @results unless ($args->{'template'});
# Line 217  sub search { Line 228  sub search {
228    
229                  $t = time();                  $t = time();
230    
231                  my $html = $self->{out}->apply(                  my $html = $self->apply(
232                          template => $template_filename,                          template => $template_filename,
233                          data => $ds,                          data => $ds,
234                          record_uri => "${database}/${prefix}/${id}",                          record_uri => "${database}/${prefix}/${id}",
# Line 237  sub search { Line 248  sub search {
248          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
249    
250          $log->debug( sprintf(          $log->debug( sprintf(
251                  "time spent: db = %.2f, out = %.2f",                  "duration breakdown: store %.6fs, apply %.6fs, total: %.6fs",
252                  $times->{db}, $times->{out},                  $times->{db}, $times->{out}, time() - $search_start_t,
253          ) );          ) );
254    
255          return \@html_results;          return \@html_results;
# Line 282  sub record { Line 293  sub record {
293                  return;                  return;
294          }          }
295    
296          my $html = $self->{out}->apply(          my $html = $self->apply(
297                  template => $args->{template},                  template => $args->{template},
298                  data => $ds,                  data => $ds,
299                  record_uri => $args->{record_uri},                  record_uri => $args->{record_uri},
# Line 351  sub load_html { Line 362  sub load_html {
362          return decode($self->{webpac_encoding}, $content);          return decode($self->{webpac_encoding}, $content);
363  }  }
364    
365    
366    =head2 apply
367    
368    Create output from in-memory data structure using Template Toolkit template.
369    
370     my $text = $tt->apply(
371            template => 'text.tt',
372            data => $ds,
373            record_uri => 'database/prefix/mfn',
374     );
375    
376    It also has follwing template toolikit filter routies defined:
377    
378    =cut
379    
380    sub apply {
381            my $self = shift;
382    
383            my $args = {@_};
384    
385            my $log = $self->{log} || die "no log?";
386    
387            foreach my $a (qw/template data/) {
388                    $log->logconfess("need $a") unless ($args->{$a});
389            }
390    
391    =head3 tt_filter_type
392    
393    filter to return values of specified from $ds, usage from TT template is in form
394    C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this:
395    
396      [% d('Title') %]
397      [% d('Author',', ' %]
398    
399    =cut
400    
401            sub tt_filter_type {
402                    my ($data,$type) = @_;
403                    
404                    die "no data?" unless ($data);
405                    $type ||= 'display';
406    
407                    my $default_delimiter = {
408                            'display' => '&#182;<br/>',
409                            'index' => '\n',
410                    };
411    
412                    return sub {
413    
414                            my ($name,$join) = @_;
415    
416                            die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
417                            # Hm? Should we die here?
418                            return unless ($name);
419    
420                            my $item = $data->{'data'}->{$name} || return;
421    
422                            my $v = $item->{$type} || return;
423    
424                            if (ref($v) eq 'ARRAY') {
425                                    if ($#{$v} == 0) {
426                                            $v = $v->[0];
427                                    } else {
428                                            $join = $default_delimiter->{$type} unless defined($join);
429                                            $v = join($join, @{$v});
430                                    }
431                            } else {
432                                    warn("TT filter $type(): field $name values aren't ARRAY, ignoring");
433                            }
434    
435                            return $v;
436                    }
437            }
438    
439            $args->{'d'} = tt_filter_type($args, 'display');
440            $args->{'display'} = tt_filter_type($args, 'display');
441    
442    =head3 tt_filter_search
443    
444    filter to return links to search, usage in TT:
445    
446      [% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %]
447    
448    =cut
449    
450            sub tt_filter_search {
451    
452                    my ($data) = @_;
453    
454                    die "no data?" unless ($data);
455                    
456                    return sub {
457    
458                            my ($display,$search,$delimiter,$template) = @_;
459                            
460                            # default delimiter
461                            $delimiter ||= '&#182;<br/>',
462    
463                            die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
464                            # Hm? Should we die here?
465                            return unless ($display);
466    
467                            my $item = $data->{'data'}->{$display} || return;
468    
469                            return unless($item->{'display'});
470                            if (! $item->{'search'}) {
471                                    warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)";
472                                    return;
473                            }
474    
475                            my @warn;
476                            foreach my $type (qw/display search/) {
477                                    push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY');
478                            }
479    
480                            if (@warn) {
481                                    warn("TT filter search(): " . join(",", @warn) . ", skipping");
482                                    return;
483                            }
484                            my @html;
485    
486                            my $d_el = $#{ $item->{'display'} };
487                            my $s_el = $#{ $item->{'search'} };
488    
489                            # easy, both fields have same number of elements or there is just
490                            # one search and multiple display
491                            if ( $d_el == $s_el || $s_el == 0 ) {
492    
493                                    foreach my $i ( 0 .. $d_el ) {
494    
495                                            my $s;
496                                            if ($s_el > 0) {
497                                                    $s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search";
498                                            } else {
499                                                    $s = $item->{'search'}->[0];
500                                            }
501                                            #$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
502                                            $s = __quotemeta( $s );
503    
504                                            my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display";
505    
506                                            my $template_arg = '';
507                                            $template_arg = qq{,'$template'} if ($template);
508    
509                                            push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>};
510                                    }
511    
512                                    return join($delimiter, @html);
513                            } else {
514                                    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>};
515                                    my $v = $item->{'display'};
516    
517                                    if ($#{$v} == 0) {
518                                            $html .= $v->[0];
519                                    } else {
520                                            $html .= join($delimiter, @{$v});
521                                    }
522                                    return $html;
523                            }
524                    }
525            }
526    
527            $args->{'search'} = tt_filter_search($args);
528    
529    =head3 load_rec
530    
531    Used mostly for onClick events like this:
532    
533      <a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a>
534    
535    It will automatically do sanity checking and create correct JavaScript code.
536    
537    =cut
538    
539            $args->{'load_rec'} = sub {
540                    my @errors;
541    
542                    my $record_uri = shift or push @errors, "record_uri missing";
543                    my $template = shift or push @errors, "template missing";
544    
545                    if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) {
546                            push @errors, "invalid format of record_uri: $record_uri";
547                    }
548    
549                    if (@errors) {
550                            return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;";
551                    } else {
552                            return "load_rec('$record_uri','$template'); return false;";
553                    }
554            };
555    
556    =head3 load_template
557    
558    Used to re-submit search request and load results in different template
559    
560      <a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a>
561    
562    =cut
563    
564            $args->{'load_template'} = sub {
565                    my $template = shift or return "Logger.error('load_template missing template name!'); return false;";
566                    return "load_template($template); return false;";
567            };
568    
569            my $out;
570    
571            $self->{'tt'}->process(
572                    $args->{'template'},
573                    $args,
574                    \$out
575            ) || $log->error( "apply can't process template: ", $self->{'tt'}->error() );
576    
577            return $out;
578    }
579    
580    
581    =head2 __quotemeta
582    
583    Helper to quote JavaScript-friendly characters
584    
585    =cut
586    
587    sub __quotemeta {
588            local $_ = shift;
589            $_ = decode('iso-8859-2', $_);
590    
591            s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) );
592            {
593                    use bytes;  
594                    s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge;
595            }
596    
597            s/\\x09/\\t/g;
598            s/\\x0A/\\n/g;
599            s/\\x0D/\\r/g;
600            s/"/\\"/g;
601            s/\\x5C/\\\\/g;
602    
603            return $_;
604    }
605    
606    
607    
608  =head1 AUTHOR  =head1 AUTHOR
609    
610  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>

Legend:
Removed from v.351  
changed lines
  Added in v.379

  ViewVC Help
Powered by ViewVC 1.1.26