--- trunk/Estraier.pm 2006/01/06 00:04:28 43 +++ trunk/Estraier.pm 2006/01/06 01:51:28 47 @@ -688,7 +688,7 @@ package Search::Estraier::Node; -use Carp qw/carp croak/; +use Carp qw/carp croak confess/; use URI; use MIME::Base64; use IO::Socket::INET; @@ -818,7 +818,7 @@ sub put_doc { my $self = shift; my $doc = shift || return; - return unless ($self->{url}); + return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); $self->shuttle_url( $self->{url} . '/put_doc', 'text/x-estraier-draft', $doc->dump_draft, @@ -854,7 +854,7 @@ Remove a registrated document using it's uri - $node->out_doc_by_uri( 'file:///document_url' ) or "can't remove document"; + $node->out_doc_by_uri( 'file:///document/uri/42' ) or "can't remove document"; Return true on success or false on failture. @@ -885,7 +885,7 @@ sub edit_doc { my $self = shift; my $doc = shift || return; - return unless ($self->{url}); + return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); $self->shuttle_url( $self->{url} . '/edit_doc', 'text/x-estraier-draft', $doc->dump_draft, @@ -910,11 +910,12 @@ return $self->_fetch_doc( id => $id ); } + =head2 get_doc_by_uri Retreive document - my $doc = $node->get_doc_by_uri( 'file:///document_uri' ) or die "can't get document"; + my $doc = $node->get_doc_by_uri( 'file:///document/uri/42' ) or die "can't get document"; Return true on success or false on failture. @@ -926,29 +927,118 @@ return $self->_fetch_doc( uri => $uri ); } + +=head2 etch_doc + +Exctract document keywords + + my $keywords = $node->etch_doc( document_id ) or die "can't etch document"; + +=cut + +sub erch_doc { + my $self = shift; + my $id = shift || return; + return $self->_fetch_doc( id => $id, etch => 1 ); +} + +=head2 etch_doc_by_uri + +Retreive document + + my $keywords = $node->etch_doc_by_uri( 'file:///document/uri/42' ) or die "can't etch document"; + +Return true on success or false on failture. + +=cut + +sub etch_doc_by_uri { + my $self = shift; + my $uri = shift || return; + return $self->_fetch_doc( uri => $uri, etch => 1 ); +} + + +=head2 uri_to_id + +Get ID of document specified by URI + + my $id = $node->uri_to_id( 'file:///document/uri/42' ); + +=cut + +sub uri_to_id { + my $self = shift; + my $uri = shift || return; + return $self->_fetch_doc( uri => $uri, path => '/uri_to_id', chomp_resbody => 1 ); +} + + =head2 _fetch_doc -Private function used for implementation of C and C. +Private function used for implementing of C, C, +C, C. - my $doc = $node->fetch_doc( id => 42 ); - my $doc = $node->fetch_doc( uri => 'file://uri/42' ); + # this will decode received draft into Search::Estraier::Document object + my $doc = $node->_fetch_doc( id => 42 ); + my $doc = $node->_fetch_doc( uri => 'file:///document/uri/42' ); + + # to extract keywords, add etch + my $doc = $node->_fetch_doc( id => 42, etch => 1 ); + my $doc = $node->_fetch_doc( uri => 'file:///document/uri/42', etch => 1 ); + + # more general form which allows implementation of + # uri_to_id + my $id = $node->_fetch_doc( + uri => 'file:///document/uri/42', + path => '/uri_to_id', + chomp_resbody => 1 + ); =cut sub _fetch_doc { my $self = shift; - my ($name,$val) = @_; - return unless ($name && defined($val) && $self->{url}); - if ($name eq 'id') { - croak "id must be numberm not '$val'" unless ($val =~ m/^\d+$/); + my $a = {@_}; + return unless ( ($a->{id} || $a->{uri}) && $self->{url} ); + + my ($arg, $resbody); + + my $path = $a->{path} || '/get_doc'; + $path = '/etch_doc' if ($a->{etch}); + + if ($a->{id}) { + croak "id must be numberm not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/); + $arg = 'id=' . $a->{id}; + } elsif ($a->{uri}) { + $arg = 'uri=' . $a->{uri}; + } else { + confess "unhandled argument. Need id or uri."; } - my $rv = $self->shuttle_url( $self->{url} . '/get_doc', + + my $rv = $self->shuttle_url( $self->{url} . $path, 'application/x-www-form-urlencoded', - "$name=$val", - my $draft, + $arg, + \$resbody, ); + return if ($rv != 200); - return new Search::Estraier::Document($draft); + + if ($a->{etch}) { + $self->{kwords} = {}; + return +{} unless ($resbody); + foreach my $l (split(/\n/, $resbody)) { + my ($k,$v) = split(/\t/, $l, 2); + $self->{kwords}->{$k} = $v if ($v); + } + return $self->{kwords}; + } elsif ($a->{chomp_resbody}) { + return unless (defined($resbody)); + chomp($resbody); + return $resbody; + } else { + return new Search::Estraier::Document($resbody); + } }