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 |
|
14 |
=head1 NAME |
15 |
|
16 |
Webpacus::Model::WebPAC - Catalyst Model |
17 |
|
18 |
=head1 SYNOPSIS |
19 |
|
20 |
See L<Webpacus> and L<WebPAC>. |
21 |
|
22 |
=head1 DESCRIPTION |
23 |
|
24 |
Catalyst Model for access to WebPAC data. |
25 |
|
26 |
=head2 new |
27 |
|
28 |
Configuration for hyperestraier in C<config.yaml> like this: |
29 |
|
30 |
--- #YAML:1.0 |
31 |
# DO NOT USE TABS FOR INDENTATION OR label/value SEPARATION!!! |
32 |
|
33 |
# configuration for hyper estraier full text search engine |
34 |
hyperestraier: |
35 |
url: 'http://localhost:1978/node/webpac2' |
36 |
user: 'admin' |
37 |
passwd: 'admin' |
38 |
|
39 |
webpac: |
40 |
db_path: '/data/webpac2/db' |
41 |
template_path: '/data/webpac2/conf/output/tt' |
42 |
template: 'html_ffzg_results_short.tt' |
43 |
# encoding comming from webpac |
44 |
webpac_encoding: 'iso-8859-2' |
45 |
# encoding expected by Catalyst |
46 |
out_encoding: 'UTF-8' |
47 |
|
48 |
=cut |
49 |
|
50 |
sub new { |
51 |
my ( $self, $c, $config ) = @_; |
52 |
|
53 |
$self = $self->NEXT::new($c, $config); |
54 |
$self->config($config); |
55 |
|
56 |
my $log = $c->log; |
57 |
$self->{log} = $log; |
58 |
|
59 |
my $est_cfg = $c->config->{hyperestraier}; |
60 |
$est_cfg->{'log'} = $log; |
61 |
|
62 |
$log->debug("using config:" . Dumper($est_cfg) ); |
63 |
|
64 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
65 |
|
66 |
my $db_path = $c->config->{webpac}->{db_path}; |
67 |
my $template_path = $c->config->{webpac}->{template_path}; |
68 |
|
69 |
$log->debug("using db path '$db_path', template path '$template_path'"); |
70 |
|
71 |
$self->{db} = new WebPAC::DB( |
72 |
path => $db_path, |
73 |
read_only => 1, |
74 |
); |
75 |
|
76 |
$self->{out} = new WebPAC::Output::TT( |
77 |
include_path => $template_path, |
78 |
filters => { foo => sub { shift } }, |
79 |
); |
80 |
|
81 |
# default template from config.yaml |
82 |
$self->{template} ||= $c->config->{webpac}->{template}; |
83 |
|
84 |
$self->{iconv} = new Text::Iconv( |
85 |
$c->config->{webpac}->{webpac_encoding}, |
86 |
$c->config->{webpac}->{out_encoding} |
87 |
); |
88 |
|
89 |
$log->debug("converting encoding from webpac_encoding '" . |
90 |
$c->config->{webpac}->{webpac_encoding} . |
91 |
"' to '" . |
92 |
$c->config->{webpac}->{out_encoding} . |
93 |
"'" |
94 |
); |
95 |
|
96 |
return $self; |
97 |
|
98 |
} |
99 |
|
100 |
sub search { |
101 |
my ( $self, $query, $template, @add_attr ) = @_; |
102 |
|
103 |
my $log = $self->{log}; |
104 |
|
105 |
$log->debug("search model query: '$query', add_attr: '" . join("','", @add_attr) . "'"); |
106 |
|
107 |
my $template_filename = $template || $self->{template}; |
108 |
|
109 |
my @results = $self->{est}->search( |
110 |
phrase => $query, |
111 |
get_attr => [ '@uri' ], |
112 |
max => 100, |
113 |
add_attr => @add_attr, |
114 |
); |
115 |
|
116 |
$log->debug("loading " . ($#results + 1) . " results"); |
117 |
|
118 |
my @html_results; |
119 |
|
120 |
for my $i ( 0 .. $#results ) { |
121 |
|
122 |
my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#); |
123 |
|
124 |
#$log->debug("load_ds( $mfn )"); |
125 |
|
126 |
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
127 |
|
128 |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
129 |
|
130 |
my $html = $self->{out}->apply( |
131 |
template => $template_filename, |
132 |
data => $ds, |
133 |
); |
134 |
|
135 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
136 |
|
137 |
push @html_results, $html; |
138 |
|
139 |
} |
140 |
|
141 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
142 |
|
143 |
return \@html_results; |
144 |
} |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
=head1 AUTHOR |
150 |
|
151 |
Dobrica Pavlinusic |
152 |
|
153 |
=head1 LICENSE |
154 |
|
155 |
This library is free software, you can redistribute it and/or modify |
156 |
it under the same terms as Perl itself. |
157 |
|
158 |
=cut |
159 |
|
160 |
1; |