/[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 224 by dpavlin, Mon Dec 5 19:15:11 2005 UTC revision 348 by dpavlin, Sat Jan 7 17:34:16 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::Store 0.03;  use WebPAC::Store 0.08;
11  use WebPAC::Output::TT 0.02;  use WebPAC::Output::TT 0.06;
12  use WebPAC::Search::Estraier 0.05;  use WebPAC::Search::Estraier 0.05;
13  use File::Slurp;  use File::Slurp;
14  use Time::HiRes;  use Time::HiRes;
15    use Encode qw/encode decode from_to/;
16    
17  =head1 NAME  =head1 NAME
18    
# Line 40  Configuration for hyperestraier in C<con Line 41  Configuration for hyperestraier in C<con
41    user: 'admin'    user: 'admin'
42    passwd: 'admin'    passwd: 'admin'
43    hits_on_page: 100    hits_on_page: 100
44      hits_for_pager: 1000
45    
46   webpac:   webpac:
47    db_path: '/data/webpac2/db'    db_path: '/data/webpac2/db'
# Line 47  Configuration for hyperestraier in C<con Line 49  Configuration for hyperestraier in C<con
49    template: 'html_ffzg_results_short.tt'    template: 'html_ffzg_results_short.tt'
50    # encoding comming from webpac    # encoding comming from webpac
51    webpac_encoding: 'iso-8859-2'    webpac_encoding: 'iso-8859-2'
   # encoding expected by Catalyst  
   out_encoding: 'UTF-8'  
52    
53  =cut  =cut
54    
# Line 64  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};          $est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding");
68    
69          $log->debug("using config:" . Dumper($est_cfg) );          $log->debug("using config:" . Dumper($est_cfg) );
70    
# Line 77  sub new { Line 77  sub new {
77          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
78    
79          # save config parametars in object          # save config parametars in object
80          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding defaultdepth/) {          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) {
81                  $self->{$f} = $c->config->{hyperestraier}->{$f} ||                  $self->{$f} = $c->config->{hyperestraier}->{$f} ||
82                          $c->config->{webpac}->{$f};                          $c->config->{webpac}->{$f};
83                  $log->debug("self->{$f} = " . $self->{$f});                  $log->debug("self->{$f} = " . $self->{$f});
# Line 101  sub new { Line 101  sub new {
101          # default template from config.yaml          # default template from config.yaml
102          $self->{template} ||= $c->config->{webpac}->{template};          $self->{template} ||= $c->config->{webpac}->{template};
103    
         $self->{iconv} = new Text::Iconv(  
                 $c->config->{webpac}->{webpac_encoding},  
                 $c->config->{webpac}->{out_encoding}  
         );  
   
104          $log->debug("converting encoding from webpac_encoding '" .          $log->debug("converting encoding from webpac_encoding '" .
105                  $c->config->{webpac}->{webpac_encoding} .                  $c->config->{webpac}->{webpac_encoding} .
                 "' to '" .  
                 $c->config->{webpac}->{out_encoding} .  
106                  "'"                  "'"
107          );          );
108    
109            $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");
110    
111          return $self;          return $self;
112    
# Line 156  sub search { Line 150  sub search {
150    
151          my $template_filename = $args->{template} || $self->{template};          my $template_filename = $args->{template} || $self->{template};
152    
153          $args->{max} ||= $self->{'hits_on_page'};          $args->{max} ||= $self->{'hits_for_pager'};
154          if (! $args->{max}) {          if (! $args->{max}) {
155                  $args->{max} = 10;                  $args->{max} = 100;
156                  $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}");
157          }          }
158    
159          my $times;      # store some times for benchmarking          my $times;      # store some times for benchmarking
# Line 192  sub search { Line 186  sub search {
186    
187          for my $i ( 0 .. $#results ) {          for my $i ( 0 .. $#results ) {
188    
189                  my ($id, $prefix);                  my ($database, $prefix, $id);
190                  if ( $results[$i]->{'@uri'} =~ m!/([^/]+)#(\d+)$!) {                  if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) {
191                          ($prefix,$id) = ($1,$2);                          ($database, $prefix,$id) = ($1,$2,$3);
192                  } else {                  } else {
193                          $log->warn("can't decode prefix#id from " .  $results[$i]->{'@uri'});                          $log->warn("can't decode database/prefix/id from " .  $results[$i]->{'@uri'});
194                          next;                          next;
195                  }                  }
196    
# Line 204  sub search { Line 198  sub search {
198    
199                  $t = time();                  $t = time();
200    
201                  my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix ) || $log->error("can't load_ds( id => $id, prefix => '$prefix' )") && next;                  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;                  $times->{db} += time() - $t;
208    
# Line 215  sub search { Line 213  sub search {
213                  my $html = $self->{out}->apply(                  my $html = $self->{out}->apply(
214                          template => $template_filename,                          template => $template_filename,
215                          data => $ds,                          data => $ds,
216                            record_uri => "${database}/${prefix}/${id}",
217                            config => $self->{databases}->{$database},
218                  );                  );
219    
220                  $times->{out} += time() - $t;                  $times->{out} += time() - $t;
221    
222                  $t = time();                  $t = time();
223    
224                  $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");                  $html = decode($self->{webpac_encoding}, $html);
   
                 $times->{iconv} += time() - $t;  
