/[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 399 by dpavlin, Sun Feb 19 12:37:27 2006 UTC revision 414 by dpavlin, Mon Feb 20 21:48:47 2006 UTC
# Line 12  use File::Slurp; Line 12  use File::Slurp;
12  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
13  use Encode qw/encode decode from_to/;  use Encode qw/encode decode from_to/;
14  use Template;  use Template;
 use Data::Dumper;  
15    
16  =head1 NAME  =head1 NAME
17    
# Line 66  sub new { Line 65  sub new {
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");
# Line 89  sub new { Line 88  sub new {
88          # save config parametars in object          # save config parametars in object
89          foreach my $f (qw/          foreach my $f (qw/
90                  db_path template_path hits_on_page webpac_encoding defaultdepth                  db_path template_path hits_on_page webpac_encoding defaultdepth
91                  masterurl                  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};
# Line 145  sub new { Line 144  sub new {
144    
145   $self->setup_site('site_name');   $self->setup_site('site_name');
146    
147  Change node URL and database name according to site name (if available)  Change node URL and database name according to site name (if available) or fallback
148    to C<defaultnode> from configuration.
149    
150  =cut  =cut
151    
152  sub setup_site {  sub setup_site {
153          my $self = shift;          my $self = shift;
154    
155          my $site = shift || return;          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;          my $url = $self->{masterurl} . '/node/' . $site;
160          $self->{est_node}->set_url( $url );          $self->{est_node}->set_url( $url );
161          $self->{log}->debug("setup_site $site");          $self->{log}->debug("setup_site $site using $url");
162  }  }
163    
164  =head2 search  =head2 search
# Line 185  sub search { Line 187  sub search {
187    
188          my $log = $self->{log};          my $log = $self->{log};
189    
190          $log->debug("search 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    
# Line 237  sub search { Line 239  sub search {
239          $cond->set_max( $page * $max );          $cond->set_max( $page * $max );
240    
241          my $result = $self->{est_node}->search($cond, $args->{depth});          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;          my $hits = $result->doc_num;
247    
248          $times->{est} += time() - $t;          $times->{est} += time() - $t;
249    
250          $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );          $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );
251    
252          $log->debug( "hints: " . Dumper($result->{hints}) );          $self->{hints} = $result->{hints};
253            $log->dumper($self->{hints}, 'hints' );
254    
255          #          #
256          # fetch results          # fetch results
# Line 298  sub search { Line 305  sub search {
305    
306                          $times->{db} += time() - $t;                          $times->{db} += time() - $t;
307    
                         #$log->debug( "ds = " . Dumper( \@html_results ) );  
   
308                          $t = time();                          $t = time();
309    
310                          my $html = $self->apply(                          my $html = $self->apply(
# Line 322  sub search { Line 327  sub search {
327    
328          }          }
329    
         #$log->debug( '@results = ' . Dumper( \@results ) );  
   
330          $log->debug( sprintf(          $log->debug( sprintf(
331                  "duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs",                  "duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs",
332                  $times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t,                  $times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t,
# Line 332  sub search { Line 335  sub search {
335          return \@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            return $hints;
372    }
373    
374    
375  =head2 record  =head2 record
376    
377    my $html = $m->record(    my $html = $m->record(
# Line 349  sub record { Line 389  sub record {
389    
390          my $args = {@_};          my $args = {@_};
391          my $log = $self->{log};          my $log = $self->{log};
392          $log->debug("record args: " . Dumper( $args ));          $log->dumper( $args, 'args' );
393    
394          foreach my $f (qw/record_uri template/) {          foreach my $f (qw/record_uri template/) {
395                  $log->fatal("need $f") unless ($args->{$f});                  $log->fatal("need $f") unless ($args->{$f});
# Line 383  sub record { Line 423  sub record {
423  }  }
424    
425    
426    =head2 list_nodes
427    
428      my @nodes = $m->list_nodes( 'site' );
429    
430    Return all databases which have records for selected site. Returned array of
431    hashes has elements C<name> and C<label>.
432    
433    =cut
434    
435    sub list_nodes {
436            my $self = shift;
437    
438            my $site = shift;
439    
440            $self->{log}->debug("list_nodes use site $site");
441    
442            $self->setup_site( $site );
443    
444            my @nodes;
445    
446            if ($self->{est_node}->doc_num > 0) {
447                    push @nodes, {
448                            name => $self->{est_node}->name,
449                            label => $self->{est_node}->label,
450                            doc_num => $self->{est_node}->doc_num,
451                    }
452            }
453    
454            # refresh set info
455            $self->{est_node}->_set_info;
456    
457            my $links = $self->{est_node}->links || return @nodes;
458    
459            $self->{log}->dumper( $links, 'links' );
460    
461            foreach my $link (@{ $links }) {
462                    my ($url, $label, $credit) = split(/\t/, $link, 3);
463                    if ($url =~ m#/node/(.+)$#) {
464                            my $node = $1;
465                            $self->setup_site( $node );
466                            $self->{est_node}->_set_info;
467                            push @nodes, {
468                                    name => $node,
469                                    label => $label,
470                                    doc_num => $self->{est_node}->doc_num,
471                            }
472                    } else {
473                            $self->{log}->warn("can't find node name in link $link");
474                    }
475            }
476    
477            $self->setup_site( $site );
478            $self->{est_node}->_set_info;
479    
480            $self->{log}->dumper( \@nodes, 'nodes' );
481    
482            return @nodes;
483    }
484    
485    =cut
486    
487    
488  =head2 save_html  =head2 save_html
489    
490    $m->save_html( '/full/path/to/file', $content );    $m->save_html( '/full/path/to/file', $content );
# Line 454  It also has follwing template toolikit f Line 556  It also has follwing template toolikit f
556    
557  =cut  =cut
558    
559    # Escape <, >, & and ", and to produce valid XML
560    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  
561    my $escape_re  = join '|' => keys %escape;
562    
563  sub apply {  sub apply {
564          my $self = shift;          my $self = shift;
565    
# Line 501  C<d('FieldName','delimiter')>, where C<d Line 607  C<d('FieldName','delimiter')>, where C<d
607                          if (ref($v) eq 'ARRAY') {                          if (ref($v) eq 'ARRAY') {
608                                  if ($#{$v} == 0) {                                  if ($#{$v} == 0) {
609                                          $v = $v->[0];                                          $v = $v->[0];
610                                            $v =~ s/($escape_re)/$escape{$1}/g;
611                                  } else {                                  } else {
612                                          $join = $default_delimiter->{$type} unless defined($join);                                          $join = $default_delimiter->{$type} unless defined($join);
613                                          $v = join($join, @{$v});                                          $v = join($join, map {
614                                                    s/($escape_re)/$escape{$1}/g;
615                                            } @{$v});
616                                  }                                  }
617                          } else {                          } else {
618                                  warn("TT filter $type(): field $name values aren't ARRAY, ignoring");                                  warn("TT filter $type(): field $name values aren't ARRAY, ignoring");

Legend:
Removed from v.399  
changed lines
  Added in v.414

  ViewVC Help
Powered by ViewVC 1.1.26