4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.04_1'; |
our $VERSION = '0.06_1'; |
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
17 |
use Search::Estraier; |
use Search::Estraier; |
18 |
|
|
19 |
# create and configure node |
# create and configure node |
20 |
my $node = new Search::Estraier::Node; |
my $node = new Search::Estraier::Node( |
21 |
$node->set_url("http://localhost:1978/node/test"); |
url => 'http://localhost:1978/node/test', |
22 |
$node->set_auth("admin","admin"); |
user => 'admin', |
23 |
|
passwd => 'admin' |
24 |
|
); |
25 |
|
|
26 |
# create document |
# create document |
27 |
my $doc = new Search::Estraier::Document; |
my $doc = new Search::Estraier::Document; |
34 |
$doc->add_text("Somewhere over the rainbow. Way up high."); |
$doc->add_text("Somewhere over the rainbow. Way up high."); |
35 |
$doc->add_text("There's a land that I heard of once in a lullaby."); |
$doc->add_text("There's a land that I heard of once in a lullaby."); |
36 |
|
|
37 |
die "error: ", $node->status,"\n" unless ($node->put_doc($doc)); |
die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) }); |
38 |
|
|
39 |
=head2 Simple searcher |
=head2 Simple searcher |
40 |
|
|
41 |
use Search::Estraier; |
use Search::Estraier; |
42 |
|
|
43 |
# create and configure node |
# create and configure node |
44 |
my $node = new Search::Estraier::Node; |
my $node = new Search::Estraier::Node( |
45 |
$node->set_url("http://localhost:1978/node/test"); |
url => 'http://localhost:1978/node/test', |
46 |
$node->set_auth("admin","admin"); |
user => 'admin', |
47 |
|
passwd => 'admin', |
48 |
|
croak_on_error => 1, |
49 |
|
); |
50 |
|
|
51 |
# create condition |
# create condition |
52 |
my $cond = new Search::Estraier::Condition; |
my $cond = new Search::Estraier::Condition; |
55 |
$cond->set_phrase("rainbow AND lullaby"); |
$cond->set_phrase("rainbow AND lullaby"); |
56 |
|
|
57 |
my $nres = $node->search($cond, 0); |
my $nres = $node->search($cond, 0); |
58 |
|
|
59 |
if (defined($nres)) { |
if (defined($nres)) { |
60 |
|
print "Got ", $nres->hits, " results\n"; |
61 |
|
|
62 |
# for each document in results |
# for each document in results |
63 |
for my $i ( 0 ... $nres->doc_num - 1 ) { |
for my $i ( 0 ... $nres->doc_num - 1 ) { |
64 |
# get result document |
# get result document |
599 |
} |
} |
600 |
|
|
601 |
|
|
602 |
|
=head2 set_skip |
603 |
|
|
604 |
|
Set number of skipped documents from beginning of results |
605 |
|
|
606 |
|
$cond->set_skip(42); |
607 |
|
|
608 |
|
Similar to C<offset> in RDBMS. |
609 |
|
|
610 |
|
=cut |
611 |
|
|
612 |
|
sub set_skip { |
613 |
|
my $self = shift; |
614 |
|
$self->{skip} = shift; |
615 |
|
} |
616 |
|
|
617 |
|
=head2 skip |
618 |
|
|
619 |
|
Return skip for this condition. |
620 |
|
|
621 |
|
print $cond->skip; |
622 |
|
|
623 |
|
=cut |
624 |
|
|
625 |
|
sub skip { |
626 |
|
my $self = shift; |
627 |
|
return $self->{skip}; |
628 |
|
} |
629 |
|
|
630 |
|
|
631 |
package Search::Estraier::ResultDocument; |
package Search::Estraier::ResultDocument; |
632 |
|
|
633 |
use Carp qw/croak/; |
use Carp qw/croak/; |
771 |
|
|
772 |
print $res->doc_num; |
print $res->doc_num; |
773 |
|
|
774 |
|
This will return real number of documents (limited by C<max>). |
775 |
|
If you want to get total number of hits, see C<hits>. |
776 |
|
|
777 |
=cut |
=cut |
778 |
|
|
779 |
sub doc_num { |
sub doc_num { |
805 |
|
|
806 |
Return specific hint from results. |
Return specific hint from results. |
807 |
|
|
808 |
print $rec->hint( 'VERSION' ); |
print $res->hint( 'VERSION' ); |
809 |
|
|
810 |
Possible hints are: C<VERSION>, C<NODE>, C<HIT>, C<HINT#n>, C<DOCNUM>, C<WORDNUM>, |
Possible hints are: C<VERSION>, C<NODE>, C<HIT>, C<HINT#n>, C<DOCNUM>, C<WORDNUM>, |
811 |
C<TIME>, C<LINK#n>, C<VIEW>. |
C<TIME>, C<LINK#n>, C<VIEW>. |
822 |
|
|
823 |
More perlish version of C<hint>. This one returns hash. |
More perlish version of C<hint>. This one returns hash. |
824 |
|
|
825 |
my %hints = $rec->hints; |
my %hints = $res->hints; |
826 |
|
|
827 |
=cut |
=cut |
828 |
|
|
831 |
return $self->{hints}; |
return $self->{hints}; |
832 |
} |
} |
833 |
|
|
834 |
|
=head2 hits |
835 |
|
|
836 |
|
Syntaxtic sugar for total number of hits for this query |
837 |
|
|
838 |
|
print $res->hits; |
839 |
|
|
840 |
|
It's same as |
841 |
|
|
842 |
|
print $res->hint('HIT'); |
843 |
|
|
844 |
|
but shorter. |
845 |
|
|
846 |
|
=cut |
847 |
|
|
848 |
|
sub hits { |
849 |
|
my $self = shift; |
850 |
|
return $self->{hints}->{'HIT'} || 0; |
851 |
|
} |
852 |
|
|
853 |
package Search::Estraier::Node; |
package Search::Estraier::Node; |
854 |
|
|
855 |
use Carp qw/carp croak confess/; |
use Carp qw/carp croak confess/; |
903 |
my $self = { |
my $self = { |
904 |
pxport => -1, |
pxport => -1, |
905 |
timeout => 0, # this used to be -1 |
timeout => 0, # this used to be -1 |
|
dnum => -1, |
|
|
wnum => -1, |
|
|
size => -1.0, |
|
906 |
wwidth => 480, |
wwidth => 480, |
907 |
hwidth => 96, |
hwidth => 96, |
908 |
awidth => 96, |
awidth => 96, |
909 |
status => -1, |
status => -1, |
910 |
}; |
}; |
911 |
|
|
912 |
bless($self, $class); |
bless($self, $class); |
913 |
|
|
914 |
if ($#_ == 0) { |
if ($#_ == 0) { |
921 |
warn "## Node debug on\n" if ($self->{debug}); |
warn "## Node debug on\n" if ($self->{debug}); |
922 |
} |
} |
923 |
|
|
924 |
|
$self->{inform} = { |
925 |
|
dnum => -1, |
926 |
|
wnum => -1, |
927 |
|
size => -1.0, |
928 |
|
}; |
929 |
|
|
930 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
931 |
} |
} |
932 |
|
|
1205 |
|
|
1206 |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
1207 |
|
|
1208 |
|
This method won't croak, even if using C<croak_on_error>. |
1209 |
|
|
1210 |
=cut |
=cut |
1211 |
|
|
1212 |
sub uri_to_id { |
sub uri_to_id { |
1213 |
my $self = shift; |
my $self = shift; |
1214 |
my $uri = shift || return; |
my $uri = shift || return; |
1215 |
return $self->_fetch_doc( uri => $uri, path => '/uri_to_id', chomp_resbody => 1 ); |
return $self->_fetch_doc( uri => $uri, path => '/uri_to_id', chomp_resbody => 1, croak_on_error => 0 ); |
1216 |
} |
} |
1217 |
|
|
1218 |
|
|
1272 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1273 |
$arg, |
$arg, |
1274 |
\$resbody, |
\$resbody, |
1275 |
|
$a->{croak_on_error}, |
1276 |
); |
); |
1277 |
|
|
1278 |
return if ($rv != 200); |
return if ($rv != 200); |
1303 |
|
|
1304 |
sub name { |
sub name { |
1305 |
my $self = shift; |
my $self = shift; |
1306 |
$self->_set_info unless ($self->{name}); |
$self->_set_info unless ($self->{inform}->{name}); |
1307 |
return $self->{name}; |
return $self->{inform}->{name}; |
1308 |
} |
} |
1309 |
|
|
1310 |
|
|
1316 |
|
|
1317 |
sub label { |
sub label { |
1318 |
my $self = shift; |
my $self = shift; |
1319 |
$self->_set_info unless ($self->{label}); |
$self->_set_info unless ($self->{inform}->{label}); |
1320 |
return $self->{label}; |
return $self->{inform}->{label}; |
1321 |
} |
} |
1322 |
|
|
1323 |
|
|
1329 |
|
|
1330 |
sub doc_num { |
sub doc_num { |
1331 |
my $self = shift; |
my $self = shift; |
1332 |
$self->_set_info if ($self->{dnum} < 0); |
$self->_set_info if ($self->{inform}->{dnum} < 0); |
1333 |
return $self->{dnum}; |
return $self->{inform}->{dnum}; |
1334 |
} |
} |
1335 |
|
|
1336 |
|
|
1342 |
|
|
1343 |
sub word_num { |
sub word_num { |
1344 |
my $self = shift; |
my $self = shift; |
1345 |
$self->_set_info if ($self->{wnum} < 0); |
$self->_set_info if ($self->{inform}->{wnum} < 0); |
1346 |
return $self->{wnum}; |
return $self->{inform}->{wnum}; |
1347 |
} |
} |
1348 |
|
|
1349 |
|
|
1355 |
|
|
1356 |
sub size { |
sub size { |
1357 |
my $self = shift; |
my $self = shift; |
1358 |
$self->_set_info if ($self->{size} < 0); |
$self->_set_info if ($self->{inform}->{size} < 0); |
1359 |
return $self->{size}; |
return $self->{inform}->{size}; |
1360 |
} |
} |
1361 |
|
|
1362 |
|
|
1389 |
); |
); |
1390 |
return if ($rv != 200); |
return if ($rv != 200); |
1391 |
|
|
1392 |
my (@docs, $hints); |
my @records = split /--------\[.*?\]--------(?::END)?\r?\n/, $resbody; |
1393 |
|
my $hintsText = splice @records, 0, 2; # starts with empty record |
1394 |
my @lines = split(/\n/, $resbody); |
my $hints = { $hintsText =~ m/^(.*?)\t(.*?)$/gsm }; |
1395 |
return unless (@lines); |
|
1396 |
|
# process records |
1397 |
my $border = $lines[0]; |
my $docs = []; |
1398 |
my $isend = 0; |
foreach my $record (@records) |
1399 |
my $lnum = 1; |
{ |
1400 |
|
# split into keys and snippets |
1401 |
while ( $lnum <= $#lines ) { |
my ($keys, $snippet) = $record =~ m/^(.*?)\n\n(.*?)$/s; |
1402 |
my $line = $lines[$lnum]; |
|
1403 |
$lnum++; |
# create document hash |
1404 |
|
my $doc = { $keys =~ m/^(.*?)=(.*?)$/gsm }; |
1405 |
#warn "## $line\n"; |
$doc->{'@keywords'} = $doc->{keywords}; |
1406 |
if ($line && $line =~ m/^\Q$border\E(:END)*$/) { |
($doc->{keywords}) = $keys =~ m/^%VECTOR\t(.*?)$/gm; |
1407 |
$isend = $1; |
$doc->{snippet} = $snippet; |
1408 |
last; |
|
1409 |
} |
push @$docs, new Search::Estraier::ResultDocument( |
1410 |
|
attrs => $doc, |
1411 |
if ($line =~ /\t/) { |
uri => $doc->{'@uri'}, |
1412 |
my ($k,$v) = split(/\t/, $line, 2); |
snippet => $snippet, |
1413 |
$hints->{$k} = $v; |
keywords => $doc->{'keywords'}, |
1414 |
} |
); |
|
} |
|
|
|
|
|
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$/); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (! $isend) { |
|
|
warn "received result doesn't have :END\n$resbody"; |
|
|
return; |
|
1415 |
} |
} |
1416 |
|
|
1417 |
#warn Dumper(\@docs, $hints); |
return new Search::Estraier::NodeResult( docs => $docs, hints => $hints ); |
|
|
|
|
return new Search::Estraier::NodeResult( docs => \@docs, hints => $hints ); |
|
1418 |
} |
} |
1419 |
|
|
1420 |
|
|
1463 |
push @args, 'wwidth=' . $self->{wwidth}; |
push @args, 'wwidth=' . $self->{wwidth}; |
1464 |
push @args, 'hwidth=' . $self->{hwidth}; |
push @args, 'hwidth=' . $self->{hwidth}; |
1465 |
push @args, 'awidth=' . $self->{awidth}; |
push @args, 'awidth=' . $self->{awidth}; |
1466 |
|
push @args, 'skip=' . $self->{skip} if ($self->{skip}); |
1467 |
|
|
1468 |
return join('&', @args); |
return join('&', @args); |
1469 |
} |
} |
1486 |
sub shuttle_url { |
sub shuttle_url { |
1487 |
my $self = shift; |
my $self = shift; |
1488 |
|
|
1489 |
my ($url, $content_type, $reqbody, $resbody) = @_; |
my ($url, $content_type, $reqbody, $resbody, $croak_on_error) = @_; |
1490 |
|
|
1491 |
|
$croak_on_error = $self->{croak_on_error} unless defined($croak_on_error); |
1492 |
|
|
1493 |
$self->{status} = -1; |
$self->{status} = -1; |
1494 |
|
|
1532 |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
1533 |
|
|
1534 |
if (! $res->is_success) { |
if (! $res->is_success) { |
1535 |
if ($self->{croak_on_error}) { |
if ($croak_on_error) { |
1536 |
croak("can't get $url: ",$res->status_line); |
croak("can't get $url: ",$res->status_line); |
1537 |
} else { |
} else { |
1538 |
return -1; |
return -1; |
1637 |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
1638 |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
1639 |
|
|
1640 |
$self->shuttle_url( $self->{url} . '/_set_link', |
if ($self->shuttle_url( $self->{url} . '/_set_link', |
1641 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1642 |
$reqbody, |
$reqbody, |
1643 |
undef |
undef |
1644 |
) == 200; |
) == 200) { |
1645 |
|
# refresh node info after adding link |
1646 |
|
$self->_set_info; |
1647 |
|
return 1; |
1648 |
|
} |
1649 |
|
} |
1650 |
|
|
1651 |
|
=head2 admins |
1652 |
|
|
1653 |
|
my @admins = @{ $node->admins }; |
1654 |
|
|
1655 |
|
Return array of users with admin rights on node |
1656 |
|
|
1657 |
|
=cut |
1658 |
|
|
1659 |
|
sub admins { |
1660 |
|
my $self = shift; |
1661 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1662 |
|
return $self->{inform}->{admins}; |
1663 |
|
} |
1664 |
|
|
1665 |
|
=head2 guests |
1666 |
|
|
1667 |
|
my @guests = @{ $node->guests }; |
1668 |
|
|
1669 |
|
Return array of users with guest rights on node |
1670 |
|
|
1671 |
|
=cut |
1672 |
|
|
1673 |
|
sub guests { |
1674 |
|
my $self = shift; |
1675 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1676 |
|
return $self->{inform}->{guests}; |
1677 |
|
} |
1678 |
|
|
1679 |
|
=head2 links |
1680 |
|
|
1681 |
|
my $links = @{ $node->links }; |
1682 |
|
|
1683 |
|
Return array of links for this node |
1684 |
|
|
1685 |
|
=cut |
1686 |
|
|
1687 |
|
sub links { |
1688 |
|
my $self = shift; |
1689 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1690 |
|
return $self->{inform}->{links}; |
1691 |
} |
} |
1692 |
|
|
1693 |
|
|
1718 |
|
|
1719 |
return if ($rv != 200 || !$resbody); |
return if ($rv != 200 || !$resbody); |
1720 |
|
|
1721 |
# it seems that response can have multiple line endings |
my @lines = split(/[\r\n]/,$resbody); |
1722 |
$resbody =~ s/[\r\n]+$//; |
|
1723 |
|
$self->{inform} = {}; |
1724 |
|
|
1725 |
|
( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum}, |
1726 |
|
$self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5); |
1727 |
|
|
1728 |
|
return $resbody unless (@lines); |
1729 |
|
|
1730 |
|
shift @lines; |
1731 |
|
|
1732 |
|
while(my $admin = shift @lines) { |
1733 |
|
push @{$self->{inform}->{admins}}, $admin; |
1734 |
|
} |
1735 |
|
|
1736 |
|
while(my $guest = shift @lines) { |
1737 |
|
push @{$self->{inform}->{guests}}, $guest; |
1738 |
|
} |
1739 |
|
|
1740 |
|
while(my $link = shift @lines) { |
1741 |
|
push @{$self->{inform}->{links}}, $link; |
1742 |
|
} |
1743 |
|
|
1744 |
( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) = |
return $resbody; |
|
split(/\t/, $resbody, 5); |
|
1745 |
|
|
1746 |
} |
} |
1747 |
|
|
1761 |
|
|
1762 |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
1763 |
|
|
1764 |
|
Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code |
1765 |
|
|
1766 |
=head1 COPYRIGHT AND LICENSE |
=head1 COPYRIGHT AND LICENSE |
1767 |
|
|