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

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

  ViewVC Help
Powered by ViewVC 1.1.26