/[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 99 by dpavlin, Tue Nov 22 14:45:12 2005 UTC revision 142 by dpavlin, Fri Nov 25 00:23:33 2005 UTC
# Line 5  use warnings; Line 5  use warnings;
5  use lib '/data/webpac2/lib';  use lib '/data/webpac2/lib';
6  use base qw/  use base qw/
7          Catalyst::Model          Catalyst::Model
         WebPAC::Search::Estraier  
8  /;  /;
9  use Data::Dumper;  use Data::Dumper;
10  use WebPAC::DB;  use WebPAC::DB;
11  use WebPAC::Output::TT;  use WebPAC::Output::TT;
12    use WebPAC::Search::Estraier 0.02;
13    use File::Slurp;
14    
15  =head1 NAME  =head1 NAME
16    
# Line 59  sub new { Line 60  sub new {
60          my $est_cfg = $c->config->{hyperestraier};          my $est_cfg = $c->config->{hyperestraier};
61          $est_cfg->{'log'} = $log;          $est_cfg->{'log'} = $log;
62    
63            $est_cfg->{encoding} = $est_cfg->{catalyst_encoding};
64    
65          $log->debug("using config:" . Dumper($est_cfg) );          $log->debug("using config:" . Dumper($est_cfg) );
66    
67          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );          $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
68    
69          my $db_path = $c->config->{webpac}->{db_path};          my $db_path = $c->config->{webpac}->{db_path};
70          my $template_path = $c->config->{webpac}->{template_path};          my $template_path = $c->config->{webpac}->{template_path};
71            $self->{template_path} = $template_path;
72    
73          $log->debug("using db path '$db_path', template path '$template_path'");          $log->debug("using db path '$db_path', template path '$template_path'");
74    
# Line 78  sub new { Line 82  sub new {
82                  filters => { foo => sub { shift } },                  filters => { foo => sub { shift } },
83          );          );
84    
85            # default template from config.yaml
86          $self->{template} ||= $c->config->{webpac}->{template};          $self->{template} ||= $c->config->{webpac}->{template};
87    
88          $self->{iconv} = new Text::Iconv(          $self->{iconv} = new Text::Iconv(
# Line 85  sub new { Line 90  sub new {
90                  $c->config->{webpac}->{out_encoding}                  $c->config->{webpac}->{out_encoding}
91          );          );
92    
93          $log->debug("converting encoding from webpac_encoding '" ,          $log->debug("converting encoding from webpac_encoding '" .
94                  $c->config->{webpac}->{webpac_encoding},                  $c->config->{webpac}->{webpac_encoding} .
95                  "' to '",                  "' to '" .
96                  $c->config->{webpac}->{out_encoding},                  $c->config->{webpac}->{out_encoding} .
97                  "'"                  "'"
98          );          );
99    
# Line 96  sub new { Line 101  sub new {
101    
102  }  }
103    
104    =head2 iconv_on_save
105    
106      my $out = $m->iconv_on_save( $content );
107    
108    Convert data saved to disk in Webpac encoding.
109    
110    =cut
111    
112    sub iconv_on_save {
113            my $self = shift;
114    
115            $self->{iconv_save} ||= new Text::Iconv(
116                    $self->config->{webpac}->{out_encoding},
117                    $self->config->{webpac}->{webpac_encoding},
118            );
119    
120            $self->{iconv_save}->convert( @_ );
121    }
122    
123    
124    =head2 search
125    
126      my $m->search( 'query phrase', 'result_template.tt', \@add_attr );
127    
128    =cut
129    
130  sub search {  sub search {
131          my ( $self, $query ) = @_;          my ( $self, $query, $template, $add_attr ) = @_;
132    
133          my $log = $self->{log};          my $log = $self->{log};
134    
135          $log->debug("search model query: -->$query<--");          $log->debug("search model query: '$query', add_attr: '" . join("','", @{$add_attr}) . "'");
136    
137          my $template_filename = $self->{template};          my $template_filename = $template || $self->{template};
138    
139          my @results = $self->{est}->search(          my @results = $self->{est}->search(
140                  query => $query,                  phrase => $query,
141                  attr => [ '@uri' ],                  get_attr => [ '@uri' ],
142                  max => 100,                  max => 100,
143                    add_attr => $add_attr,
144          );          );
145    
146          $log->debug("loading " . ($#results + 1) . " results");          $log->debug("loading " . ($#results + 1) . " results");
147    
148            my @html_results;
149    
150          for my $i ( 0 .. $#results ) {          for my $i ( 0 .. $#results ) {
151    
152                  my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);                  my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);
153    
154                  # $log->debug("load_ds( $mfn )");                  #$log->debug("load_ds( $mfn )");
155    
156                    my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
157            
158                    #$log->debug( "ds = " . Dumper( \@html_results ) );
159    
160                  my $ds = $self->{db}->load_ds( $mfn ) || next;                  my $html = $self->{out}->apply(
                 $results[$i]->{ html } = $self->{iconv}->convert(  
                 $self->{out}->apply(  
161                          template => $template_filename,                          template => $template_filename,
162                          data => $ds,                          data => $ds,
163                  ) );                  );
164    
165                    $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
166    
167                    push @html_results, $html;
168    
169          }          }
170    
171          return \@results;          #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
172    
173            return \@html_results;
174  }  }
175    
176    =head2 save_html
177    
178              $m->save_html( '/full/path/to/file', $content );
179    
180    It will use C<iconv_on_save> to convert content encoding back to
181    Webpac codepage, recode JavaScript Unicode entities (%u1234),
182    strip extra newlines at beginning and end, and save to
183    C</full/path/to/file.new> and if that succeeds, just rename
184    it over original file which should be atomic on filesystem level.
185    
186    =cut
187    
188    sub save_html {
189            my ($self, $path, $content) = @_;
190    
191            $content = $self->iconv_on_save( $content ) || die "no content?";
192    
193            sub _conv_js {
194                    my $t = shift || return;
195                    return $self->{iconv}->convert(chr(hex($t)));
196            }
197            $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
198            $content =~ s/^[\n\r]+//s;
199            $content =~ s/[\n\r]+$/\n/s;
200    
201            write_file($path . '.new', $content) || die "can't save ${path}.new $!";
202            rename $path . '.new', $path || die "can't rename to $path: $!";
203    }
204    
205    =head2 load_html
206    
207      my $html = $m->load_html('/full/path/to/file');
208    
209    This will convert file from Webpac encoding to Catalyst and
210    convert that data to escaped HTML (for sending into
211    C<< <textarea/> >> tags in html.
212    
213    =cut
214    
215    sub load_html {
216            my ($self, $path) = @_;
217    
218            die "no path?" unless ($path);
219    
220            my $content = read_file($path) || die "can't read $path: $!";
221            #$content = $q->escapeHTML($iconv_utf8->convert($content));
222            $content = $self->{iconv}->convert($content);
223    
224            return $content;
225    }
226    
227  =head1 AUTHOR  =head1 AUTHOR
228    

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

  ViewVC Help
Powered by ViewVC 1.1.26