/[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 200 by dpavlin, Wed Nov 30 23:21:30 2005 UTC revision 322 by dpavlin, Sun Dec 25 23:31:59 2005 UTC
# Line 7  use base qw/ Line 7  use base qw/
7          Catalyst::Model          Catalyst::Model
8  /;  /;
9  use Data::Dumper;  use Data::Dumper;
10  use WebPAC::DB;  use WebPAC::Store 0.08;
11  use WebPAC::Output::TT 0.02;  use WebPAC::Output::TT 0.06;
12  use WebPAC::Search::Estraier 0.02;  use WebPAC::Search::Estraier 0.05;
13  use File::Slurp;  use File::Slurp;
14  use Time::HiRes;  use Time::HiRes;
15    
# Line 34  Configuration for hyperestraier in C<con Line 34  Configuration for hyperestraier in C<con
34    
35   # configuration for hyper estraier full text search engine   # configuration for hyper estraier full text search engine
36   hyperestraier:   hyperestraier:
37    url: 'http://localhost:1978/node/webpac2'    masterurl: 'http://localhost:1978/node/webpac2'
38      defaultnode: 'webpac2'
39      defaultdepth: 1
40    user: 'admin'    user: 'admin'
41    passwd: 'admin'    passwd: 'admin'
42    hits_on_page: 100    hits_on_page: 100
43      hits_for_pager: 1000
44    
45   webpac:   webpac:
46    db_path: '/data/webpac2/db'    db_path: '/data/webpac2/db'
# Line 62  sub new { Line 65  sub new {
65          my $est_cfg = $c->config->{hyperestraier};          my $est_cfg = $c->config->{hyperestraier};
66          $est_cfg->{'log'} = $log;          $est_cfg->{'log'} = $log;
67    
68          $est_cfg->{encoding} = $est_cfg->{catalyst_encoding};          $est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding");
69    
70          $log->debug("using config:" . Dumper($est_cfg) );          $log->debug("using config:" . Dumper($est_cfg) );
71    
72            if (! $est_cfg->{database}) {
73                    my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration");
74                    $log->info("using default node $defaultnode");
75                    $est_cfg->{database} = $defaultnode;
76            }
77    
78          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
79    
80          # save config parametars in object          # save config parametars in object
81          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding/) {          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding defaultdepth/) {
82                  $self->{$f} = $c->config->{hyperestraier}->{$f} ||                  $self->{$f} = $c->config->{hyperestraier}->{$f} ||
83                          $c->config->{webpac}->{$f};                          $c->config->{webpac}->{$f};
84                  $log->debug("self->{$f} = " . $self->{$f});                  $log->debug("self->{$f} = " . $self->{$f});
# Line 79  sub new { Line 88  sub new {
88    
89          $log->debug("using db path '$db_path', template path '$template_path'");          $log->debug("using db path '$db_path', template path '$template_path'");
90    
91          $self->{db} = new WebPAC::DB(          $self->{db} = new WebPAC::Store(
92                  path => $db_path,                  path => $db_path,
93                  read_only => 1,                  read_only => 1,
94                    database => $est_cfg->{database},
95          );          );
96    
97          $self->{out} = new WebPAC::Output::TT(          $self->{out} = new WebPAC::Output::TT(
# Line 104  sub new { Line 114  sub new {
114                  "'"                  "'"
115          );          );
116    
117            $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");
118    
119          return $self;          return $self;
120    
# Line 118  sub new { Line 129  sub new {
129          get_attr => [ '@uri' ],          get_attr => [ '@uri' ],
130          max => 42,          max => 42,
131          template => 'result_template.tt',          template => 'result_template.tt',
132            depth => 1,
133    );    );
134    
135  All fields are standard C<WebPAC::Search::Estraier> parametars except  All fields are standard C<WebPAC::Search::Estraier> parametars except
# Line 146  sub search { Line 158  sub search {
158    
159          my $template_filename = $args->{template} || $self->{template};          my $template_filename = $args->{template} || $self->{template};
160    
161          $args->{max} ||= $self->{'hits_on_page'};          $args->{max} ||= $self->{'hits_for_pager'};
162          if (! $args->{max}) {          if (! $args->{max}) {
163                  $args->{max} = 10;                  $args->{max} = 100;
164                  $log->warn("max not set when calling model. Using default of 10");                  $log->warn("max not set when calling model. Using default of $args->{max}");
165          }          }
166    
167          my $times;      # store some times for benchmarking          my $times;      # store some times for benchmarking
168    
169          my $t = time();          my $t = time();
170    
171            # transfer depth of search
172            if (! $args->{depth}) {
173                    my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration");
174                    $args->{depth} = $default;
175                    $log->warn("using default search depth $default");
176            }
177    
178          my @results = $self->{est}->search( %{ $args } );          my @results = $self->{est}->search( %{ $args } );
179    
180          $times->{est} += time() - $t;          $times->{est} += time() - $t;
# Line 175  sub search { Line 194  sub search {
194    
195          for my $i ( 0 .. $#results ) {          for my $i ( 0 .. $#results ) {
196    
197                  my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);                  my ($database, $prefix, $id);
198                    if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) {
199                            ($database, $prefix,$id) = ($1,$2,$3);
200                    } else {
201                            $log->warn("can't decode database/prefix/id from " .  $results[$i]->{'@uri'});
202                            next;
203                    }
204    
205                  #$log->debug("load_ds( $mfn )");                  #$log->debug("load_ds( id => $id, prefix => '$prefix' )");
206    
207                  $t = time();                  $t = time();
208    
209                  my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;                  my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id );
210                    if (! $ds) {
211                            $log->error("can't load_ds( ${database}/${prefix}/${id} )");
212                            next;
213                    }
214    
215                  $times->{db} += time() - $t;                  $times->{db} += time() - $t;
216    
# Line 192  sub search { Line 221  sub search {
221                  my $html = $self->{out}->apply(                  my $html = $self->{out}->apply(
222                          template => $template_filename,                          template => $template_filename,
223                          data => $ds,                          data => $ds,
224                            record_uri => "${database}/${prefix}/${id}",
225                            config => $self->{databases}->{$database},
226                  );                  );
227    
228                  $times->{out} += time() - $t;                  $times->{out} += time() - $t;
# Line 235  sub record { Line 266  sub record {
266          my $log = $self->{log};          my $log = $self->{log};
267          $log->debug("args: " . Dumper( $args ));          $log->debug("args: " . Dumper( $args ));
268    
269          foreach my $f (qw/mfn template/) {          foreach my $f (qw/record_uri template/) {
270                  $log->die("need $f") unless ($args->{$f});                  $log->fatal("need $f") unless ($args->{$f});
271          }          }
272    
273          my $mfn = $args->{mfn};          my ($database, $prefix, $id);
274    
275          my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;          if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) {
276                    ($database, $prefix, $id) = ($1,$2,$3);
277            } else {
278                    $log->error("can't parse $args->{record_uri} into prefix, database and uri");
279                    return;
280            }
281    
282            my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database );
283            if (! $ds) {
284                    $log->error("can't load_ds( $database/$prefix/$id )");
285                    return;
286            }
287    
288          my $html = $self->{out}->apply(          my $html = $self->{out}->apply(
289                  template => $args->{template},                  template => $args->{template},
290                  data => $ds,                  data => $ds,
291                    record_uri => $args->{record_uri},
292                    config => $self->{databases}->{$database},
293          );          );
294    
295          $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");          $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
# Line 253  sub record { Line 297  sub record {
297          return $html;          return $html;
298  }  }
299    
300    
301  =head2 save_html  =head2 save_html
302    
303    $m->save_html( '/full/path/to/file', $content );    $m->save_html( '/full/path/to/file', $content );
# Line 275  sub save_html { Line 320  sub save_html {
320          $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;          $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
321          $content =~ s/^[\n\r]+//s;          $content =~ s/^[\n\r]+//s;
322          $content =~ s/[\n\r]+$/\n/s;          $content =~ s/[\n\r]+$/\n/s;
323            $content =~ s/\n\r/\n/gs;
324    
325          my ($from, $to) = (          my ($from, $to) = (
326                  $self->{out_encoding},                  $self->{out_encoding},

Legend:
Removed from v.200  
changed lines
  Added in v.322

  ViewVC Help
Powered by ViewVC 1.1.26