/[webpac2]/Webpacus/lib/Webpacus/Model/WebPAC.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /Webpacus/lib/Webpacus/Model/WebPAC.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 222 - (show annotations)
Mon Dec 5 19:15:01 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 7632 byte(s)
 r11533@llin:  dpavlin | 2005-12-05 02:53:10 +0100
 initial work on support new WebPAC multi-database version

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 );
94
95 $self->{out} = new WebPAC::Output::TT(
96 include_path => $template_path,
97 filters => { foo => sub { shift } },
98 );
99
100 # default template from config.yaml
101 $self->{template} ||= $c->config->{webpac}->{template};
102
103 $self->{iconv} = new Text::Iconv(
104 $c->config->{webpac}->{webpac_encoding},
105 $c->config->{webpac}->{out_encoding}
106 );
107
108 $log->debug("converting encoding from webpac_encoding '" .
109 $c->config->{webpac}->{webpac_encoding} .
110 "' to '" .
111 $c->config->{webpac}->{out_encoding} .
112 "'"
113 );
114
115
116 return $self;
117
118 }
119
120
121 =head2 search
122
123 my $m->search(
124 phrase => 'query phrase',
125 add_attr => \@add_attr
126 get_attr => [ '@uri' ],
127 max => 42,
128 template => 'result_template.tt',
129 depth => 1,
130 );
131
132 All fields are standard C<WebPAC::Search::Estraier> parametars except
133 C<template> which will (if specified) return results in HTML using
134 selected template.
135
136 =cut
137
138 sub search {
139 my $self = shift;
140
141 my $args = {@_};
142
143 my $log = $self->{log};
144
145 $log->debug("args: " . Dumper( $args ));
146
147 my $query = $args->{phrase} || $log->warn("no query phrase") && return;
148
149 $log->debug("search model query: '$query'");
150 if ($args->{add_attr}) {
151 $log->debug(" + add_attr: " .
152 join("','", @{ $args->{add_attr} })
153 );
154 }
155
156 my $template_filename = $args->{template} || $self->{template};
157
158 $args->{max} ||= $self->{'hits_on_page'};
159 if (! $args->{max}) {
160 $args->{max} = 10;
161 $log->warn("max not set when calling model. Using default of 10");
162 }
163
164 my $times; # store some times for benchmarking
165
166 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 } );
176
177 $times->{est} += time() - $t;
178
179 my $hits = $#results + 1;
180
181 $log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) );
182
183 # just return results?
184 return @results unless ($args->{'template'});
185
186 #
187 # construct HTML results
188 #
189
190 my @html_results;
191
192 for my $i ( 0 .. $#results ) {
193
194 my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);
195
196 #$log->debug("load_ds( $mfn )");
197
198 $t = time();
199
200 my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
201
202 $times->{db} += time() - $t;
203
204 #$log->debug( "ds = " . Dumper( \@html_results ) );
205
206 $t = time();
207
208 my $html = $self->{out}->apply(
209 template => $template_filename,
210 data => $ds,
211 );
212
213 $times->{out} += time() - $t;
214
215 $t = time();
216
217 $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
218
219 $times->{iconv} += time() - $t;
220
221 push @html_results, $html;
222
223 }
224
225 #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
226
227 $log->debug( sprintf(
228 "time spent: db = %.2f, out = %.2f, iconv = %.2f",
229 $times->{db}, $times->{out}, $times->{iconv},
230 ) );
231
232 return \@html_results;
233 }
234
235 =head2 record
236
237 my $html = $m->record(
238 mfn => 42,
239 template => 'foo.tt',
240 );
241
242 This will load one record, convert it to html using C<template> and return
243 it.
244
245 =cut
246
247 sub record {
248 my $self = shift;
249
250 my $args = {@_};
251 my $log = $self->{log};
252 $log->debug("args: " . Dumper( $args ));
253
254 foreach my $f (qw/mfn template/) {
255 $log->die("need $f") unless ($args->{$f});
256 }
257
258 my $mfn = $args->{mfn};
259
260 my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
261
262 my $html = $self->{out}->apply(
263 template => $args->{template},
264 data => $ds,
265 );
266
267 $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
268
269 return $html;
270 }
271
272 =head2 save_html
273
274 $m->save_html( '/full/path/to/file', $content );
275
276 It will use C<Text::Iconv> to convert content encoding back to
277 Webpac codepage, recode JavaScript Unicode entities (%u1234),
278 strip extra newlines at beginning and end, and save to
279 C</full/path/to/file.new> and if that succeeds, just rename
280 it over original file which should be atomic on filesystem level.
281
282 =cut
283
284 sub save_html {
285 my ($self, $path, $content) = @_;
286
287 sub _conv_js {
288 my $t = shift || return;
289 return $self->{iconv}->convert(chr(hex($t)));
290 }
291 $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
292 $content =~ s/^[\n\r]+//s;
293 $content =~ s/[\n\r]+$/\n/s;
294
295 my ($from, $to) = (
296 $self->{out_encoding},
297 $self->{webpac_encoding},
298 );
299
300 $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', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!";
308 rename $path . '.new', $path || die "can't rename to $path: $!";
309 }
310
311 =head2 load_html
312
313 my $html = $m->load_html('/full/path/to/file');
314
315 This will convert file from Webpac encoding to Catalyst and
316 convert that data to escaped HTML (for sending into
317 C<< <textarea/> >> tags in html.
318
319 =cut
320
321 sub load_html {
322 my ($self, $path) = @_;
323
324 die "no path?" unless ($path);
325
326 my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!";
327 #$content = $q->escapeHTML($iconv_utf8->convert($content));
328 $content = $self->{iconv}->convert($content);
329
330 return $content;
331 }
332
333 =head1 AUTHOR
334
335 Dobrica Pavlinusic
336
337 =head1 LICENSE
338
339 This library is free software, you can redistribute it and/or modify
340 it under the same terms as Perl itself.
341
342 =cut
343
344 1;

  ViewVC Help
Powered by ViewVC 1.1.26