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.05; |
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 |
|
|
34 |
|
|
35 |
# configuration for hyper estraier full text search engine |
# configuration for hyper estraier full text search engine |
36 |
hyperestraier: |
hyperestraier: |
37 |
url: 'http://localhost:1978/node/webpac2' |
masterurl: 'http://localhost:1978/node/webpac2' |
38 |
|
defaultnode: 'webpac2' |
39 |
|
defaultdepth: 1 |
40 |
user: 'admin' |
user: 'admin' |
41 |
passwd: 'admin' |
passwd: 'admin' |
42 |
hits_on_page: 100 |
hits_on_page: 100 |
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 |
|
|
71 |
|
if (! $est_cfg->{database}) { |
72 |
|
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
73 |
|
$log->info("using default node $defaultnode"); |
74 |
|
$est_cfg->{database} = $defaultnode; |
75 |
|
} |
76 |
|
|
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/) { |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_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}); |
87 |
|
|
88 |
$log->debug("using db path '$db_path', template path '$template_path'"); |
$log->debug("using db path '$db_path', template path '$template_path'"); |
89 |
|
|
90 |
$self->{db} = new WebPAC::DB( |
$self->{db} = new WebPAC::Store( |
91 |
path => $db_path, |
path => $db_path, |
92 |
read_only => 1, |
read_only => 1, |
93 |
|
database => $est_cfg->{database}, |
94 |
); |
); |
95 |
|
|
96 |
$self->{out} = new WebPAC::Output::TT( |
$self->{out} = new WebPAC::Output::TT( |
118 |
|
|
119 |
} |
} |
120 |
|
|
|
=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; |
|
|
my $content = shift || return; |
|
|
|
|
|
$self->{iconv_save} ||= new Text::Iconv( |
|
|
$self->config->{webpac}->{out_encoding}, |
|
|
$self->config->{webpac}->{webpac_encoding}, |
|
|
); |
|
|
|
|
|
return $self->{iconv_save}->convert( $content ); |
|
|
} |
|
|
|
|
121 |
|
|
122 |
=head2 search |
=head2 search |
123 |
|
|
127 |
get_attr => [ '@uri' ], |
get_attr => [ '@uri' ], |
128 |
max => 42, |
max => 42, |
129 |
template => 'result_template.tt', |
template => 'result_template.tt', |
130 |
|
depth => 1, |
131 |
); |
); |
132 |
|
|
133 |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
166 |
|
|
167 |
my $t = time(); |
my $t = time(); |
168 |
|
|
169 |
|
# transfer depth of search |
170 |
|
if (! $args->{depth}) { |
171 |
|
my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration"); |
172 |
|
$args->{depth} = $default; |
173 |
|
$log->warn("using default search depth $default"); |
174 |
|
} |
175 |
|
|
176 |
my @results = $self->{est}->search( %{ $args } ); |
my @results = $self->{est}->search( %{ $args } ); |
177 |
|
|
178 |
$times->{est} += time() - $t; |
$times->{est} += time() - $t; |
192 |
|
|
193 |
for my $i ( 0 .. $#results ) { |
for my $i ( 0 .. $#results ) { |
194 |
|
|
195 |
my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#); |
my ($database, $prefix, $id); |
196 |
|
if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)#(\d+)$!) { |
197 |
|
($database, $prefix,$id) = ($1,$2,$3); |
198 |
|
} else { |
199 |
|
$log->warn("can't decode prefix#id from " . $results[$i]->{'@uri'}); |
200 |
|
next; |
201 |
|
} |
202 |
|
|
203 |
#$log->debug("load_ds( $mfn )"); |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
204 |
|
|
205 |
$t = time(); |
$t = time(); |
206 |
|
|
207 |
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 ); |
208 |
|
if (! $ds) { |
209 |
|
$log->error("can't load_ds( ${database}/${prefix}#${id} )"); |
210 |
|
next; |
211 |
|
} |
212 |
|
|
213 |
$times->{db} += time() - $t; |
$times->{db} += time() - $t; |
214 |
|
|
219 |
my $html = $self->{out}->apply( |
my $html = $self->{out}->apply( |
220 |
template => $template_filename, |
template => $template_filename, |
221 |
data => $ds, |
data => $ds, |
222 |
|
record_uri => "${database}/${prefix}/${id}", |
223 |
); |
); |
224 |
|
|
225 |
$times->{out} += time() - $t; |
$times->{out} += time() - $t; |
263 |
my $log = $self->{log}; |
my $log = $self->{log}; |
264 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("args: " . Dumper( $args )); |
265 |
|
|
266 |
foreach my $f (qw/mfn template/) { |
foreach my $f (qw/record_uri template/) { |
267 |
$log->die("need $f") unless ($args->{$f}); |
$log->fatal("need $f") unless ($args->{$f}); |
268 |
} |
} |
269 |
|
|
270 |
my $mfn = $args->{mfn}; |
my ($database, $prefix, $id); |
271 |
|
|
272 |
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) { |
273 |
|
($database, $prefix, $id) = ($1,$2,$3); |
274 |
|
} else { |
275 |
|
$log->error("can't parse $args->{record_uri} into prefix, database and uri"); |
276 |
|
return; |
277 |
|
} |
278 |
|
|
279 |
|
my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database ); |
280 |
|
if (! $ds) { |
281 |
|
$log->error("can't load_ds( $database/$prefix/$id )"); |
282 |
|
return; |
283 |
|
} |
284 |
|
|
285 |
my $html = $self->{out}->apply( |
my $html = $self->{out}->apply( |
286 |
template => $args->{template}, |
template => $args->{template}, |
287 |
data => $ds, |
data => $ds, |
288 |
|
record_uri => $args->{record_uri}, |
289 |
); |
); |
290 |
|
|
291 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
297 |
|
|
298 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
299 |
|
|
300 |
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 |
301 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
302 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
303 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
315 |
$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex; |
$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex; |
316 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
317 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
318 |
|
$content =~ s/\n\r/\n/gs; |
319 |
|
|
320 |
my $iconv_on_save = new Text::Iconv( |
my ($from, $to) = ( |
321 |
$self->config->{webpac}->{out_encoding}, |
$self->{out_encoding}, |
322 |
$self->config->{webpac}->{webpac_encoding}, |
$self->{webpac_encoding}, |
323 |
); |
); |
|
$self->{log}->debug( "content BEFORE : $content" ); |
|
|
no utf8; |
|
|
$content = $iconv_on_save->convert( $content ) || die "no content?"; |
|
324 |
|
|
325 |
$self->{log}->debug( "content AFTER: $content" ); |
$self->{log}->debug("using iconv to convert from $from to $to encoding"); |
326 |
|
|
327 |
|
my $iconv_on_save = new Text::Iconv($from, $to) |
328 |
|
|| $self->{log}->fatal("can't create iconv for saving"); |
329 |
|
|
330 |
|
$content = $iconv_on_save->convert( $content ) || die "no content?"; |
331 |
|
|
332 |
write_file($path . '.new', $content) || die "can't save ${path}.new $!"; |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
333 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
334 |
} |
} |
335 |
|
|
348 |
|
|
349 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
350 |
|
|
351 |
my $content = read_file($path) || die "can't read $path: $!"; |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
352 |
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
353 |
$content = $self->{iconv}->convert($content); |
$content = $self->{iconv}->convert($content); |
354 |
|
|