/[Search-Estraier]/trunk/Estraier.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 15 by dpavlin, Wed Jan 4 22:24:57 2006 UTC revision 36 by dpavlin, Thu Jan 5 21:51:54 2006 UTC
# Line 306  sub delete { Line 306  sub delete {
306    
307  package Search::Estraier::Condition;  package Search::Estraier::Condition;
308    
309    use Carp qw/confess croak/;
310    
311  use Search::Estraier;  use Search::Estraier;
312  our @ISA = qw/Search::Estraier/;  our @ISA = qw/Search::Estraier/;
313    
314    =head1 Search::Estraier::Condition
315    
316    =head2 new
317    
318      my $cond = new Search::HyperEstraier::Condition;
319    
320    =cut
321    
322    sub new {
323            my $class = shift;
324            my $self = {};
325            bless($self, $class);
326    
327            $self->{max} = -1;
328            $self->{options} = 0;
329    
330            $self ? return $self : return undef;
331    }
332    
333    =head2 set_phrase
334    
335      $cond->set_phrase('search phrase');
336    
337    =cut
338    
339    sub set_phrase {
340            my $self = shift;
341            $self->{phrase} = $self->_s( shift );
342    }
343    
344    =head2 add_attr
345    
346      $cond->add_attr('@URI STRINC /~dpavlin/');
347    
348    =cut
349    
350    sub add_attr {
351            my $self = shift;
352            my $attr = shift || return;
353            push @{ $self->{attrs} }, $self->_s( $attr );
354    }
355    
356    =head2 set_order
357    
358      $cond->set_order('@mdate NUMD');
359    
360    =cut
361    
362    sub set_order {
363            my $self = shift;
364            $self->{order} = shift;
365    }
366    
367    =head2 set_max
368    
369      $cond->set_max(42);
370    
371    =cut
372    
373    sub set_max {
374            my $self = shift;
375            my $max = shift;
376            croak "set_max needs number" unless ($max =~ m/^\d+$/);
377            $self->{max} = $max;
378    }
379    
380    =head2 set_options
381    
382      $cond->set_options( SURE => 1 );
383    
384    =cut
385    
386  my $options = {  my $options = {
387          # check N-gram keys skipping by three          # check N-gram keys skipping by three
388          SURE => 1 << 0,          SURE => 1 << 0,
# Line 324  my $options = { Line 398  my $options = {
398          SIMPLE => 1 << 10,          SIMPLE => 1 << 10,
399  };  };
400    
401  =head1 Search::Estraier::Condition  sub set_options {
402            my $self = shift;
403            my $option = shift;
404            confess "unknown option" unless ($options->{$option});
405            $self->{options} ||= $options->{$option};
406    }
407    
408    =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    =head2 order
422    
423    Return search result order.
424    
425      print $cond->order;
426    
427    =cut
428    
429    sub order {
430            my $self = shift;
431            return $self->{order};
432    }
433    
434    =head2 attrs
435    
436    Return search result attrs.
437    
438      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    my $cond = new Search::HyperEstraier::Condition;    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  =cut
501    
502  sub new {  sub new {
503          my $class = shift;          my $class = shift;
504          my $self = {};          my $self = {@_};
505          bless($self, $class);          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;          $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    
 package Search::Estraier::Master;  
527    
528  use Carp;  =head2 attr_names
529    
530  =head1 Search::Estraier::Master  Returns array with attribute names from result document object.
531    
532  Controll node master. This requires user with administration priviledges.    my @attrs = $rdoc->attr_names;
533    
534  =cut  =cut
535    
536  {  sub attr_names {
537          package RequestAgent;          my $self = shift;
538          our @ISA = qw(LWP::UserAgent);          croak "attr_names return array, not scalar" if (! wantarray);
539            return sort keys %{ $self->{attrs} };
540    }
541    
542          sub new {  =head2 attr
                 my $self = LWP::UserAgent::new(@_);  
                 $self->agent("Search-Estraier/$Search::Estraer::VERSION");  
                 $self;  
         }  
543    
544          sub get_basic_credentials {  Returns value of an attribute.
545                  my($self, $realm, $uri) = @_;  
546  #               return ($user, $password);    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  =head2 new    print $rdoc->snippet;
561    
562  Create new connection to node master.  =cut
563    
564    my $master = new Search::Estraier::Master(  sub snippet {
565          url => 'http://localhost:1978',          my $self = shift;
566          user => 'admin',          return $self->{snippet};
567          passwd => 'admin',  }
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
# Line 386  sub new { Line 603  sub new {
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/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            $self ? return $self : return undef;
693    }
694    
695    =head2 set_url
696    
697    Specify URL to node server
698    
699      $node->set_url('http://localhost:1978');
700    
701    =cut
702    
703    sub set_url {
704            my $self = shift;
705            $self->{url} = shift;
706    }
707    
708    =head2 set_proxy
709    
710    Specify proxy server to connect to node server
711    
712      $node->set_proxy('proxy.example.com', 8080);
713    
714    =cut
715    
716    sub set_proxy {
717            my $self = shift;
718            my ($host,$port) = @_;
719            croak "proxy port must be number" unless ($port =~ m/^\d+$/);
720            $self->{pxhost} = $host;
721            $self->{pxport} = $port;
722    }
723    
724    =head2 set_timeout
725    
726    Specify timeout of connection in seconds
727    
728      $node->set_timeout( 15 );
729    
730    =cut
731    
732    sub set_timeout {
733            my $self = shift;
734            my $sec = shift;
735            croak "timeout must be number" unless ($sec =~ m/^\d+$/);
736            $self->{timeout} = $sec;
737    }
738    
739    =head2 set_auth
740    
741    Specify name and password for authentication to node server.
742    
743      $node->set_auth('clint','eastwood');
744    
745    =cut
746    
747    sub set_auth {
748            my $self = shift;
749            my ($login,$passwd) = @_;
750            $self->{auth} = encode_base64( "$login:$passwd" );
751    }
752    
753    =head2 status
754    
755    Return status code of last request.
756    
757      print $res->status;
758    
759    C<-1> means connection failure.
760    
761    =cut
762    
763    sub status {
764            my $self = shift;
765            return $self->{status};
766    }
767    
768    =head2 shuttle_url
769    
770    This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node
771    master.
772    
773      my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody );
774    
775    C<$resheads> and C<$resbody> booleans controll if response headers and/or response
776    body will be saved within object.
777    
778    =cut
779    
780    sub shuttle_url {
781            my $self = shift;
782    
783            my ($url, $content_type, $reqbody, $resbody) = @_;
784    
785            my $status = -1;
786    
787            warn $url;
788    
789            $url = new URI($url);
790            return -1 unless ($url && $url->scheme && $url->scheme eq 'http' && $url->host && $url->port > 1);
791    
792            my ($host,$port,$query) = ($url->host, $url->port, $url->path);
793    
794            if ($self->{pxhost}) {
795                    ($host,$port) = ($self->{pxhost}, $self->{pxport});
796                    $query = "http://$host:$port/$query";
797            }
798    
799            $query .= '?' + $url->query if ($url->query && ! $reqbody);
800    
801            my $sock = IO::Socket::INET->new(
802                    PeerAddr        => $host,
803                    PeerPort        => $port,
804                    Proto           => 'tcp',
805                    Timeout         => $self->{timeout} || 90,
806            ) || return -1;
807    
808            if ($reqbody) {
809                    print $sock "POST $query HTTP/1.0\r\n";
810            } else {
811                    print $sock "GET $query HTTP/1.0\r\n";
812            }
813    
814            print $sock "Host: $url->host:$url->port\r\n";
815            print $sock "Connection: close\r\n";
816            print $sock "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n";
817            print $sock "Content-Type $content_type\r\n";
818            print $sock "Authorization: Basic $self->{auth}\r\n";
819            {
820                    use bytes;
821                    print $sock "Content-Length: ", length($reqbody), "\r\n";
822            }
823            print $sock "\r\n";
824    
825            print $sock $$reqbody if ($reqbody);
826    
827            my $line = <$sock>;
828            chomp($line);
829            my ($schema, $res_status, undef) = split(/  */, $line, 3);
830            return if ($schema !~ /^HTTP/ || ! $res_status);
831    
832            $self->{status} = $res_status;
833    
834            # skip rest of headers
835            do {
836                    $line = <$sock>;
837                    chomp($line);
838            } until ($line eq '');
839    
840            # read body
841            my $len = 0;
842            do {
843                    $len = read($sock, my $buf, 8192);
844                    $$resbody .= $buf if ($resbody);
845            } while ($len);
846    
847            return $status;
848    }
849    
850  ###  ###
851    

Legend:
Removed from v.15  
changed lines
  Added in v.36

  ViewVC Help
Powered by ViewVC 1.1.26