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

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

revision 128 by dpavlin, Mon May 8 12:00:43 2006 UTC revision 136 by dpavlin, Tue May 9 14:03:36 2006 UTC
# Line 872  or in more verbose form Line 872  or in more verbose form
872    
873    my $node = new Search::HyperEstraier::Node(    my $node = new Search::HyperEstraier::Node(
874          url => 'http://localhost:1978/node/test',          url => 'http://localhost:1978/node/test',
875            user => 'admin',
876            passwd => 'admin'
877          debug => 1,          debug => 1,
878          croak_on_error => 1          croak_on_error => 1
879    );    );
# Line 884  with following arguments: Line 886  with following arguments:
886    
887  URL to node  URL to node
888    
889    =item user
890    
891    specify username for node server authentication
892    
893    =item passwd
894    
895    password for authentication
896    
897  =item debug  =item debug
898    
899  dumps a B<lot> of debugging output  dumps a B<lot> of debugging output
# Line 914  sub new { Line 924  sub new {
924          if ($#_ == 0) {          if ($#_ == 0) {
925                  $self->{url} = shift;                  $self->{url} = shift;
926          } else {          } else {
                 my $args = {@_};  
   
927                  %$self = ( %$self, @_ );                  %$self = ( %$self, @_ );
928    
929                    $self->set_auth( $self->{user}, $self->{passwd} ) if ($self->{user});
930    
931                  warn "## Node debug on\n" if ($self->{debug});                  warn "## Node debug on\n" if ($self->{debug});
932          }          }
933    
# Line 1690  sub links { Line 1700  sub links {
1700          return $self->{inform}->{links};          return $self->{inform}->{links};
1701  }  }
1702    
1703    =head2 master
1704    
1705    Set actions on Hyper Estraier node master (C<estmaster> process)
1706    
1707      $node->master(
1708            action => 'sync'
1709      );
1710    
1711    All available actions are documented in
1712    L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol>
1713    
1714    =cut
1715    
1716    my $estmaster_rest = {
1717            shutdown => {
1718                    status => 202,
1719            },
1720            sync => {
1721                    status => 202,
1722            },
1723            backup => {
1724                    status => 202,
1725            },
1726            userlist => {
1727                    status => 200,
1728                    returns => [ qw/name passwd flags fname misc/ ],
1729            },
1730            useradd => {
1731                    required => [ qw/name passwd flags/ ],
1732                    optional => [ qw/fname misc/ ],
1733                    status => 200,
1734            },
1735            userdel => {
1736                    required => [ qw/name/ ],
1737                    status => 200,
1738            },
1739            nodelist => {
1740                    status => 200,
1741                    returns => [ qw/name label doc_num word_num size/ ],
1742            },
1743            nodeadd => {
1744                    required => [ qw/name/ ],
1745                    optional => [ qw/label/ ],
1746                    status => 200,
1747            },
1748            nodedel => {
1749                    required => [ qw/name/ ],
1750                    status => 200,
1751            },
1752            nodeclr => {
1753                    required => [ qw/name/ ],
1754                    status => 200,
1755            },
1756            nodertt => {
1757                    status => 200,  
1758            },
1759    };
1760    
1761    sub master {
1762            my $self = shift;
1763    
1764            my $args = {@_};
1765    
1766            # have action?
1767            my $action = $args->{action} || croak "need action, available: ",
1768                    join(", ",keys %{ $estmaster_rest });
1769    
1770            # check if action is valid
1771            my $rest = $estmaster_rest->{$action};
1772            croak "action '$action' is not supported, available actions: ",
1773                    join(", ",keys %{ $estmaster_rest }) unless ($rest);
1774    
1775            croak "BUG: action '$action' needs return status" unless ($rest->{status});
1776    
1777            my @args;
1778    
1779            if ($rest->{required} || $rest->{optional}) {
1780    
1781                    map {
1782                            croak "need parametar '$_' for action '$action'" unless ($args->{$_});
1783                            push @args, $_ . '=' . uri_escape( $args->{$_} );
1784                    } ( @{ $rest->{required} } );
1785    
1786                    map {
1787                            push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_});
1788                    } ( @{ $rest->{optional} } );
1789    
1790            }
1791    
1792            my $uri = new URI( $self->{url} );
1793    
1794            my $resbody;
1795    
1796            my $status = $self->shuttle_url(
1797                    'http://' . $uri->host_port . '/master?action=' . $action ,
1798                    'application/x-www-form-urlencoded',
1799                    join('&', @args),
1800                    \$resbody,
1801                    1,
1802            ) or confess "shuttle_url failed";
1803    
1804            if ($status == $rest->{status}) {
1805                    if ($rest->{returns} && wantarray) {
1806    
1807                            my @results;
1808                            my $fields = $#{$rest->{returns}};
1809    
1810                            foreach my $line ( split(/[\r\n]/,$resbody) ) {
1811                                    my @e = split(/\t/, $line, $fields + 1);
1812                                    my $row;
1813                                    foreach my $i ( 0 .. $fields) {
1814                                            $row->{ $rest->{returns}->[$i] } = $e[ $i ];
1815                                    }
1816                                    push @results, $row;
1817                            }
1818    
1819                            return @results;
1820    
1821                    } elsif ($resbody) {
1822                            chomp $resbody;
1823                            return $resbody;
1824                    } else {
1825                            return 0E0;
1826                    }
1827            }
1828    
1829            carp "expected status $rest->{status}, but got $status";
1830            return undef;
1831    }
1832    
1833  =head1 PRIVATE METHODS  =head1 PRIVATE METHODS
1834    

Legend:
Removed from v.128  
changed lines
  Added in v.136

  ViewVC Help
Powered by ViewVC 1.1.26