/[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 378 - (show annotations)
Sat Jan 21 23:27:16 2006 UTC (18 years, 3 months ago) by dpavlin
File size: 8516 byte(s)
 r423@llin:  dpavlin | 2006-01-22 00:29:57 +0100
 begin of refactoring: move html record creation to Model::Record

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

  ViewVC Help
Powered by ViewVC 1.1.26