/[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 142 by dpavlin, Fri Nov 25 00:23:33 2005 UTC revision 178 by dpavlin, Sun Nov 27 05:07:01 2005 UTC
# Line 11  use WebPAC::DB; Line 11  use WebPAC::DB;
11  use WebPAC::Output::TT;  use WebPAC::Output::TT;
12  use WebPAC::Search::Estraier 0.02;  use WebPAC::Search::Estraier 0.02;
13  use File::Slurp;  use File::Slurp;
14    use Time::HiRes;
15    
16  =head1 NAME  =head1 NAME
17    
# Line 36  Configuration for hyperestraier in C<con Line 37  Configuration for hyperestraier in C<con
37    url: 'http://localhost:1978/node/webpac2'    url: 'http://localhost:1978/node/webpac2'
38    user: 'admin'    user: 'admin'
39    passwd: 'admin'    passwd: 'admin'
40      hits_on_page: 100
41    
42   webpac:   webpac:
43    db_path: '/data/webpac2/db'    db_path: '/data/webpac2/db'
# Line 66  sub new { Line 68  sub new {
68    
69          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
70    
71          my $db_path = $c->config->{webpac}->{db_path};          # save config parametars in object
72          my $template_path = $c->config->{webpac}->{template_path};          foreach my $f (qw/db_path template_path hits_on_page/) {
73          $self->{template_path} = $template_path;                  $self->{$f} = $c->config->{hyperestraier}->{$f} ||
74                            $c->config->{webpac}->{$f};
75                    $log->debug("self->{$f} = " . $self->{$f});
76            }
77            my $db_path = $self->{db_path};
78            my $template_path = $self->{template_path};
79    
80          $log->debug("using db path '$db_path', template path '$template_path'");          $log->debug("using db path '$db_path', template path '$template_path'");
81    
# Line 97  sub new { Line 104  sub new {
104                  "'"                  "'"
105          );          );
106    
107    
108          return $self;          return $self;
109    
110  }  }
# Line 111  Convert data saved to disk in Webpac enc Line 119  Convert data saved to disk in Webpac enc
119    
120  sub iconv_on_save {  sub iconv_on_save {
121          my $self = shift;          my $self = shift;
122            my $content = shift || return;
123    
124          $self->{iconv_save} ||= new Text::Iconv(          $self->{iconv_save} ||= new Text::Iconv(
125                  $self->config->{webpac}->{out_encoding},                  $self->config->{webpac}->{out_encoding},
126                  $self->config->{webpac}->{webpac_encoding},                  $self->config->{webpac}->{webpac_encoding},
127          );          );
128    
129          $self->{iconv_save}->convert( @_ );          return $self->{iconv_save}->convert( $content );
130  }  }
131    
132    
133  =head2 search  =head2 search
134    
135    my $m->search( 'query phrase', 'result_template.tt', \@add_attr );    my $m->search(
136            phrase => 'query phrase',
137            add_attr => \@add_attr
138            get_attr => [ '@uri' ],
139            max => 42,
140            template => 'result_template.tt',
141      );
142    
143    All fields are standard C<WebPAC::Search::Estraier> parametars except
144    C<template> which will (if specified) return results in HTML using
145    selected template.
146    
147  =cut  =cut
148    
149  sub search {  sub search {
150          my ( $self, $query, $template, $add_attr ) = @_;          my $self = shift;
151    
152            my $args = {@_};
153    
154          my $log = $self->{log};          my $log = $self->{log};
155    
156          $log->debug("search model query: '$query', add_attr: '" . join("','", @{$add_attr}) . "'");          $log->debug("args: " . Dumper( $args ));
157    
158          my $template_filename = $template || $self->{template};          my $query = $args->{phrase} || $log->warn("no query phrase") && return;
159    
160          my @results = $self->{est}->search(          $log->debug("search model query: '$query'");
161                  phrase => $query,          if ($args->{add_attr}) {
162                  get_attr => [ '@uri' ],                  $log->debug(" + add_attr: " .
163                  max => 100,                          join("','", @{ $args->{add_attr} })
164                  add_attr => $add_attr,                  );
165          );          }
166    
167            my $template_filename = $args->{template} || $self->{template};
168    
169            $args->{max} ||= $self->{'hits_on_page'};
170            if (! $args->{max}) {
171                    $args->{max} = 10;
172                    $log->warn("max not set when calling model. Using default of 10");
173            }
174    
175            my $times;      # store some times for benchmarking
176    
177            my $t = time();
178    
179            my @results = $self->{est}->search( %{ $args } );
180    
181          $log->debug("loading " . ($#results + 1) . " results");          $times->{est} += time() - $t;
182    
183            my $hits = $#results + 1;
184    
185            $log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) );
186    
187            # just return results?
188            return @results unless ($args->{'template'});
189    
190            #
191            # construct HTML results
192            #
193    
194          my @html_results;          my @html_results;
195    
# Line 153  sub search { Line 199  sub search {
199    
200                  #$log->debug("load_ds( $mfn )");                  #$log->debug("load_ds( $mfn )");
201    
202                    $t = time();
203    
204                  my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;                  my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
205            
206                    $times->{db} += time() - $t;
207    
208                  #$log->debug( "ds = " . Dumper( \@html_results ) );                  #$log->debug( "ds = " . Dumper( \@html_results ) );
209    
210                    $t = time();
211    
212                  my $html = $self->{out}->apply(                  my $html = $self->{out}->apply(
213                          template => $template_filename,                          template => $template_filename,
214                          data => $ds,                          data => $ds,
215                  );                  );
216    
217                    $times->{out} += time() - $t;
218    
219                    $t = time();
220    
221                  $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");                  $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
222    
223                    $times->{iconv} += time() - $t;
224    
225                  push @html_results, $html;                  push @html_results, $html;
226    
227          }          }
228    
229          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
230    
231            $log->debug( sprintf(
232                    "time spent: db = %.2f, out = %.2f, iconv = %.2f",
233                    $times->{db}, $times->{out}, $times->{iconv},
234            ) );
235    
236          return \@html_results;          return \@html_results;
237  }  }
238    
239    =head2 record
240    
241      my $html = $m->record(
242            mfn => 42,
243            template => 'foo.tt',
244      );
245    
246    This will load one record, convert it to html using C<template> and return
247    it.
248    
249    =cut
250    
251    sub record {
252            my $self = shift;
253    
254            my $args = {@_};
255            my $log = $self->{log};
256            $log->debug("args: " . Dumper( $args ));
257    
258            foreach my $f (qw/mfn template/) {
259                    $log->die("need $f") unless ($args->{$f});
260            }
261    
262            my $mfn = $args->{mfn};
263    
264            my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
265    
266            my $html = $self->{out}->apply(
267                    template => $args->{template},
268                    data => $ds,
269            );
270    
271            $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
272    
273            return $html;
274    }
275    
276  =head2 save_html  =head2 save_html
277    
278    $m->save_html( '/full/path/to/file', $content );    $m->save_html( '/full/path/to/file', $content );
# Line 188  it over original file which should be at Line 288  it over original file which should be at
288  sub save_html {  sub save_html {
289          my ($self, $path, $content) = @_;          my ($self, $path, $content) = @_;
290    
         $content = $self->iconv_on_save( $content ) || die "no content?";  
   
291          sub _conv_js {          sub _conv_js {
292                  my $t = shift || return;                  my $t = shift || return;
293                  return $self->{iconv}->convert(chr(hex($t)));                  return $self->{iconv}->convert(chr(hex($t)));
# Line 198  sub save_html { Line 296  sub save_html {
296          $content =~ s/^[\n\r]+//s;          $content =~ s/^[\n\r]+//s;
297          $content =~ s/[\n\r]+$/\n/s;          $content =~ s/[\n\r]+$/\n/s;
298    
299            my $iconv_on_save = new Text::Iconv(
300                    $self->config->{webpac}->{out_encoding},
301                    $self->config->{webpac}->{webpac_encoding},
302            );
303            $self->{log}->debug( "content BEFORE : $content" );
304            no utf8;
305            $content = $iconv_on_save->convert( $content ) || die "no content?";
306    
307            $self->{log}->debug( "content AFTER: $content" );
308    
309          write_file($path . '.new', $content) || die "can't save ${path}.new $!";          write_file($path . '.new', $content) || die "can't save ${path}.new $!";
310          rename $path . '.new', $path || die "can't rename to $path: $!";          rename $path . '.new', $path || die "can't rename to $path: $!";
311  }  }

Legend:
Removed from v.142  
changed lines
  Added in v.178

  ViewVC Help
Powered by ViewVC 1.1.26