/[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 380 by dpavlin, Sun Jan 22 00:44:37 2006 UTC revision 384 by dpavlin, Sun Jan 22 10:58:58 2006 UTC
# Line 7  use base qw/ Line 7  use base qw/
7          Catalyst::Model          Catalyst::Model
8  /;  /;
9  use WebPAC::Store 0.08;  use WebPAC::Store 0.08;
10  use WebPAC::Search::Estraier 0.05;  use Search::Estraier 0.04;
11  use File::Slurp;  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/;
# Line 74  sub new { Line 74  sub new {
74                  $est_cfg->{database} = $defaultnode;                  $est_cfg->{database} = $defaultnode;
75          }          }
76    
77          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          my $url = $est_cfg->{masterurl} . '/node/' . $est_cfg->{database};
78    
79            $log->info("opening Hyper Estraier index $url as $est_cfg->{'user'}");
80    
81            $self->{est_node} = Search::Estraier::Node->new(
82                    url => $url,
83                    user => $est_cfg->{user},
84                    passwd => $est_cfg->{passwd},
85            );
86    
87            $log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node});
88    
89          # save config parametars in object          # save config parametars in object
90          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) {          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) {
# Line 101  sub new { Line 111  sub new {
111                  "'"                  "'"
112          );          );
113    
114          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");          $self->{databases} = $c->config->{databases} || $log->fatal("can't find databases in config");
115    
116          # create Template toolkit instance          # create Template toolkit instance
117          $self->{'tt'} = Template->new(          $self->{'tt'} = Template->new(
# Line 159  sub search { Line 169  sub search {
169    
170          my $query = $args->{phrase} || $log->warn("no query phrase") && return;          my $query = $args->{phrase} || $log->warn("no query phrase") && return;
171    
         $log->debug("search model query: '$query'");  
         if ($args->{add_attr}) {  
                 $log->debug(" + add_attr: " .  
                         join("','", @{ $args->{add_attr} })  
                 );  
         }  
   
172          my $template_filename = $args->{template} || $self->{template};          my $template_filename = $args->{template} || $self->{template};
173    
174          $args->{max} ||= $self->{'hits_for_pager'};          $args->{max} ||= $self->{'hits_for_pager'};
# Line 184  sub search { Line 187  sub search {
187                  $args->{depth} = $default;                  $args->{depth} = $default;
188                  $log->warn("using default search depth $default");                  $log->warn("using default search depth $default");
189          }          }
190            $args->{depth} ||= 0;
191    
192          my @results = $self->{est}->search( %{ $args } );          $log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]');
193    
194          $times->{est} += time() - $t;          #
195            # construct condition for Hyper Estraier
196            #
197            my $cond = Search::Estraier::Condition->new();
198            if ( ref($args->{add_attr}) eq 'ARRAY' ) {
199                    $log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) );
200                    map {
201                            $cond->add_attr( $_ );
202                            $log->debug(" + $_");
203                    } @{ $args->{add_attr} };
204            };
205    
206            $cond->set_phrase( $query ) if ($query);
207            $cond->set_options( $args->{options} ) if ($args->{options});
208            $cond->set_order( $args->{order} ) if ($args->{order});
209    
210            my $max = $args->{max} || 7;
211            my $page = $args->{page} || 1;
212            if ($page < 1) {
213                    $log->warn("page number $page < 1");
214                    $page = 1;
215            }
216    
217          my $hits = $#results + 1;          $cond->set_max( $page * $max );
218    
219          $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );          my $result = $self->{est_node}->search($cond, $args->{depth});
220            my $hits = $result->doc_num;
221    
222          # just return results?          $times->{est} += time() - $t;
223          return @results unless ($args->{'template'});  
224            $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );
225    
226          #          #
227          # construct HTML results          # fetch results
228          #          #
229    
230          my @html_results;          my @results;
231    
232          for my $i ( 0 .. $#results ) {          for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) {
233    
234                  my ($database, $prefix, $id);                  $t = time();
235                  if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) {  
236                          ($database, $prefix,$id) = ($1,$2,$3);                  #$log->debug("get_doc($i)");
237                  } else {                  my $doc = $result->get_doc( $i );
238                          $log->warn("can't decode database/prefix/id from " .  $results[$i]->{'@uri'});                  if (! $doc) {
239                            $log->warn("can't find result $i");
240                          next;                          next;
241                  }                  }
242    
243                  #$log->debug("load_ds( id => $id, prefix => '$prefix' )");                  my $hash;
244    
245                  $t = time();                  foreach my $attr (@{ $args->{get_attr} }) {
246                            my $val = $doc->attr( $attr );
247                  my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id );                          #$log->debug("attr $attr = ", $val || 'undef');
248                  if (! $ds) {                          $hash->{$attr} = $val if (defined($val));
                         $log->error("can't load_ds( ${database}/${prefix}/${id} )");  
                         next;  
249                  }                  }
250    
251                  $times->{db} += time() - $t;                  $times->{hash} += time() - $t;
252    
253                  #$log->debug( "ds = " . Dumper( \@html_results ) );                  next unless ($hash);
254    
255                  $t = time();                  if (! $args->{'template'}) {
256                            push @results, $hash;
257                    } else {
258                            my ($database, $prefix, $id);
259    
260                  my $html = $self->apply(                          if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) {
261                          template => $template_filename,                                  ($database, $prefix,$id) = ($1,$2,$3);
262                          data => $ds,                          } else {
263                          record_uri => "${database}/${prefix}/${id}",                                  $log->warn("can't decode database/prefix/id from " .  $hash->{'@uri'});
264                          config => $self->{databases}->{$database},                                  next;
265                  );                          }
266    
267                  $times->{out} += time() - $t;                          #$log->debug("load_ds( id => $id, prefix => '$prefix' )");
268    
269                  $t = time();                          $t = time();
270    
271                            my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id );
272                            if (! $ds) {
273                                    $log->error("can't load_ds( ${database}/${prefix}/${id} )");
274                                    next;
275                            }
276    
277                  $html = decode($self->{webpac_encoding}, $html);                          $times->{db} += time() - $t;
278    
279                  push @html_results, $html;                          #$log->debug( "ds = " . Dumper( \@html_results ) );
280    
281                            $t = time();
282    
283                            my $html = $self->apply(
284                                    template => $template_filename,
285                                    data => $ds,
286                                    record_uri => "${database}/${prefix}/${id}",
287                                    config => $self->{databases}->{$database},
288                            );
289    
290                            $times->{apply} += time() - $t;
291    
292                            $t = time();
293    
294                            $html = decode($self->{webpac_encoding}, $html);
295    
296                            $times->{decode} += time() - $t;
297    
298                            push @results, $html;
299                    }
300    
301          }          }
302    
303          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );          #$log->debug( '@results = ' . Dumper( \@results ) );
304    
305          $log->debug( sprintf(          $log->debug( sprintf(
306                  "duration breakdown: store %.6fs, apply %.6fs, total: %.6fs",                  "duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs",
307                  $times->{db}, $times->{out}, time() - $search_start_t,                  $times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t,
308          ) );          ) );
309    
310          return \@html_results;          return \@results;
311  }  }
312    
313  =head2 record  =head2 record
# Line 385  sub apply { Line 440  sub apply {
440          my $log = $self->{log} || die "no log?";          my $log = $self->{log} || die "no log?";
441    
442          foreach my $a (qw/template data/) {          foreach my $a (qw/template data/) {
443                  $log->logconfess("need $a") unless ($args->{$a});                  $log->fatal("need $a") unless ($args->{$a});
444          }          }
445    
446  =head3 tt_filter_type  =head3 tt_filter_type
# Line 494  filter to return links to search, usage Line 549  filter to return links to search, usage
549    
550                                          my $s;                                          my $s;
551                                          if ($s_el > 0) {                                          if ($s_el > 0) {
552                                                  $s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search";                                                  $s = $item->{'search'}->[$i] or warn "can't find value $i for type search in field $search";
553                                          } else {                                          } else {
554                                                  $s = $item->{'search'}->[0];                                                  $s = $item->{'search'}->[0];
555                                          }                                          }
556                                          #$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;                                          #$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
557                                          $s = __quotemeta( $s );                                          $s = __quotemeta( $s );
558    
559                                          my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display";                                          my $d = $item->{'display'}->[$i] or warn "can't find value $i for type display in field $display";
560    
561                                          my $template_arg = '';                                          my $template_arg = '';
562                                          $template_arg = qq{,'$template'} if ($template);                                          $template_arg = qq{,'$template'} if ($template);

Legend:
Removed from v.380  
changed lines
  Added in v.384

  ViewVC Help
Powered by ViewVC 1.1.26