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; |
use Time::HiRes; |
15 |
|
use Encode qw/encode decode from_to/; |
16 |
|
use Data::HTMLDumper; |
17 |
|
|
18 |
=head1 NAME |
=head1 NAME |
19 |
|
|
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 |
hits_on_page: 100 |
45 |
|
hits_for_pager: 1000 |
46 |
|
|
47 |
webpac: |
webpac: |
48 |
db_path: '/data/webpac2/db' |
db_path: '/data/webpac2/db' |
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 |
|
|
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 |
|
return unless (@_); |
102 |
|
my $out; |
103 |
|
my $i = 1; |
104 |
|
foreach my $v (@_) { |
105 |
|
$out .= qq{<div id="dump_$i">} . |
106 |
|
Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) . |
107 |
|
qq{</div>}; |
108 |
|
$i++; |
109 |
|
} |
110 |
|
$out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out); |
111 |
|
return $out; |
112 |
|
} |
113 |
|
}, |
114 |
); |
); |
115 |
|
|
116 |
# default template from config.yaml |
# default template from config.yaml |
117 |
$self->{template} ||= $c->config->{webpac}->{template}; |
$self->{template} ||= $c->config->{webpac}->{template}; |
118 |
|
|
|
$self->{iconv} = new Text::Iconv( |
|
|
$c->config->{webpac}->{webpac_encoding}, |
|
|
$c->config->{webpac}->{out_encoding} |
|
|
); |
|
|
|
|
119 |
$log->debug("converting encoding from webpac_encoding '" . |
$log->debug("converting encoding from webpac_encoding '" . |
120 |
$c->config->{webpac}->{webpac_encoding} . |
$c->config->{webpac}->{webpac_encoding} . |
|
"' to '" . |
|
|
$c->config->{webpac}->{out_encoding} . |
|
121 |
"'" |
"'" |
122 |
); |
); |
123 |
|
|
124 |
# save config parametars in object |
$self->{databases} = $c->config->{databases} || $log->error("can't find databases in config"); |
|
foreach my $f (qw/hits_on_page/) { |
|
|
$self->{$_} = $c->config->{$_}; |
|
|
$log->debug("self->{$_} = " . $c->config->{$_}); |
|
|
} |
|
125 |
|
|
126 |
return $self; |
return $self; |
127 |
|
|
128 |
} |
} |
129 |
|
|
|
=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( @_ ); |
|
|
} |
|
|
|
|
130 |
|
|
131 |
=head2 search |
=head2 search |
132 |
|
|
136 |
get_attr => [ '@uri' ], |
get_attr => [ '@uri' ], |
137 |
max => 42, |
max => 42, |
138 |
template => 'result_template.tt', |
template => 'result_template.tt', |
139 |
|
depth => 1, |
140 |
); |
); |
141 |
|
|
142 |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
152 |
|
|
153 |
my $log = $self->{log}; |
my $log = $self->{log}; |
154 |
|
|
155 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("search args: " . Dumper( $args )); |
156 |
|
|
157 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
158 |
|
|
165 |
|
|
166 |
my $template_filename = $args->{template} || $self->{template}; |
my $template_filename = $args->{template} || $self->{template}; |
167 |
|
|
168 |
$args->{max} ||= $self->{'hits_on_page'}; |
$args->{max} ||= $self->{'hits_for_pager'}; |
169 |
if (! $args->{max}) { |
if (! $args->{max}) { |
170 |
$args->{max} = 10; |
$args->{max} = 100; |
171 |
$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}"); |
172 |
} |
} |
173 |
|
|
174 |
my $times; # store some times for benchmarking |
my $times; # store some times for benchmarking |
175 |
|
|
176 |
my $t = time(); |
my $t = time(); |
177 |
|
|
178 |
|
# transfer depth of search |
179 |
|
if (! $args->{depth}) { |
180 |
|
my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration"); |
181 |
|
$args->{depth} = $default; |
182 |
|
$log->warn("using default search depth $default"); |
183 |
|
} |
184 |
|
|
185 |
my @results = $self->{est}->search( %{ $args } ); |
my @results = $self->{est}->search( %{ $args } ); |
186 |
|
|
187 |
$times->{est} += time() - $t; |
$times->{est} += time() - $t; |
201 |
|
|
202 |
for my $i ( 0 .. $#results ) { |
for my $i ( 0 .. $#results ) { |
203 |
|
|
204 |
my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#); |
my ($database, $prefix, $id); |
205 |
|
if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { |
206 |
|
($database, $prefix,$id) = ($1,$2,$3); |
207 |
|
} else { |
208 |
|
$log->warn("can't decode database/prefix/id from " . $results[$i]->{'@uri'}); |
209 |
|
next; |
210 |
|
} |
211 |
|
|
212 |
#$log->debug("load_ds( $mfn )"); |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
213 |
|
|
214 |
$t = time(); |
$t = time(); |
215 |
|
|
216 |
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
217 |
|
if (! $ds) { |
218 |
|
$log->error("can't load_ds( ${database}/${prefix}/${id} )"); |
219 |
|
next; |
220 |
|
} |
221 |
|
|
222 |
$times->{db} += time() - $t; |
$times->{db} += time() - $t; |
223 |
|
|
228 |
my $html = $self->{out}->apply( |
my $html = $self->{out}->apply( |
229 |
template => $template_filename, |
template => $template_filename, |
230 |
data => $ds, |
data => $ds, |
231 |
|
record_uri => "${database}/${prefix}/${id}", |
232 |
|
config => $self->{databases}->{$database}, |
233 |
); |
); |
234 |
|
|
235 |
$times->{out} += time() - $t; |
$times->{out} += time() - $t; |
236 |
|
|
237 |
$t = time(); |
$t = time(); |
238 |
|
|
239 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
|
|
|
|
$times->{iconv} += time() - $t; |
|
240 |
|
|
241 |
push @html_results, $html; |
push @html_results, $html; |
242 |
|
|
245 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
246 |
|
|
247 |
$log->debug( sprintf( |
$log->debug( sprintf( |
248 |
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
"time spent: db = %.2f, out = %.2f", |
249 |
$times->{db}, $times->{out}, $times->{iconv}, |
$times->{db}, $times->{out}, |
250 |
) ); |
) ); |
251 |
|
|
252 |
return \@html_results; |
return \@html_results; |
253 |
} |
} |
254 |
|
|
255 |
|
=head2 record |
256 |
|
|
257 |
|
my $html = $m->record( |
258 |
|
mfn => 42, |
259 |
|
template => 'foo.tt', |
260 |
|
); |
261 |
|
|
262 |
|
This will load one record, convert it to html using C<template> and return |
263 |
|
it. |
264 |
|
|
265 |
|
=cut |
266 |
|
|
267 |
|
sub record { |
268 |
|
my $self = shift; |
269 |
|
|
270 |
|
my $args = {@_}; |
271 |
|
my $log = $self->{log}; |
272 |
|
$log->debug("record args: " . Dumper( $args )); |
273 |
|
|
274 |
|
foreach my $f (qw/record_uri template/) { |
275 |
|
$log->fatal("need $f") unless ($args->{$f}); |
276 |
|
} |
277 |
|
|
278 |
|
my ($database, $prefix, $id); |
279 |
|
|
280 |
|
if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) { |
281 |
|
($database, $prefix, $id) = ($1,$2,$3); |
282 |
|
} else { |
283 |
|
$log->error("can't parse $args->{record_uri} into prefix, database and uri"); |
284 |
|
return; |
285 |
|
} |
286 |
|
|
287 |
|
my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database ); |
288 |
|
if (! $ds) { |
289 |
|
$log->error("can't load_ds( $database/$prefix/$id )"); |
290 |
|
return; |
291 |
|
} |
292 |
|
|
293 |
|
my $html = $self->{out}->apply( |
294 |
|
template => $args->{template}, |
295 |
|
data => $ds, |
296 |
|
record_uri => $args->{record_uri}, |
297 |
|
config => $self->{databases}->{$database}, |
298 |
|
); |
299 |
|
|
300 |
|
$html = decode($self->{webpac_encoding}, $html); |
301 |
|
|
302 |
|
return $html; |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
=head2 save_html |
=head2 save_html |
307 |
|
|
308 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
309 |
|
|
310 |
It will use C<iconv_on_save> to convert content encoding back to |
It will use C<Encode> to convert content encoding back to |
311 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
312 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
313 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
318 |
sub save_html { |
sub save_html { |
319 |
my ($self, $path, $content) = @_; |
my ($self, $path, $content) = @_; |
320 |
|
|
321 |
$content = $self->iconv_on_save( $content ) || die "no content?"; |
# FIXME Should this be UTF-8 or someting? |
322 |
|
my $js_encoding = $self->{webpac_encoding}; |
323 |
|
$js_encoding = 'UTF-16'; |
324 |
|
|
325 |
sub _conv_js { |
sub _conv_js { |
326 |
my $t = shift || return; |
return '0x' . $_[1]; |
327 |
return $self->{iconv}->convert(chr(hex($t))); |
return encode($_[0], chr(hex($_[1]))); |
328 |
} |
} |
329 |
$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; |
330 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
331 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
332 |
|
$content =~ s/\n\r/\n/gs; |
333 |
|
|
334 |
|
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
335 |
|
$self->{log}->debug("convert encoding to $disk_encoding"); |
336 |
|
from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content"); |
337 |
|
|
338 |
write_file($path . '.new', $content) || die "can't save ${path}.new $!"; |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
339 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
340 |
} |
} |
341 |
|
|
354 |
|
|
355 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
356 |
|
|
357 |
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); |
|
358 |
|
|
359 |
return $content; |
return decode($self->{webpac_encoding}, $content); |
360 |
} |
} |
361 |
|
|
362 |
=head1 AUTHOR |
=head1 AUTHOR |
363 |
|
|
364 |
Dobrica Pavlinusic |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
365 |
|
|
366 |
=head1 LICENSE |
=head1 LICENSE |
367 |
|
|