/[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 348 by dpavlin, Sat Jan 7 17:34:16 2006 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::Store 0.08;
11    use WebPAC::Output::TT 0.06;
12    use WebPAC::Search::Estraier 0.05;
13    use File::Slurp;
14    use Time::HiRes;
15    use Encode qw/encode decode from_to/;
16    
17  =head1 NAME  =head1 NAME
18    
# Line 28  Configuration for hyperestraier in C<con Line 35  Configuration for hyperestraier in C<con
35    
36   # configuration for hyper estraier full text search engine   # configuration for hyper estraier full text search engine
37   hyperestraier:   hyperestraier:
38      url: 'http://localhost:1978/node/webpac2'    masterurl: 'http://localhost:1978/node/webpac2'
39      user: 'admin'    defaultnode: 'webpac2'
40      passwd: 'admin'    defaultdepth: 1
41      user: 'admin'
42      passwd: 'admin'
43      hits_on_page: 100
44      hits_for_pager: 1000
45    
46     webpac:
47      db_path: '/data/webpac2/db'
48      template_path: '/data/webpac2/conf/output/tt'
49      template: 'html_ffzg_results_short.tt'
50      # encoding comming from webpac
51      webpac_encoding: 'iso-8859-2'
52    
53  =cut  =cut
54    
# Line 41  sub new { Line 59  sub new {
59          $self->config($config);          $self->config($config);
60    
61          my $log = $c->log;          my $log = $c->log;
62            $self->{log} = $log;
63    
64  #       if (! $c->stash->{est}) {          my $est_cfg = $c->config->{hyperestraier};
65            $est_cfg->{'log'} = $log;
66    
67                  my $est_cfg = $c->config->{hyperestraier};          $est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding");
                 $est_cfg->{'log'} = $log;  
68    
69                  $log->debug("using config:" . Dumper($est_cfg) );          $log->debug("using config:" . Dumper($est_cfg) );
70    
71  #               $c->stash->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          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 } );
78    
79            # save config parametars in object
80            foreach my $f (qw/db_path template_path hits_on_page webpac_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'");
89    
90            $self->{db} = new WebPAC::Store(
91                    path => $db_path,
92                    read_only => 1,
93                    database => $est_cfg->{database},
94            );
95    
96            $self->{out} = new WebPAC::Output::TT(
97                    include_path => $template_path,
98                    filters => { foo => sub { shift } },
99            );
100    
101            # default template from config.yaml
102            $self->{template} ||= $c->config->{webpac}->{template};
103    
104            $log->debug("converting encoding from webpac_encoding '" .
105                    $c->config->{webpac}->{webpac_encoding} .
106                    "'"
107            );
108    
109  #       $log->debug("param: " . Dumper($c->req->params));          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");
   
 #       $c->stash->{est}->search(  
 #               query => $c->req->params->{Title},  
 #               max => 100,  
 #       );  
110    
111          return $self;          return $self;
112    
113  }  }
114    
115    
116    =head2 search
117    
118      my $m->search(
119            phrase => 'query phrase',
120            add_attr => \@add_attr
121            get_attr => [ '@uri' ],
122            max => 42,
123            template => 'result_template.tt',
124            depth => 1,
125      );
126    
127    All fields are standard C<WebPAC::Search::Estraier> parametars except
128    C<template> which will (if specified) return results in HTML using
129    selected template.
130    
131    =cut
132    
133    sub search {
134            my $self = shift;
135    
136            my $args = {@_};
137    
138            my $log = $self->{log};
139    
140            $log->debug("args: " . Dumper( $args ));
141    
142            my $query = $args->{phrase} || $log->warn("no query phrase") && return;
143    
144            $log->debug("search model query: '$query'");
145            if ($args->{add_attr}) {
146                    $log->debug(" + add_attr: " .
147                            join("','", @{ $args->{add_attr} })
148                    );
149            }
150    
151            my $template_filename = $args->{template} || $self->{template};
152    
153            $args->{max} ||= $self->{'hits_for_pager'};
154            if (! $args->{max}) {
155                    $args->{max} = 100;
156                    $log->warn("max not set when calling model. Using default of $args->{max}");
157            }
158    
159            my $times;      # store some times for benchmarking
160    
161            my $t = time();
162    
163            # transfer depth of search
164            if (! $args->{depth}) {
165                    my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration");
166                    $args->{depth} = $default;
167                    $log->warn("using default search depth $default");
168            }
169    
170            my @results = $self->{est}->search( %{ $args } );
171    
172            $times->{est} += time() - $t;
173    
174            my $hits = $#results + 1;
175    
176            $log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) );
177    
178            # just return results?
179            return @results unless ($args->{'template'});
180    
181            #
182            # construct HTML results
183            #
184    
185            my @html_results;
186    
187            for my $i ( 0 .. $#results ) {
188    
189                    my ($database, $prefix, $id);
190                    if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) {
191                            ($database, $prefix,$id) = ($1,$2,$3);
192                    } else {
193                            $log->warn("can't decode database/prefix/id from " .  $results[$i]->{'@uri'});
194                            next;
195                    }
196    
197                    #$log->debug("load_ds( id => $id, prefix => '$prefix' )");
198    
199                    $t = time();
200    
201                    my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id );
202                    if (! $ds) {
203                            $log->error("can't load_ds( ${database}/${prefix}/${id} )");
204                            next;
205                    }
206    
207                    $times->{db} += time() - $t;
208    
209                    #$log->debug( "ds = " . Dumper( \@html_results ) );
210    
211                    $t = time();
212    
213                    my $html = $self->{out}->apply(
214                            template => $template_filename,
215                            data => $ds,
216                            record_uri => "${database}/${prefix}/${id}",
217                            config => $self->{databases}->{$database},
218                    );
219    
220                    $times->{out} += time() - $t;
221    
222                    $t = time();
223    
224                    $html = decode($self->{webpac_encoding}, $html);
225    
226                    push @html_results, $html;
227    
228            }
229    
230            #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
231    
232            $log->debug( sprintf(
233                    "time spent: db = %.2f, out = %.2f",
234                    $times->{db}, $times->{out},
235            ) );
236    
237            return \@html_results;
238    }
239    
240    =head2 record
241    
242      my $html = $m->record(
243            mfn => 42,
244            template => 'foo.tt',
245      );
246    
247    This will load one record, convert it to html using C<template> and return
248    it.
249    
250    =cut
251    
252    sub record {
253            my $self = shift;
254    
255            my $args = {@_};
256            my $log = $self->{log};
257            $log->debug("args: " . Dumper( $args ));
258    
259            foreach my $f (qw/record_uri template/) {
260                    $log->fatal("need $f") unless ($args->{$f});
261            }
262    
263            my ($database, $prefix, $id);
264    
265            if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) {
266                    ($database, $prefix, $id) = ($1,$2,$3);
267            } else {
268                    $log->error("can't parse $args->{record_uri} into prefix, database and uri");
269                    return;
270            }
271    
272            my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database );
273            if (! $ds) {
274                    $log->error("can't load_ds( $database/$prefix/$id )");
275                    return;
276            }
277    
278            my $html = $self->{out}->apply(
279                    template => $args->{template},
280                    data => $ds,
281                    record_uri => $args->{record_uri},
282                    config => $self->{databases}->{$database},
283            );
284    
285            $html = decode($self->{webpac_encoding}, $html);
286    
287            return $html;
288    }
289    
290    
291    =head2 save_html
292    
293      $m->save_html( '/full/path/to/file', $content );
294    
295    It will use C<Encode> to convert content encoding back to
296    Webpac codepage, recode JavaScript Unicode entities (%u1234),
297    strip extra newlines at beginning and end, and save to
298    C</full/path/to/file.new> and if that succeeds, just rename
299    it over original file which should be atomic on filesystem level.
300    
301    =cut
302    
303    sub save_html {
304            my ($self, $path, $content) = @_;
305    
306            # FIXME Should this be UTF-8 or someting?
307            my $js_encoding = $self->{webpac_encoding};
308            $js_encoding = 'UTF-16';
309    
310            sub _conv_js {
311                    return '0x' . $_[1];
312                    return encode($_[0], chr(hex($_[1])));
313            }
314            #$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex;
315            $content =~ s/^[\n\r]+//s;
316            $content =~ s/[\n\r]+$/\n/s;
317            $content =~ s/\n\r/\n/gs;
318    
319            my $disk_encoding = $self->{webpac_encoding} || 'utf-8';
320            $self->{log}->debug("convert encoding to $disk_encoding");
321            from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content");
322    
323            write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!";
324            rename $path . '.new', $path || die "can't rename to $path: $!";
325    }
326    
327    =head2 load_html
328    
329      my $html = $m->load_html('/full/path/to/file');
330    
331    This will convert file from Webpac encoding to Catalyst and
332    convert that data to escaped HTML (for sending into
333    C<< <textarea/> >> tags in html.
334    
335    =cut
336    
337    sub load_html {
338            my ($self, $path) = @_;
339    
340            die "no path?" unless ($path);
341    
342            my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!";
343    
344            return decode($self->{webpac_encoding}, $content);
345    }
346    
347  =head1 AUTHOR  =head1 AUTHOR
348    
349  Dobrica Pavlinusic  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>
350    
351  =head1 LICENSE  =head1 LICENSE
352    

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

  ViewVC Help
Powered by ViewVC 1.1.26