/[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 135 - (show annotations)
Thu Nov 24 22:29:29 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 5102 byte(s)
 r9109@llin:  dpavlin | 2005-11-24 18:47:17 +0100
 more work on editor. textareas now load content

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
15 =head1 NAME
16
17 Webpacus::Model::WebPAC - Catalyst Model
18
19 =head1 SYNOPSIS
20
21 See L<Webpacus> and L<WebPAC>.
22
23 =head1 DESCRIPTION
24
25 Catalyst Model for access to WebPAC data.
26
27 =head2 new
28
29 Configuration for hyperestraier in C<config.yaml> like this:
30
31 --- #YAML:1.0
32 # DO NOT USE TABS FOR INDENTATION OR label/value SEPARATION!!!
33
34 # configuration for hyper estraier full text search engine
35 hyperestraier:
36 url: 'http://localhost:1978/node/webpac2'
37 user: 'admin'
38 passwd: 'admin'
39
40 webpac:
41 db_path: '/data/webpac2/db'
42 template_path: '/data/webpac2/conf/output/tt'
43 template: 'html_ffzg_results_short.tt'
44 # encoding comming from webpac
45 webpac_encoding: 'iso-8859-2'
46 # encoding expected by Catalyst
47 out_encoding: 'UTF-8'
48
49 =cut
50
51 sub new {
52 my ( $self, $c, $config ) = @_;
53
54 $self = $self->NEXT::new($c, $config);
55 $self->config($config);
56
57 my $log = $c->log;
58 $self->{log} = $log;
59
60 my $est_cfg = $c->config->{hyperestraier};
61 $est_cfg->{'log'} = $log;
62
63 $log->debug("using config:" . Dumper($est_cfg) );
64
65 $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
66
67 my $db_path = $c->config->{webpac}->{db_path};
68 my $template_path = $c->config->{webpac}->{template_path};
69 $self->{template_path} = $template_path;
70
71 $log->debug("using db path '$db_path', template path '$template_path'");
72
73 $self->{db} = new WebPAC::DB(
74 path => $db_path,
75 read_only => 1,
76 );
77
78 $self->{out} = new WebPAC::Output::TT(
79 include_path => $template_path,
80 filters => { foo => sub { shift } },
81 );
82
83 # default template from config.yaml
84 $self->{template} ||= $c->config->{webpac}->{template};
85
86 $self->{iconv} = new Text::Iconv(
87 $c->config->{webpac}->{webpac_encoding},
88 $c->config->{webpac}->{out_encoding}
89 );
90
91 $log->debug("converting encoding from webpac_encoding '" .
92 $c->config->{webpac}->{webpac_encoding} .
93 "' to '" .
94 $c->config->{webpac}->{out_encoding} .
95 "'"
96 );
97
98 return $self;
99
100 }
101
102 =head2 iconv_on_save
103
104 my $out = $m->iconv_on_save( $content );
105
106 Convert data saved to disk in Webpac encoding.
107
108 =cut
109
110 sub iconv_on_save {
111 my $self = shift;
112
113 $self->{iconv_save} ||= new Text::Iconv(
114 $self->config->{webpac}->{out_encoding},
115 $self->config->{webpac}->{webpac_encoding},
116 );
117
118 $self->{iconv_save}->convert( @_ );
119 }
120
121
122 =head2 search
123
124 my $m->search( 'query phrase', 'result_template.tt', \@add_attr );
125
126 =cut
127
128 sub search {
129 my ( $self, $query, $template, $add_attr ) = @_;
130
131 my $log = $self->{log};
132
133 $log->debug("search model query: '$query', add_attr: '" . join("','", @{$add_attr}) . "'");
134
135 my $template_filename = $template || $self->{template};
136
137 my @results = $self->{est}->search(
138 phrase => $query,
139 get_attr => [ '@uri' ],
140 max => 100,
141 add_attr => $add_attr,
142 );
143
144 $log->debug("loading " . ($#results + 1) . " results");
145
146 my @html_results;
147
148 for my $i ( 0 .. $#results ) {
149
150 my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);
151
152 #$log->debug("load_ds( $mfn )");
153
154 my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
155
156 #$log->debug( "ds = " . Dumper( \@html_results ) );
157
158 my $html = $self->{out}->apply(
159 template => $template_filename,
160 data => $ds,
161 );
162
163 $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
164
165 push @html_results, $html;
166
167 }
168
169 #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
170
171 return \@html_results;
172 }
173
174 =head2 save_html
175
176 $m->save_html( '/full/path/to/file', $content );
177
178 It will use C<iconv_on_save> to convert content encoding back to
179 Webpac codepage, recode JavaScript Unicode entities (%u1234),
180 strip extra newlines at beginning and end, and save to
181 C</full/path/to/file.new> and if that succeeds, just rename
182 it over original file which should be atomic on filesystem level.
183
184 =cut
185
186 sub save_html {
187 my ($self, $path, $content) = @_;
188
189 $content = $self->iconv_on_save( $content ) || die "no content?";
190
191 sub _conv_js {
192 my $t = shift || return;
193 return $self->{iconv}->convert(chr(hex($t)));
194 }
195 $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
196 $content =~ s/^[\n\r]+//s;
197 $content =~ s/[\n\r]+$/\n/s;
198
199 write_file($path . '.new', $content) || die "can't save ${path}.new $!";
200 rename $path . '.new', $path || die "can't rename to $path: $!";
201 }
202
203 =head2 load_html
204
205 my $html = $m->load_html('/full/path/to/file');
206
207 This will convert file from Webpac encoding to Catalyst and
208 convert that data to escaped HTML (for sending into
209 C<< <textarea/> >> tags in html.
210
211 =cut
212
213 sub load_html {
214 my ($self, $path) = @_;
215
216 die "no path?" unless ($path);
217
218 my $content = read_file($path) || die "can't read $path: $!";
219 #$content = $q->escapeHTML($iconv_utf8->convert($content));
220 $content = $self->{iconv}->convert($content);
221
222 return $content;
223 }
224
225 =head1 AUTHOR
226
227 Dobrica Pavlinusic
228
229 =head1 LICENSE
230
231 This library is free software, you can redistribute it and/or modify
232 it under the same terms as Perl itself.
233
234 =cut
235
236 1;

  ViewVC Help
Powered by ViewVC 1.1.26