/[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 403 by dpavlin, Sun Feb 19 18:12:57 2006 UTC revision 452 by dpavlin, Sun May 7 20:32:49 2006 UTC
# Line 153  sub setup_site { Line 153  sub setup_site {
153          my $self = shift;          my $self = shift;
154    
155          my $site = shift || $self->{defaultnode};          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);          $self->{log}->fatal("setup_site can't find site or defaultnode") unless ($site);
162    
163          my $url = $self->{masterurl} . '/node/' . $site;          my $url = $self->{masterurl} . '/node/' . $site;
164          $self->{est_node}->set_url( $url );          $self->{est_node}->set_url( $url );
165          $self->{log}->debug("setup_site $site using $url");          $self->{log}->debug("setup_site '$site' using $url");
166  }  }
167    
168  =head2 search  =head2 search
# Line 193  sub search { Line 197  sub search {
197    
198          my $template_filename = $args->{template} || $self->{template};          my $template_filename = $args->{template} || $self->{template};
199    
200          $args->{max} ||= $self->{'hits_for_pager'};          $args->{hits_on_page} ||= $self->{'hits_for_pager'};
201          if (! $args->{max}) {          if (! $args->{hits_on_page}) {
202                  $args->{max} = 100;                  $args->{hits_on_page} = 100;
203                  $log->warn("max not set when calling model. Using default of $args->{max}");                  $log->warn("max not set when calling model. Using default of $args->{hits_on_page}");
204          }          }
205    
206          my $times;      # store some times for benchmarking          my $times;      # store some times for benchmarking
# Line 211  sub search { Line 215  sub search {
215          }          }
216          $args->{depth} ||= 0;          $args->{depth} ||= 0;
217    
218          $log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]');          $log->debug("searching " . $self->{est_node}->{url} . " hits on page: $args->{hits_on_page} depth: $args->{depth} phrase: " . ($query || '[none]') );
219    
220          #          #
221          # construct condition for Hyper Estraier          # construct condition for Hyper Estraier
# Line 229  sub search { Line 233  sub search {
233          $cond->set_options( $args->{options} ) if ($args->{options});          $cond->set_options( $args->{options} ) if ($args->{options});
234          $cond->set_order( $args->{order} ) if ($args->{order});          $cond->set_order( $args->{order} ) if ($args->{order});
235    
236          my $max = $args->{max} || 7;          my $hits_on_page = $args->{hits_on_page} || 7;
237          my $page = $args->{page} || 1;          my $page = $args->{page} || 1;
238          if ($page < 1) {          if ($page < 1) {
239                  $log->warn("page number $page < 1");                  $log->warn("page number $page < 1");
240                  $page = 1;                  $page = 1;
241          }          }
242    
243          $cond->set_max( $page * $max );          $cond->set_max( my $max = $page * $hits_on_page );
244            $cond->set_skip( my $skip = ( $page - 1 ) * $hits_on_page );
245    
246            $log->debug("search max: $max, skip: $skip");
247    
248          my $result = $self->{est_node}->search($cond, $args->{depth});          my $result = $self->{est_node}->search($cond, $args->{depth});
249            if (! $result) {
250                    $self->{log}->fatal("search didn't return result");
251                    return;
252            }
253          my $hits = $result->doc_num;          my $hits = $result->doc_num;
254    
255          $times->{est} += time() - $t;          $times->{est} += time() - $t;
256    
257          $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );          $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );
258    
259          $log->dumper($result->{hints}, 'result->hints' );          $self->{hints} = $result->{hints};
260            #$log->dumper($self->{hints}, 'original hints' );
261    
262          #          #
263          # fetch results          # fetch results
# Line 253  sub search { Line 265  sub search {
265    
266          my @results;          my @results;
267    
268          for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) {          
269    
270            for my $i ( 0 .. ( $hits < $max ? ($hits-1) : ($max-1) ) ) {
271    
272                  $t = time();                  $t = time();
273    
# Line 330  sub search { Line 344  sub search {
344          return \@results;          return \@results;
345  }  }
346    
347    =head2 hints
348    
349      my $hints = $m->hints;
350    
351    Return various useful hints about result
352    
353    =cut
354    
355    sub hints {
356            my $self = shift;
357    
358            unless ($self->{hints}) {
359                    $self->{log}->fatal("no hints found!");
360                    return;
361            }
362    
363            my $hints;
364    
365            while (my ($key,$val) = each %{ $self->{hints} }) {
366    
367                    #$self->{log}->debug("current hint $key = $val");
368    
369                    if ($key =~ m/^(?:HITS*|TIME|DOCNUM|WORDNUM)$/) {
370                            $hints->{ lc($key) } = $val;
371                    } elsif ($key =~ m/^HINT#/) {
372                            my ($word,$count) = split(/\t/,$val,2);
373                            $hints->{words}->{$word} = $count;
374                    } elsif ($key =~ m/^LINK#/) {
375                            my ($url,undef,undef,undef,undef,undef,$results) = split(/\t/,$val,7);
376                            if ($url =~ m#/node/(.+)$#) {
377                                    $hints->{node}->{$1} = $results;
378                            } else {
379                                    $self->{log}->debug("url $url doesn't have /node/ in it!");
380                            }
381                    } else {
382                            $self->{log}->debug("unknown hint $key = $val");
383                    }
384    
385            }
386    
387            $self->{log}->dumper($hints, 'model hints' );
388    
389            return $hints;
390    }
391    
392    
393  =head2 record  =head2 record
394    
395    my $html = $m->record(    my $html = $m->record(
# Line 388  sub record { Line 448  sub record {
448  Return all databases which have records for selected site. Returned array of  Return all databases which have records for selected site. Returned array of
449  hashes has elements C<name> and C<label>.  hashes has elements C<name> and C<label>.
450    
451    This function does cacheing inside C<< $self->{nodes_in_site} >>, but you
452    probably didn't want to know that.
453    
454  =cut  =cut
455    
456  sub list_nodes {  sub list_nodes {
457          my $self = shift;          my $self = shift;
458    
459          my $site = shift;          my $site = shift || $self->{defaultnode};
460    
461            # cache?
462            if ($self->{nodes_in_site}->{$site} && ref($self->{nodes_in_site}->{$site}) eq 'ARRAY') {
463                    $self->{log}->debug("list_nodes for site $site and returns from cache");
464                    return @{ $self->{nodes_in_site}->{$site} };
465            };
466    
467          $self->{log}->debug("list_nodes use site $site");          $self->{log}->debug("list_nodes for site $site");
468    
469          $self->setup_site( $site );          $self->setup_site( $site );
470    
# Line 405  sub list_nodes { Line 474  sub list_nodes {
474                  push @nodes, {                  push @nodes, {
475                          name => $self->{est_node}->name,                          name => $self->{est_node}->name,
476                          label => $self->{est_node}->label,                          label => $self->{est_node}->label,
477                            doc_num => $self->{est_node}->doc_num,
478                  }                  }
479          }          }
480    
# Line 418  sub list_nodes { Line 488  sub list_nodes {
488          foreach my $link (@{ $links }) {          foreach my $link (@{ $links }) {
489                  my ($url, $label, $credit) = split(/\t/, $link, 3);                  my ($url, $label, $credit) = split(/\t/, $link, 3);
490                  if ($url =~ m#/node/(.+)$#) {                  if ($url =~ m#/node/(.+)$#) {
491                            my $node = $1;
492                            $self->setup_site( $node );
493                            $self->{est_node}->_set_info;
494                            $label = decode('UTF-8', $label);
495                          push @nodes, {                          push @nodes, {
496                                  name => $1,                                  name => $node,
497                                  label => $label,                                  label => $label,
498                                    doc_num => $self->{est_node}->doc_num,
499                          }                          }
500                  } else {                  } else {
501                          $self->{log}->warn("can't find node name in link $link");                          $self->{log}->warn("can't find node name in link $link");
502                  }                  }
503          }          }
504    
505          $self->{log}->dumper( \@nodes, 'nodes' );          $self->setup_site( $site );
506            $self->{est_node}->_set_info;
507    
508            $self->{nodes_in_site}->{$site} = \@nodes;
509    
510          return @nodes;          return @nodes;
511  }  }
512    
 =cut  
   
   
