/[webpac2]/trunk/lib/WebPAC/Output/Estraier.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 /trunk/lib/WebPAC/Output/Estraier.pm

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

revision 210 by dpavlin, Mon Dec 5 17:47:04 2005 UTC revision 525 by dpavlin, Mon May 22 18:26:56 2006 UTC
# Line 5  use strict; Line 5  use strict;
5    
6  use base qw/WebPAC::Common/;  use base qw/WebPAC::Common/;
7    
8  use HyperEstraier;  use Search::Estraier 0.06;
9  use Text::Iconv;  use Encode qw/from_to/;
10  use Data::Dumper;  use Data::Dumper;
11  use LWP::Simple;  use LWP;
12  use URI::Escape;  use URI::Escape;
13    use List::Util qw/first/;
14    
15  =head1 NAME  =head1 NAME
16    
# Line 17  WebPAC::Output::Estraier - Create Hyper Line 18  WebPAC::Output::Estraier - Create Hyper
18    
19  =head1 VERSION  =head1 VERSION
20    
21  Version 0.02  Version 0.12
22    
23  =cut  =cut
24    
25  our $VERSION = '0.02';  our $VERSION = '0.12';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# Line 39  Connect to Hyper Estraier index using HT Line 40  Connect to Hyper Estraier index using HT
40          user => 'admin',          user => 'admin',
41          passwd => 'admin',          passwd => 'admin',
42          database => 'demo',          database => 'demo',
43            label => 'node label',
44          encoding => 'iso-8859-2',          encoding => 'iso-8859-2',
45            clean => 1,
46   );   );
47    
48  Options are:  Options are:
# Line 62  password for user Line 65  password for user
65    
66  name of database from which data comes  name of database from which data comes
67    
68    =item label
69    
70    label for node (optional)
71    
72  =item encoding  =item encoding
73    
74  character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>  character encoding of C<data_structure> if it's differenet than C<ISO-8859-2>
# Line 76  Name of database will be used to form UR Line 83  Name of database will be used to form UR
83    
84  sub new {  sub new {
85          my $class = shift;          my $class = shift;
86          my $self = {@_};          my $self = {@_};
87          bless($self, $class);          bless($self, $class);
88    
89          my $log = $self->_get_logger;          my $log = $self->_get_logger;
90    
91          $log->debug("self: ", sub { Dumper($self) });          #$log->debug("self: ", sub { Dumper($self) });
92    
93          foreach my $p (qw/masterurl user passwd database/) {          foreach my $p (qw/masterurl user passwd database/) {
94                  $log->logdie("need $p") unless ($self->{$p});                  $log->logdie("need $p") unless ($self->{$p});
95          }          }
96    
97            $self->{encoding} ||= 'ISO-8859-2';
98    
99          my $url = $self->{masterurl} . '/node/' . $self->{database};          my $url = $self->{masterurl} . '/node/' . $self->{database};
         $url =~ s#//#/#g;  
100          $self->{url} = $url;          $self->{url} = $url;
101    
102          $log->info("opening Hyper Estraier index $self->{url}");          $self->{label} ||= "WebPAC $self->{database}";
103    
104          my @nodes = $self->est_master( action => 'nodelist' );          $self->{db} = Search::Estraier::Node->new(
105                    url => $url,
106          if (! grep(/$self->{database}/, @nodes)) {                  user => $self->{user},
107                  $log->info("creating index $url");                  passwd => $self->{passwd},
108                  $self->est_master(                  debug => $self->{debug},
109                          action => 'nodeadd',                  create => 1,
110                          name => $self->{database},                  label => $self->convert( $self->{label} ),
111                          label => "WebPAC $self->{database}",          );
112                  ) || $log->logdie("can't create Hyper Estraier node $self->{database}");  
113            $log->info("using ", $self->{clean} ? "new " : "", "index $self->{url} '$self->{label}' with encoding $self->{encoding}");
114    
115            if ($self->{clean}) {
116                    $log->debug("clean $self->{database}");
117                    $self->master( action => 'nodeclr', name => $self->{database} );
118            } else {
119                    $log->debug("opening index $self->{url}");
120          }          }
121    
         $self->{'db'} = HyperEstraier::Node->new($self->{url});  
         $self->{'db'}->set_auth($self->{'user'}, $self->{passwd});  
   
         my $encoding = $self->{'encoding'} || 'ISO-8859-2';  
         $log->info("using encoding $encoding");  
   
         $self->{'iconv'} = new Text::Iconv($encoding, 'UTF-8') or  
                 $log->logdie("can't create conversion from $encoding to UTF-8");  
   
122          $self ? return $self : return undef;          $self ? return $self : return undef;
123  }  }
124    
# Line 125  Adds one entry to database. Line 131  Adds one entry to database.
131          id => 42,          id => 42,
132          ds => $ds,          ds => $ds,
133          type => 'display',          type => 'display',
         url_prefix => 'database name',  
