4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.04_1'; |
our $VERSION = '0.07_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 |
|
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 |
103 |
=cut |
=cut |
104 |
|
|
105 |
sub _s { |
sub _s { |
106 |
my $text = $_[1] || return; |
my $text = $_[1]; |
107 |
|
return unless defined($text); |
108 |
$text =~ s/\s\s+/ /gs; |
$text =~ s/\s\s+/ /gs; |
109 |
$text =~ s/^\s+//; |
$text =~ s/^\s+//; |
110 |
$text =~ s/\s+$//; |
$text =~ s/\s+$//; |
332 |
my $draft; |
my $draft; |
333 |
|
|
334 |
foreach my $attr_name (sort keys %{ $self->{attrs} }) { |
foreach my $attr_name (sort keys %{ $self->{attrs} }) { |
335 |
$draft .= $attr_name . '=' . $self->{attrs}->{$attr_name} . "\n"; |
next unless defined(my $v = $self->{attrs}->{$attr_name}); |
336 |
|
$draft .= $attr_name . '=' . $v . "\n"; |
337 |
} |
} |
338 |
|
|
339 |
if ($self->{kwords}) { |
if ($self->{kwords}) { |
381 |
|
|
382 |
package Search::Estraier::Condition; |
package Search::Estraier::Condition; |
383 |
|
|
384 |
use Carp qw/confess croak/; |
use Carp qw/carp confess croak/; |
385 |
|
|
386 |
use Search::Estraier; |
use Search::Estraier; |
387 |
our @ISA = qw/Search::Estraier/; |
our @ISA = qw/Search::Estraier/; |
459 |
|
|
460 |
=head2 set_options |
=head2 set_options |
461 |
|
|
462 |
$cond->set_options( SURE => 1 ); |
$cond->set_options( 'SURE' ); |
463 |
|
|
464 |
|
$cond->set_options( qw/AGITO NOIDF SIMPLE/ ); |
465 |
|
|
466 |
|
Possible options are: |
467 |
|
|
468 |
|
=over 8 |
469 |
|
|
470 |
|
=item SURE |
471 |
|
|
472 |
|
check every N-gram |
473 |
|
|
474 |
|
=item USUAL |
475 |
|
|
476 |
|
check every second N-gram |
477 |
|
|
478 |
|
=item FAST |
479 |
|
|
480 |
|
check every third N-gram |
481 |
|
|
482 |
|
=item AGITO |
483 |
|
|
484 |
|
check every fourth N-gram |
485 |
|
|
486 |
|
=item NOIDF |
487 |
|
|
488 |
|
don't perform TF-IDF tuning |
489 |
|
|
490 |
|
=item SIMPLE |
491 |
|
|
492 |
|
use simplified query phrase |
493 |
|
|
494 |
|
=back |
495 |
|
|
496 |
|
Skipping N-grams will speed up search, but reduce accuracy. Every call to C<set_options> will reset previous |
497 |
|
options; |
498 |
|
|
499 |
|
This option changed in version C<0.04> of this module. It's backwards compatibile. |
500 |
|
|
501 |
=cut |
=cut |
502 |
|
|
503 |
my $options = { |
my $options = { |
|
# check N-gram keys skipping by three |
|
504 |
SURE => 1 << 0, |
SURE => 1 << 0, |
|
# check N-gram keys skipping by two |
|
505 |
USUAL => 1 << 1, |
USUAL => 1 << 1, |
|
# without TF-IDF tuning |
|
506 |
FAST => 1 << 2, |
FAST => 1 << 2, |
|
# with the simplified phrase |
|
507 |
AGITO => 1 << 3, |
AGITO => 1 << 3, |
|
# check every N-gram key |
|
508 |
NOIDF => 1 << 4, |
NOIDF => 1 << 4, |
|
# check N-gram keys skipping by one |
|
509 |
SIMPLE => 1 << 10, |
SIMPLE => 1 << 10, |
510 |
}; |
}; |
511 |
|
|
512 |
sub set_options { |
sub set_options { |
513 |
my $self = shift; |
my $self = shift; |
514 |
my $option = shift; |
my $opt = 0; |
515 |
confess "unknown option" unless ($options->{$option}); |
foreach my $option (@_) { |
516 |
$self->{options} ||= $options->{$option}; |
my $mask; |
517 |
|
unless ($mask = $options->{$option}) { |
518 |
|
if ($option eq '1') { |
519 |
|
next; |
520 |
|
} else { |
521 |
|
croak "unknown option $option"; |
522 |
|
} |
523 |
|
} |
524 |
|
$opt += $mask; |
525 |
|
} |
526 |
|
$self->{options} = $opt; |
527 |
} |
} |
528 |
|
|
529 |
|
|
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/; |
774 |
|
|
775 |
print $res->doc_num; |
print $res->doc_num; |
776 |
|
|
777 |
|
This will return real number of documents (limited by C<max>). |
778 |
|
If you want to get total number of hits, see C<hits>. |
779 |
|
|
780 |
=cut |
=cut |
781 |
|
|
782 |
sub doc_num { |
sub doc_num { |
808 |
|
|
809 |
Return specific hint from results. |
Return specific hint from results. |
810 |
|
|
811 |
print $rec->hint( 'VERSION' ); |
print $res->hint( 'VERSION' ); |
812 |
|
|
813 |
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>, |
814 |
C<TIME>, C<LINK#n>, C<VIEW>. |
C<TIME>, C<LINK#n>, C<VIEW>. |
821 |
return $self->{hints}->{$key}; |
return $self->{hints}->{$key}; |
822 |
} |
} |
823 |
|
|
824 |
|
=head2 hints |
825 |
|
|
826 |
|
More perlish version of C<hint>. This one returns hash. |
827 |
|
|
828 |
|
my %hints = $res->hints; |
829 |
|
|
830 |
|
=cut |
831 |
|
|
832 |
|
sub hints { |
833 |
|
my $self = shift; |
834 |
|
return $self->{hints}; |
835 |
|
} |
836 |
|
|
837 |
|
=head2 hits |
838 |
|
|
839 |
|
Syntaxtic sugar for total number of hits for this query |
840 |
|
|
841 |
|
print $res->hits; |
842 |
|
|
843 |
|
It's same as |
844 |
|
|
845 |
|
print $res->hint('HIT'); |
846 |
|
|
847 |
|
but shorter. |
848 |
|
|
849 |
|
=cut |
850 |
|
|
851 |
|
sub hits { |
852 |
|
my $self = shift; |
853 |
|
return $self->{hints}->{'HIT'} || 0; |
854 |
|
} |
855 |
|
|
856 |
package Search::Estraier::Node; |
package Search::Estraier::Node; |
857 |
|
|
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 |
|
|
1053 |
|
|
1054 |
$node->put_doc( $document_draft ) or die "can't add document"; |
$node->put_doc( $document_draft ) or die "can't add document"; |
1055 |
|
|
1056 |
Return true on success or false on failture. |
Return true on success or false on failure. |
1057 |
|
|
1058 |
=cut |
=cut |
1059 |
|
|
1061 |
my $self = shift; |
my $self = shift; |
1062 |
my $doc = shift || return; |
my $doc = shift || return; |
1063 |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
1064 |
$self->shuttle_url( $self->{url} . '/put_doc', |
if ($self->shuttle_url( $self->{url} . '/put_doc', |
1065 |
'text/x-estraier-draft', |
'text/x-estraier-draft', |
1066 |
$doc->dump_draft, |
$doc->dump_draft, |
1067 |
undef |
undef |
1068 |
) == 200; |
) == 200) { |
1069 |
|
$self->_clear_info; |
1070 |
|
return 1; |
1071 |
|
} |
1072 |
|
return undef; |
1073 |
} |
} |
1074 |
|
|
1075 |
|
|
1088 |
my $id = shift || return; |
my $id = shift || return; |
1089 |
return unless ($self->{url}); |
return unless ($self->{url}); |
1090 |
croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/); |
croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/); |
1091 |
$self->shuttle_url( $self->{url} . '/out_doc', |
if ($self->shuttle_url( $self->{url} . '/out_doc', |
1092 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1093 |
"id=$id", |
"id=$id", |
1094 |
undef |
undef |
1095 |
) == 200; |
) == 200) { |
1096 |
|
$self->_clear_info; |
1097 |
|
return 1; |
1098 |
|
} |
1099 |
|
return undef; |
1100 |
} |
} |
1101 |
|
|
1102 |
|
|
1114 |
my $self = shift; |
my $self = shift; |
1115 |
my $uri = shift || return; |
my $uri = shift || return; |
1116 |
return unless ($self->{url}); |
return unless ($self->{url}); |
1117 |
$self->shuttle_url( $self->{url} . '/out_doc', |
if ($self->shuttle_url( $self->{url} . '/out_doc', |
1118 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1119 |
"uri=" . uri_escape($uri), |
"uri=" . uri_escape($uri), |
1120 |
undef |
undef |
1121 |
) == 200; |
) == 200) { |
1122 |
|
$self->_clear_info; |
1123 |
|
return 1; |
1124 |
|
} |
1125 |
|
return undef; |
1126 |
} |
} |
1127 |
|
|
1128 |
|
|
1140 |
my $self = shift; |
my $self = shift; |
1141 |
my $doc = shift || return; |
my $doc = shift || return; |
1142 |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
1143 |
$self->shuttle_url( $self->{url} . '/edit_doc', |
if ($self->shuttle_url( $self->{url} . '/edit_doc', |
1144 |
'text/x-estraier-draft', |
'text/x-estraier-draft', |
1145 |
$doc->dump_draft, |
$doc->dump_draft, |
1146 |
undef |
undef |
1147 |
) == 200; |
) == 200) { |
1148 |
|
$self->_clear_info; |
1149 |
|
return 1; |
1150 |
|
} |
1151 |
|
return undef; |
1152 |
} |
} |
1153 |
|
|
1154 |
|
|
1257 |
|
|
1258 |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
my $id = $node->uri_to_id( 'file:///document/uri/42' ); |
1259 |
|
|
1260 |
|
This method won't croak, even if using C<croak_on_error>. |
1261 |
|
|
1262 |
=cut |
=cut |
1263 |
|
|
1264 |
sub uri_to_id { |
sub uri_to_id { |
1265 |
my $self = shift; |
my $self = shift; |
1266 |
my $uri = shift || return; |
my $uri = shift || return; |
1267 |
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 ); |
1268 |
} |
} |
1269 |
|
|
1270 |
|
|
1324 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1325 |
$arg, |
$arg, |
1326 |
\$resbody, |
\$resbody, |
1327 |
|
$a->{croak_on_error}, |
1328 |
); |
); |
1329 |
|
|
1330 |
return if ($rv != 200); |
return if ($rv != 200); |
1355 |
|
|
1356 |
sub name { |
sub name { |
1357 |
my $self = shift; |
my $self = shift; |
1358 |
$self->_set_info unless ($self->{name}); |
$self->_set_info unless ($self->{inform}->{name}); |
1359 |
return $self->{name}; |
return $self->{inform}->{name}; |
1360 |
} |
} |
1361 |
|
|
1362 |
|
|
1368 |
|
|
1369 |
sub label { |
sub label { |
1370 |
my $self = shift; |
my $self = shift; |
1371 |
$self->_set_info unless ($self->{label}); |
$self->_set_info unless ($self->{inform}->{label}); |
1372 |
return $self->{label}; |
return $self->{inform}->{label}; |
1373 |
} |
} |
1374 |
|
|
1375 |
|
|
1381 |
|
|
1382 |
sub doc_num { |
sub doc_num { |
1383 |
my $self = shift; |
my $self = shift; |
1384 |
$self->_set_info if ($self->{dnum} < 0); |
$self->_set_info if ($self->{inform}->{dnum} < 0); |
1385 |
return $self->{dnum}; |
return $self->{inform}->{dnum}; |
1386 |
} |
} |
1387 |
|
|
1388 |
|
|
1394 |
|
|
1395 |
sub word_num { |
sub word_num { |
1396 |
my $self = shift; |
my $self = shift; |
1397 |
$self->_set_info if ($self->{wnum} < 0); |
$self->_set_info if ($self->{inform}->{wnum} < 0); |
1398 |
return $self->{wnum}; |
return $self->{inform}->{wnum}; |
1399 |
} |
} |
1400 |
|
|
1401 |
|
|
1407 |
|
|
1408 |
sub size { |
sub size { |
1409 |
my $self = shift; |
my $self = shift; |
1410 |
$self->_set_info if ($self->{size} < 0); |
$self->_set_info if ($self->{inform}->{size} < 0); |
1411 |
return $self->{size}; |
return $self->{inform}->{size}; |
1412 |
} |
} |
1413 |
|
|
1414 |
|
|
1441 |
); |
); |
1442 |
return if ($rv != 200); |
return if ($rv != 200); |
1443 |
|
|
1444 |
my (@docs, $hints); |
my @records = split /--------\[.*?\]--------(?::END)?\r?\n/, $resbody; |
1445 |
|
my $hintsText = splice @records, 0, 2; # starts with empty record |
1446 |
my @lines = split(/\n/, $resbody); |
my $hints = { $hintsText =~ m/^(.*?)\t(.*?)$/gsm }; |
1447 |
return unless (@lines); |
|
1448 |
|
# process records |
1449 |
my $border = $lines[0]; |
my $docs = []; |
1450 |
my $isend = 0; |
foreach my $record (@records) |
1451 |
my $lnum = 1; |
{ |
1452 |
|
# split into keys and snippets |
1453 |
while ( $lnum <= $#lines ) { |
my ($keys, $snippet) = $record =~ m/^(.*?)\n\n(.*?)$/s; |
1454 |
my $line = $lines[$lnum]; |
|
1455 |
$lnum++; |
# create document hash |
1456 |
|
my $doc = { $keys =~ m/^(.*?)=(.*?)$/gsm }; |
1457 |
#warn "## $line\n"; |
$doc->{'@keywords'} = $doc->{keywords}; |
1458 |
if ($line && $line =~ m/^\Q$border\E(:END)*$/) { |
($doc->{keywords}) = $keys =~ m/^%VECTOR\t(.*?)$/gm; |
1459 |
$isend = $1; |
$doc->{snippet} = $snippet; |
1460 |
last; |
|
1461 |
} |
push @$docs, new Search::Estraier::ResultDocument( |
1462 |
|
attrs => $doc, |
1463 |
if ($line =~ /\t/) { |
uri => $doc->{'@uri'}, |
1464 |
my ($k,$v) = split(/\t/, $line, 2); |
snippet => $snippet, |
1465 |
$hints->{$k} = $v; |
keywords => $doc->{'keywords'}, |
1466 |
} |
); |
1467 |
} |
} |
1468 |
|
|
1469 |
my $snum = $lnum; |
return new Search::Estraier::NodeResult( docs => $docs, hints => $hints ); |
|
|
|
|
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; |
|
|
} |
|
|
|
|
|
#warn Dumper(\@docs, $hints); |
|
|
|
|
|
return new Search::Estraier::NodeResult( docs => \@docs, hints => $hints ); |
|
1470 |
} |
} |
1471 |
|
|
1472 |
|
|
1515 |
push @args, 'wwidth=' . $self->{wwidth}; |
push @args, 'wwidth=' . $self->{wwidth}; |
1516 |
push @args, 'hwidth=' . $self->{hwidth}; |
push @args, 'hwidth=' . $self->{hwidth}; |
1517 |
push @args, 'awidth=' . $self->{awidth}; |
push @args, 'awidth=' . $self->{awidth}; |
1518 |
|
push @args, 'skip=' . $cond->{skip} if ($cond->{skip}); |
1519 |
|
|
1520 |
return join('&', @args); |
return join('&', @args); |
1521 |
} |
} |
1538 |
sub shuttle_url { |
sub shuttle_url { |
1539 |
my $self = shift; |
my $self = shift; |
1540 |
|
|
1541 |
my ($url, $content_type, $reqbody, $resbody) = @_; |
my ($url, $content_type, $reqbody, $resbody, $croak_on_error) = @_; |
1542 |
|
|
1543 |
|
$croak_on_error = $self->{croak_on_error} unless defined($croak_on_error); |
1544 |
|
|
1545 |
$self->{status} = -1; |
$self->{status} = -1; |
1546 |
|
|
1584 |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
1585 |
|
|
1586 |
if (! $res->is_success) { |
if (! $res->is_success) { |
1587 |
if ($self->{croak_on_error}) { |
if ($croak_on_error) { |
1588 |
croak("can't get $url: ",$res->status_line); |
croak("can't get $url: ",$res->status_line); |
1589 |
} else { |
} else { |
1590 |
return -1; |
return -1; |
1662 |
croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/); |
croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/); |
1663 |
|
|
1664 |
$self->shuttle_url( $self->{url} . '/_set_user', |
$self->shuttle_url( $self->{url} . '/_set_user', |
1665 |
'text/plain', |
'application/x-www-form-urlencoded', |
1666 |
'name=' . uri_escape($name) . '&mode=' . $mode, |
'name=' . uri_escape($name) . '&mode=' . $mode, |
1667 |
undef |
undef |
1668 |
) == 200; |
) == 200; |
1689 |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
my $reqbody = 'url=' . uri_escape($url) . '&label=' . uri_escape($label); |
1690 |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
1691 |
|
|
1692 |
$self->shuttle_url( $self->{url} . '/_set_link', |
if ($self->shuttle_url( $self->{url} . '/_set_link', |
1693 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1694 |
$reqbody, |
$reqbody, |
1695 |
undef |
undef |
1696 |
) == 200; |
) == 200) { |
1697 |
|
# refresh node info after adding link |
1698 |
|
$self->_clear_info; |
1699 |
|
return 1; |
1700 |
|
} |
1701 |
|
return undef; |
1702 |
|
} |
1703 |
|
|
1704 |
|
=head2 admins |
1705 |
|
|
1706 |
|
my @admins = @{ $node->admins }; |
1707 |
|
|
1708 |
|
Return array of users with admin rights on node |
1709 |
|
|
1710 |
|
=cut |
1711 |
|
|
1712 |
|
sub admins { |
1713 |
|
my $self = shift; |
1714 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1715 |
|
return $self->{inform}->{admins}; |
1716 |
|
} |
1717 |
|
|
1718 |
|
=head2 guests |
1719 |
|
|
1720 |
|
my @guests = @{ $node->guests }; |
1721 |
|
|
1722 |
|
Return array of users with guest rights on node |
1723 |
|
|
1724 |
|
=cut |
1725 |
|
|
1726 |
|
sub guests { |
1727 |
|
my $self = shift; |
1728 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1729 |
|
return $self->{inform}->{guests}; |
1730 |
|
} |
1731 |
|
|
1732 |
|
=head2 links |
1733 |
|
|
1734 |
|
my $links = @{ $node->links }; |
1735 |
|
|
1736 |
|
Return array of links for this node |
1737 |
|
|
1738 |
|
=cut |
1739 |
|
|
1740 |
|
sub links { |
1741 |
|
my $self = shift; |
1742 |
|
$self->_set_info unless ($self->{inform}->{name}); |
1743 |
|
return $self->{inform}->{links}; |
1744 |
} |
} |
1745 |
|
|
1746 |
|
=head2 master |
1747 |
|
|
1748 |
|
Set actions on Hyper Estraier node master (C<estmaster> process) |
1749 |
|
|
1750 |
|
$node->master( |
1751 |
|
action => 'sync' |
1752 |
|
); |
1753 |
|
|
1754 |
|
All available actions are documented in |
1755 |
|
L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol> |
1756 |
|
|
1757 |
|
=cut |
1758 |
|
|
1759 |
|
my $estmaster_rest = { |
1760 |
|
shutdown => { |
1761 |
|
status => 202, |
1762 |
|
}, |
1763 |
|
sync => { |
1764 |
|
status => 202, |
1765 |
|
}, |
1766 |
|
backup => { |
1767 |
|
status => 202, |
1768 |
|
}, |
1769 |
|
userlist => { |
1770 |
|
status => 200, |
1771 |
|
returns => [ qw/name passwd flags fname misc/ ], |
1772 |
|
}, |
1773 |
|
useradd => { |
1774 |
|
required => [ qw/name passwd flags/ ], |
1775 |
|
optional => [ qw/fname misc/ ], |
1776 |
|
status => 200, |
1777 |
|
}, |
1778 |
|
userdel => { |
1779 |
|
required => [ qw/name/ ], |
1780 |
|
status => 200, |
1781 |
|
}, |
1782 |
|
nodelist => { |
1783 |
|
status => 200, |
1784 |
|
returns => [ qw/name label doc_num word_num size/ ], |
1785 |
|
}, |
1786 |
|
nodeadd => { |
1787 |
|
required => [ qw/name/ ], |
1788 |
|
optional => [ qw/label/ ], |
1789 |
|
status => 200, |
1790 |
|
}, |
1791 |
|
nodedel => { |
1792 |
|
required => [ qw/name/ ], |
1793 |
|
status => 200, |
1794 |
|
}, |
1795 |
|
nodeclr => { |
1796 |
|
required => [ qw/name/ ], |
1797 |
|
status => 200, |
1798 |
|
}, |
1799 |
|
nodertt => { |
1800 |
|
status => 200, |
1801 |
|
}, |
1802 |
|
}; |
1803 |
|
|
1804 |
|
sub master { |
1805 |
|
my $self = shift; |
1806 |
|
|
1807 |
|
my $args = {@_}; |
1808 |
|
|
1809 |
|
# have action? |
1810 |
|
my $action = $args->{action} || croak "need action, available: ", |
1811 |
|
join(", ",keys %{ $estmaster_rest }); |
1812 |
|
|
1813 |
|
# check if action is valid |
1814 |
|
my $rest = $estmaster_rest->{$action}; |
1815 |
|
croak "action '$action' is not supported, available actions: ", |
1816 |
|
join(", ",keys %{ $estmaster_rest }) unless ($rest); |
1817 |
|
|
1818 |
|
croak "BUG: action '$action' needs return status" unless ($rest->{status}); |
1819 |
|
|
1820 |
|
my @args; |
1821 |
|
|
1822 |
|
if ($rest->{required} || $rest->{optional}) { |
1823 |
|
|
1824 |
|
map { |
1825 |
|
croak "need parametar '$_' for action '$action'" unless ($args->{$_}); |
1826 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ); |
1827 |
|
} ( @{ $rest->{required} } ); |
1828 |
|
|
1829 |
|
map { |
1830 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); |
1831 |
|
} ( @{ $rest->{optional} } ); |
1832 |
|
|
1833 |
|
} |
1834 |
|
|
1835 |
|
my $uri = new URI( $self->{url} ); |
1836 |
|
|
1837 |
|
my $resbody; |
1838 |
|
|
1839 |
|
my $status = $self->shuttle_url( |
1840 |
|
'http://' . $uri->host_port . '/master?action=' . $action , |
1841 |
|
'application/x-www-form-urlencoded', |
1842 |
|
join('&', @args), |
1843 |
|
\$resbody, |
1844 |
|
1, |
1845 |
|
) or confess "shuttle_url failed"; |
1846 |
|
|
1847 |
|
if ($status == $rest->{status}) { |
1848 |
|
|
1849 |
|
# refresh node info after sync |
1850 |
|
$self->_clear_info if ($action eq 'sync' || $action =~ m/^node(?:add|del|clr)$/); |
1851 |
|
|
1852 |
|
if ($rest->{returns} && wantarray) { |
1853 |
|
|
1854 |
|
my @results; |
1855 |
|
my $fields = $#{$rest->{returns}}; |
1856 |
|
|
1857 |
|
foreach my $line ( split(/[\r\n]/,$resbody) ) { |
1858 |
|
my @e = split(/\t/, $line, $fields + 1); |
1859 |
|
my $row; |
1860 |
|
foreach my $i ( 0 .. $fields) { |
1861 |
|
$row->{ $rest->{returns}->[$i] } = $e[ $i ]; |
1862 |
|
} |
1863 |
|
push @results, $row; |
1864 |
|
} |
1865 |
|
|
1866 |
|
return @results; |
1867 |
|
|
1868 |
|
} elsif ($resbody) { |
1869 |
|
chomp $resbody; |
1870 |
|
return $resbody; |
1871 |
|
} else { |
1872 |
|
return 0E0; |
1873 |
|
} |
1874 |
|
} |
1875 |
|
|
1876 |
|
carp "expected status $rest->{status}, but got $status"; |
1877 |
|
return undef; |
1878 |
|
} |
1879 |
|
|
1880 |
=head1 PRIVATE METHODS |
=head1 PRIVATE METHODS |
1881 |
|
|
1904 |
|
|
1905 |
return if ($rv != 200 || !$resbody); |
return if ($rv != 200 || !$resbody); |
1906 |
|
|
1907 |
# it seems that response can have multiple line endings |
my @lines = split(/[\r\n]/,$resbody); |
1908 |
$resbody =~ s/[\r\n]+$//; |
|
1909 |
|
$self->_clear_info; |
1910 |
|
|
1911 |
|
( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum}, |
1912 |
|
$self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5); |
1913 |
|
|
1914 |
( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) = |
return $resbody unless (@lines); |
1915 |
split(/\t/, $resbody, 5); |
|
1916 |
|
shift @lines; |
1917 |
|
|
1918 |
|
while(my $admin = shift @lines) { |
1919 |
|
push @{$self->{inform}->{admins}}, $admin; |
1920 |
|
} |
1921 |
|
|
1922 |
|
while(my $guest = shift @lines) { |
1923 |
|
push @{$self->{inform}->{guests}}, $guest; |
1924 |
|
} |
1925 |
|
|
1926 |
|
while(my $link = shift @lines) { |
1927 |
|
push @{$self->{inform}->{links}}, $link; |
1928 |
|
} |
1929 |
|
|
1930 |
|
return $resbody; |
1931 |
|
|
1932 |
|
} |
1933 |
|
|
1934 |
|
=head2 _clear_info |
1935 |
|
|
1936 |
|
Clear information for node |
1937 |
|
|
1938 |
|
$node->_clear_info; |
1939 |
|
|
1940 |
|
On next call to C<name>, C<label>, C<doc_num>, C<word_num> or C<size> node |
1941 |
|
info will be fetch again from Hyper Estraier. |
1942 |
|
|
1943 |
|
=cut |
1944 |
|
sub _clear_info { |
1945 |
|
my $self = shift; |
1946 |
|
$self->{inform} = { |
1947 |
|
dnum => -1, |
1948 |
|
wnum => -1, |
1949 |
|
size => -1.0, |
1950 |
|
}; |
1951 |
} |
} |
1952 |
|
|
1953 |
### |
### |
1966 |
|
|
1967 |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
1968 |
|
|
1969 |
|
Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code |
1970 |
|
|
1971 |
=head1 COPYRIGHT AND LICENSE |
=head1 COPYRIGHT AND LICENSE |
1972 |
|
|