/[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 322 - (show annotations)
Sun Dec 25 23:31:59 2005 UTC (18 years, 4 months ago) by dpavlin
File size: 8694 byte(s)
 r12242@llin:  dpavlin | 2005-12-24 13:05:27 +0100
 a try at implementing search links which work by updateing search form
 (not fully usable yet).

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

  ViewVC Help
Powered by ViewVC 1.1.26