134          text => 'optional text from which snippet is created',          text => 'optional text from which snippet is created',
135    );    );
136    
137  This function will create  entries in index using following URI format:  This function will create  entries in index using following URI format:
138    
139    C<file:///database%20name/000>    C<file:///type/database%20name/000>
140    
141  Each tag in C<data_structure> with specified C<type> will create one  Each tag in C<data_structure> with specified C<type> will create one
142  attribute and corresponding hidden text (used for search).  attribute and corresponding hidden text (used for search).
# Line 141  attribute and corresponding hidden text Line 146  attribute and corresponding hidden text
146  sub add {  sub add {
147          my $self = shift;          my $self = shift;
148    
149          my $args = {@_};          my $args = {@_};
150    
151          my $log = $self->_get_logger;          my $log = $self->_get_logger;
152    
# Line 153  sub add { Line 158  sub add {
158          }          }
159    
160          my $type = $args->{'type'};          my $type = $args->{'type'};
161          my $mfn = $args->{'id'};          my $id = $args->{'id'};
162    
163          my $uri = "file:///$type/$database/$mfn";          my $uri = "file:///$type/$database/$id";
164          $log->debug("creating $uri");          $log->debug("creating $uri");
165    
166          my $doc = HyperEstraier::Document->new;          my $doc = Search::Estraier::Document->new;
167          $doc->add_attr('@uri', $self->{'iconv'}->convert($uri) );          $doc->add_attr('@uri', $self->convert($uri) );
168    
169          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );          $log->debug("ds = ", sub { Dumper($args->{'ds'}) } );
170    
# Line 176  sub add { Line 181  sub add {
181    
182                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });                  my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} });
183    
184                  $log->logconfess("no values for $tag/$type") unless ($vals);                  next if (! $vals);
185    
186                  $vals = $self->{'iconv'}->convert( $vals ) or                  $vals = $self->convert( $vals ) or
187                          $log->logdie("can't convert '$vals' to UTF-8");                          $log->logdie("can't convert '$vals' to UTF-8");
188    
189                  $doc->add_attr( $tag, $vals );                  $doc->add_attr( $tag, $vals );
# Line 187  sub add { Line 192  sub add {
192    
193          my $text = $args->{'text'};          my $text = $args->{'text'};
194          if ( $text ) {          if ( $text ) {
195                  $text = $self->{'iconv'}->convert( $text ) or                  $text = $self->convert( $text ) or
196                          $log->logdie("can't convert '$text' to UTF-8");                          $log->logdie("can't convert '$text' to UTF-8");
197                  $doc->add_text( $text );                  $doc->add_text( $text );
198          }          }
199    
200          $log->debug("adding ", sub { $doc->dump_draft } );          $log->debug("adding ", sub { $doc->dump_draft } );
201          $self->{'db'}->put_doc($doc) || $log->logdie("can't add document $uri to index");          $self->{'db'}->put_doc($doc) || $log->warn("can't add document $uri with draft " . $doc->dump_draft . " to node " . $self->{url} . " status: " . $self->{db}->status());
202    
203          return 1;          return 1;
204  }  }
205    
206  =head2 est_master  =head2 add_link
   
 Issue administrative commands to C<estmaster> process and receive response  
 as array of lines  
207    
208    my $nodelist = $self->est_master( action => nodelist );    $est->add_link(
209            from => 'ps',
210            to => 'webpac2',
211            credit => 10000,
212      );
213    
214  =cut  =cut
215    
216  my $estmaster_actions = {  sub add_link {
         userdel => [ qw/name/ ],  
         nodelist => [],  
         nodeadd => [ qw/name label/ ],  
         nodedel => [ qw/name/ ],  
 };  
   
 sub est_master {  
217          my $self = shift;          my $self = shift;
218    
219          my $args = {@_};          my $args = {@_};
220          my $log = $self->_get_logger;          my $log = $self->_get_logger;
221    
222          $log->debug(Dumper($args));          my $node = first { $_->{name} eq $args->{to} } $self->master( action => 'nodelist' );
223    
224          my $action = $args->{action} || $log->logconfess("no action specified");          if (! $node) {
225                    $log->warn("can't find node $args->{to}, skipping link creaton");
226          $log->logdie("action '$action' isn't supported") unless ($estmaster_actions->{$action});                  return;
227            }
228    
229          my $url = $self->{masterurl} . '/master?action=' . $action;          my $label = $node->{label};
230    
231          foreach my $arg (@{ $estmaster_actions->{$action} }) {          if (! $label) {
232                  $log->logdie("missing parametar $arg for action $action") unless ($args->{$arg});                  $log->warn("can't find label for $args->{to}, skipping link creaton");
233                  $url .= '&' . $arg . '=' . uri_escape( $args->{$arg} );                  return;
234          }          }
235    
236          $log->debug("calling $url");          $log->debug("using label $label for $args->{to}");
237    
238          my $tsv = get($url);          return $self->{db}->set_link(
239                    $self->{masterurl} . '/node/' . $args->{to},
240                    $label,
241                    $args->{credit},
242            );
243    }
244    
         if (! $tsv) {  
                 $log->warn("unable to call $url");  
                 return;  
         }  
245    
246          return split(/\n/, $tsv);  =head2 master
247    
248    Issue administrative commands to C<estmaster> process. See documentation for
249    C<master> in L<Search::Estraier>::Node.
250    
251      $self->master(
252            action => 'nodeclr',
253            name => 'foobar',
254      );
255    
256    =cut
257    
258    sub master {
259            my $self = shift;
260            $self->{db}->master( @_ );
261    }
262    
263    
264    =head2 convert
265    
266     my $utf8_string = $self->convert('string in codepage');
267    
268    =cut
269    
270    sub convert {
271            my $self = shift;
272    
273            my $text = shift || return;
274            from_to($text, $self->{encoding}, 'UTF-8');
275            return $text;
276  }  }
277    
278  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.210  
changed lines
  Added in v.525

  ViewVC Help
Powered by ViewVC 1.1.26