/[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 167 - (show annotations)
Sat Nov 26 21:11:41 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 7056 byte(s)
 r11170@llin:  dpavlin | 2005-11-26 22:13:58 +0100
 implemented template list and switching

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::DB;
11 use WebPAC::Output::TT;
12 use WebPAC::Search::Estraier 0.02;
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 url: 'http://localhost:1978/node/webpac2'
38 user: 'admin'
39 passwd: 'admin'
40 hits_on_page: 100
41
42 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 =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 $self->{log} = $log;
61
62 my $est_cfg = $c->config->{hyperestraier};
63 $est_cfg->{'log'} = $log;
64
65 $est_cfg->{encoding} = $est_cfg->{catalyst_encoding};
66
67 $log->debug("using config:" . Dumper($est_cfg) );
68
69 $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
70
71 # 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
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 # default template from config.yaml
93 $self->{template} ||= $c->config->{webpac}->{template};
94
95 $self->{iconv} = new Text::Iconv(
96 $c->config->{webpac}->{webpac_encoding},
97 $c->config->{webpac}->{out_encoding}
98 );
99
100 $log->debug("converting encoding from webpac_encoding '" .
101 $c->config->{webpac}->{webpac_encoding} .
102 "' to '" .
103 $c->config->{webpac}->{out_encoding} .
104 "'"
105 );
106
107
108 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 {
149 my $self = shift;
150
151 my $args = {@_};
152
153 my $log = $self->{log};
154
155 $log->debug("args: " . Dumper( $args ));
156
157 my $query = $args->{phrase} || $log->warn("no query phrase") && return;
158
159 $log->debug("search model query: '$query'");
160 if ($args->{add_attr}) {
161 $log->debug(" + add_attr: " .
162 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 my @html_results;
194
195 for my $i ( 0 .. $#results ) {
196
197 my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);
198
199 #$log->debug("load_ds( $mfn )");
200
201 $t = time();
202
203 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,
213 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 record
239
240 my $html = $m->record(
241 mfn => 42,
242 template => 'foo.tt',
243 );
244
245 This will load one record, convert it to html using C<template> and return
246 it.
247
248 =cut
249
250 sub record {
251 my $self = shift;
252
253 my $args = {@_};
254 my $log = $self->{log};
255 $log->debug("args: " . Dumper( $args ));
256
257 foreach my $f (qw/mfn template/) {
258 $log->die("need $f") unless ($args->{$f});
259 }
260
261 my $mfn = $args->{mfn};
262
263 my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
264
265 my $html = $self->{out}->apply(
266 template => $args->{template},
267 data => $ds,
268 );
269
270 $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
271
272 return $html;
273 }
274
275 =head2 save_html
276
277 $m->save_html( '/full/path/to/file', $content );
278
279 It will use C<iconv_on_save> to convert content encoding back to
280 Webpac codepage, recode JavaScript Unicode entities (%u1234),
281 strip extra newlines at beginning and end, and save to
282 C</full/path/to/file.new> and if that succeeds, just rename
283 it over original file which should be atomic on filesystem level.
284
285 =cut
286
287 sub save_html {
288 my ($self, $path, $content) = @_;
289
290 $content = $self->iconv_on_save( $content ) || die "no content?";
291
292 sub _conv_js {
293 my $t = shift || return;
294 return $self->{iconv}->convert(chr(hex($t)));
295 }
296 $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
297 $content =~ s/^[\n\r]+//s;
298 $content =~ s/[\n\r]+$/\n/s;
299
300 write_file($path . '.new', $content) || die "can't save ${path}.new $!";
301 rename $path . '.new', $path || die "can't rename to $path: $!";
302 }
303
304 =head2 load_html
305
306 my $html = $m->load_html('/full/path/to/file');
307
308 This will convert file from Webpac encoding to Catalyst and
309 convert that data to escaped HTML (for sending into
310 C<< <textarea/> >> tags in html.
311
312 =cut
313
314 sub load_html {
315 my ($self, $path) = @_;
316
317 die "no path?" unless ($path);
318
319 my $content = read_file($path) || die "can't read $path: $!";
320 #$content = $q->escapeHTML($iconv_utf8->convert($content));
321 $content = $self->{iconv}->convert($content);
322
323 return $content;
324 }
325
326 =head1 AUTHOR
327
328 Dobrica Pavlinusic
329
330 =head1 LICENSE
331
332 This library is free software, you can redistribute it and/or modify
333 it under the same terms as Perl itself.
334
335 =cut
336
337 1;

  ViewVC Help
Powered by ViewVC 1.1.26