/[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 99 by dpavlin, Tue Nov 22 14:45:12 2005 UTC revision 237 by dpavlin, Tue Dec 6 23:49:13 2005 UTC
# Line 5  use warnings; Line 5  use warnings;
5  use lib '/data/webpac2/lib';  use lib '/data/webpac2/lib';
6  use base qw/  use base qw/
7          Catalyst::Model          Catalyst::Model
         WebPAC::Search::Estraier  
8  /;  /;
9  use Data::Dumper;  use Data::Dumper;
10  use WebPAC::DB;  use WebPAC::Store 0.08;
11  use WebPAC::Output::TT;  use WebPAC::Output::TT 0.02;
12    use WebPAC::Search::Estraier 0.05;
13    use File::Slurp;
14    use Time::HiRes;
15    
16  =head1 NAME  =head1 NAME
17    
# Line 32  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
43    
44   webpac:   webpac:
45    db_path: '/data/webpac2/db'    db_path: '/data/webpac2/db'
# Line 59  sub new { Line 64  sub new {
64          my $est_cfg = $c->config->{hyperestraier};          my $est_cfg = $c->config->{hyperestraier};
65          $est_cfg->{'log'} = $log;          $est_cfg->{'log'} = $log;
66    
67            $est_cfg->{encoding} = $est_cfg->{catalyst_encoding};
68    
69          $log->debug("using config:" . Dumper($est_cfg) );          $log->debug("using config:" . Dumper($est_cfg) );
70    
71            if (! $est_cfg->{database}) {
72                    my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration");
73                    $log->info("using default node $defaultnode");
74                    $est_cfg->{database} = $defaultnode;
75            }
76    
77          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
78    
79          my $db_path = $c->config->{webpac}->{db_path};          # save config parametars in object
80          my $template_path = $c->config->{webpac}->{template_path};          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding defaultdepth/) {
81                    $self->{$f} = $c->config->{hyperestraier}->{$f} ||
82                            $c->config->{webpac}->{$f};
83                    $log->debug("self->{$f} = " . $self->{$f});
84            }
85            my $db_path = $self->{db_path};
86            my $template_path = $self->{template_path};
87    
88          $log->debug("using db path '$db_path', template path '$template_path'");          $log->debug("using db path '$db_path', template path '$template_path'");
89    
90          $self->{db} = new WebPAC::DB(          $self->{db} = new WebPAC::Store(
91                  path => $db_path,                  path => $db_path,
92                  read_only => 1,                  read_only => 1,
93                    database => $est_cfg->{database},
94          );          );
95    
96          $self->{out} = new WebPAC::Output::TT(          $self->{out} = new WebPAC::Output::TT(
# Line 78  sub new { Line 98  sub new {
98                  filters => { foo => sub { shift } },                  filters => { foo => sub { shift } },
99          );          );
100    
101            # default template from config.yaml
102          $self->{template} ||= $c->config->{webpac}->{template};          $self->{template} ||= $c->config->{webpac}->{template};
103    
104          $self->{iconv} = new Text::Iconv(          $self->{iconv} = new Text::Iconv(
# Line 85  sub new { Line 106  sub new {
106                  $c->config->{webpac}->{out_encoding}                  $c->config->{webpac}->{out_encoding}
107          );          );
108    
109          $log->debug("converting encoding from webpac_encoding '" ,          $log->debug("converting encoding from webpac_encoding '" .
110                  $c->config->{webpac}->{webpac_encoding},                  $c->config->{webpac}->{webpac_encoding} .
111                  "' to '",                  "' to '" .
112                  $c->config->{webpac}->{out_encoding},                  $c->config->{webpac}->{out_encoding} .
113                  "'"                  "'"
114          );          );
115    
116    
117          return $self;          return $self;
118    
119  }  }
120    
121    
122    =head2 search
123    
124      my $m->search(
125            phrase => 'query phrase',
126            add_attr => \@add_attr
127            get_attr => [ '@uri' ],
128            max => 42,
129            template => 'result_template.tt',
130            depth => 1,
131      );
132    
133    All fields are standard C<WebPAC::Search::Estraier> parametars except
134    C<template> which will (if specified) return results in HTML using
135    selected template.
136    
137    =cut
138    
139  sub search {  sub search {
140          my ( $self, $query ) = @_;          my $self = shift;
141    
142            my $args = {@_};
143    
144          my $log = $self->{log};          my $log = $self->{log};
145    
146          $log->debug("search model query: -->$query<--");          $log->debug("args: " . Dumper( $args ));
147    
148          my $template_filename = $self->{template};          my $query = $args->{phrase} || $log->warn("no query phrase") && return;
149    
150          my @results = $self->{est}->search(          $log->debug("search model query: '$query'");
151                  query => $query,          if ($args->{add_attr}) {
152                  attr => [ '@uri' ],                  $log->debug(" + add_attr: " .
153                  max => 100,                          join("','", @{ $args->{add_attr} })
154          );                  );
155            }
156    
157            my $template_filename = $args->{template} || $self->{template};
158    
159            $args->{max} ||= $self->{'hits_on_page'};
160            if (! $args->{max}) {
161                    $args->{max} = 10;
162                    $log->warn("max not set when calling model. Using default of 10");
163            }
164    
165            my $times;      # store some times for benchmarking
166    
167            my $t = time();
168    
169            # transfer depth of search
170            if (! $args->{depth}) {
171                    my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration");
172                    $args->{depth} = $default;
173                    $log->warn("using default search depth $default");
174            }
175    
176            my @results = $self->{est}->search( %{ $args } );
177    
178            $times->{est} += time() - $t;
179    
180            my $hits = $#results + 1;
181    
182            $log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) );
183    
184          $log->debug("loading " . ($#results + 1) . " results");          # just return results?
185            return @results unless ($args->{'template'});
186    
187            #
188            # construct HTML results
189            #
190    
191            my @html_results;
192    
193          for my $i ( 0 .. $#results ) {          for my $i ( 0 .. $#results ) {
194    
195                  my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);                  my ($database, $prefix, $id);
196                    if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)#(\d+)$!) {
197                            ($database, $prefix,$id) = ($1,$2,$3);
198                    } else {
199                            $log->warn("can't decode prefix#id from " .  $results[$i]->{'@uri'});
200                            next;
201                    }
202    
203                    #$log->debug("load_ds( id => $id, prefix => '$prefix' )");
204    
205                    $t = time();
206    
207                    my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ) || $log->error("can't load_ds( $database / $prefix # $id )") && next;
208    
209                  # $log->debug("load_ds( $mfn )");                  $times->{db} += time() - $t;
210    
211                  my $ds = $self->{db}->load_ds( $mfn ) || next;                  #$log->debug( "ds = " . Dumper( \@html_results ) );
212                  $results[$i]->{ html } = $self->{iconv}->convert(  
213                  $self->{out}->apply(                  $t = time();
214    
215                    my $html = $self->{out}->apply(
216                          template => $template_filename,                          template => $template_filename,
217                          data => $ds,                          data => $ds,
218                  ) );                          record_uri => "${database}/${prefix}#${id}",
219                    );
220    
221                    $times->{out} += time() - $t;
222    
223                    $t = time();
224    
225                    $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
226    
227                    $times->{iconv} += time() - $t;
228    
229                    push @html_results, $html;
230    
231          }          }
232    
233          return \@results;          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
234    
235            $log->debug( sprintf(
236                    "time spent: db = %.2f, out = %.2f, iconv = %.2f",
237                    $times->{db}, $times->{out}, $times->{iconv},
238            ) );
239    
240            return \@html_results;
241  }  }
242    
243    =head2 record
244    
245              my $html = $m->record(
246            mfn => 42,
247            template => 'foo.tt',
248      );
249    
250    This will load one record, convert it to html using C<template> and return
251    it.
252    
253    =cut
254    
255    sub record {
256            my $self = shift;
257    
258            my $args = {@_};
259            my $log = $self->{log};
260            $log->debug("args: " . Dumper( $args ));
261    
262            foreach my $f (qw/mfn template prefix/) {
263                    $log->fatal("need $f") unless ($args->{$f});
264            }
265    
266            my $mfn = $args->{mfn};
267    
268            my $ds = $self->{db}->load_ds( id => $mfn, prefix => 'FIXME' ) || $log->error("can't load_ds( $mfn )") && return;
269    
270            my $html = $self->{out}->apply(
271                    template => $args->{template},
272                    data => $ds,
273            );
274    
275            $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
276    
277            return $html;
278    }
279    
280    =head2 save_html
281    
282      $m->save_html( '/full/path/to/file', $content );
283    
284    It will use C<Text::Iconv> to convert content encoding back to
285    Webpac codepage, recode JavaScript Unicode entities (%u1234),
286    strip extra newlines at beginning and end, and save to
287    C</full/path/to/file.new> and if that succeeds, just rename
288    it over original file which should be atomic on filesystem level.
289    
290    =cut
291    
292    sub save_html {
293            my ($self, $path, $content) = @_;
294    
295            sub _conv_js {
296                    my $t = shift || return;
297                    return $self->{iconv}->convert(chr(hex($t)));
298            }
299            $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
300            $content =~ s/^[\n\r]+//s;
301            $content =~ s/[\n\r]+$/\n/s;
302    
303            my ($from, $to) = (
304                    $self->{out_encoding},
305                    $self->{webpac_encoding},
306            );
307    
308            $self->{log}->debug("using iconv to convert from $from to $to encoding");
309    
310            my $iconv_on_save = new Text::Iconv($from, $to)
311                    || $self->{log}->fatal("can't create iconv for saving");
312    
313            $content = $iconv_on_save->convert( $content ) || die "no content?";
314    
315            write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!";
316            rename $path . '.new', $path || die "can't rename to $path: $!";
317    }
318    
319    =head2 load_html
320    
321      my $html = $m->load_html('/full/path/to/file');
322    
323    This will convert file from Webpac encoding to Catalyst and
324    convert that data to escaped HTML (for sending into
325    C<< <textarea/> >> tags in html.
326    
327    =cut
328    
329    sub load_html {
330            my ($self, $path) = @_;
331    
332            die "no path?" unless ($path);
333    
334            my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!";
335            #$content = $q->escapeHTML($iconv_utf8->convert($content));
336            $content = $self->{iconv}->convert($content);
337    
338            return $content;
339    }
340    
341  =head1 AUTHOR  =head1 AUTHOR
342    

Legend:
Removed from v.99  
changed lines
  Added in v.237

  ViewVC Help
Powered by ViewVC 1.1.26