/[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 322 by dpavlin, Sun Dec 25 23:31:59 2005 UTC revision 378 by dpavlin, Sat Jan 21 23:27:16 2006 UTC
# Line 6  use lib '/data/webpac2/lib'; Line 6  use lib '/data/webpac2/lib';
6  use base qw/  use base qw/
7          Catalyst::Model          Catalyst::Model
8  /;  /;
 use Data::Dumper;  
9  use WebPAC::Store 0.08;  use WebPAC::Store 0.08;
 use WebPAC::Output::TT 0.06;  
10  use WebPAC::Search::Estraier 0.05;  use WebPAC::Search::Estraier 0.05;
11  use File::Slurp;  use File::Slurp;
12  use Time::HiRes;  use Time::HiRes qw/time/;
13    use Encode qw/encode decode from_to/;
14    use Data::Dumper;
15    
16  =head1 NAME  =head1 NAME
17    
# Line 48  Configuration for hyperestraier in C<con Line 48  Configuration for hyperestraier in C<con
48    template: 'html_ffzg_results_short.tt'    template: 'html_ffzg_results_short.tt'
49    # encoding comming from webpac    # encoding comming from webpac
50    webpac_encoding: 'iso-8859-2'    webpac_encoding: 'iso-8859-2'
   # encoding expected by Catalyst  
   out_encoding: 'UTF-8'  
51    
52  =cut  =cut
53    
# Line 78  sub new { Line 76  sub new {
76          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
77    
78          # save config parametars in object          # save config parametars in object
79          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/) {
80                  $self->{$f} = $c->config->{hyperestraier}->{$f} ||                  $self->{$f} = $c->config->{hyperestraier}->{$f} ||
81                          $c->config->{webpac}->{$f};                          $c->config->{webpac}->{$f};
82                  $log->debug("self->{$f} = " . $self->{$f});                  $log->debug("self->{$f} = " . $self->{$f});
# Line 94  sub new { Line 92  sub new {
92                  database => $est_cfg->{database},                  database => $est_cfg->{database},
93          );          );
94    
         $self->{out} = new WebPAC::Output::TT(  
                 include_path => $template_path,  
                 filters => { foo => sub { shift } },  
         );  
   
95          # default template from config.yaml          # default template from config.yaml
96          $self->{template} ||= $c->config->{webpac}->{template};          $self->{template} ||= $c->config->{webpac}->{template};
97    
         $self->{iconv} = new Text::Iconv(  
                 $c->config->{webpac}->{webpac_encoding},  
                 $c->config->{webpac}->{out_encoding}  
         );  
   
98          $log->debug("converting encoding from webpac_encoding '" .          $log->debug("converting encoding from webpac_encoding '" .
99                  $c->config->{webpac}->{webpac_encoding} .                  $c->config->{webpac}->{webpac_encoding} .
                 "' to '" .  
                 $c->config->{webpac}->{out_encoding} .  
100                  "'"                  "'"
101          );          );
102    
103          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");          $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");
104    
105            $self->{model_record} = $c->comp('Model::Record') or $log->error("can't find Model::Record");
106    
107          return $self;          return $self;
108    
109  }  }
# Line 141  selected template. Line 129  selected template.
129  sub search {  sub search {
130          my $self = shift;          my $self = shift;
131    
132            my $search_start_t = time();
133    
134          my $args = {@_};          my $args = {@_};
135    
136          my $log = $self->{log};          my $log = $self->{log};
137    
138          $log->debug("args: " . Dumper( $args ));          $log->debug("search args: " . Dumper( $args ));
139    
140          my $query = $args->{phrase} || $log->warn("no query phrase") && return;          my $query = $args->{phrase} || $log->warn("no query phrase") && return;
141    
# Line 181  sub search { Line 171  sub search {
171    
172          my $hits = $#results + 1;          my $hits = $#results + 1;
173    
174          $log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) );          $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );
175    
176          # just return results?          # just return results?
177          return @results unless ($args->{'template'});          return @results unless ($args->{'template'});
# Line 218  sub search { Line 208  sub search {
208    
209                  $t = time();                  $t = time();
210    
211                  my $html = $self->{out}->apply(                  my $html = '[no output]';
212                          template => $template_filename,  
213                          data => $ds,                  if ($self->{model_record}) {
214                          record_uri => "${database}/${prefix}/${id}",                          $html = $self->{model_record}->apply(
215                          config => $self->{databases}->{$database},                                  template => $template_filename,
216                  );                                  data => $ds,
217                                    record_uri => "${database}/${prefix}/${id}",
218                                    config => $self->{databases}->{$database},
219                            );
220                    } else {
221                            $log->warn("skipped apply");
222                    }
223    
224                  $times->{out} += time() - $t;                  $times->{out} += time() - $t;
225    
226                  $t = time();                  $t = time();
227    
228                  $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");                  $html = decode($self->{webpac_encoding}, $html);
   
                 $times->{iconv} += time() - $t;  
