4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.04_2'; |
our $VERSION = '0.07_1'; |
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
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 |
602 |
} |
} |
603 |
|
|
604 |
|
|
605 |
|
=head2 set_skip |
606 |
|
|
607 |
|
Set number of skipped documents from beginning of results |
608 |
|
|
609 |
|
$cond->set_skip(42); |
610 |
|
|
611 |
|
Similar to C<offset> in RDBMS. |
612 |
|
|
613 |
|
=cut |
614 |
|
|
615 |
|
sub set_skip { |
616 |
|
my $self = shift; |
617 |
|
$self->{skip} = shift; |
618 |
|
} |
619 |
|
|
620 |
|
=head2 skip |
621 |
|
|
622 |
|
Return skip for this condition. |
623 |
|
|
624 |
|
print $cond->skip; |
625 |
|
|
626 |
|
=cut |
627 |
|
|
628 |
|
sub skip { |
629 |
|
my $self = shift; |
630 |
|
return $self->{skip}; |
631 |
|
} |
632 |
|
|
633 |
|
|
634 |
package Search::Estraier::ResultDocument; |
package Search::Estraier::ResultDocument; |
635 |
|
|
636 |
use Carp qw/croak/; |
use Carp qw/croak/; |
821 |
return $self->{hints}->{$key}; |
return $self->{hints}->{$key}; |
822 |
} |
} |
823 |
|
|
824 |
=head2 hits |
=head2 hints |
825 |
|
|
826 |
More perlish version of C<hint>. This one returns hash. |
More perlish version of C<hint>. This one returns hash. |
827 |
|
|
875 |
|
|
876 |
my $node = new Search::HyperEstraier::Node( |
my $node = new Search::HyperEstraier::Node( |
877 |
url => 'http://localhost:1978/node/test', |
url => 'http://localhost:1978/node/test', |
878 |
|
user => 'admin', |
879 |
|
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 |
); |
); |
891 |
|
|
892 |
URL to node |
URL to node |
893 |
|
|
894 |
|
=item user |
895 |
|
|
896 |
|
specify username for node server authentication |
897 |
|
|
898 |
|
=item passwd |
899 |
|
|
900 |
|
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 |
926 |
my $self = { |
my $self = { |
927 |
pxport => -1, |
pxport => -1, |
928 |
timeout => 0, # this used to be -1 |
timeout => 0, # this used to be -1 |
|
dnum => -1, |
|
|
wnum => -1, |
|
|
size => -1.0, |
|
929 |
wwidth => 480, |
wwidth => 480, |
930 |
hwidth => 96, |
hwidth => 96, |
931 |
awidth => 96, |
awidth => 96, |
932 |
status => -1, |
status => -1, |
933 |
}; |
}; |
934 |
|
|
935 |
bless($self, $class); |
bless($self, $class); |
936 |
|
|
937 |
if ($#_ == 0) { |
if ($#_ == 0) { |
938 |
$self->{url} = shift; |
$self->{url} = shift; |
939 |
} else { |
} else { |
|
my $args = {@_}; |
|
|
|
|
940 |
%$self = ( %$self, @_ ); |
%$self = ( %$self, @_ ); |
941 |
|
|
942 |
|
$self->set_auth( $self->{user}, $self->{passwd} ) if ($self->{user}); |
943 |
|
|
944 |
warn "## Node debug on\n" if ($self->{debug}); |
warn "## Node debug on\n" if ($self->{debug}); |
945 |
} |
} |
946 |
|
|
947 |
|
$self->{inform} = { |
948 |
|
dnum => -1, |
949 |
|
wnum => -1, |
950 |
|
size => -1.0, |
951 |
|
}; |
952 |
|
|
953 |
|
if ($self->{create}) { |
954 |
|
if (! eval { $self->name } || $@) { |
955 |
|
my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#); |
956 |
|
croak "can't find node name in '$self->{url}'" unless ($name); |
957 |
|
my $label = $self->{label} || $name; |
958 |
|
$self->master( |
959 |
|
action => 'nodeadd', |
960 |
|
name => $name, |
961 |
|
label => $label, |
962 |
|
) || croak "can't create node $name ($label)"; |
963 |
|
} |
964 |
|
} |
965 |
|
|
966 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
967 |
} |
} |
968 |
|
|
1339 |
|
|
1340 |
sub name { |
sub name { |
1341 |
my $self = shift; |
my $self = shift; |
1342 |
$self->_set_info unless ($self->{name}); |
$self->_set_info unless ($self->{inform}->{name}); |
1343 |
return $self->{name}; |
return $self->{inform}->{name}; |
1344 |
} |
} |
1345 |
|
|
1346 |
|
|
1352 |
|
|
1353 |
sub label { |
sub label { |
1354 |
my $self = shift; |
my $self = shift; |
1355 |
$self->_set_info unless ($self->{label}); |
$self->_set_info unless ($self->{inform}->{label}); |
1356 |
return $self->{label}; |
return $self->{inform}->{label}; |
1357 |
} |
} |
1358 |
|
|
1359 |
|
|
1365 |
|
|
1366 |
sub doc_num { |
sub doc_num { |
1367 |
my $self = shift; |
my $self = shift; |
1368 |
$self->_set_info if ($self->{dnum} < 0); |
$self->_set_info if ($self->{inform}->{dnum} < 0); |
1369 |
return $self->{dnum}; |
return $self->{inform}->{dnum}; |
1370 |
} |
} |
1371 |
|
|
1372 |
|
|
1378 |
|
|
1379 |
sub word_num { |
sub word_num { |
1380 |
my $self = shift; |
my $self = shift; |
1381 |
$self->_set_info if ($self->{wnum} < 0); |
$self->_set_info if ($self->{inform}->{wnum} < 0); |
1382 |
return $self->{wnum}; |
return $self->{inform}->{wnum}; |
1383 |
} |
} |
1384 |
|
|
1385 |
|
|
1391 |
|
|
1392 |
sub size { |
sub size { |
1393 |
my $self = shift; |
my $self = shift; |
1394 |
$self->_set_info if ($self->{size} < 0); |
$self->_set_info if ($self->{inform}->{size} < 0); |
1395 |
return $self->{size}; |
return $self->{inform}->{size}; |
1396 |
} |
} |
1397 |
|
|
1398 |
|
|
1425 |
); |
); |
1426 |
return if ($rv != 200); |
return if ($rv != 200); |
1427 |
|
|
1428 |
my (@docs, $hints); |
my @records = split /--------\[.*?\]--------(?::END)?\r?\n/, $resbody; |
1429 |
|
my $hintsText = splice @records, 0, 2; # starts with empty record |
1430 |
my @lines = split(/\n/, $resbody); |
my $hints = { $hintsText =~ m/^(.*?)\t(.*?)$/gsm }; |
1431 |
return unless (@lines); |
|
1432 |
|
# process records |
1433 |
my $border = $lines[0]; |
my $docs = []; |
1434 |
my $isend = 0; |
foreach my $record (@records) |
1435 |
my $lnum = 1; |
{ |
1436 |
|
# split into keys and snippets |
1437 |
while ( $lnum <= $#lines ) { |
my ($keys, $snippet) = $record =~ m/^(.*?)\n\n(.*?)$/s; |
1438 |
my $line = $lines[$lnum]; |
|
1439 |
$lnum++; |
# create document hash |
1440 |
|
my $doc = { $keys =~ m/^(.*?)=(.*?)$/gsm }; |
1441 |
#warn "## $line\n"; |
$doc->{'@keywords'} = $doc->{keywords}; |
1442 |
if ($line && $line =~ m/^\Q$border\E(:END)*$/) { |
($doc->{keywords}) = $keys =~ m/^%VECTOR\t(.*?)$/gm; |
1443 |
$isend = $1; |
$doc->{snippet} = $snippet; |
1444 |
last; |
|
1445 |
} |
push @$docs, new Search::Estraier::ResultDocument( |
1446 |
|
attrs => $doc, |
1447 |
if ($line =~ /\t/) { |
uri => $doc->{'@uri'}, |
1448 |
my ($k,$v) = split(/\t/, $line, 2); |
snippet => $snippet, |
1449 |
$hints->{$k} = $v; |
keywords => $doc->{'keywords'}, |
1450 |
} |
); |
|
} |
|
|
|
|
|
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$/); |
|
|
} |
|
|
|
|
1451 |
} |
} |
1452 |
|
|
1453 |
if (! $isend) { |
return new Search::Estraier::NodeResult( docs => $docs, hints => $hints ); |
|
warn "received result doesn't have :END\n$resbody"; |
|
|
return; |
|
|
} |
|
|
|
|
|
#warn Dumper(\@docs, $hints); |
|
|
|
|
|
return new Search::Estraier::NodeResult( docs => \@docs, hints => $hints ); |
|
1454 |
} |
} |
1455 |
|
|
1456 |
|
|
1499 |
push @args, 'wwidth=' . $self->{wwidth}; |
push @args, 'wwidth=' . $self->{wwidth}; |
1500 |
push @args, 'hwidth=' . $self->{hwidth}; |
push @args, 'hwidth=' . $self->{hwidth}; |
1501 |
push @args, 'awidth=' . $self->{awidth}; |
push @args, 'awidth=' . $self->{awidth}; |
1502 |
|
push @args, 'skip=' . $self->{skip} if ($self->{skip}); |
1503 |
|
|
1504 |
return join('&', @args); |
return join('&', @args); |
1505 |
} |
} |
1694 |
|
|
1695 |
sub admins { |
sub admins { |
1696 |
my $self = shift; |
my $self = shift; |
1697 |
$self->_set_info unless ($self->{name}); |
$self->_set_info unless ($self->{inform}->{name}); |
1698 |
return $self->{admins}; |
return $self->{inform}->{admins}; |
1699 |
} |
} |
1700 |
|
|
1701 |
=head2 guests |
=head2 guests |
1708 |
|
|
1709 |
sub guests { |
sub guests { |
1710 |
my $self = shift; |
my $self = shift; |
1711 |
$self->_set_info unless ($self->{name}); |
$self->_set_info unless ($self->{inform}->{name}); |
1712 |
return $self->{guests}; |
return $self->{inform}->{guests}; |
1713 |
} |
} |
1714 |
|
|
1715 |
=head2 links |
=head2 links |
1722 |
|
|
1723 |
sub links { |
sub links { |
1724 |
my $self = shift; |
my $self = shift; |
1725 |
$self->_set_info unless ($self->{name}); |
$self->_set_info unless ($self->{inform}->{name}); |
1726 |
return $self->{links}; |
return $self->{inform}->{links}; |
1727 |
} |
} |
1728 |
|
|
1729 |
|
=head2 master |
1730 |
|
|
1731 |
|
Set actions on Hyper Estraier node master (C<estmaster> process) |
1732 |
|
|
1733 |
|
$node->master( |
1734 |
|
action => 'sync' |
1735 |
|
); |
1736 |
|
|
1737 |
|
All available actions are documented in |
1738 |
|
L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol> |
1739 |
|
|
1740 |
|
=cut |
1741 |
|
|
1742 |
|
my $estmaster_rest = { |
1743 |
|
shutdown => { |
1744 |
|
status => 202, |
1745 |
|
}, |
1746 |
|
sync => { |
1747 |
|
status => 202, |
1748 |
|
}, |
1749 |
|
backup => { |
1750 |
|
status => 202, |
1751 |
|
}, |
1752 |
|
userlist => { |
1753 |
|
status => 200, |
1754 |
|
returns => [ qw/name passwd flags fname misc/ ], |
1755 |
|
}, |
1756 |
|
useradd => { |
1757 |
|
required => [ qw/name passwd flags/ ], |
1758 |
|
optional => [ qw/fname misc/ ], |
1759 |
|
status => 200, |
1760 |
|
}, |
1761 |
|
userdel => { |
1762 |
|
required => [ qw/name/ ], |
1763 |
|
status => 200, |
1764 |
|
}, |
1765 |
|
nodelist => { |
1766 |
|
status => 200, |
1767 |
|
returns => [ qw/name label doc_num word_num size/ ], |
1768 |
|
}, |
1769 |
|
nodeadd => { |
1770 |
|
required => [ qw/name/ ], |
1771 |
|
optional => [ qw/label/ ], |
1772 |
|
status => 200, |
1773 |
|
}, |
1774 |
|
nodedel => { |
1775 |
|
required => [ qw/name/ ], |
1776 |
|
status => 200, |
1777 |
|
}, |
1778 |
|
nodeclr => { |
1779 |
|
required => [ qw/name/ ], |
1780 |
|
status => 200, |
1781 |
|
}, |
1782 |
|
nodertt => { |
1783 |
|
status => 200, |
1784 |
|
}, |
1785 |
|
}; |
1786 |
|
|
1787 |
|
sub master { |
1788 |
|
my $self = shift; |
1789 |
|
|
1790 |
|
my $args = {@_}; |
1791 |
|
|
1792 |
|
# have action? |
1793 |
|
my $action = $args->{action} || croak "need action, available: ", |
1794 |
|
join(", ",keys %{ $estmaster_rest }); |
1795 |
|
|
1796 |
|
# check if action is valid |
1797 |
|
my $rest = $estmaster_rest->{$action}; |
1798 |
|
croak "action '$action' is not supported, available actions: ", |
1799 |
|
join(", ",keys %{ $estmaster_rest }) unless ($rest); |
1800 |
|
|
1801 |
|
croak "BUG: action '$action' needs return status" unless ($rest->{status}); |
1802 |
|
|
1803 |
|
my @args; |
1804 |
|
|
1805 |
|
if ($rest->{required} || $rest->{optional}) { |
1806 |
|
|
1807 |
|
map { |
1808 |
|
croak "need parametar '$_' for action '$action'" unless ($args->{$_}); |
1809 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ); |
1810 |
|
} ( @{ $rest->{required} } ); |
1811 |
|
|
1812 |
|
map { |
1813 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); |
1814 |
|
} ( @{ $rest->{optional} } ); |
1815 |
|
|
1816 |
|
} |
1817 |
|
|
1818 |
|
my $uri = new URI( $self->{url} ); |
1819 |
|
|
1820 |
|
my $resbody; |
1821 |
|
|
1822 |
|
my $status = $self->shuttle_url( |
1823 |
|
'http://' . $uri->host_port . '/master?action=' . $action , |
1824 |
|
'application/x-www-form-urlencoded', |
1825 |
|
join('&', @args), |
1826 |
|
\$resbody, |
1827 |
|
1, |
1828 |
|
) or confess "shuttle_url failed"; |
1829 |
|
|
1830 |
|
if ($status == $rest->{status}) { |
1831 |
|
|
1832 |
|
# refresh node info after sync |
1833 |
|
$self->_set_info if ($action eq 'sync'); |
1834 |
|
|
1835 |
|
if ($rest->{returns} && wantarray) { |
1836 |
|
|
1837 |
|
my @results; |
1838 |
|
my $fields = $#{$rest->{returns}}; |
1839 |
|
|
1840 |
|
foreach my $line ( split(/[\r\n]/,$resbody) ) { |
1841 |
|
my @e = split(/\t/, $line, $fields + 1); |
1842 |
|
my $row; |
1843 |
|
foreach my $i ( 0 .. $fields) { |
1844 |
|
$row->{ $rest->{returns}->[$i] } = $e[ $i ]; |
1845 |
|
} |
1846 |
|
push @results, $row; |
1847 |
|
} |
1848 |
|
|
1849 |
|
return @results; |
1850 |
|
|
1851 |
|
} elsif ($resbody) { |
1852 |
|
chomp $resbody; |
1853 |
|
return $resbody; |
1854 |
|
} else { |
1855 |
|
return 0E0; |
1856 |
|
} |
1857 |
|
} |
1858 |
|
|
1859 |
|
carp "expected status $rest->{status}, but got $status"; |
1860 |
|
return undef; |
1861 |
|
} |
1862 |
|
|
1863 |
=head1 PRIVATE METHODS |
=head1 PRIVATE METHODS |
1864 |
|
|
1888 |
return if ($rv != 200 || !$resbody); |
return if ($rv != 200 || !$resbody); |
1889 |
|
|
1890 |
my @lines = split(/[\r\n]/,$resbody); |
my @lines = split(/[\r\n]/,$resbody); |
1891 |
|
|
1892 |
( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) = |
$self->{inform} = {}; |
1893 |
split(/\t/, shift @lines, 5); |
|
1894 |
|
( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum}, |
1895 |
|
$self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5); |
1896 |
|
|
1897 |
return $resbody unless (@lines); |
return $resbody unless (@lines); |
1898 |
|
|
1899 |
shift @lines; |
shift @lines; |
1900 |
|
|
1901 |
while(my $admin = shift @lines) { |
while(my $admin = shift @lines) { |
1902 |
push @{$self->{admins}}, $admin; |
push @{$self->{inform}->{admins}}, $admin; |
1903 |
} |
} |
1904 |
|
|
1905 |
while(my $guest = shift @lines) { |
while(my $guest = shift @lines) { |
1906 |
push @{$self->{guests}}, $guest; |
push @{$self->{inform}->{guests}}, $guest; |
1907 |
} |
} |
1908 |
|
|
1909 |
while(my $link = shift @lines) { |
while(my $link = shift @lines) { |
1910 |
push @{$self->{links}}, $link; |
push @{$self->{inform}->{links}}, $link; |
1911 |
} |
} |
1912 |
|
|
1913 |
return $resbody; |
return $resbody; |
1930 |
|
|
1931 |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
1932 |
|
|
1933 |
|
Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code |
1934 |
|
|
1935 |
=head1 COPYRIGHT AND LICENSE |
=head1 COPYRIGHT AND LICENSE |
1936 |
|
|