272 |
|
|
273 |
$draft .= "\n"; |
$draft .= "\n"; |
274 |
|
|
275 |
$draft .= join("\n", @{ $self->{dtexts} }) . "\n"; |
$draft .= join("\n", @{ $self->{dtexts} }) . "\n" if ($self->{dtexts}); |
276 |
$draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n"; |
$draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n" if ($self->{htexts}); |
277 |
|
|
278 |
return $draft; |
return $draft; |
279 |
} |
} |
324 |
my $self = {}; |
my $self = {}; |
325 |
bless($self, $class); |
bless($self, $class); |
326 |
|
|
327 |
|
$self->{max} = -1; |
328 |
|
$self->{options} = 0; |
329 |
|
|
330 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
331 |
} |
} |
332 |
|
|
405 |
$self->{options} ||= $options->{$option}; |
$self->{options} ||= $options->{$option}; |
406 |
} |
} |
407 |
|
|
408 |
package Search::Estraier::Master; |
=head2 phrase |
409 |
|
|
410 |
|
Return search phrase. |
411 |
|
|
412 |
|
print $cond->phrase; |
413 |
|
|
414 |
|
=cut |
415 |
|
|
416 |
|
sub phrase { |
417 |
|
my $self = shift; |
418 |
|
return $self->{phrase}; |
419 |
|
} |
420 |
|
|
421 |
use Carp; |
=head2 order |
422 |
|
|
423 |
=head1 Search::Estraier::Master |
Return search result order. |
424 |
|
|
425 |
Controll node master. This requires user with administration priviledges. |
print $cond->order; |
426 |
|
|
427 |
=cut |
=cut |
428 |
|
|
429 |
{ |
sub order { |
430 |
package RequestAgent; |
my $self = shift; |
431 |
our @ISA = qw(LWP::UserAgent); |
return $self->{order}; |
432 |
|
} |
433 |
|
|
434 |
sub new { |
=head2 attrs |
|
my $self = LWP::UserAgent::new(@_); |
|
|
$self->agent("Search-Estraier/$Search::Estraer::VERSION"); |
|
|
$self; |
|
|
} |
|
435 |
|
|
436 |
sub get_basic_credentials { |
Return search result attrs. |
437 |
my($self, $realm, $uri) = @_; |
|
438 |
# return ($user, $password); |
my @cond_attrs = $cond->attrs; |
439 |
} |
|
440 |
|
=cut |
441 |
|
|
442 |
|
sub attrs { |
443 |
|
my $self = shift; |
444 |
|
#croak "attrs return array, not scalar" if (! wantarray); |
445 |
|
return @{ $self->{attrs} }; |
446 |
} |
} |
447 |
|
|
448 |
|
=head2 max |
449 |
|
|
450 |
|
Return maximum number of results. |
451 |
|
|
452 |
|
print $cond->max; |
453 |
|
|
454 |
|
C<-1> is returned for unitialized value, C<0> is unlimited. |
455 |
|
|
456 |
|
=cut |
457 |
|
|
458 |
|
sub max { |
459 |
|
my $self = shift; |
460 |
|
return $self->{max}; |
461 |
|
} |
462 |
|
|
463 |
|
=head2 options |
464 |
|
|
465 |
|
Return options for this condition. |
466 |
|
|
467 |
|
print $cond->options; |
468 |
|
|
469 |
|
Options are returned in numerical form. |
470 |
|
|
471 |
|
=cut |
472 |
|
|
473 |
|
sub options { |
474 |
|
my $self = shift; |
475 |
|
return $self->{options}; |
476 |
|
} |
477 |
|
|
478 |
|
|
479 |
|
package Search::Estraier::ResultDocument; |
480 |
|
|
481 |
|
use Carp qw/croak/; |
482 |
|
|
483 |
|
#use Search::Estraier; |
484 |
|
#our @ISA = qw/Search::Estraier/; |
485 |
|
|
486 |
|
=head1 Search::Estraier::ResultDocument |
487 |
|
|
488 |
=head2 new |
=head2 new |
489 |
|
|
490 |
Create new connection to node master. |
my $rdoc = new Search::HyperEstraier::ResultDocument( |
491 |
|
uri => 'http://localhost/document/uri/42', |
492 |
|
attrs => { |
493 |
|
foo => 1, |
494 |
|
bar => 2, |
495 |
|
}, |
496 |
|
snippet => 'this is a text of snippet' |
497 |
|
keywords => 'this\tare\tkeywords' |
498 |
|
); |
499 |
|
|
500 |
|
=cut |
501 |
|
|
502 |
|
sub new { |
503 |
|
my $class = shift; |
504 |
|
my $self = {@_}; |
505 |
|
bless($self, $class); |
506 |
|
|
507 |
|
foreach my $f (qw/uri attrs snippet keywords/) { |
508 |
|
croak "missing $f for ResultDocument" unless defined($self->{$f}); |
509 |
|
} |
510 |
|
|
511 |
|
$self ? return $self : return undef; |
512 |
|
} |
513 |
|
|
514 |
|
=head2 uri |
515 |
|
|
516 |
|
Return URI of result document |
517 |
|
|
518 |
|
print $rdoc->uri; |
519 |
|
|
520 |
|
=cut |
521 |
|
|
522 |
|
sub uri { |
523 |
|
my $self = shift; |
524 |
|
return $self->{uri}; |
525 |
|
} |
526 |
|
|
527 |
|
|
528 |
|
=head2 attr_names |
529 |
|
|
530 |
my $master = new Search::Estraier::Master( |
Returns array with attribute names from result document object. |
531 |
url => 'http://localhost:1978', |
|
532 |
user => 'admin', |
my @attrs = $rdoc->attr_names; |
533 |
passwd => 'admin', |
|
534 |
|
=cut |
535 |
|
|
536 |
|
sub attr_names { |
537 |
|
my $self = shift; |
538 |
|
croak "attr_names return array, not scalar" if (! wantarray); |
539 |
|
return sort keys %{ $self->{attrs} }; |
540 |
|
} |
541 |
|
|
542 |
|
=head2 attr |
543 |
|
|
544 |
|
Returns value of an attribute. |
545 |
|
|
546 |
|
my $value = $rdoc->attr( 'attribute' ); |
547 |
|
|
548 |
|
=cut |
549 |
|
|
550 |
|
sub attr { |
551 |
|
my $self = shift; |
552 |
|
my $name = shift || return; |
553 |
|
return $self->{attrs}->{ $name }; |
554 |
|
} |
555 |
|
|
556 |
|
=head2 snippet |
557 |
|
|
558 |
|
Return snippet from result document |
559 |
|
|
560 |
|
print $rdoc->snippet; |
561 |
|
|
562 |
|
=cut |
563 |
|
|
564 |
|
sub snippet { |
565 |
|
my $self = shift; |
566 |
|
return $self->{snippet}; |
567 |
|
} |
568 |
|
|
569 |
|
=head2 keywords |
570 |
|
|
571 |
|
Return keywords from result document |
572 |
|
|
573 |
|
print $rdoc->keywords; |
574 |
|
|
575 |
|
=cut |
576 |
|
|
577 |
|
sub keywords { |
578 |
|
my $self = shift; |
579 |
|
return $self->{keywords}; |
580 |
|
} |
581 |
|
|
582 |
|
|
583 |
|
package Search::Estraier::NodeResult; |
584 |
|
|
585 |
|
use Carp qw/croak/; |
586 |
|
|
587 |
|
#use Search::Estraier; |
588 |
|
#our @ISA = qw/Search::Estraier/; |
589 |
|
|
590 |
|
=head1 Search::Estraier::NodeResult |
591 |
|
|
592 |
|
=head2 new |
593 |
|
|
594 |
|
my $res = new Search::HyperEstraier::NodeResult( |
595 |
|
docs => @array_of_rdocs, |
596 |
|
hits => %hash_with_hints, |
597 |
); |
); |
598 |
|
|
599 |
=cut |
=cut |
603 |
my $self = {@_}; |
my $self = {@_}; |
604 |
bless($self, $class); |
bless($self, $class); |
605 |
|
|
606 |
foreach my $p (qw/url user passwd/) { |
foreach my $f (qw/docs hints/) { |
607 |
croak "need $p" unless ($self->{$p}); |
croak "missing $f for ResultDocument" unless defined($self->{$f}); |
608 |
} |
} |
609 |
|
|
610 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
611 |
} |
} |
612 |
|
|
613 |
|
=head2 doc_num |
614 |
|
|
615 |
|
Return number of documents |
616 |
|
|
617 |
|
print $res->doc_num; |
618 |
|
|
619 |
|
=cut |
620 |
|
|
621 |
|
sub doc_num { |
622 |
|
my $self = shift; |
623 |
|
return $#{$self->{docs}}; |
624 |
|
} |
625 |
|
|
626 |
|
=head2 get_doc |
627 |
|
|
628 |
|
Return single document |
629 |
|
|
630 |
|
my $doc = $res->get_doc( 42 ); |
631 |
|
|
632 |
|
Returns undef if document doesn't exist. |
633 |
|
|
634 |
|
=cut |
635 |
|
|
636 |
|
sub get_doc { |
637 |
|
my $self = shift; |
638 |
|
my $num = shift; |
639 |
|
croak "expect number as argument" unless ($num =~ m/^\d+$/); |
640 |
|
return undef if ($num < 0 || $num > $self->{docs}); |
641 |
|
return $self->{docs}->[$num]; |
642 |
|
} |
643 |
|
|
644 |
|
=head2 hint |
645 |
|
|
646 |
|
Return specific hint from results. |
647 |
|
|
648 |
|
print $rec->hint( 'VERSION' ); |
649 |
|
|
650 |
|
Possible hints are: C<VERSION>, C<NODE>, C<HIT>, C<HINT#n>, C<DOCNUM>, C<WORDNUM>, |
651 |
|
C<TIME>, C<LINK#n>, C<VIEW>. |
652 |
|
|
653 |
|
=cut |
654 |
|
|
655 |
|
sub hint { |
656 |
|
my $self = shift; |
657 |
|
my $key = shift || return; |
658 |
|
return $self->{hints}->{$key}; |
659 |
|
} |
660 |
|
|
661 |
|
|
662 |
|
package Search::Estraier::Node; |
663 |
|
|
664 |
|
use Carp qw/carp croak/; |
665 |
|
use URI; |
666 |
|
use MIME::Base64; |
667 |
|
use IO::Socket::INET; |
668 |
|
|
669 |
|
=head1 Search::Estraier::Node |
670 |
|
|
671 |
|
=head2 new |
672 |
|
|
673 |
|
my $node = new Search::HyperEstraier::Node; |
674 |
|
|
675 |
|
=cut |
676 |
|
|
677 |
|
sub new { |
678 |
|
my $class = shift; |
679 |
|
my $self = { |
680 |
|
pxport => -1, |
681 |
|
timeout => 0, # this used to be -1 |
682 |
|
dnum => -1, |
683 |
|
wnum => -1, |
684 |
|
size => -1.0, |
685 |
|
wwidth => 480, |
686 |
|
hwidth => 96, |
687 |
|
awidth => 96, |
688 |
|
status => -1, |
689 |
|
}; |
690 |
|
bless($self, $class); |
691 |
|
|
692 |
|
if (@_) { |
693 |
|
$self->{debug} = shift; |
694 |
|
warn "## Node debug on\n"; |
695 |
|
} |
696 |
|
|
697 |
|
$self ? return $self : return undef; |
698 |
|
} |
699 |
|
|
700 |
|
=head2 set_url |
701 |
|
|
702 |
|
Specify URL to node server |
703 |
|
|
704 |
|
$node->set_url('http://localhost:1978'); |
705 |
|
|
706 |
|
=cut |
707 |
|
|
708 |
|
sub set_url { |
709 |
|
my $self = shift; |
710 |
|
$self->{url} = shift; |
711 |
|
} |
712 |
|
|
713 |
|
=head2 set_proxy |
714 |
|
|
715 |
|
Specify proxy server to connect to node server |
716 |
|
|
717 |
|
$node->set_proxy('proxy.example.com', 8080); |
718 |
|
|
719 |
|
=cut |
720 |
|
|
721 |
|
sub set_proxy { |
722 |
|
my $self = shift; |
723 |
|
my ($host,$port) = @_; |
724 |
|
croak "proxy port must be number" unless ($port =~ m/^\d+$/); |
725 |
|
$self->{pxhost} = $host; |
726 |
|
$self->{pxport} = $port; |
727 |
|
} |
728 |
|
|
729 |
|
=head2 set_timeout |
730 |
|
|
731 |
|
Specify timeout of connection in seconds |
732 |
|
|
733 |
|
$node->set_timeout( 15 ); |
734 |
|
|
735 |
|
=cut |
736 |
|
|
737 |
|
sub set_timeout { |
738 |
|
my $self = shift; |
739 |
|
my $sec = shift; |
740 |
|
croak "timeout must be number" unless ($sec =~ m/^\d+$/); |
741 |
|
$self->{timeout} = $sec; |
742 |
|
} |
743 |
|
|
744 |
|
=head2 set_auth |
745 |
|
|
746 |
|
Specify name and password for authentication to node server. |
747 |
|
|
748 |
|
$node->set_auth('clint','eastwood'); |
749 |
|
|
750 |
|
=cut |
751 |
|
|
752 |
|
sub set_auth { |
753 |
|
my $self = shift; |
754 |
|
my ($login,$passwd) = @_; |
755 |
|
my $basic_auth = encode_base64( "$login:$passwd" ); |
756 |
|
chomp($basic_auth); |
757 |
|
$self->{auth} = $basic_auth; |
758 |
|
} |
759 |
|
|
760 |
|
=head2 status |
761 |
|
|
762 |
|
Return status code of last request. |
763 |
|
|
764 |
|
print $node->status; |
765 |
|
|
766 |
|
C<-1> means connection failure. |
767 |
|
|
768 |
|
=cut |
769 |
|
|
770 |
|
sub status { |
771 |
|
my $self = shift; |
772 |
|
return $self->{status}; |
773 |
|
} |
774 |
|
|
775 |
|
=head2 put_doc |
776 |
|
|
777 |
|
Add a document |
778 |
|
|
779 |
|
$node->put_doc( $document_draft ) or die "can't add document"; |
780 |
|
|
781 |
|
Return true on success or false on failture. |
782 |
|
|
783 |
|
=cut |
784 |
|
|
785 |
|
sub put_doc { |
786 |
|
my $self = shift; |
787 |
|
my $doc = shift || return; |
788 |
|
return unless ($self->{url}); |
789 |
|
$self->shuttle_url( $self->{url} . '/put_doc', |
790 |
|
'text/x-estraier-draft', |
791 |
|
$doc->dump_draft, |
792 |
|
undef |
793 |
|
) == 200; |
794 |
|
} |
795 |
|
|
796 |
|
|
797 |
|
=head2 out_doc |
798 |
|
|
799 |
|
Remove a document |
800 |
|
|
801 |
|
$node->out_doc( document_id ) or "can't remove document"; |
802 |
|
|
803 |
|
Return true on success or false on failture. |
804 |
|
|
805 |
|
=cut |
806 |
|
|
807 |
|
sub out_doc { |
808 |
|
my $self = shift; |
809 |
|
my $id = shift || return; |
810 |
|
return unless ($self->{url}); |
811 |
|
croak "id must be number" unless ($id =~ m/^\d+$/); |
812 |
|
$self->shuttle_url( $self->{url} . '/out_doc', |
813 |
|
'application/x-www-form-urlencoded', |
814 |
|
"id=$id", |
815 |
|
undef |
816 |
|
) == 200; |
817 |
|
} |
818 |
|
|
819 |
|
|
820 |
|
=head2 out_doc_by_uri |
821 |
|
|
822 |
|
Remove a registrated document using it's uri |
823 |
|
|
824 |
|
$node->out_doc_by_uri( 'file:///document_url' ) or "can't remove document"; |
825 |
|
|
826 |
|
Return true on success or false on failture. |
827 |
|
|
828 |
|
=cut |
829 |
|
|
830 |
|
sub out_doc_by_uri { |
831 |
|
my $self = shift; |
832 |
|
my $uri = shift || return; |
833 |
|
return unless ($self->{url}); |
834 |
|
$self->shuttle_url( $self->{url} . '/out_doc', |
835 |
|
'application/x-www-form-urlencoded', |
836 |
|
"uri=$uri", |
837 |
|
undef |
838 |
|
) == 200; |
839 |
|
} |
840 |
|
|
841 |
|
=head2 shuttle_url |
842 |
|
|
843 |
|
This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node |
844 |
|
master. |
845 |
|
|
846 |
|
my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody ); |
847 |
|
|
848 |
|
C<$resheads> and C<$resbody> booleans controll if response headers and/or response |
849 |
|
body will be saved within object. |
850 |
|
|
851 |
|
=cut |
852 |
|
|
853 |
|
sub shuttle_url { |
854 |
|
my $self = shift; |
855 |
|
|
856 |
|
my ($url, $content_type, $reqbody, $resbody) = @_; |
857 |
|
|
858 |
|
$self->{status} = -1; |
859 |
|
|
860 |
|
warn "## $url\n" if ($self->{debug}); |
861 |
|
|
862 |
|
$url = new URI($url); |
863 |
|
if ( |
864 |
|
!$url || !$url->scheme || !$url->scheme eq 'http' || |
865 |
|
!$url->host || !$url->port || $url->port < 1 |
866 |
|
) { |
867 |
|
carp "can't parse $url\n"; |
868 |
|
return -1; |
869 |
|
} |
870 |
|
|
871 |
|
my ($host,$port,$query) = ($url->host, $url->port, $url->path); |
872 |
|
|
873 |
|
if ($self->{pxhost}) { |
874 |
|
($host,$port) = ($self->{pxhost}, $self->{pxport}); |
875 |
|
$query = "http://$host:$port/$query"; |
876 |
|
} |
877 |
|
|
878 |
|
$query .= '?' . $url->query if ($url->query && ! $reqbody); |
879 |
|
|
880 |
|
my $headers; |
881 |
|
|
882 |
|
if ($reqbody) { |
883 |
|
$headers .= "POST $query HTTP/1.0\r\n"; |
884 |
|
} else { |
885 |
|
$headers .= "GET $query HTTP/1.0\r\n"; |
886 |
|
} |
887 |
|
|
888 |
|
$headers .= "Host: " . $url->host . ":" . $url->port . "\r\n"; |
889 |
|
$headers .= "Connection: close\r\n"; |
890 |
|
$headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; |
891 |
|
$headers .= "Content-Type: $content_type\r\n"; |
892 |
|
$headers .= "Authorization: Basic $self->{auth}\r\n"; |
893 |
|
my $len = 0; |
894 |
|
{ |
895 |
|
use bytes; |
896 |
|
$len = length($reqbody) if ($reqbody); |
897 |
|
} |
898 |
|
$headers .= "Content-Length: $len\r\n"; |
899 |
|
$headers .= "\r\n"; |
900 |
|
|
901 |
|
my $sock = IO::Socket::INET->new( |
902 |
|
PeerAddr => $host, |
903 |
|
PeerPort => $port, |
904 |
|
Proto => 'tcp', |
905 |
|
Timeout => $self->{timeout} || 90, |
906 |
|
); |
907 |
|
|
908 |
|
if (! $sock) { |
909 |
|
carp "can't open socket to $host:$port"; |
910 |
|
return -1; |
911 |
|
} |
912 |
|
|
913 |
|
warn $headers if ($self->{debug}); |
914 |
|
|
915 |
|
print $sock $headers or |
916 |
|
carp "can't send headers to network:\n$headers\n" and return -1; |
917 |
|
|
918 |
|
if ($reqbody) { |
919 |
|
warn "$reqbody\n" if ($self->{debug}); |
920 |
|
print $sock $reqbody or |
921 |
|
carp "can't send request body to network:\n$$reqbody\n" and return -1; |
922 |
|
} |
923 |
|
|
924 |
|
my $line = <$sock>; |
925 |
|
chomp($line); |
926 |
|
my ($schema, $res_status, undef) = split(/ */, $line, 3); |
927 |
|
return if ($schema !~ /^HTTP/ || ! $res_status); |
928 |
|
|
929 |
|
$self->{status} = $res_status; |
930 |
|
warn "## response status: $res_status\n" if ($self->{debug}); |
931 |
|
|
932 |
|
# skip rest of headers |
933 |
|
$line = <$sock>; |
934 |
|
while ($line) { |
935 |
|
$line = <$sock>; |
936 |
|
$line =~ s/[\r\n]+$//; |
937 |
|
warn "## ", $line || 'NULL', " ##\n" if ($self->{debug}); |
938 |
|
}; |
939 |
|
|
940 |
|
# read body |
941 |
|
$len = 0; |
942 |
|
do { |
943 |
|
$len = read($sock, my $buf, 8192); |
944 |
|
$$resbody .= $buf if ($resbody); |
945 |
|
} while ($len); |
946 |
|
|
947 |
|
warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug}); |
948 |
|
|
949 |
|
return $self->{status}; |
950 |
|
} |
951 |
|
|
952 |
### |
### |
953 |
|
|