5 |
use lib '/data/webpac2/lib'; |
use lib '/data/webpac2/lib'; |
6 |
use base qw/ |
use base qw/ |
7 |
Catalyst::Model |
Catalyst::Model |
|
WebPAC::Search::Estraier |
|
8 |
/; |
/; |
9 |
use Data::Dumper; |
use Data::Dumper; |
10 |
use WebPAC::DB; |
use WebPAC::DB; |
11 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
12 |
|
use WebPAC::Search::Estraier 0.02; |
13 |
|
use File::Slurp; |
14 |
|
use Time::HiRes; |
15 |
|
|
16 |
=head1 NAME |
=head1 NAME |
17 |
|
|
37 |
url: 'http://localhost:1978/node/webpac2' |
url: 'http://localhost:1978/node/webpac2' |
38 |
user: 'admin' |
user: 'admin' |
39 |
passwd: 'admin' |
passwd: 'admin' |
40 |
|
hits_on_page: 100 |
41 |
|
|
42 |
webpac: |
webpac: |
43 |
db_path: '/data/webpac2/db' |
db_path: '/data/webpac2/db' |
62 |
my $est_cfg = $c->config->{hyperestraier}; |
my $est_cfg = $c->config->{hyperestraier}; |
63 |
$est_cfg->{'log'} = $log; |
$est_cfg->{'log'} = $log; |
64 |
|
|
65 |
|
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding}; |
66 |
|
|
67 |
$log->debug("using config:" . Dumper($est_cfg) ); |
$log->debug("using config:" . Dumper($est_cfg) ); |
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}; |
my $db_path = $c->config->{webpac}->{db_path}; |
72 |
my $template_path = $c->config->{webpac}->{template_path}; |
my $template_path = $c->config->{webpac}->{template_path}; |
73 |
|
$self->{template_path} = $template_path; |
74 |
|
|
75 |
$log->debug("using db path '$db_path', template path '$template_path'"); |
$log->debug("using db path '$db_path', template path '$template_path'"); |
76 |
|
|
84 |
filters => { foo => sub { shift } }, |
filters => { foo => sub { shift } }, |
85 |
); |
); |
86 |
|
|
87 |
|
# default template from config.yaml |
88 |
$self->{template} ||= $c->config->{webpac}->{template}; |
$self->{template} ||= $c->config->{webpac}->{template}; |
89 |
|
|
90 |
$self->{iconv} = new Text::Iconv( |
$self->{iconv} = new Text::Iconv( |
92 |
$c->config->{webpac}->{out_encoding} |
$c->config->{webpac}->{out_encoding} |
93 |
); |
); |
94 |
|
|
95 |
$log->debug("converting encoding from webpac_encoding '" , |
$log->debug("converting encoding from webpac_encoding '" . |
96 |
$c->config->{webpac}->{webpac_encoding}, |
$c->config->{webpac}->{webpac_encoding} . |
97 |
"' to '", |
"' to '" . |
98 |
$c->config->{webpac}->{out_encoding}, |
$c->config->{webpac}->{out_encoding} . |
99 |
"'" |
"'" |
100 |
); |
); |
101 |
|
|
102 |
|
# save config parametars in object |
103 |
|
foreach my $f (qw/hits_on_page/) { |
104 |
|
$self->{$f} = $c->config->{hyperestraier}->{$f}; |
105 |
|
$log->debug("self->{$f} = " . $self->{$f}); |
106 |
|
} |
107 |
|
|
108 |
return $self; |
return $self; |
109 |
|
|
110 |
} |
} |
111 |
|
|
112 |
|
=head2 iconv_on_save |
113 |
|
|
114 |
|
my $out = $m->iconv_on_save( $content ); |
115 |
|
|
116 |
|
Convert data saved to disk in Webpac encoding. |
117 |
|
|
118 |
|
=cut |
119 |
|
|
120 |
|
sub iconv_on_save { |
121 |
|
my $self = shift; |
122 |
|
|
123 |
|
$self->{iconv_save} ||= new Text::Iconv( |
124 |
|
$self->config->{webpac}->{out_encoding}, |
125 |
|
$self->config->{webpac}->{webpac_encoding}, |
126 |
|
); |
127 |
|
|
128 |
|
$self->{iconv_save}->convert( @_ ); |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
=head2 search |
133 |
|
|
134 |
|
my $m->search( |
135 |
|
phrase => 'query phrase', |
136 |
|
add_attr => \@add_attr |
137 |
|
get_attr => [ '@uri' ], |
138 |
|
max => 42, |
139 |
|
template => 'result_template.tt', |
140 |
|
); |
141 |
|
|
142 |
|
All fields are standard C<WebPAC::Search::Estraier> parametars except |
143 |
|
C<template> which will (if specified) return results in HTML using |
144 |
|
selected template. |
145 |
|
|
146 |
|
=cut |
147 |
|
|
148 |
sub search { |
sub search { |
149 |
my ( $self, $query ) = @_; |
my $self = shift; |
150 |
|
|
151 |
|
my $args = {@_}; |
152 |
|
|
153 |
my $log = $self->{log}; |
my $log = $self->{log}; |
154 |
|
|
155 |
$log->debug("search model query: -->$query<--"); |
$log->debug("args: " . Dumper( $args )); |
156 |
|
|
157 |
my $template_filename = $self->{template}; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
158 |
|
|
159 |
my @results = $self->{est}->search( |
$log->debug("search model query: '$query'"); |
160 |
query => $query, |
if ($args->{add_attr}) { |
161 |
attr => [ '@uri' ], |
$log->debug(" + add_attr: " . |
162 |
max => 100, |
join("','", @{ $args->{add_attr} }) |
163 |
); |
); |
164 |
|
} |
165 |
|
|
166 |
|
my $template_filename = $args->{template} || $self->{template}; |
167 |
|
|
168 |
|
$args->{max} ||= $self->{'hits_on_page'}; |
169 |
|
if (! $args->{max}) { |
170 |
|
$args->{max} = 10; |
171 |
|
$log->warn("max not set when calling model. Using default of 10"); |
172 |
|
} |
173 |
|
|
174 |
|
my $times; # store some times for benchmarking |
175 |
|
|
176 |
|
my $t = time(); |
177 |
|
|
178 |
|
my @results = $self->{est}->search( %{ $args } ); |
179 |
|
|
180 |
|
$times->{est} += time() - $t; |
181 |
|
|
182 |
|
my $hits = $#results + 1; |
183 |
|
|
184 |
|
$log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) ); |
185 |
|
|
186 |
|
# just return results? |
187 |
|
return @results unless ($args->{'template'}); |
188 |
|
|
189 |
|
# |
190 |
|
# construct HTML results |
191 |
|
# |
192 |
|
|
193 |
$log->debug("loading " . ($#results + 1) . " results"); |
my @html_results; |
194 |
|
|
195 |
for my $i ( 0 .. $#results ) { |
for my $i ( 0 .. $#results ) { |
196 |
|
|
197 |
my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#); |
my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#); |
198 |
|
|
199 |
# $log->debug("load_ds( $mfn )"); |
#$log->debug("load_ds( $mfn )"); |
200 |
|
|
201 |
my $ds = $self->{db}->load_ds( $mfn ) || next; |
$t = time(); |
202 |
$results[$i]->{ html } = $self->{iconv}->convert( |
|
203 |
$self->{out}->apply( |
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
204 |
|
|
205 |
|
$times->{db} += time() - $t; |
206 |
|
|
207 |
|
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
208 |
|
|
209 |
|
$t = time(); |
210 |
|
|
211 |
|
my $html = $self->{out}->apply( |
212 |
template => $template_filename, |
template => $template_filename, |
213 |
data => $ds, |
data => $ds, |
214 |
) ); |
); |
215 |
|
|
216 |
|
$times->{out} += time() - $t; |
217 |
|
|
218 |
|
$t = time(); |
219 |
|
|
220 |
|
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
221 |
|
|
222 |
|
$times->{iconv} += time() - $t; |
223 |
|
|
224 |
|
push @html_results, $html; |
225 |
|
|
226 |
|
} |
227 |
|
|
228 |
|
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
229 |
|
|
230 |
|
$log->debug( sprintf( |
231 |
|
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
232 |
|
$times->{db}, $times->{out}, $times->{iconv}, |
233 |
|
) ); |
234 |
|
|
235 |
|
return \@html_results; |
236 |
|
} |
237 |
|
|
238 |
|
=head2 save_html |
239 |
|
|
240 |
|
$m->save_html( '/full/path/to/file', $content ); |
241 |
|
|
242 |
|
It will use C<iconv_on_save> to convert content encoding back to |
243 |
|
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
244 |
|
strip extra newlines at beginning and end, and save to |
245 |
|
C</full/path/to/file.new> and if that succeeds, just rename |
246 |
|
it over original file which should be atomic on filesystem level. |
247 |
|
|
248 |
|
=cut |
249 |
|
|
250 |
|
sub save_html { |
251 |
|
my ($self, $path, $content) = @_; |
252 |
|
|
253 |
|
$content = $self->iconv_on_save( $content ) || die "no content?"; |
254 |
|
|
255 |
|
sub _conv_js { |
256 |
|
my $t = shift || return; |
257 |
|
return $self->{iconv}->convert(chr(hex($t))); |
258 |
} |
} |
259 |
|
$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex; |
260 |
|
$content =~ s/^[\n\r]+//s; |
261 |
|
$content =~ s/[\n\r]+$/\n/s; |
262 |
|
|
263 |
return \@results; |
write_file($path . '.new', $content) || die "can't save ${path}.new $!"; |
264 |
|
rename $path . '.new', $path || die "can't rename to $path: $!"; |
265 |
} |
} |
266 |
|
|
267 |
|
=head2 load_html |
268 |
|
|
269 |
|
my $html = $m->load_html('/full/path/to/file'); |
270 |
|
|
271 |
|
This will convert file from Webpac encoding to Catalyst and |
272 |
|
convert that data to escaped HTML (for sending into |
273 |
|
C<< <textarea/> >> tags in html. |
274 |
|
|
275 |
|
=cut |
276 |
|
|
277 |
|
sub load_html { |
278 |
|
my ($self, $path) = @_; |
279 |
|
|
280 |
|
die "no path?" unless ($path); |
281 |
|
|
282 |
|
my $content = read_file($path) || die "can't read $path: $!"; |
283 |
|
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
284 |
|
$content = $self->{iconv}->convert($content); |
285 |
|
|
286 |
|
return $content; |
287 |
|
} |
288 |
|
|
289 |
=head1 AUTHOR |
=head1 AUTHOR |
290 |
|
|