20 |
my $node = new Search::Estraier::Node( |
my $node = new Search::Estraier::Node( |
21 |
url => 'http://localhost:1978/node/test', |
url => 'http://localhost:1978/node/test', |
22 |
user => 'admin', |
user => 'admin', |
23 |
passwd => 'admin' |
passwd => 'admin', |
24 |
|
create => 1, |
25 |
|
label => 'Label for node', |
26 |
|
croak_on_error => 1, |
27 |
); |
); |
28 |
|
|
29 |
# create document |
# create document |
877 |
url => 'http://localhost:1978/node/test', |
url => 'http://localhost:1978/node/test', |
878 |
user => 'admin', |
user => 'admin', |
879 |
passwd => 'admin' |
passwd => 'admin' |
880 |
|
create => 1, |
881 |
|
label => 'optional node label', |
882 |
debug => 1, |
debug => 1, |
883 |
croak_on_error => 1 |
croak_on_error => 1 |
884 |
); |
); |
899 |
|
|
900 |
password for authentication |
password for authentication |
901 |
|
|
902 |
|
=item create |
903 |
|
|
904 |
|
create node if it doesn't exists |
905 |
|
|
906 |
|
=item label |
907 |
|
|
908 |
|
optional label for new node if C<create> is used |
909 |
|
|
910 |
=item debug |
=item debug |
911 |
|
|
912 |
dumps a B<lot> of debugging output |
dumps a B<lot> of debugging output |
950 |
size => -1.0, |
size => -1.0, |
951 |
}; |
}; |
952 |
|
|
953 |
|
if ($self->{create}) { |
954 |
|
eval { |
955 |
|
$self->name; |
956 |
|
}; |
957 |
|
if ($@) { |
958 |
|
my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#); |
959 |
|
croak "can't find node name in '$self->{url}'" unless ($name); |
960 |
|
my $label = $self->{label} || $name; |
961 |
|
$self->master( |
962 |
|
action => 'nodeadd', |
963 |
|
name => $name, |
964 |
|
label => $label, |
965 |
|
) || croak "can't create node $name ($label)"; |
966 |
|
} |
967 |
|
} |
968 |
|
|
969 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
970 |
} |
} |
971 |
|
|
1729 |
return $self->{inform}->{links}; |
return $self->{inform}->{links}; |
1730 |
} |
} |
1731 |
|
|
1732 |
|
=head2 master |
1733 |
|
|
1734 |
|
Set actions on Hyper Estraier node master (C<estmaster> process) |
1735 |
|
|
1736 |
|
$node->master( |
1737 |
|
action => 'sync' |
1738 |
|
); |
1739 |
|
|
1740 |
|
All available actions are documented in |
1741 |
|
L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol> |
1742 |
|
|
1743 |
|
=cut |
1744 |
|
|
1745 |
|
my $estmaster_rest = { |
1746 |
|
shutdown => { |
1747 |
|
status => 202, |
1748 |
|
}, |
1749 |
|
sync => { |
1750 |
|
status => 202, |
1751 |
|
}, |
1752 |
|
backup => { |
1753 |
|
status => 202, |
1754 |
|
}, |
1755 |
|
userlist => { |
1756 |
|
status => 200, |
1757 |
|
returns => [ qw/name passwd flags fname misc/ ], |
1758 |
|
}, |
1759 |
|
useradd => { |
1760 |
|
required => [ qw/name passwd flags/ ], |
1761 |
|
optional => [ qw/fname misc/ ], |
1762 |
|
status => 200, |
1763 |
|
}, |
1764 |
|
userdel => { |
1765 |
|
required => [ qw/name/ ], |
1766 |
|
status => 200, |
1767 |
|
}, |
1768 |
|
nodelist => { |
1769 |
|
status => 200, |
1770 |
|
returns => [ qw/name label doc_num word_num size/ ], |
1771 |
|
}, |
1772 |
|
nodeadd => { |
1773 |
|
required => [ qw/name/ ], |
1774 |
|
optional => [ qw/label/ ], |
1775 |
|
status => 200, |
1776 |
|
}, |
1777 |
|
nodedel => { |
1778 |
|
required => [ qw/name/ ], |
1779 |
|
status => 200, |
1780 |
|
}, |
1781 |
|
nodeclr => { |
1782 |
|
required => [ qw/name/ ], |
1783 |
|
status => 200, |
1784 |
|
}, |
1785 |
|
nodertt => { |
1786 |
|
status => 200, |
1787 |
|
}, |
1788 |
|
}; |
1789 |
|
|
1790 |
|
sub master { |
1791 |
|
my $self = shift; |
1792 |
|
|
1793 |
|
my $args = {@_}; |
1794 |
|
|
1795 |
|
# have action? |
1796 |
|
my $action = $args->{action} || croak "need action, available: ", |
1797 |
|
join(", ",keys %{ $estmaster_rest }); |
1798 |
|
|
1799 |
|
# check if action is valid |
1800 |
|
my $rest = $estmaster_rest->{$action}; |
1801 |
|
croak "action '$action' is not supported, available actions: ", |
1802 |
|
join(", ",keys %{ $estmaster_rest }) unless ($rest); |
1803 |
|
|
1804 |
|
croak "BUG: action '$action' needs return status" unless ($rest->{status}); |
1805 |
|
|
1806 |
|
my @args; |
1807 |
|
|
1808 |
|
if ($rest->{required} || $rest->{optional}) { |
1809 |
|
|
1810 |
|
map { |
1811 |
|
croak "need parametar '$_' for action '$action'" unless ($args->{$_}); |
1812 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ); |
1813 |
|
} ( @{ $rest->{required} } ); |
1814 |
|
|
1815 |
|
map { |
1816 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); |
1817 |
|
} ( @{ $rest->{optional} } ); |
1818 |
|
|
1819 |
|
} |
1820 |
|
|
1821 |
|
my $uri = new URI( $self->{url} ); |
1822 |
|
|
1823 |
|
my $resbody; |
1824 |
|
|
1825 |
|
my $status = $self->shuttle_url( |
1826 |
|
'http://' . $uri->host_port . '/master?action=' . $action , |
1827 |
|
'application/x-www-form-urlencoded', |
1828 |
|
join('&', @args), |
1829 |
|
\$resbody, |
1830 |
|
1, |
1831 |
|
) or confess "shuttle_url failed"; |
1832 |
|
|
1833 |
|
if ($status == $rest->{status}) { |
1834 |
|
if ($rest->{returns} && wantarray) { |
1835 |
|
|
1836 |
|
my @results; |
1837 |
|
my $fields = $#{$rest->{returns}}; |
1838 |
|
|
1839 |
|
foreach my $line ( split(/[\r\n]/,$resbody) ) { |
1840 |
|
my @e = split(/\t/, $line, $fields + 1); |
1841 |
|
my $row; |
1842 |
|
foreach my $i ( 0 .. $fields) { |
1843 |
|
$row->{ $rest->{returns}->[$i] } = $e[ $i ]; |
1844 |
|
} |
1845 |
|
push @results, $row; |
1846 |
|
} |
1847 |
|
|
1848 |
|
return @results; |
1849 |
|
|
1850 |
|
} elsif ($resbody) { |
1851 |
|
chomp $resbody; |
1852 |
|
return $resbody; |
1853 |
|
} else { |
1854 |
|
return 0E0; |
1855 |
|
} |
1856 |
|
} |
1857 |
|
|
1858 |
|
carp "expected status $rest->{status}, but got $status"; |
1859 |
|
return undef; |
1860 |
|
} |
1861 |
|
|
1862 |
=head1 PRIVATE METHODS |
=head1 PRIVATE METHODS |
1863 |
|
|