--- trunk/Estraier.pm 2006/01/06 01:51:28 47 +++ trunk/Estraier.pm 2006/01/06 20:58:26 57 @@ -645,7 +645,7 @@ sub doc_num { my $self = shift; - return $#{$self->{docs}}; + return $#{$self->{docs}} + 1; } @@ -692,6 +692,7 @@ use URI; use MIME::Base64; use IO::Socket::INET; +use URI::Escape qw/uri_escape/; =head1 Search::Estraier::Node @@ -716,10 +717,10 @@ }; bless($self, $class); - if (@_) { - $self->{debug} = shift; - warn "## Node debug on\n"; - } + my $args = {@_}; + + $self->{debug} = $args->{debug}; + warn "## Node debug on\n" if ($self->{debug}); $self ? return $self : return undef; } @@ -866,7 +867,7 @@ return unless ($self->{url}); $self->shuttle_url( $self->{url} . '/out_doc', 'application/x-www-form-urlencoded', - "uri=$uri", + "uri=" . uri_escape($uri), undef ) == 200; } @@ -928,6 +929,40 @@ } +=head2 get_doc_attr + +Retrieve the value of an atribute from object + + my $val = $node->get_doc_attr( document_id, 'attribute_name' ) or + die "can't get document attribute"; + +=cut + +sub get_doc_attr { + my $self = shift; + my ($id,$name) = @_; + return unless ($id && $name); + return $self->_fetch_doc( id => $id, attr => $name ); +} + + +=head2 get_doc_attr_by_uri + +Retrieve the value of an atribute from object + + my $val = $node->get_doc_attr_by_uri( document_id, 'attribute_name' ) or + die "can't get document attribute"; + +=cut + +sub get_doc_attr_by_uri { + my $self = shift; + my ($uri,$name) = @_; + return unless ($uri && $name); + return $self->_fetch_doc( uri => $uri, attr => $name ); +} + + =head2 etch_doc Exctract document keywords @@ -936,7 +971,7 @@ =cut -sub erch_doc { +sub etch_doc { my $self = shift; my $id = shift || return; return $self->_fetch_doc( id => $id, etch => 1 ); @@ -987,6 +1022,10 @@ my $doc = $node->_fetch_doc( id => 42, etch => 1 ); my $doc = $node->_fetch_doc( uri => 'file:///document/uri/42', etch => 1 ); + # to get document attrubute add attr + my $doc = $node->_fetch_doc( id => 42, attr => '@mdate' ); + my $doc = $node->_fetch_doc( uri => 'file:///document/uri/42', attr => '@mdate' ); + # more general form which allows implementation of # uri_to_id my $id = $node->_fetch_doc( @@ -1011,11 +1050,17 @@ croak "id must be numberm not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/); $arg = 'id=' . $a->{id}; } elsif ($a->{uri}) { - $arg = 'uri=' . $a->{uri}; + $arg = 'uri=' . uri_escape($a->{uri}); } else { confess "unhandled argument. Need id or uri."; } + if ($a->{attr}) { + $path = '/get_doc_attr'; + $arg .= '&attr=' . uri_escape($a->{attr}); + $a->{chomp_resbody} = 1; + } + my $rv = $self->shuttle_url( $self->{url} . $path, 'application/x-www-form-urlencoded', $arg, @@ -1042,6 +1087,232 @@ } +=head2 name + + my $node_name = $node->name; + +=cut + +sub name { + my $self = shift; + $self->_set_info unless ($self->{name}); + return $self->{name}; +} + + +=head2 label + + my $node_label = $node->label; + +=cut + +sub label { + my $self = shift; + $self->_set_info unless ($self->{label}); + return $self->{label}; +} + + +=head2 doc_num + + my $documents_in_node = $node->doc_num; + +=cut + +sub doc_num { + my $self = shift; + $self->_set_info if ($self->{dnum} < 0); + return $self->{dnum}; +} + + +=head2 word_num + + my $words_in_node = $node->word_num; + +=cut + +sub word_num { + my $self = shift; + $self->_set_info if ($self->{wnum} < 0); + return $self->{wnum}; +} + + +=head2 size + + my $node_size = $node->size; + +=cut + +sub size { + my $self = shift; + $self->_set_info if ($self->{size} < 0); + return $self->{size}; +} + + +=head2 search + +Search documents which match condition + + my $nres = $node->search( $cond, $depth ); + +C<$cond> is C object, while <$depth> specifies +depth for meta search. + +Function results C object. + +=cut + +sub search { + my $self = shift; + my ($cond, $depth) = @_; + return unless ($cond && defined($depth) && $self->{url}); + croak "cond mush be Search::Estraier::Condition, not '$cond->isa'" unless ($cond->isa('Search::Estraier::Condition')); + croak "depth needs number, not '$depth'" unless ($depth =~ m/^\d+$/); + + my $resbody; + + my $rv = $self->shuttle_url( $self->{url} . '/search', + 'application/x-www-form-urlencoded', + $self->cond_to_query( $cond ), + \$resbody, + ); + return if ($rv != 200); + + my (@docs, $hints); + + my @lines = split(/\n/, $resbody); + return unless (@lines); + + my $border = $lines[0]; + my $isend = 0; + my $lnum = 1; + + while ( $lnum <= $#lines ) { + my $line = $lines[$lnum]; + $lnum++; + + #warn "## $line\n"; + if ($line && $line =~ m/^\Q$border\E(:END)*$/) { + $isend = $1; + last; + } + + if ($line =~ /\t/) { + my ($k,$v) = split(/\t/, $line, 2); + $hints->{$k} = $v; + } + } + + my $snum = $lnum; + + while( ! $isend && $lnum <= $#lines ) { + my $line = $lines[$lnum]; + #warn "# $lnum: $line\n"; + $lnum++; + + if ($line && $line =~ m/^\Q$border\E/) { + if ($lnum > $snum) { + my $rdattrs; + my $rdvector; + my $rdsnippet; + + my $rlnum = $snum; + while ($rlnum < $lnum - 1 ) { + #my $rdline = $self->_s($lines[$rlnum]); + my $rdline = $lines[$rlnum]; + $rlnum++; + last unless ($rdline); + if ($rdline =~ /^%/) { + $rdvector = $1 if ($rdline =~ /^%VECTOR\t(.+)$/); + } elsif($rdline =~ /=/) { + $rdattrs->{$1} = $2 if ($rdline =~ /^(.+)=(.+)$/); + } else { + confess "invalid format of response"; + } + } + while($rlnum < $lnum - 1) { + my $rdline = $lines[$rlnum]; + $rlnum++; + $rdsnippet .= "$rdline\n"; + } + #warn Dumper($rdvector, $rdattrs, $rdsnippet); + if (my $rduri = $rdattrs->{'@uri'}) { + push @docs, new Search::Estraier::ResultDocument( + uri => $rduri, + attrs => $rdattrs, + snippet => $rdsnippet, + keywords => $rdvector, + ); + } + } + $snum = $lnum; + #warn "### $line\n"; + $isend = 1 if ($line =~ /:END$/); + } + + } + + if (! $isend) { + warn "received result doesn't have :END\n$resbody"; + return; + } + + #warn Dumper(\@docs, $hints); + + return new Search::Estraier::NodeResult( docs => \@docs, hints => $hints ); +} + + +=head2 cond_to_query + +Return URI encoded string generated from Search::Estraier::Condition + + my $args = $node->cond_to_query( $cond ); + +=cut + +sub cond_to_query { + my $self = shift; + + my $cond = shift || return; + croak "condition must be Search::Estraier::Condition, not '$cond->isa'" unless ($cond->isa('Search::Estraier::Condition')); + + my @args; + + if (my $phrase = $cond->phrase) { + push @args, 'phrase=' . uri_escape($phrase); + } + + if (my @attrs = $cond->attrs) { + for my $i ( 0 .. $#attrs ) { + push @args,'attr' . ($i+1) . '=' . uri_escape( $attrs[$i] ); + } + } + + if (my $order = $cond->order) { + push @args, 'order=' . uri_escape($order); + } + + if (my $max = $cond->max) { + push @args, 'max=' . $max; + } else { + push @args, 'max=' . (1 << 30); + } + + if (my $options = $cond->options) { + push @args, 'options=' . $options; + } + + push @args, 'depth=' . $self->{depth} if ($self->{depth}); + push @args, 'wwidth=' . $self->{wwidth}; + push @args, 'hwidth=' . $self->{hwidth}; + push @args, 'awidth=' . $self->{awidth}; + + return join('&', @args); +} =head2 shuttle_url @@ -1049,7 +1320,7 @@ This is method which uses C to communicate with Hyper Estraier node master. - my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody ); + my $rv = shuttle_url( $url, $content_type, $req_body, \$resbody ); C<$resheads> and C<$resbody> booleans controll if response headers and/or response body will be saved within object. @@ -1155,6 +1426,139 @@ return $self->{status}; } + +=head2 set_snippet_width + +Set width of snippets in results + + $node->set_snippet_width( $wwidth, $hwidth, $awidth ); + +C<$wwidth> specifies whole width of snippet. It's C<480> by default. If it's C<0> snippet +is not sent with results. If it is negative, whole document text is sent instead of snippet. + +C<$hwidth> specified width of strings from beginning of string. Default +value is C<96>. Negative or zero value keep previous value. + +C<$awidth> specifies width of strings around each highlighted word. It's C<96> by default. +If negative of zero value is provided previous value is kept unchanged. + +=cut + +sub set_snippet_width { + my $self = shift; + + my ($wwidth, $hwidth, $awidth) = @_; + $self->{wwidth} = $wwidth; + $self->{hwidth} = $hwidth if ($hwidth >= 0); + $self->{awidth} = $awidth if ($awidth >= 0); +} + + +=head2 set_user + +Manage users of node + + $node->set_user( 'name', $mode ); + +C<$mode> can be one of: + +=over 4 + +=item 0 + +delete account + +=item 1 + +set administrative right for user + +=item 2 + +set user account as guest + +=back + +Return true on success, otherwise false. + +=cut + +sub set_user { + my $self = shift; + my ($name, $mode) = @_; + + return unless ($self->{url}); + croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/); + + $self->shuttle_url( $self->{url} . '/_set_user', + 'text/plain', + 'name=' . uri_escape($name) . '&mode=' . $mode, + undef + ) == 200; +} + + +=head2 set_link + +Manage node links + + $node->set_link('http://localhost:1978/node/another', 'another node label', $credit); + +If C<$credit> is negative, link is removed. + +=cut + +sub set_link { + my $self = shift; + my ($url, $label, $credit) = @_; + + return unless ($self->{url}); + croak "mode credit be number, not '$credit'" unless ($credit =~ m/^\d+$/); + + my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); + $reqbody .= '&credit=' . $credit if ($credit > 0); + + $self->shuttle_url( $self->{url} . '/_set_link', + 'text/plain', + $reqbody, + undef + ) == 200; +} + + +=head1 PRIVATE METHODS + +You could call those directly, but you don't have to. I hope. + +=head2 _set_info + +Set information for node + + $node->_set_info; + +=cut + +sub _set_info { + my $self = shift; + + $self->{status} = -1; + return unless ($self->{url}); + + my $resbody; + my $rv = $self->shuttle_url( $self->{url} . '/inform', + 'text/plain', + undef, + \$resbody, + ); + + return if ($rv != 200 || !$resbody); + + chomp($resbody); + + ( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) = + split(/\t/, $resbody, 5); + +} + ### =head1 EXPORT