229    
230                  push @html_results, $html;                  push @html_results, $html;
231    
# Line 240  sub search { Line 234  sub search {
234          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
235    
236          $log->debug( sprintf(          $log->debug( sprintf(
237                  "time spent: db = %.2f, out = %.2f, iconv = %.2f",                  "duration breakdown: store %.6fs, apply %.6fs, total: %.6fs",
238                  $times->{db}, $times->{out}, $times->{iconv},                  $times->{db}, $times->{out}, time() - $search_start_t,
239          ) );          ) );
240    
241          return \@html_results;          return \@html_results;
# Line 264  sub record { Line 258  sub record {
258    
259          my $args = {@_};          my $args = {@_};
260          my $log = $self->{log};          my $log = $self->{log};
261          $log->debug("args: " . Dumper( $args ));          $log->debug("record args: " . Dumper( $args ));
262    
263          foreach my $f (qw/record_uri template/) {          foreach my $f (qw/record_uri template/) {
264                  $log->fatal("need $f") unless ($args->{$f});                  $log->fatal("need $f") unless ($args->{$f});
# Line 285  sub record { Line 279  sub record {
279                  return;                  return;
280          }          }
281    
282          my $html = $self->{out}->apply(          my $html = $self->{model_record}->apply(
283                  template => $args->{template},                  template => $args->{template},
284                  data => $ds,                  data => $ds,
285                  record_uri => $args->{record_uri},                  record_uri => $args->{record_uri},
286                  config => $self->{databases}->{$database},                  config => $self->{databases}->{$database},
287          );          );
288    
289          $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");          $html = decode($self->{webpac_encoding}, $html);
290    
291          return $html;          return $html;
292  }  }
# Line 302  sub record { Line 296  sub record {
296    
297    $m->save_html( '/full/path/to/file', $content );    $m->save_html( '/full/path/to/file', $content );
298    
299  It will use C<Text::Iconv> to convert content encoding back to  It will use C<Encode> to convert content encoding back to
300  Webpac codepage, recode JavaScript Unicode entities (%u1234),  Webpac codepage, recode JavaScript Unicode entities (%u1234),
301  strip extra newlines at beginning and end, and save to  strip extra newlines at beginning and end, and save to
302  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 313  it over original file which should be at Line 307  it over original file which should be at
307  sub save_html {  sub save_html {
308          my ($self, $path, $content) = @_;          my ($self, $path, $content) = @_;
309    
310            # FIXME Should this be UTF-8 or someting?
311            my $js_encoding = $self->{webpac_encoding};
312            $js_encoding = 'UTF-16';
313    
314          sub _conv_js {          sub _conv_js {
315                  my $t = shift || return;                  return '0x' . $_[1];
316                  return $self->{iconv}->convert(chr(hex($t)));                  return encode($_[0], chr(hex($_[1])));
317          }          }
318          $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;
319          $content =~ s/^[\n\r]+//s;          $content =~ s/^[\n\r]+//s;
320          $content =~ s/[\n\r]+$/\n/s;          $content =~ s/[\n\r]+$/\n/s;
321          $content =~ s/\n\r/\n/gs;          $content =~ s/\n\r/\n/gs;
322    
323          my ($from, $to) = (          my $disk_encoding = $self->{webpac_encoding} || 'utf-8';
324                  $self->{out_encoding},          $self->{log}->debug("convert encoding to $disk_encoding");
325                  $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?";  
326    
327          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 $!";
328          rename $path . '.new', $path || die "can't rename to $path: $!";          rename $path . '.new', $path || die "can't rename to $path: $!";
# Line 354  sub load_html { Line 344  sub load_html {
344          die "no path?" unless ($path);          die "no path?" unless ($path);
345    
346          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);  
347    
348          return $content;          return decode($self->{webpac_encoding}, $content);
349  }  }
350    
351  =head1 AUTHOR  =head1 AUTHOR
352    
353  Dobrica Pavlinusic  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>
354    
355  =head1 LICENSE  =head1 LICENSE
356    

Legend:
Removed from v.322  
changed lines
  Added in v.378

  ViewVC Help
Powered by ViewVC 1.1.26