1 |
dpavlin |
92 |
package Webpacus::Model::WebPAC; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
use lib '/data/webpac2/lib'; |
6 |
dpavlin |
93 |
use base qw/ |
7 |
|
|
Catalyst::Model |
8 |
|
|
/; |
9 |
dpavlin |
92 |
use Data::Dumper; |
10 |
dpavlin |
95 |
use WebPAC::DB; |
11 |
|
|
use WebPAC::Output::TT; |
12 |
dpavlin |
116 |
use WebPAC::Search::Estraier 0.02; |
13 |
dpavlin |
135 |
use File::Slurp; |
14 |
dpavlin |
155 |
use Time::HiRes; |
15 |
dpavlin |
92 |
|
16 |
|
|
=head1 NAME |
17 |
|
|
|
18 |
|
|
Webpacus::Model::WebPAC - Catalyst Model |
19 |
|
|
|
20 |
|
|
=head1 SYNOPSIS |
21 |
|
|
|
22 |
|
|
See L<Webpacus> and L<WebPAC>. |
23 |
|
|
|
24 |
|
|
=head1 DESCRIPTION |
25 |
|
|
|
26 |
|
|
Catalyst Model for access to WebPAC data. |
27 |
|
|
|
28 |
|
|
=head2 new |
29 |
|
|
|
30 |
|
|
Configuration for hyperestraier in C<config.yaml> like this: |
31 |
|
|
|
32 |
|
|
--- #YAML:1.0 |
33 |
|
|
# DO NOT USE TABS FOR INDENTATION OR label/value SEPARATION!!! |
34 |
|
|
|
35 |
|
|
# configuration for hyper estraier full text search engine |
36 |
|
|
hyperestraier: |
37 |
dpavlin |
96 |
url: 'http://localhost:1978/node/webpac2' |
38 |
|
|
user: 'admin' |
39 |
|
|
passwd: 'admin' |
40 |
dpavlin |
143 |
hits_on_page: 100 |
41 |
dpavlin |
92 |
|
42 |
dpavlin |
96 |
webpac: |
43 |
|
|
db_path: '/data/webpac2/db' |
44 |
|
|
template_path: '/data/webpac2/conf/output/tt' |
45 |
|
|
template: 'html_ffzg_results_short.tt' |
46 |
|
|
# encoding comming from webpac |
47 |
|
|
webpac_encoding: 'iso-8859-2' |
48 |
|
|
# encoding expected by Catalyst |
49 |
|
|
out_encoding: 'UTF-8' |
50 |
|
|
|
51 |
dpavlin |
92 |
=cut |
52 |
|
|
|
53 |
|
|
sub new { |
54 |
|
|
my ( $self, $c, $config ) = @_; |
55 |
|
|
|
56 |
|
|
$self = $self->NEXT::new($c, $config); |
57 |
|
|
$self->config($config); |
58 |
|
|
|
59 |
|
|
my $log = $c->log; |
60 |
dpavlin |
94 |
$self->{log} = $log; |
61 |
dpavlin |
92 |
|
62 |
dpavlin |
93 |
my $est_cfg = $c->config->{hyperestraier}; |
63 |
|
|
$est_cfg->{'log'} = $log; |
64 |
dpavlin |
92 |
|
65 |
dpavlin |
142 |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding}; |
66 |
|
|
|
67 |
dpavlin |
93 |
$log->debug("using config:" . Dumper($est_cfg) ); |
68 |
dpavlin |
92 |
|
69 |
dpavlin |
93 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
70 |
dpavlin |
92 |
|
71 |
dpavlin |
167 |
# save config parametars in object |
72 |
|
|
foreach my $f (qw/db_path template_path hits_on_page/) { |
73 |
|
|
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
74 |
|
|
$c->config->{webpac}->{$f}; |
75 |
|
|
$log->debug("self->{$f} = " . $self->{$f}); |
76 |
|
|
} |
77 |
|
|
my $db_path = $self->{db_path}; |
78 |
|
|
my $template_path = $self->{template_path}; |
79 |
dpavlin |
95 |
|
80 |
|
|
$log->debug("using db path '$db_path', template path '$template_path'"); |
81 |
|
|
|
82 |
|
|
$self->{db} = new WebPAC::DB( |
83 |
|
|
path => $db_path, |
84 |
|
|
read_only => 1, |
85 |
|
|
); |
86 |
|
|
|
87 |
|
|
$self->{out} = new WebPAC::Output::TT( |
88 |
|
|
include_path => $template_path, |
89 |
|
|
filters => { foo => sub { shift } }, |
90 |
|
|
); |
91 |
|
|
|
92 |
dpavlin |
101 |
# default template from config.yaml |
93 |
dpavlin |
95 |
$self->{template} ||= $c->config->{webpac}->{template}; |
94 |
|
|
|
95 |
dpavlin |
96 |
$self->{iconv} = new Text::Iconv( |
96 |
|
|
$c->config->{webpac}->{webpac_encoding}, |
97 |
|
|
$c->config->{webpac}->{out_encoding} |
98 |
|
|
); |
99 |
|
|
|
100 |
dpavlin |
100 |
$log->debug("converting encoding from webpac_encoding '" . |
101 |
|
|
$c->config->{webpac}->{webpac_encoding} . |
102 |
|
|
"' to '" . |
103 |
|
|
$c->config->{webpac}->{out_encoding} . |
104 |
dpavlin |
99 |
"'" |
105 |
|
|
); |
106 |
dpavlin |
96 |
|
107 |
dpavlin |
155 |
|
108 |
dpavlin |
92 |
return $self; |
109 |
|
|
|
110 |
|
|
} |
111 |
|
|
|
112 |
dpavlin |
135 |
=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 |
dpavlin |
178 |
my $content = shift || return; |
123 |
dpavlin |
135 |
|
124 |
|
|
$self->{iconv_save} ||= new Text::Iconv( |
125 |
|
|
$self->config->{webpac}->{out_encoding}, |
126 |
|
|
$self->config->{webpac}->{webpac_encoding}, |
127 |
|
|
); |
128 |
|
|
|
129 |
dpavlin |
178 |
return $self->{iconv_save}->convert( $content ); |
130 |
dpavlin |
135 |
} |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
=head2 search |
134 |
|
|
|
135 |
dpavlin |
150 |
my $m->search( |
136 |
|
|
phrase => 'query phrase', |
137 |
dpavlin |
155 |
add_attr => \@add_attr |
138 |
|
|
get_attr => [ '@uri' ], |
139 |
|
|
max => 42, |
140 |
dpavlin |
150 |
template => 'result_template.tt', |
141 |
|
|
); |
142 |
dpavlin |
135 |
|
143 |
dpavlin |
155 |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
144 |
|
|
C<template> which will (if specified) return results in HTML using |
145 |
|
|
selected template. |
146 |
|
|
|
147 |
dpavlin |
135 |
=cut |
148 |
|
|
|
149 |
dpavlin |
93 |
sub search { |
150 |
dpavlin |
150 |
my $self = shift; |
151 |
dpavlin |
93 |
|
152 |
dpavlin |
150 |
my $args = {@_}; |
153 |
|
|
|
154 |
dpavlin |
95 |
my $log = $self->{log}; |
155 |
dpavlin |
94 |
|
156 |
dpavlin |
150 |
$log->debug("args: " . Dumper( $args )); |
157 |
dpavlin |
95 |
|
158 |
dpavlin |
150 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
159 |
dpavlin |
95 |
|
160 |
dpavlin |
155 |
$log->debug("search model query: '$query'"); |
161 |
|
|
if ($args->{add_attr}) { |
162 |
|
|
$log->debug(" + add_attr: " . |
163 |
|
|
join("','", @{ $args->{add_attr} }) |
164 |
|
|
); |
165 |
|
|
} |
166 |
dpavlin |
150 |
|
167 |
|
|
my $template_filename = $args->{template} || $self->{template}; |
168 |
|
|
|
169 |
dpavlin |
155 |
$args->{max} ||= $self->{'hits_on_page'}; |
170 |
|
|
if (! $args->{max}) { |
171 |
|
|
$args->{max} = 10; |
172 |
|
|
$log->warn("max not set when calling model. Using default of 10"); |
173 |
|
|
} |
174 |
dpavlin |
93 |
|
175 |
dpavlin |
155 |
my $times; # store some times for benchmarking |
176 |
|
|
|
177 |
|
|
my $t = time(); |
178 |
|
|
|
179 |
|
|
my @results = $self->{est}->search( %{ $args } ); |
180 |
|
|
|
181 |
|
|
$times->{est} += time() - $t; |
182 |
|
|
|
183 |
dpavlin |
150 |
my $hits = $#results + 1; |
184 |
dpavlin |
99 |
|
185 |
dpavlin |
155 |
$log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) ); |
186 |
dpavlin |
150 |
|
187 |
dpavlin |
155 |
# just return results? |
188 |
|
|
return @results unless ($args->{'template'}); |
189 |
|
|
|
190 |
|
|
# |
191 |
|
|
# construct HTML results |
192 |
|
|
# |
193 |
|
|
|
194 |
dpavlin |
100 |
my @html_results; |
195 |
|
|
|
196 |
dpavlin |
95 |
for my $i ( 0 .. $#results ) { |
197 |
|
|
|
198 |
|
|
my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#); |
199 |
|
|
|
200 |
dpavlin |
115 |
#$log->debug("load_ds( $mfn )"); |
201 |
dpavlin |
95 |
|
202 |
dpavlin |
155 |
$t = time(); |
203 |
|
|
|
204 |
dpavlin |
115 |
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
205 |
dpavlin |
155 |
|
206 |
|
|
$times->{db} += time() - $t; |
207 |
|
|
|
208 |
dpavlin |
115 |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
209 |
|
|
|
210 |
dpavlin |
155 |
$t = time(); |
211 |
|
|
|
212 |
dpavlin |
100 |
my $html = $self->{out}->apply( |
213 |
dpavlin |
95 |
template => $template_filename, |
214 |
|
|
data => $ds, |
215 |
dpavlin |
100 |
); |
216 |
|
|
|
217 |
dpavlin |
155 |
$times->{out} += time() - $t; |
218 |
|
|
|
219 |
|
|
$t = time(); |
220 |
|
|
|
221 |
dpavlin |
100 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
222 |
|
|
|
223 |
dpavlin |
155 |
$times->{iconv} += time() - $t; |
224 |
|
|
|
225 |
dpavlin |
100 |
push @html_results, $html; |
226 |
|
|
|
227 |
dpavlin |
95 |
} |
228 |
|
|
|
229 |
dpavlin |
115 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
230 |
|
|
|
231 |
dpavlin |
155 |
$log->debug( sprintf( |
232 |
|
|
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
233 |
|
|
$times->{db}, $times->{out}, $times->{iconv}, |
234 |
|
|
) ); |
235 |
|
|
|
236 |
dpavlin |
100 |
return \@html_results; |
237 |
dpavlin |
93 |
} |
238 |
|
|
|
239 |
dpavlin |
165 |
=head2 record |
240 |
|
|
|
241 |
|
|
my $html = $m->record( |
242 |
|
|
mfn => 42, |
243 |
|
|
template => 'foo.tt', |
244 |
|
|
); |
245 |
|
|
|
246 |
|
|
This will load one record, convert it to html using C<template> and return |
247 |
|
|
it. |
248 |
|
|
|
249 |
|
|
=cut |
250 |
|
|
|
251 |
|
|
sub record { |
252 |
|
|
my $self = shift; |
253 |
|
|
|
254 |
|
|
my $args = {@_}; |
255 |
|
|
my $log = $self->{log}; |
256 |
|
|
$log->debug("args: " . Dumper( $args )); |
257 |
|
|
|
258 |
|
|
foreach my $f (qw/mfn template/) { |
259 |
|
|
$log->die("need $f") unless ($args->{$f}); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
my $mfn = $args->{mfn}; |
263 |
|
|
|
264 |
|
|
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
265 |
|
|
|
266 |
|
|
my $html = $self->{out}->apply( |
267 |
|
|
template => $args->{template}, |
268 |
|
|
data => $ds, |
269 |
|
|
); |
270 |
|
|
|
271 |
|
|
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
272 |
|
|
|
273 |
|
|
return $html; |
274 |
|
|
} |
275 |
|
|
|
276 |
dpavlin |
135 |
=head2 save_html |
277 |
dpavlin |
93 |
|
278 |
dpavlin |
135 |
$m->save_html( '/full/path/to/file', $content ); |
279 |
dpavlin |
93 |
|
280 |
dpavlin |
135 |
It will use C<iconv_on_save> to convert content encoding back to |
281 |
|
|
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
282 |
|
|
strip extra newlines at beginning and end, and save to |
283 |
|
|
C</full/path/to/file.new> and if that succeeds, just rename |
284 |
|
|
it over original file which should be atomic on filesystem level. |
285 |
|
|
|
286 |
|
|
=cut |
287 |
|
|
|
288 |
|
|
sub save_html { |
289 |
|
|
my ($self, $path, $content) = @_; |
290 |
|
|
|
291 |
|
|
sub _conv_js { |
292 |
|
|
my $t = shift || return; |
293 |
|
|
return $self->{iconv}->convert(chr(hex($t))); |
294 |
|
|
} |
295 |
|
|
$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex; |
296 |
|
|
$content =~ s/^[\n\r]+//s; |
297 |
|
|
$content =~ s/[\n\r]+$/\n/s; |
298 |
|
|
|
299 |
dpavlin |
178 |
my $iconv_on_save = new Text::Iconv( |
300 |
|
|
$self->config->{webpac}->{out_encoding}, |
301 |
|
|
$self->config->{webpac}->{webpac_encoding}, |
302 |
|
|
); |
303 |
|
|
$self->{log}->debug( "content BEFORE : $content" ); |
304 |
|
|
no utf8; |
305 |
|
|
$content = $iconv_on_save->convert( $content ) || die "no content?"; |
306 |
|
|
|
307 |
|
|
$self->{log}->debug( "content AFTER: $content" ); |
308 |
|
|
|
309 |
dpavlin |
135 |
write_file($path . '.new', $content) || die "can't save ${path}.new $!"; |
310 |
|
|
rename $path . '.new', $path || die "can't rename to $path: $!"; |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
=head2 load_html |
314 |
|
|
|
315 |
|
|
my $html = $m->load_html('/full/path/to/file'); |
316 |
|
|
|
317 |
|
|
This will convert file from Webpac encoding to Catalyst and |
318 |
|
|
convert that data to escaped HTML (for sending into |
319 |
|
|
C<< <textarea/> >> tags in html. |
320 |
|
|
|
321 |
|
|
=cut |
322 |
|
|
|
323 |
|
|
sub load_html { |
324 |
|
|
my ($self, $path) = @_; |
325 |
|
|
|
326 |
|
|
die "no path?" unless ($path); |
327 |
|
|
|
328 |
|
|
my $content = read_file($path) || die "can't read $path: $!"; |
329 |
|
|
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
330 |
|
|
$content = $self->{iconv}->convert($content); |
331 |
|
|
|
332 |
|
|
return $content; |
333 |
|
|
} |
334 |
|
|
|
335 |
dpavlin |
92 |
=head1 AUTHOR |
336 |
|
|
|
337 |
|
|
Dobrica Pavlinusic |
338 |
|
|
|
339 |
|
|
=head1 LICENSE |
340 |
|
|
|
341 |
|
|
This library is free software, you can redistribute it and/or modify |
342 |
|
|
it under the same terms as Perl itself. |
343 |
|
|
|
344 |
|
|
=cut |
345 |
|
|
|
346 |
|
|
1; |