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.04; |
|
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 |
|
|
40 |
user: 'admin' |
user: 'admin' |
41 |
passwd: 'admin' |
passwd: 'admin' |
42 |
hits_on_page: 100 |
hits_on_page: 100 |
43 |
|
hits_for_pager: 1000 |
44 |
|
|
45 |
webpac: |
webpac: |
46 |
db_path: '/data/webpac2/db' |
db_path: '/data/webpac2/db' |
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 |
|
|
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}); |
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"); |
104 |
|
|
105 |
|
$self->{model_record} = $c->comp('Model::Record') or $log->error("can't find Model::Record"); |
106 |
|
|
107 |
return $self; |
return $self; |
108 |
|
|
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 |
|
|
148 |
|
|
149 |
my $template_filename = $args->{template} || $self->{template}; |
my $template_filename = $args->{template} || $self->{template}; |
150 |
|
|
151 |
$args->{max} ||= $self->{'hits_on_page'}; |
$args->{max} ||= $self->{'hits_for_pager'}; |
152 |
if (! $args->{max}) { |
if (! $args->{max}) { |
153 |
$args->{max} = 10; |
$args->{max} = 100; |
154 |
$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}"); |
155 |
} |
} |
156 |
|
|
157 |
my $times; # store some times for benchmarking |
my $times; # store some times for benchmarking |
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'}); |
185 |
for my $i ( 0 .. $#results ) { |
for my $i ( 0 .. $#results ) { |
186 |
|
|
187 |
my ($database, $prefix, $id); |
my ($database, $prefix, $id); |
188 |
if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)#(\d+)$!) { |
if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { |
189 |
($database, $prefix,$id) = ($1,$2,$3); |
($database, $prefix,$id) = ($1,$2,$3); |
190 |
} else { |
} else { |
191 |
$log->warn("can't decode prefix#id from " . $results[$i]->{'@uri'}); |
$log->warn("can't decode database/prefix/id from " . $results[$i]->{'@uri'}); |
192 |
next; |
next; |
193 |
} |
} |
194 |
|
|
198 |
|
|
199 |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
200 |
if (! $ds) { |
if (! $ds) { |
201 |
$log->error("can't load_ds( ${database}/${prefix}#${id} )"); |
$log->error("can't load_ds( ${database}/${prefix}/${id} )"); |
202 |
next; |
next; |
203 |
} |
} |
204 |
|
|
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 |
); |
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 |
|
|
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; |
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}); |
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}, |
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 |
} |
} |
293 |
|
|
294 |
|
|
295 |
=head2 save_html |
=head2 save_html |
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 |
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; |
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: $!"; |
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 |
|
|