4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.04_1'; |
our $VERSION = '0.04_2'; |
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 |
742 |
|
|
743 |
print $res->doc_num; |
print $res->doc_num; |
744 |
|
|
745 |
|
This will return real number of documents (limited by C<max>). |
746 |
|
If you want to get total number of hits, see C<hits>. |
747 |
|
|
748 |
=cut |
=cut |
749 |
|
|
750 |
sub doc_num { |
sub doc_num { |
776 |
|
|
777 |
Return specific hint from results. |
Return specific hint from results. |
778 |
|
|
779 |
print $rec->hint( 'VERSION' ); |
print $res->hint( 'VERSION' ); |
780 |
|
|
781 |
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>, |
782 |
C<TIME>, C<LINK#n>, C<VIEW>. |
C<TIME>, C<LINK#n>, C<VIEW>. |
789 |
return $self->{hints}->{$key}; |
return $self->{hints}->{$key}; |
790 |
} |
} |
791 |
|
|
792 |
=head2 hints |
=head2 hits |
793 |
|
|
794 |
More perlish version of C<hint>. This one returns hash. |
More perlish version of C<hint>. This one returns hash. |
795 |
|
|
796 |
my %hints = $rec->hints; |
my %hints = $res->hints; |
797 |
|
|
798 |
=cut |
=cut |
799 |
|
|
802 |
return $self->{hints}; |
return $self->{hints}; |
803 |
} |
} |
804 |
|
|
805 |
|
=head2 hits |
806 |
|
|
807 |
|
Syntaxtic sugar for total number of hits for this query |
808 |
|
|
809 |
|
print $res->hits; |
810 |
|
|
811 |
|
It's same as |
812 |
|
|
813 |
|
print $res->hint('HIT'); |
814 |
|
|
815 |
|
but shorter. |
816 |
|
|
817 |
|
=cut |
818 |
|
|
819 |
|
sub hits { |
820 |
|
my $self = shift; |
821 |
|
return $self->{hints}->{'HIT'} || 0; |
822 |
|
} |
823 |
|
|
824 |
package Search::Estraier::Node; |
package Search::Estraier::Node; |
825 |
|
|
826 |
use Carp qw/carp croak confess/; |
use Carp qw/carp croak confess/; |
1172 |
|
|
1173 |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
1174 |
|
|
1175 |
|
This method won't croak, even if using C<croak_on_error>. |
1176 |
|
|
1177 |
=cut |
=cut |
1178 |
|
|
1179 |
sub uri_to_id { |
sub uri_to_id { |
1180 |
my $self = shift; |
my $self = shift; |
1181 |
my $uri = shift || return; |
my $uri = shift || return; |
1182 |
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 ); |
1183 |
} |
} |
1184 |
|
|
1185 |
|
|
1239 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1240 |
$arg, |
$arg, |
1241 |
\$resbody, |
\$resbody, |
1242 |
|
$a->{croak_on_error}, |
1243 |
); |
); |
1244 |
|
|
1245 |
return if ($rv != 200); |
return if ($rv != 200); |
1508 |
sub shuttle_url { |
sub shuttle_url { |
1509 |
my $self = shift; |
my $self = shift; |
1510 |
|
|
1511 |
my ($url, $content_type, $reqbody, $resbody) = @_; |
my ($url, $content_type, $reqbody, $resbody, $croak_on_error) = @_; |
1512 |
|
|
1513 |
|
$croak_on_error = $self->{croak_on_error} unless defined($croak_on_error); |
1514 |
|
|
1515 |
$self->{status} = -1; |
$self->{status} = -1; |
1516 |
|
|
1554 |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
1555 |
|
|
1556 |
if (! $res->is_success) { |
if (! $res->is_success) { |
1557 |
if ($self->{croak_on_error}) { |
if ($croak_on_error) { |
1558 |
croak("can't get $url: ",$res->status_line); |
croak("can't get $url: ",$res->status_line); |
1559 |
} else { |
} else { |
1560 |
return -1; |
return -1; |
1659 |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
1660 |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
1661 |
|
|
1662 |
$self->shuttle_url( $self->{url} . '/_set_link', |
if ($self->shuttle_url( $self->{url} . '/_set_link', |
1663 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1664 |
$reqbody, |
$reqbody, |
1665 |
undef |
undef |
1666 |
) == 200; |
) == 200) { |
1667 |
|
# refresh node info after adding link |
1668 |
|
$self->_set_info; |
1669 |
|
return 1; |
1670 |
|
} |
1671 |
|
} |
1672 |
|
|
1673 |
|
=head2 admins |
1674 |
|
|
1675 |
|
my @admins = @{ $node->admins }; |
1676 |
|
|
1677 |
|
Return array of users with admin rights on node |
1678 |
|
|
1679 |
|
=cut |
1680 |
|
|
1681 |
|
sub admins { |
1682 |
|
my $self = shift; |
1683 |
|
$self->_set_info unless ($self->{name}); |
1684 |
|
return $self->{admins}; |
1685 |
|
} |
1686 |
|
|
1687 |
|
=head2 guests |
1688 |
|
|
1689 |
|
my @guests = @{ $node->guests }; |
1690 |
|
|
1691 |
|
Return array of users with guest rights on node |
1692 |
|
|
1693 |
|
=cut |
1694 |
|
|
1695 |
|
sub guests { |
1696 |
|
my $self = shift; |
1697 |
|
$self->_set_info unless ($self->{name}); |
1698 |
|
return $self->{guests}; |
1699 |
|
} |
1700 |
|
|
1701 |
|
=head2 links |
1702 |
|
|
1703 |
|
my $links = @{ $node->links }; |
1704 |
|
|
1705 |
|
Return array of links for this node |
1706 |
|
|
1707 |
|
=cut |
1708 |
|
|
1709 |
|
sub links { |
1710 |
|
my $self = shift; |
1711 |
|
$self->_set_info unless ($self->{name}); |
1712 |
|
return $self->{links}; |
1713 |
} |
} |
1714 |
|
|
1715 |
|
|
1740 |
|
|
1741 |
return if ($rv != 200 || !$resbody); |
return if ($rv != 200 || !$resbody); |
1742 |
|
|
1743 |
# it seems that response can have multiple line endings |
my @lines = split(/[\r\n]/,$resbody); |
1744 |
$resbody =~ s/[\r\n]+$//; |
|
|
|
|
1745 |
( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) = |
( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) = |
1746 |
split(/\t/, $resbody, 5); |
split(/\t/, shift @lines, 5); |
1747 |
|
|
1748 |
|
return $resbody unless (@lines); |
1749 |
|
|
1750 |
|
shift @lines; |
1751 |
|
|
1752 |
|
while(my $admin = shift @lines) { |
1753 |
|
push @{$self->{admins}}, $admin; |
1754 |
|
} |
1755 |
|
|
1756 |
|
while(my $guest = shift @lines) { |
1757 |
|
push @{$self->{guests}}, $guest; |
1758 |
|
} |
1759 |
|
|
1760 |
|
while(my $link = shift @lines) { |
1761 |
|
push @{$self->{links}}, $link; |
1762 |
|
} |
1763 |
|
|
1764 |
|
return $resbody; |
1765 |
|
|
1766 |
} |
} |
1767 |
|
|