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