7 |
Catalyst::Model |
Catalyst::Model |
8 |
/; |
/; |
9 |
use Data::Dumper; |
use Data::Dumper; |
10 |
use WebPAC::DB; |
use WebPAC::Store 0.03; |
11 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT 0.02; |
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 |
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 |
); |
); |
117 |
|
|
118 |
} |
} |
119 |
|
|
|
=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 ); |
|
|
} |
|
|
|
|
120 |
|
|
121 |
=head2 search |
=head2 search |
122 |
|
|
126 |
get_attr => [ '@uri' ], |
get_attr => [ '@uri' ], |
127 |
max => 42, |
max => 42, |
128 |
template => 'result_template.tt', |
template => 'result_template.tt', |
129 |
|
depth => 1, |
130 |
); |
); |
131 |
|
|
132 |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
165 |
|
|
166 |
my $t = time(); |
my $t = time(); |
167 |
|
|
168 |
|
# transfer depth of search |
169 |
|
if (! $args->{depth}) { |
170 |
|
my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration"); |
171 |
|
$args->{depth} = $default; |
172 |
|
$log->warn("using default search depth $default"); |
173 |
|
} |
174 |
|
|
175 |
my @results = $self->{est}->search( %{ $args } ); |
my @results = $self->{est}->search( %{ $args } ); |
176 |
|
|
177 |
$times->{est} += time() - $t; |
$times->{est} += time() - $t; |
273 |
|
|
274 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
275 |
|
|
276 |
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 |
277 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
278 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
279 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
292 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
293 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
294 |
|
|
295 |
my $iconv_on_save = new Text::Iconv( |
my ($from, $to) = ( |
296 |
$self->config->{webpac}->{out_encoding}, |
$self->{out_encoding}, |
297 |
$self->config->{webpac}->{webpac_encoding}, |
$self->{webpac_encoding}, |
298 |
); |
); |
|
$self->{log}->debug( "content BEFORE : $content" ); |
|
|
no utf8; |
|
|
$content = $iconv_on_save->convert( $content ) || die "no content?"; |
|
299 |
|
|
300 |
$self->{log}->debug( "content AFTER: $content" ); |
$self->{log}->debug("using iconv to convert from $from to $to encoding"); |
301 |
|
|
302 |
|
my $iconv_on_save = new Text::Iconv($from, $to) |
303 |
|
|| $self->{log}->fatal("can't create iconv for saving"); |
304 |
|
|
305 |
|
$content = $iconv_on_save->convert( $content ) || die "no content?"; |
306 |
|
|
307 |
write_file($path . '.new', $content) || die "can't save ${path}.new $!"; |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
308 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
309 |
} |
} |
310 |
|
|
323 |
|
|
324 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
325 |
|
|
326 |
my $content = read_file($path) || die "can't read $path: $!"; |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
327 |
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
328 |
$content = $self->{iconv}->convert($content); |
$content = $self->{iconv}->convert($content); |
329 |
|
|