513  =head2 save_html  =head2 save_html
514    
515    $m->save_html( '/full/path/to/file', $content );    $m->save_html( '/full/path/to/file', $content );
# Line 506  It also has follwing template toolikit f Line 581  It also has follwing template toolikit f
581    
582  =cut  =cut
583    
584    # Escape <, >, & and ", and to produce valid XML
585    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  
586    my $escape_re  = join '|' => keys %escape;
587    
588  sub apply {  sub apply {
589          my $self = shift;          my $self = shift;
590    
# Line 553  C<d('FieldName','delimiter')>, where C<d Line 632  C<d('FieldName','delimiter')>, where C<d
632                          if (ref($v) eq 'ARRAY') {                          if (ref($v) eq 'ARRAY') {
633                                  if ($#{$v} == 0) {                                  if ($#{$v} == 0) {
634                                          $v = $v->[0];                                          $v = $v->[0];
635                                            $v =~ s/($escape_re)/$escape{$1}/g;
636                                  } else {                                  } else {
637                                          $join = $default_delimiter->{$type} unless defined($join);                                          $join = $default_delimiter->{$type} unless defined($join);
638                                          $v = join($join, @{$v});                                          $v = join($join, map {
639                                                    s/($escape_re)/$escape{$1}/g;
640                                            } @{$v});
641                                  }                                  }
642                          } else {                          } else {
643                                  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.403  
changed lines
  Added in v.452

  ViewVC Help
Powered by ViewVC 1.1.26