8 |
/; |
/; |
9 |
use Data::Dumper; |
use Data::Dumper; |
10 |
use WebPAC::DB; |
use WebPAC::DB; |
11 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT 0.02; |
12 |
use WebPAC::Search::Estraier 0.02; |
use WebPAC::Search::Estraier 0.02; |
13 |
use File::Slurp; |
use File::Slurp; |
14 |
use Time::HiRes; |
use Time::HiRes; |
68 |
|
|
69 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
70 |
|
|
71 |
my $db_path = $c->config->{webpac}->{db_path}; |
# save config parametars in object |
72 |
my $template_path = $c->config->{webpac}->{template_path}; |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding/) { |
73 |
$self->{template_path} = $template_path; |
$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'"); |
$log->debug("using db path '$db_path', template path '$template_path'"); |
81 |
|
|
104 |
"'" |
"'" |
105 |
); |
); |
106 |
|
|
|
# save config parametars in object |
|
|
foreach my $f (qw/hits_on_page/) { |
|
|
$self->{$f} = $c->config->{hyperestraier}->{$f}; |
|
|
$log->debug("self->{$f} = " . $self->{$f}); |
|
|
} |
|
107 |
|
|
108 |
return $self; |
return $self; |
109 |
|
|
110 |
} |
} |
111 |
|
|
|
=head2 iconv_on_save |
|
|
|
|
|
my $out = $m->iconv_on_save( $content ); |
|
|
|
|
|
Convert data saved to disk in Webpac encoding. |
|
|
|
|
|
=cut |
|
|
|
|
|
sub iconv_on_save { |
|
|
my $self = shift; |
|
|
|
|
|
$self->{iconv_save} ||= new Text::Iconv( |
|
|
$self->config->{webpac}->{out_encoding}, |
|
|
$self->config->{webpac}->{webpac_encoding}, |
|
|
); |
|
|
|
|
|
$self->{iconv_save}->convert( @_ ); |
|
|
} |
|
|
|
|
112 |
|
|
113 |
=head2 search |
=head2 search |
114 |
|
|
216 |
return \@html_results; |
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 |
=head2 save_html |
257 |
|
|
258 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
259 |
|
|
260 |
It will use C<iconv_on_save> to convert content encoding back to |
It will use C<Text::Iconv> to convert content encoding back to |
261 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
262 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
263 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
268 |
sub save_html { |
sub save_html { |
269 |
my ($self, $path, $content) = @_; |
my ($self, $path, $content) = @_; |
270 |
|
|
|
$content = $self->iconv_on_save( $content ) || die "no content?"; |
|
|
|
|
271 |
sub _conv_js { |
sub _conv_js { |
272 |
my $t = shift || return; |
my $t = shift || return; |
273 |
return $self->{iconv}->convert(chr(hex($t))); |
return $self->{iconv}->convert(chr(hex($t))); |
276 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
277 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
278 |
|
|
279 |
write_file($path . '.new', $content) || die "can't save ${path}.new $!"; |
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: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
293 |
} |
} |
294 |
|
|
307 |
|
|
308 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
309 |
|
|
310 |
my $content = read_file($path) || die "can't read $path: $!"; |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
311 |
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
312 |
$content = $self->{iconv}->convert($content); |
$content = $self->{iconv}->convert($content); |
313 |
|
|