/[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

Diff of /Webpacus/lib/Webpacus/Model/WebPAC.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 142 by dpavlin, Fri Nov 25 00:23:33 2005 UTC revision 378 by dpavlin, Sat Jan 21 23:27:16 2006 UTC
# Line 6  use lib '/data/webpac2/lib'; Line 6  use lib '/data/webpac2/lib';
6  use base qw/  use base qw/
7          Catalyst::Model          Catalyst::Model
8  /;  /;
9  use Data::Dumper;  use WebPAC::Store 0.08;
10  use WebPAC::DB;  use WebPAC::Search::Estraier 0.05;
 use WebPAC::Output::TT;  
 use WebPAC::Search::Estraier 0.02;  
11  use File::Slurp;  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  =head1 NAME
17    
# Line 33  Configuration for hyperestraier in C<con Line 34  Configuration for hyperestraier in C<con
34    
35   # configuration for hyper estraier full text search engine   # configuration for hyper estraier full text search engine
36   hyperestraier:   hyperestraier:
37    url: 'http://localhost:1978/node/webpac2'    masterurl: 'http://localhost:1978/node/webpac2'
38      defaultnode: 'webpac2'
39      defaultdepth: 1
40    user: 'admin'    user: 'admin'
41    passwd: 'admin'    passwd: 'admin'
42      hits_on_page: 100
43      hits_for_pager: 1000
44    
45   webpac:   webpac:
46    db_path: '/data/webpac2/db'    db_path: '/data/webpac2/db'
# Line 43  Configuration for hyperestraier in C<con Line 48  Configuration for hyperestraier in C<con
48    template: 'html_ffzg_results_short.tt'    template: 'html_ffzg_results_short.tt'
49    # encoding comming from webpac    # encoding comming from webpac
50    webpac_encoding: 'iso-8859-2'    webpac_encoding: 'iso-8859-2'
   # encoding expected by Catalyst  
   out_encoding: 'UTF-8'  
51    
52  =cut  =cut
53    
# Line 60  sub new { Line 63  sub new {
63          my $est_cfg = $c->config->{hyperestraier};          my $est_cfg = $c->config->{hyperestraier};
64          $est_cfg->{'log'} = $log;          $est_cfg->{'log'} = $log;
65    
66          $est_cfg->{encoding} = $est_cfg->{catalyst_encoding};          $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) );          $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 } );          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
77    
78          my $db_path = $c->config->{webpac}->{db_path};          # save config parametars in object
79          my $template_path = $c->config->{webpac}->{template_path};          foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) {
80          $self->{template_path} = $template_path;                  $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'");          $log->debug("using db path '$db_path', template path '$template_path'");
88    
89          $self->{db} = new WebPAC::DB(          $self->{db} = new WebPAC::Store(
90                  path => $db_path,                  path => $db_path,
91                  read_only => 1,                  read_only => 1,
92          );                  database => $est_cfg->{database},
   
         $self->{out} = new WebPAC::Output::TT(  
                 include_path => $template_path,  
                 filters => { foo => sub { shift } },  
93          );          );
94    
95          # default template from config.yaml          # default template from config.yaml
96          $self->{template} ||= $c->config->{webpac}->{template};          $self->{template} ||= $c->config->{webpac}->{template};
97    
         $self->{iconv} = new Text::Iconv(  
                 $c->config->{webpac}->{webpac_encoding},  
                 $c->config->{webpac}->{out_encoding}  
         );  
   
98          $log->debug("converting encoding from webpac_encoding '" .          $log->debug("converting encoding from webpac_encoding '" .
99                  $c->config->{webpac}->{webpac_encoding} .                  $c->config->{webpac}->{webpac_encoding} .
                 "' to '" .  
                 $c->config->{webpac}->{out_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;          return $self;
108    
109  }  }
110    
 =head2 iconv_on_save  
111    
112    my $out = $m->iconv_on_save( $content );  =head2 search
113    
114  Convert data saved to disk in Webpac encoding.    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  =cut
128    
129  sub iconv_on_save {  sub search {
130          my $self = shift;          my $self = shift;
131    
132          $self->{iconv_save} ||= new Text::Iconv(          my $search_start_t = time();
                 $self->config->{webpac}->{out_encoding},  
                 $self->config->{webpac}->{webpac_encoding},  
         );  
133    
134          $self->{iconv_save}->convert( @_ );          my $args = {@_};
 }  
135    
136            my $log = $self->{log};
137    
138  =head2 search          $log->debug("search args: " . Dumper( $args ));
139    
140    my $m->search( 'query phrase', 'result_template.tt', \@add_attr );          my $query = $args->{phrase} || $log->warn("no query phrase") && return;
141    
142  =cut          $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  sub search {          my $template_filename = $args->{template} || $self->{template};
         my ( $self, $query, $template, $add_attr ) = @_;  
150    
151          my $log = $self->{log};          $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          $log->debug("search model query: '$query', add_attr: '" . join("','", @{$add_attr}) . "'");          my $times;      # store some times for benchmarking
158    
159          my $template_filename = $template || $self->{template};          my $t = time();
160    
161          my @results = $self->{est}->search(          # transfer depth of search
162                  phrase => $query,          if (! $args->{depth}) {
163                  get_attr => [ '@uri' ],                  my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration");
164                  max => 100,                  $args->{depth} = $default;
165                  add_attr => $add_attr,                  $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          $log->debug("loading " . ($#results + 1) . " results");          #
180            # construct HTML results
181            #
182    
183          my @html_results;          my @html_results;
184    
185          for my $i ( 0 .. $#results ) {          for my $i ( 0 .. $#results ) {
186    
187                  my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);                  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                  #$log->debug("load_ds( $mfn )");                  $times->{db} += time() - $t;
206    
                 my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;  
           
207                  #$log->debug( "ds = " . Dumper( \@html_results ) );                  #$log->debug( "ds = " . Dumper( \@html_results ) );
208    
209                  my $html = $self->{out}->apply(                  $t = time();
210                          template => $template_filename,  
211                          data => $ds,                  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                  $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");                  $t = time();
227    
228                    $html = decode($self->{webpac_encoding}, $html);
229    
230                  push @html_results, $html;                  push @html_results, $html;
231    
# Line 170  sub search { Line 233  sub search {
233    
234          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );          #$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;          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  =head2 save_html
296    
297    $m->save_html( '/full/path/to/file', $content );    $m->save_html( '/full/path/to/file', $content );
298    
299  It will use C<iconv_on_save> to convert content encoding back to  It will use C<Encode> to convert content encoding back to
300  Webpac codepage, recode JavaScript Unicode entities (%u1234),  Webpac codepage, recode JavaScript Unicode entities (%u1234),
301  strip extra newlines at beginning and end, and save to  strip extra newlines at beginning and end, and save to
302  C</full/path/to/file.new> and if that succeeds, just rename  C</full/path/to/file.new> and if that succeeds, just rename
# Line 188  it over original file which should be at Line 307  it over original file which should be at
307  sub save_html {  sub save_html {
308          my ($self, $path, $content) = @_;          my ($self, $path, $content) = @_;
309    
310          $content = $self->iconv_on_save( $content ) || die "no content?";          # 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 {          sub _conv_js {
315                  my $t = shift || return;                  return '0x' . $_[1];
316                  return $self->{iconv}->convert(chr(hex($t)));                  return encode($_[0], chr(hex($_[1])));
317          }          }
318          $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;          #$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex;
319          $content =~ s/^[\n\r]+//s;          $content =~ s/^[\n\r]+//s;
320          $content =~ s/[\n\r]+$/\n/s;          $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', $content) || die "can't save ${path}.new $!";          write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!";
328          rename $path . '.new', $path || die "can't rename to $path: $!";          rename $path . '.new', $path || die "can't rename to $path: $!";
329  }  }
330    
# Line 217  sub load_html { Line 343  sub load_html {
343    
344          die "no path?" unless ($path);          die "no path?" unless ($path);
345    
346          my $content = read_file($path) || die "can't read $path: $!";          my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!";
         #$content = $q->escapeHTML($iconv_utf8->convert($content));  
         $content = $self->{iconv}->convert($content);  
347    
348          return $content;          return decode($self->{webpac_encoding}, $content);
349  }  }
350    
351  =head1 AUTHOR  =head1 AUTHOR
352    
353  Dobrica Pavlinusic  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>
354    
355  =head1 LICENSE  =head1 LICENSE
356    

Legend:
Removed from v.142  
changed lines
  Added in v.378

  ViewVC Help
Powered by ViewVC 1.1.26