225    
226                  push @html_results, $html;                  push @html_results, $html;
227    
# Line 232  sub search { Line 230  sub search {
230          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
231    
232          $log->debug( sprintf(          $log->debug( sprintf(
233                  "time spent: db = %.2f, out = %.2f, iconv = %.2f",                  "time spent: db = %.2f, out = %.2f",
234                  $times->{db}, $times->{out}, $times->{iconv},                  $times->{db}, $times->{out},
235          ) );          ) );
236    
237          return \@html_results;          return \@html_results;
# Line 258  sub record { Line 256  sub record {
256          my $log = $self->{log};          my $log = $self->{log};
257          $log->debug("args: " . Dumper( $args ));          $log->debug("args: " . Dumper( $args ));
258    
259          foreach my $f (qw/mfn template prefix/) {          foreach my $f (qw/record_uri template/) {
260                  $log->die("need $f") unless ($args->{$f});                  $log->fatal("need $f") unless ($args->{$f});
261          }          }
262    
263          my $mfn = $args->{mfn};          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 => $mfn, prefix => 'FIXME' ) || $log->error("can't load_ds( $mfn )") && next;          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(          my $html = $self->{out}->apply(
279                  template => $args->{template},                  template => $args->{template},
280                  data => $ds,                  data => $ds,
281                    record_uri => $args->{record_uri},
282                    config => $self->{databases}->{$database},
283          );          );
284    
285          $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");          $html = decode($self->{webpac_encoding}, $html);
286    
287          return $html;          return $html;
288  }  }
289    
290    
291  =head2 save_html  =head2 save_html
292    
293    $m->save_html( '/full/path/to/file', $content );    $m->save_html( '/full/path/to/file', $content );
294    
295  It will use C<Text::Iconv> to convert content encoding back to  It will use C<Encode> to convert content encoding back to
296  Webpac codepage, recode JavaScript Unicode entities (%u1234),  Webpac codepage, recode JavaScript Unicode entities (%u1234),
297  strip extra newlines at beginning and end, and save to  strip extra newlines at beginning and end, and save to
298  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 291  it over original file which should be at Line 303  it over original file which should be at
303  sub save_html {  sub save_html {
304          my ($self, $path, $content) = @_;          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 {          sub _conv_js {
311                  my $t = shift || return;                  return '0x' . $_[1];
312                  return $self->{iconv}->convert(chr(hex($t)));                  return encode($_[0], chr(hex($_[1])));
313          }          }
314          $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;
315          $content =~ s/^[\n\r]+//s;          $content =~ s/^[\n\r]+//s;
316          $content =~ s/[\n\r]+$/\n/s;          $content =~ s/[\n\r]+$/\n/s;
317            $content =~ s/\n\r/\n/gs;
318    
319          my ($from, $to) = (          my $disk_encoding = $self->{webpac_encoding} || 'utf-8';
320                  $self->{out_encoding},          $self->{log}->debug("convert encoding to $disk_encoding");
321                  $self->{webpac_encoding},          from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content");
         );  
   
         $self->{log}->debug("using iconv to convert from $from to $to encoding");  
   
         my $iconv_on_save = new Text::Iconv($from, $to)  
                 || $self->{log}->fatal("can't create iconv for saving");  
   
         $content = $iconv_on_save->convert( $content ) || die "no content?";  
322    
323          write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!";          write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!";
324          rename $path . '.new', $path || die "can't rename to $path: $!";          rename $path . '.new', $path || die "can't rename to $path: $!";
# Line 331  sub load_html { Line 340  sub load_html {
340          die "no path?" unless ($path);          die "no path?" unless ($path);
341    
342          my $content = read_file($path, {binmode => ':raw' }) || 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);  
343    
344          return $content;          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.224  
changed lines
  Added in v.348

  ViewVC Help
Powered by ViewVC 1.1.26