4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.04_1'; |
our $VERSION = '0.07_3'; |
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 |
|
create => 1, |
25 |
|
label => 'Label for node', |
26 |
|
croak_on_error => 1, |
27 |
|
); |
28 |
|
|
29 |
# create document |
# create document |
30 |
my $doc = new Search::Estraier::Document; |
my $doc = new Search::Estraier::Document; |
37 |
$doc->add_text("Somewhere over the rainbow. Way up high."); |
$doc->add_text("Somewhere over the rainbow. Way up high."); |
38 |
$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."); |
39 |
|
|
40 |
die "error: ", $node->status,"\n" unless ($node->put_doc($doc)); |
die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) }); |
41 |
|
|
42 |
=head2 Simple searcher |
=head2 Simple searcher |
43 |
|
|
44 |
use Search::Estraier; |
use Search::Estraier; |
45 |
|
|
46 |
# create and configure node |
# create and configure node |
47 |
my $node = new Search::Estraier::Node; |
my $node = new Search::Estraier::Node( |
48 |
$node->set_url("http://localhost:1978/node/test"); |
url => 'http://localhost:1978/node/test', |
49 |
$node->set_auth("admin","admin"); |
user => 'admin', |
50 |
|
passwd => 'admin', |
51 |
|
croak_on_error => 1, |
52 |
|
); |
53 |
|
|
54 |
# create condition |
# create condition |
55 |
my $cond = new Search::Estraier::Condition; |
my $cond = new Search::Estraier::Condition; |
58 |
$cond->set_phrase("rainbow AND lullaby"); |
$cond->set_phrase("rainbow AND lullaby"); |
59 |
|
|
60 |
my $nres = $node->search($cond, 0); |
my $nres = $node->search($cond, 0); |
61 |
|
|
62 |
if (defined($nres)) { |
if (defined($nres)) { |
63 |
|
print "Got ", $nres->hits, " results\n"; |
64 |
|
|
65 |
# for each document in results |
# for each document in results |
66 |
for my $i ( 0 ... $nres->doc_num - 1 ) { |
for my $i ( 0 ... $nres->doc_num - 1 ) { |
67 |
# get result document |
# get result document |
120 |
|
|
121 |
=head1 Search::Estraier::Document |
=head1 Search::Estraier::Document |
122 |
|
|
123 |
This class implements Document which is collection of attributes |
This class implements Document which is single item in Hyper Estraier. |
124 |
(key=value), vectors (also key value) display text and hidden text. |
|
125 |
|
It's is collection of: |
126 |
|
|
127 |
|
=over 4 |
128 |
|
|
129 |
|
=item attributes |
130 |
|
|
131 |
|
C<< 'key' => 'value' >> pairs which can later be used for filtering of results |
132 |
|
|
133 |
|
You can add common filters to C<attrindex> in estmaster's C<_conf> |
134 |
|
file for better performance. See C<attrindex> in |
135 |
|
L<Hyper Estraier P2P Guide|http://hyperestraier.sourceforge.net/nguide-en.html>. |
136 |
|
|
137 |
|
=item vectors |
138 |
|
|
139 |
|
also C<< 'key' => 'value' >> pairs |
140 |
|
|
141 |
|
=item display text |
142 |
|
|
143 |
|
Text which will be used to create searchable corpus of your index and |
144 |
|
included in snippet output. |
145 |
|
|
146 |
|
=item hidden text |
147 |
|
|
148 |
|
Text which will be searchable, but will not be included in snippet. |
149 |
|
|
150 |
|
=back |
151 |
|
|
152 |
=head2 new |
=head2 new |
153 |
|
|
182 |
|
|
183 |
if ($line =~ m/^%VECTOR\t(.+)$/) { |
if ($line =~ m/^%VECTOR\t(.+)$/) { |
184 |
my @fields = split(/\t/, $1); |
my @fields = split(/\t/, $1); |
185 |
for my $i ( 0 .. ($#fields - 1) ) { |
if ($#fields % 2 == 1) { |
186 |
$self->{kwords}->{ $fields[ $i ] } = $fields[ $i + 1 ]; |
$self->{kwords} = { @fields }; |
187 |
$i++; |
} else { |
188 |
|
warn "can't decode $line\n"; |
189 |
} |
} |
190 |
next; |
next; |
191 |
} elsif ($line =~ m/^%/) { |
} elsif ($line =~ m/^%/) { |
269 |
push @{ $self->{htexts} }, $self->_s($text); |
push @{ $self->{htexts} }, $self->_s($text); |
270 |
} |
} |
271 |
|
|
272 |
|
=head2 add_vectors |
273 |
|
|
274 |
|
Add a vectors |
275 |
|
|
276 |
|
$doc->add_vector( |
277 |
|
'vector_name' => 42, |
278 |
|
'another' => 12345, |
279 |
|
); |
280 |
|
|
281 |
|
=cut |
282 |
|
|
283 |
|
sub add_vectors { |
284 |
|
my $self = shift; |
285 |
|
return unless (@_); |
286 |
|
|
287 |
|
# this is ugly, but works |
288 |
|
die "add_vector needs HASH as argument" unless ($#_ % 2 == 1); |
289 |
|
|
290 |
|
$self->{kwords} = {@_}; |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
=head2 id |
=head2 id |
295 |
|
|
379 |
my $draft; |
my $draft; |
380 |
|
|
381 |
foreach my $attr_name (sort keys %{ $self->{attrs} }) { |
foreach my $attr_name (sort keys %{ $self->{attrs} }) { |
382 |
next unless(my $v = $self->{attrs}->{$attr_name}); |
next unless defined(my $v = $self->{attrs}->{$attr_name}); |
383 |
$draft .= $attr_name . '=' . $v . "\n"; |
$draft .= $attr_name . '=' . $v . "\n"; |
384 |
} |
} |
385 |
|
|
386 |
if ($self->{kwords}) { |
if ($self->{kwords}) { |
387 |
$draft .= '%%VECTOR'; |
$draft .= '%VECTOR'; |
388 |
while (my ($key, $value) = each %{ $self->{kwords} }) { |
while (my ($key, $value) = each %{ $self->{kwords} }) { |
389 |
$draft .= "\t$key\t$value"; |
$draft .= "\t$key\t$value"; |
390 |
} |
} |
428 |
|
|
429 |
package Search::Estraier::Condition; |
package Search::Estraier::Condition; |
430 |
|
|
431 |
use Carp qw/confess croak/; |
use Carp qw/carp confess croak/; |
432 |
|
|
433 |
use Search::Estraier; |
use Search::Estraier; |
434 |
our @ISA = qw/Search::Estraier/; |
our @ISA = qw/Search::Estraier/; |
506 |
|
|
507 |
=head2 set_options |
=head2 set_options |
508 |
|
|
509 |
$cond->set_options( SURE => 1 ); |
$cond->set_options( 'SURE' ); |
510 |
|
|
511 |
|
$cond->set_options( qw/AGITO NOIDF SIMPLE/ ); |
512 |
|
|
513 |
|
Possible options are: |
514 |
|
|
515 |
|
=over 8 |
516 |
|
|
517 |
|
=item SURE |
518 |
|
|
519 |
|
check every N-gram |
520 |
|
|
521 |
|
=item USUAL |
522 |
|
|
523 |
|
check every second N-gram |
524 |
|
|
525 |
|
=item FAST |
526 |
|
|
527 |
|
check every third N-gram |
528 |
|
|
529 |
|
=item AGITO |
530 |
|
|
531 |
|
check every fourth N-gram |
532 |
|
|
533 |
|
=item NOIDF |
534 |
|
|
535 |
|
don't perform TF-IDF tuning |
536 |
|
|
537 |
|
=item SIMPLE |
538 |
|
|
539 |
|
use simplified query phrase |
540 |
|
|
541 |
|
=back |
542 |
|
|
543 |
|
Skipping N-grams will speed up search, but reduce accuracy. Every call to C<set_options> will reset previous |
544 |
|
options; |
545 |
|
|
546 |
|
This option changed in version C<0.04> of this module. It's backwards compatibile. |
547 |
|
|
548 |
=cut |
=cut |
549 |
|
|
550 |
my $options = { |
my $options = { |
|
# check N-gram keys skipping by three |
|
551 |
SURE => 1 << 0, |
SURE => 1 << 0, |
|
# check N-gram keys skipping by two |
|
552 |
USUAL => 1 << 1, |
USUAL => 1 << 1, |
|
# without TF-IDF tuning |
|
553 |
FAST => 1 << 2, |
FAST => 1 << 2, |
|
# with the simplified phrase |
|
554 |
AGITO => 1 << 3, |
AGITO => 1 << 3, |
|
# check every N-gram key |
|
555 |
NOIDF => 1 << 4, |
NOIDF => 1 << 4, |
|
# check N-gram keys skipping by one |
|
556 |
SIMPLE => 1 << 10, |
SIMPLE => 1 << 10, |
557 |
}; |
}; |
558 |
|
|
559 |
sub set_options { |
sub set_options { |
560 |
my $self = shift; |
my $self = shift; |
561 |
my $option = shift; |
my $opt = 0; |
562 |
confess "unknown option" unless ($options->{$option}); |
foreach my $option (@_) { |
563 |
$self->{options} ||= $options->{$option}; |
my $mask; |
564 |
|
unless ($mask = $options->{$option}) { |
565 |
|
if ($option eq '1') { |
566 |
|
next; |
567 |
|
} else { |
568 |
|
croak "unknown option $option"; |
569 |
|
} |
570 |
|
} |
571 |
|
$opt += $mask; |
572 |
|
} |
573 |
|
$self->{options} = $opt; |
574 |
} |
} |
575 |
|
|
576 |
|
|
649 |
} |
} |
650 |
|
|
651 |
|
|
652 |
|
=head2 set_skip |
653 |
|
|
654 |
|
Set number of skipped documents from beginning of results |
655 |
|
|
656 |
|
$cond->set_skip(42); |
657 |
|
|
658 |
|
Similar to C<offset> in RDBMS. |
659 |
|
|
660 |
|
=cut |
661 |
|
|
662 |
|
sub set_skip { |
663 |
|
my $self = shift; |
664 |
|
$self->{skip} = shift; |
665 |
|
} |
666 |
|
|
667 |
|
=head2 skip |
668 |
|
|
669 |
|
Return skip for this condition. |
670 |
|
|
671 |
|
print $cond->skip; |
672 |
|
|
673 |
|
=cut |
674 |
|
|
675 |
|
sub skip { |
676 |
|
my $self = shift; |
677 |
|
return $self->{skip}; |
678 |
|
} |
679 |
|
|
680 |
|
=head2 set_mask |
681 |
|
|
682 |
|
Filter out some links when searching. |
683 |
|
|
684 |
|
Argument array of link numbers, starting with 0 (current node). |
685 |
|
|
686 |
|
$cond->set_mask(qw/0 1 4/); |
687 |
|
|
688 |
|
=cut |
689 |
|
|
690 |
|
sub set_mask { |
691 |
|
my $self = shift; |
692 |
|
return unless (@_); |
693 |
|
$self->{mask} = \@_; |
694 |
|
} |
695 |
|
|
696 |
|
|
697 |
package Search::Estraier::ResultDocument; |
package Search::Estraier::ResultDocument; |
698 |
|
|
699 |
use Carp qw/croak/; |
use Carp qw/croak/; |
837 |
|
|
838 |
print $res->doc_num; |
print $res->doc_num; |
839 |
|
|
840 |
|
This will return real number of documents (limited by C<max>). |
841 |
|
If you want to get total number of hits, see C<hits>. |
842 |
|
|
843 |
=cut |
=cut |
844 |
|
|
845 |
sub doc_num { |
sub doc_num { |
871 |
|
|
872 |
Return specific hint from results. |
Return specific hint from results. |
873 |
|
|
874 |
print $rec->hint( 'VERSION' ); |
print $res->hint( 'VERSION' ); |
875 |
|
|
876 |
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>, |
877 |
C<TIME>, C<LINK#n>, C<VIEW>. |
C<TIME>, C<LINK#n>, C<VIEW>. |
888 |
|
|
889 |
More perlish version of C<hint>. This one returns hash. |
More perlish version of C<hint>. This one returns hash. |
890 |
|
|
891 |
my %hints = $rec->hints; |
my %hints = $res->hints; |
892 |
|
|
893 |
=cut |
=cut |
894 |
|
|
897 |
return $self->{hints}; |
return $self->{hints}; |
898 |
} |
} |
899 |
|
|
900 |
|
=head2 hits |
901 |
|
|
902 |
|
Syntaxtic sugar for total number of hits for this query |
903 |
|
|
904 |
|
print $res->hits; |
905 |
|
|
906 |
|
It's same as |
907 |
|
|
908 |
|
print $res->hint('HIT'); |
909 |
|
|
910 |
|
but shorter. |
911 |
|
|
912 |
|
=cut |
913 |
|
|
914 |
|
sub hits { |
915 |
|
my $self = shift; |
916 |
|
return $self->{hints}->{'HIT'} || 0; |
917 |
|
} |
918 |
|
|
919 |
package Search::Estraier::Node; |
package Search::Estraier::Node; |
920 |
|
|
921 |
use Carp qw/carp croak confess/; |
use Carp qw/carp croak confess/; |
938 |
|
|
939 |
my $node = new Search::HyperEstraier::Node( |
my $node = new Search::HyperEstraier::Node( |
940 |
url => 'http://localhost:1978/node/test', |
url => 'http://localhost:1978/node/test', |
941 |
|
user => 'admin', |
942 |
|
passwd => 'admin' |
943 |
|
create => 1, |
944 |
|
label => 'optional node label', |
945 |
debug => 1, |
debug => 1, |
946 |
croak_on_error => 1 |
croak_on_error => 1 |
947 |
); |
); |
954 |
|
|
955 |
URL to node |
URL to node |
956 |
|
|
957 |
|
=item user |
958 |
|
|
959 |
|
specify username for node server authentication |
960 |
|
|
961 |
|
=item passwd |
962 |
|
|
963 |
|
password for authentication |
964 |
|
|
965 |
|
=item create |
966 |
|
|
967 |
|
create node if it doesn't exists |
968 |
|
|
969 |
|
=item label |
970 |
|
|
971 |
|
optional label for new node if C<create> is used |
972 |
|
|
973 |
=item debug |
=item debug |
974 |
|
|
975 |
dumps a B<lot> of debugging output |
dumps a B<lot> of debugging output |
989 |
my $self = { |
my $self = { |
990 |
pxport => -1, |
pxport => -1, |
991 |
timeout => 0, # this used to be -1 |
timeout => 0, # this used to be -1 |
|
dnum => -1, |
|
|
wnum => -1, |
|
|
size => -1.0, |
|
992 |
wwidth => 480, |
wwidth => 480, |
993 |
hwidth => 96, |
hwidth => 96, |
994 |
awidth => 96, |
awidth => 96, |
995 |
status => -1, |
status => -1, |
996 |
}; |
}; |
997 |
|
|
998 |
bless($self, $class); |
bless($self, $class); |
999 |
|
|
1000 |
if ($#_ == 0) { |
if ($#_ == 0) { |
1001 |
$self->{url} = shift; |
$self->{url} = shift; |
1002 |
} else { |
} else { |
|
my $args = {@_}; |
|
|
|
|
1003 |
%$self = ( %$self, @_ ); |
%$self = ( %$self, @_ ); |
1004 |
|
|
1005 |
|
$self->set_auth( $self->{user}, $self->{passwd} ) if ($self->{user}); |
1006 |
|
|
1007 |
warn "## Node debug on\n" if ($self->{debug}); |
warn "## Node debug on\n" if ($self->{debug}); |
1008 |
} |
} |
1009 |
|
|
1010 |
|
$self->{inform} = { |
1011 |
|
dnum => -1, |
1012 |
|
wnum => -1, |
1013 |
|
size => -1.0, |
1014 |
|
}; |
1015 |
|
|
1016 |
|
if ($self->{create}) { |
1017 |
|
if (! eval { $self->name } || $@) { |
1018 |
|
my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#); |
1019 |
|
croak "can't find node name in '$self->{url}'" unless ($name); |
1020 |
|
my $label = $self->{label} || $name; |
1021 |
|
$self->master( |
1022 |
|
action => 'nodeadd', |
1023 |
|
name => $name, |
1024 |
|
label => $label, |
1025 |
|
) || croak "can't create node $name ($label)"; |
1026 |
|
} |
1027 |
|
} |
1028 |
|
|
1029 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
1030 |
} |
} |
1031 |
|
|
1116 |
|
|
1117 |
$node->put_doc( $document_draft ) or die "can't add document"; |
$node->put_doc( $document_draft ) or die "can't add document"; |
1118 |
|
|
1119 |
Return true on success or false on failture. |
Return true on success or false on failure. |
1120 |
|
|
1121 |
=cut |
=cut |
1122 |
|
|
1124 |
my $self = shift; |
my $self = shift; |
1125 |
my $doc = shift || return; |
my $doc = shift || return; |
1126 |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
1127 |
$self->shuttle_url( $self->{url} . '/put_doc', |
if ($self->shuttle_url( $self->{url} . '/put_doc', |
1128 |
'text/x-estraier-draft', |
'text/x-estraier-draft', |
1129 |
$doc->dump_draft, |
$doc->dump_draft, |
1130 |
undef |
undef |
1131 |
) == 200; |
) == 200) { |
1132 |
|
$self->_clear_info; |
1133 |
|
return 1; |
1134 |
|
} |
1135 |
|
return undef; |
1136 |
} |
} |
1137 |
|
|
1138 |
|
|
1151 |
my $id = shift || return; |
my $id = shift || return; |
1152 |
return unless ($self->{url}); |
return unless ($self->{url}); |
1153 |
croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/); |
croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/); |
1154 |
$self->shuttle_url( $self->{url} . '/out_doc', |
if ($self->shuttle_url( $self->{url} . '/out_doc', |
1155 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1156 |
"id=$id", |
"id=$id", |
1157 |
undef |
undef |
1158 |
) == 200; |
) == 200) { |
1159 |
|
$self->_clear_info; |
1160 |
|
return 1; |
1161 |
|
} |
1162 |
|
return undef; |
1163 |
} |
} |
1164 |
|
|
1165 |
|
|
1177 |
my $self = shift; |
my $self = shift; |
1178 |
my $uri = shift || return; |
my $uri = shift || return; |
1179 |
return unless ($self->{url}); |
return unless ($self->{url}); |
1180 |
$self->shuttle_url( $self->{url} . '/out_doc', |
if ($self->shuttle_url( $self->{url} . '/out_doc', |
1181 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1182 |
"uri=" . uri_escape($uri), |
"uri=" . uri_escape($uri), |
1183 |
undef |
undef |
1184 |
) == 200; |
) == 200) { |
1185 |
|
$self->_clear_info; |
1186 |
|
return 1; |
1187 |
|
} |
1188 |
|
return undef; |
1189 |
} |
} |
1190 |
|
|
1191 |
|
|
1203 |
my $self = shift; |
my $self = shift; |
1204 |
my $doc = shift || return; |
my $doc = shift || return; |
1205 |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
1206 |
$self->shuttle_url( $self->{url} . '/edit_doc', |
if ($self->shuttle_url( $self->{url} . '/edit_doc', |
1207 |
'text/x-estraier-draft', |
'text/x-estraier-draft', |
1208 |
$doc->dump_draft, |
$doc->dump_draft, |
1209 |
undef |
undef |
1210 |
) == 200; |
) == 200) { |
1211 |
|
$self->_clear_info; |
1212 |
|
return 1; |
1213 |
|
} |
1214 |
|
return undef; |
1215 |
} |
} |
1216 |
|
|
1217 |
|
|
1320 |
|
|
1321 |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
1322 |
|
|
1323 |
|
This method won't croak, even if using C<croak_on_error>. |
1324 |
|
|
1325 |
=cut |
=cut |
1326 |
|
|
1327 |
sub uri_to_id { |
sub uri_to_id { |
1328 |
my $self = shift; |
my $self = shift; |
1329 |
my $uri = shift || return; |
my $uri = shift || return; |
1330 |
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 ); |
1331 |
} |
} |
1332 |
|
|
1333 |
|
|
1369 |
$path = '/etch_doc' if ($a->{etch}); |
$path = '/etch_doc' if ($a->{etch}); |
1370 |
|
|
1371 |
if ($a->{id}) { |
if ($a->{id}) { |
1372 |
croak "id must be numberm not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/); |
croak "id must be number not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/); |
1373 |
$arg = 'id=' . $a->{id}; |
$arg = 'id=' . $a->{id}; |
1374 |
} elsif ($a->{uri}) { |
} elsif ($a->{uri}) { |
1375 |
$arg = 'uri=' . uri_escape($a->{uri}); |
$arg = 'uri=' . uri_escape($a->{uri}); |
1387 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1388 |
$arg, |
$arg, |
1389 |
\$resbody, |
\$resbody, |
1390 |
|
$a->{croak_on_error}, |
1391 |
); |
); |
1392 |
|
|
1393 |
return if ($rv != 200); |
return if ($rv != 200); |
1418 |
|
|
1419 |
sub name { |
sub name { |
1420 |
my $self = shift; |
my $self = shift; |
1421 |
$self->_set_info unless ($self->{name}); |
$self->_set_info unless ($self->{inform}->{name}); |
1422 |
return $self->{name}; |
return $self->{inform}->{name}; |
1423 |
} |
} |
1424 |
|
|
1425 |
|
|
1431 |
|
|
1432 |
sub label { |
sub label { |
1433 |
my $self = shift; |
my $self = shift; |
1434 |
$self->_set_info unless ($self->{label}); |
$self->_set_info unless ($self->{inform}->{label}); |
1435 |
return $self->{label}; |
return $self->{inform}->{label}; |
1436 |
} |
} |
1437 |
|
|
1438 |
|
|
1444 |
|
|
1445 |
sub doc_num { |
sub doc_num { |
1446 |
my $self = shift; |
my $self = shift; |
1447 |
$self->_set_info if ($self->{dnum} < 0); |
$self->_set_info if ($self->{inform}->{dnum} < 0); |
1448 |
return $self->{dnum}; |
return $self->{inform}->{dnum}; |
1449 |
} |
} |
1450 |
|
|
1451 |
|
|
1457 |
|
|
1458 |
sub word_num { |
sub word_num { |
1459 |
my $self = shift; |
my $self = shift; |
1460 |
$self->_set_info if ($self->{wnum} < 0); |
$self->_set_info if ($self->{inform}->{wnum} < 0); |
1461 |
return $self->{wnum}; |
return $self->{inform}->{wnum}; |
1462 |
} |
} |
1463 |
|
|
1464 |
|
|
1470 |
|
|
1471 |
sub size { |
sub size { |
1472 |
my $self = shift; |
my $self = shift; |
1473 |
$self->_set_info if ($self->{size} < 0); |
$self->_set_info if ($self->{inform}->{size} < 0); |
1474 |
return $self->{size}; |
return $self->{inform}->{size}; |
1475 |
} |
} |
1476 |
|
|
1477 |
|
|
1504 |
); |
); |
1505 |
return if ($rv != 200); |
return if ($rv != 200); |
1506 |
|
|
1507 |
my (@docs, $hints); |
my @records = split /--------\[.*?\]--------(?::END)?\r?\n/, $resbody; |
1508 |
|
my $hintsText = splice @records, 0, 2; # starts with empty record |
1509 |
my @lines = split(/\n/, $resbody); |
my $hints = { $hintsText =~ m/^(.*?)\t(.*?)$/gsm }; |
1510 |
return unless (@lines); |
|
1511 |
|
# process records |
1512 |
my $border = $lines[0]; |
my $docs = []; |
1513 |
my $isend = 0; |
foreach my $record (@records) |
1514 |
my $lnum = 1; |
{ |
1515 |
|
# split into keys and snippets |
1516 |
while ( $lnum <= $#lines ) { |
my ($keys, $snippet) = $record =~ m/^(.*?)\n\n(.*?)$/s; |
1517 |
my $line = $lines[$lnum]; |
|
1518 |
$lnum++; |
# create document hash |
1519 |
|
my $doc = { $keys =~ m/^(.*?)=(.*?)$/gsm }; |
1520 |
#warn "## $line\n"; |
$doc->{'@keywords'} = $doc->{keywords}; |
1521 |
if ($line && $line =~ m/^\Q$border\E(:END)*$/) { |
($doc->{keywords}) = $keys =~ m/^%VECTOR\t(.*?)$/gm; |
1522 |
$isend = $1; |
$doc->{snippet} = $snippet; |
1523 |
last; |
|
1524 |
} |
push @$docs, new Search::Estraier::ResultDocument( |
1525 |
|
attrs => $doc, |
1526 |
if ($line =~ /\t/) { |
uri => $doc->{'@uri'}, |
1527 |
my ($k,$v) = split(/\t/, $line, 2); |
snippet => $snippet, |
1528 |
$hints->{$k} = $v; |
keywords => $doc->{'keywords'}, |
1529 |
} |
); |
|
} |
|
|
|
|
|
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$/); |
|
|
} |
|
|
|
|
1530 |
} |
} |
1531 |
|
|
1532 |
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 ); |
|
1533 |
} |
} |
1534 |
|
|
1535 |
|
|
1578 |
push @args, 'wwidth=' . $self->{wwidth}; |
push @args, 'wwidth=' . $self->{wwidth}; |
1579 |
push @args, 'hwidth=' . $self->{hwidth}; |
push @args, 'hwidth=' . $self->{hwidth}; |
1580 |
push @args, 'awidth=' . $self->{awidth}; |
push @args, 'awidth=' . $self->{awidth}; |
1581 |
|
push @args, 'skip=' . $cond->{skip} if ($cond->{skip}); |
1582 |
|
|
1583 |
|
if ($cond->{mask}) { |
1584 |
|
my $mask = 0; |
1585 |
|
map { $mask += ( 2 ** $_ ) } @{ $cond->{mask} }; |
1586 |
|
|
1587 |
|
push @args, 'mask=' . $mask if ($mask); |
1588 |
|
} |
1589 |
|
|
1590 |
return join('&', @args); |
return join('&', @args); |
1591 |
} |
} |
1608 |
sub shuttle_url { |
sub shuttle_url { |
1609 |
my $self = shift; |
my $self = shift; |
1610 |
|
|
1611 |
my ($url, $content_type, $reqbody, $resbody) = @_; |
my ($url, $content_type, $reqbody, $resbody, $croak_on_error) = @_; |
1612 |
|
|
1613 |
|
$croak_on_error = $self->{croak_on_error} unless defined($croak_on_error); |
1614 |
|
|
1615 |
$self->{status} = -1; |
$self->{status} = -1; |
1616 |
|
|
1654 |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
1655 |
|
|
1656 |
if (! $res->is_success) { |
if (! $res->is_success) { |
1657 |
if ($self->{croak_on_error}) { |
if ($croak_on_error) { |
1658 |
croak("can't get $url: ",$res->status_line); |
croak("can't get $url: ",$res->status_line); |
1659 |
} else { |
} else { |
1660 |
return -1; |
return -1; |
1732 |
croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/); |
croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/); |
1733 |
|
|
1734 |
$self->shuttle_url( $self->{url} . '/_set_user', |
$self->shuttle_url( $self->{url} . '/_set_user', |
1735 |
'text/plain', |
'application/x-www-form-urlencoded', |
1736 |
'name=' . uri_escape($name) . '&mode=' . $mode, |
'name=' . uri_escape($name) . '&mode=' . $mode, |
1737 |
undef |
undef |
1738 |
) == 200; |
) == 200; |
1759 |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
1760 |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
1761 |
|
|
1762 |
$self->shuttle_url( $self->{url} . '/_set_link', |
if ($self->shuttle_url( $self->{url} . '/_set_link', |
1763 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1764 |
$reqbody, |
$reqbody, |
1765 |
undef |
undef |
1766 |
) == 200; |
) == 200) { |
1767 |
|
# refresh node info after adding link |
1768 |
|
$self->_clear_info; |
1769 |
|
return 1; |
1770 |
|
} |
1771 |
|
return undef; |
1772 |
|
} |
1773 |
|
|
1774 |
|
=head2 admins |
1775 |
|
|
1776 |
|
my @admins = @{ $node->admins }; |
1777 |
|
|
1778 |
|
Return array of users with admin rights on node |
1779 |
|
|
1780 |
|
=cut |
1781 |
|
|
1782 |
|
sub admins { |
1783 |
|
my $self = shift; |
1784 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1785 |
|
return $self->{inform}->{admins}; |
1786 |
} |
} |
1787 |
|
|
1788 |
|
=head2 guests |
1789 |
|
|
1790 |
|
my @guests = @{ $node->guests }; |
1791 |
|
|
1792 |
|
Return array of users with guest rights on node |
1793 |
|
|
1794 |
|
=cut |
1795 |
|
|
1796 |
|
sub guests { |
1797 |
|
my $self = shift; |
1798 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1799 |
|
return $self->{inform}->{guests}; |
1800 |
|
} |
1801 |
|
|
1802 |
|
=head2 links |
1803 |
|
|
1804 |
|
my $links = @{ $node->links }; |
1805 |
|
|
1806 |
|
Return array of links for this node |
1807 |
|
|
1808 |
|
=cut |
1809 |
|
|
1810 |
|
sub links { |
1811 |
|
my $self = shift; |
1812 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1813 |
|
return $self->{inform}->{links}; |
1814 |
|
} |
1815 |
|
|
1816 |
|
=head2 cacheusage |
1817 |
|
|
1818 |
|
Return cache usage for a node |
1819 |
|
|
1820 |
|
my $cache = $node->cacheusage; |
1821 |
|
|
1822 |
|
=cut |
1823 |
|
|
1824 |
|
sub cacheusage { |
1825 |
|
my $self = shift; |
1826 |
|
|
1827 |
|
return unless ($self->{url}); |
1828 |
|
|
1829 |
|
my $resbody; |
1830 |
|
my $rv = $self->shuttle_url( $self->{url} . '/cacheusage', |
1831 |
|
'text/plain', |
1832 |
|
undef, |
1833 |
|
\$resbody, |
1834 |
|
); |
1835 |
|
|
1836 |
|
return if ($rv != 200 || !$resbody); |
1837 |
|
|
1838 |
|
return $resbody; |
1839 |
|
} |
1840 |
|
|
1841 |
|
=head2 master |
1842 |
|
|
1843 |
|
Set actions on Hyper Estraier node master (C<estmaster> process) |
1844 |
|
|
1845 |
|
$node->master( |
1846 |
|
action => 'sync' |
1847 |
|
); |
1848 |
|
|
1849 |
|
All available actions are documented in |
1850 |
|
L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol> |
1851 |
|
|
1852 |
|
=cut |
1853 |
|
|
1854 |
|
my $estmaster_rest = { |
1855 |
|
shutdown => { |
1856 |
|
status => 202, |
1857 |
|
}, |
1858 |
|
sync => { |
1859 |
|
status => 202, |
1860 |
|
}, |
1861 |
|
backup => { |
1862 |
|
status => 202, |
1863 |
|
}, |
1864 |
|
userlist => { |
1865 |
|
status => 200, |
1866 |
|
returns => [ qw/name passwd flags fname misc/ ], |
1867 |
|
}, |
1868 |
|
useradd => { |
1869 |
|
required => [ qw/name passwd flags/ ], |
1870 |
|
optional => [ qw/fname misc/ ], |
1871 |
|
status => 200, |
1872 |
|
}, |
1873 |
|
userdel => { |
1874 |
|
required => [ qw/name/ ], |
1875 |
|
status => 200, |
1876 |
|
}, |
1877 |
|
nodelist => { |
1878 |
|
status => 200, |
1879 |
|
returns => [ qw/name label doc_num word_num size/ ], |
1880 |
|
}, |
1881 |
|
nodeadd => { |
1882 |
|
required => [ qw/name/ ], |
1883 |
|
optional => [ qw/label/ ], |
1884 |
|
status => 200, |
1885 |
|
}, |
1886 |
|
nodedel => { |
1887 |
|
required => [ qw/name/ ], |
1888 |
|
status => 200, |
1889 |
|
}, |
1890 |
|
nodeclr => { |
1891 |
|
required => [ qw/name/ ], |
1892 |
|
status => 200, |
1893 |
|
}, |
1894 |
|
nodertt => { |
1895 |
|
status => 200, |
1896 |
|
}, |
1897 |
|
}; |
1898 |
|
|
1899 |
|
sub master { |
1900 |
|
my $self = shift; |
1901 |
|
|
1902 |
|
my $args = {@_}; |
1903 |
|
|
1904 |
|
# have action? |
1905 |
|
my $action = $args->{action} || croak "need action, available: ", |
1906 |
|
join(", ",keys %{ $estmaster_rest }); |
1907 |
|
|
1908 |
|
# check if action is valid |
1909 |
|
my $rest = $estmaster_rest->{$action}; |
1910 |
|
croak "action '$action' is not supported, available actions: ", |
1911 |
|
join(", ",keys %{ $estmaster_rest }) unless ($rest); |
1912 |
|
|
1913 |
|
croak "BUG: action '$action' needs return status" unless ($rest->{status}); |
1914 |
|
|
1915 |
|
my @args; |
1916 |
|
|
1917 |
|
if ($rest->{required} || $rest->{optional}) { |
1918 |
|
|
1919 |
|
map { |
1920 |
|
croak "need parametar '$_' for action '$action'" unless ($args->{$_}); |
1921 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ); |
1922 |
|
} ( @{ $rest->{required} } ); |
1923 |
|
|
1924 |
|
map { |
1925 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); |
1926 |
|
} ( @{ $rest->{optional} } ); |
1927 |
|
|
1928 |
|
} |
1929 |
|
|
1930 |
|
my $uri = new URI( $self->{url} ); |
1931 |
|
|
1932 |
|
my $resbody; |
1933 |
|
|
1934 |
|
my $status = $self->shuttle_url( |
1935 |
|
'http://' . $uri->host_port . '/master?action=' . $action , |
1936 |
|
'application/x-www-form-urlencoded', |
1937 |
|
join('&', @args), |
1938 |
|
\$resbody, |
1939 |
|
1, |
1940 |
|
) or confess "shuttle_url failed"; |
1941 |
|
|
1942 |
|
if ($status == $rest->{status}) { |
1943 |
|
|
1944 |
|
# refresh node info after sync |
1945 |
|
$self->_clear_info if ($action eq 'sync' || $action =~ m/^node(?:add|del|clr)$/); |
1946 |
|
|
1947 |
|
if ($rest->{returns} && wantarray) { |
1948 |
|
|
1949 |
|
my @results; |
1950 |
|
my $fields = $#{$rest->{returns}}; |
1951 |
|
|
1952 |
|
foreach my $line ( split(/[\r\n]/,$resbody) ) { |
1953 |
|
my @e = split(/\t/, $line, $fields + 1); |
1954 |
|
my $row; |
1955 |
|
foreach my $i ( 0 .. $fields) { |
1956 |
|
$row->{ $rest->{returns}->[$i] } = $e[ $i ]; |
1957 |
|
} |
1958 |
|
push @results, $row; |
1959 |
|
} |
1960 |
|
|
1961 |
|
return @results; |
1962 |
|
|
1963 |
|
} elsif ($resbody) { |
1964 |
|
chomp $resbody; |
1965 |
|
return $resbody; |
1966 |
|
} else { |
1967 |
|
return 0E0; |
1968 |
|
} |
1969 |
|
} |
1970 |
|
|
1971 |
|
carp "expected status $rest->{status}, but got $status"; |
1972 |
|
return undef; |
1973 |
|
} |
1974 |
|
|
1975 |
=head1 PRIVATE METHODS |
=head1 PRIVATE METHODS |
1976 |
|
|
1999 |
|
|
2000 |
return if ($rv != 200 || !$resbody); |
return if ($rv != 200 || !$resbody); |
2001 |
|
|
2002 |
# it seems that response can have multiple line endings |
my @lines = split(/[\r\n]/,$resbody); |
2003 |
$resbody =~ s/[\r\n]+$//; |
|
2004 |
|
$self->_clear_info; |
2005 |
|
|
2006 |
|
( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum}, |
2007 |
|
$self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5); |
2008 |
|
|
2009 |
|
return $resbody unless (@lines); |
2010 |
|
|
2011 |
( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) = |
shift @lines; |
2012 |
split(/\t/, $resbody, 5); |
|
2013 |
|
while(my $admin = shift @lines) { |
2014 |
|
push @{$self->{inform}->{admins}}, $admin; |
2015 |
|
} |
2016 |
|
|
2017 |
|
while(my $guest = shift @lines) { |
2018 |
|
push @{$self->{inform}->{guests}}, $guest; |
2019 |
|
} |
2020 |
|
|
2021 |
|
while(my $link = shift @lines) { |
2022 |
|
push @{$self->{inform}->{links}}, $link; |
2023 |
|
} |
2024 |
|
|
2025 |
|
return $resbody; |
2026 |
|
|
2027 |
|
} |
2028 |
|
|
2029 |
|
=head2 _clear_info |
2030 |
|
|
2031 |
|
Clear information for node |
2032 |
|
|
2033 |
|
$node->_clear_info; |
2034 |
|
|
2035 |
|
On next call to C<name>, C<label>, C<doc_num>, C<word_num> or C<size> node |
2036 |
|
info will be fetch again from Hyper Estraier. |
2037 |
|
|
2038 |
|
=cut |
2039 |
|
sub _clear_info { |
2040 |
|
my $self = shift; |
2041 |
|
$self->{inform} = { |
2042 |
|
dnum => -1, |
2043 |
|
wnum => -1, |
2044 |
|
size => -1.0, |
2045 |
|
}; |
2046 |
} |
} |
2047 |
|
|
2048 |
### |
### |
2057 |
|
|
2058 |
Hyper Estraier Ruby interface on which this module is based. |
Hyper Estraier Ruby interface on which this module is based. |
2059 |
|
|
2060 |
|
Hyper Estraier now also has pure-perl binding included in distribution. It's |
2061 |
|
a faster way to access databases directly if you are not running |
2062 |
|
C<estmaster> P2P server. |
2063 |
|
|
2064 |
=head1 AUTHOR |
=head1 AUTHOR |
2065 |
|
|
2066 |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
2067 |
|
|
2068 |
|
Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code |
2069 |
|
|
2070 |
=head1 COPYRIGHT AND LICENSE |
=head1 COPYRIGHT AND LICENSE |
2071 |
|
|