--- trunk/Estraier.pm 2006/05/09 12:21:26 134 +++ trunk/Estraier.pm 2006/05/15 22:26:08 150 @@ -4,7 +4,7 @@ use strict; use warnings; -our $VERSION = '0.06_1'; +our $VERSION = '0.07_1'; =head1 NAME @@ -20,7 +20,10 @@ my $node = new Search::Estraier::Node( url => 'http://localhost:1978/node/test', user => 'admin', - passwd => 'admin' + passwd => 'admin', + create => 1, + label => 'Label for node', + croak_on_error => 1, ); # create document @@ -874,6 +877,8 @@ url => 'http://localhost:1978/node/test', user => 'admin', passwd => 'admin' + create => 1, + label => 'optional node label', debug => 1, croak_on_error => 1 ); @@ -894,6 +899,14 @@ password for authentication +=item create + +create node if it doesn't exists + +=item label + +optional label for new node if C is used + =item debug dumps a B of debugging output @@ -937,6 +950,19 @@ size => -1.0, }; + if ($self->{create}) { + if (! eval { $self->name } || $@) { + my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#); + croak "can't find node name in '$self->{url}'" unless ($name); + my $label = $self->{label} || $name; + $self->master( + action => 'nodeadd', + name => $name, + label => $label, + ) || croak "can't create node $name ($label)"; + } + } + $self ? return $self : return undef; } @@ -1058,11 +1084,15 @@ my $id = shift || return; return unless ($self->{url}); croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/); - $self->shuttle_url( $self->{url} . '/out_doc', + if ($self->shuttle_url( $self->{url} . '/out_doc', 'application/x-www-form-urlencoded', "id=$id", undef - ) == 200; + ) == 200) { + $self->_set_info; + return $id; + } + return undef; } @@ -1080,11 +1110,15 @@ my $self = shift; my $uri = shift || return; return unless ($self->{url}); - $self->shuttle_url( $self->{url} . '/out_doc', + if ($self->shuttle_url( $self->{url} . '/out_doc', 'application/x-www-form-urlencoded', "uri=" . uri_escape($uri), undef - ) == 200; + ) == 200) { + $self->_set_info; + return $uri; + } + return undef; } @@ -1656,6 +1690,7 @@ $self->_set_info; return 1; } + return undef; } =head2 admins @@ -1725,32 +1760,32 @@ }, userlist => { status => 200, - returns => qw/name passwd flags fname misc/, + returns => [ qw/name passwd flags fname misc/ ], }, useradd => { - required => qw/name passwd flags/, - optional => qw/fname misc/, + required => [ qw/name passwd flags/ ], + optional => [ qw/fname misc/ ], status => 200, }, userdel => { - required => qw/name/, + required => [ qw/name/ ], status => 200, }, nodelist => { status => 200, - returns => qw/name label doc_num word_num size/, + returns => [ qw/name label doc_num word_num size/ ], }, nodeadd => { - required => qw/name/, - optional => qw/label/, + required => [ qw/name/ ], + optional => [ qw/label/ ], status => 200, }, nodedel => { - required => qw/name/, + required => [ qw/name/ ], status => 200, }, nodeclr => { - required => qw/name/, + required => [ qw/name/ ], status => 200, }, nodertt => { @@ -1781,11 +1816,11 @@ map { croak "need parametar '$_' for action '$action'" unless ($args->{$_}); push @args, $_ . '=' . uri_escape( $args->{$_} ); - } ( keys %{ $rest->{required} } ); + } ( @{ $rest->{required} } ); map { push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); - } ( keys %{ $rest->{optional} } ); + } ( @{ $rest->{optional} } ); } @@ -1793,33 +1828,45 @@ my $resbody; - if ($self->shuttle_url( + my $status = $self->shuttle_url( 'http://' . $uri->host_port . '/master?action=' . $action , 'application/x-www-form-urlencoded', join('&', @args), \$resbody, 1, - ) == $rest->{status}) { - return 0E0 unless ($rest->{returns}); + ) or confess "shuttle_url failed"; + + if ($status == $rest->{status}) { - if (wantarray) { + # refresh node info after sync + $self->_set_info if ($action eq 'sync'); + + if ($rest->{returns} && wantarray) { my @results; + my $fields = $#{$rest->{returns}}; foreach my $line ( split(/[\r\n]/,$resbody) ) { - my @e = split(/\t/, $line); + my @e = split(/\t/, $line, $fields + 1); my $row; - map { $row->{$_} = shift @e; } @{ $rest->{returns} }; + foreach my $i ( 0 .. $fields) { + $row->{ $rest->{returns}->[$i] } = $e[ $i ]; + } push @results, $row; } return @results; - } else { - carp "calling master action '$action', but not expecting array back, returning whole body"; + } elsif ($resbody) { + chomp $resbody; return $resbody; + } else { + return 0E0; } } + + carp "expected status $rest->{status}, but got $status"; + return undef; } =head1 PRIVATE METHODS