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

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

  ViewVC Help
Powered by ViewVC 1.1.26