/[Search-Estraier]/trunk/lib/Search/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/lib/Search/Estraier.pm

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

revision 108 by dpavlin, Sun Feb 19 17:13:57 2006 UTC revision 139 by dpavlin, Wed May 10 13:45:08 2006 UTC
# Line 4  use 5.008; Line 4  use 5.008;
4  use strict;  use strict;
5  use warnings;  use warnings;
6    
7  our $VERSION = '0.04_2';  our $VERSION = '0.06_1';
8    
9  =head1 NAME  =head1 NAME
10    
# Line 20  Search::Estraier - pure perl module to u Line 20  Search::Estraier - pure perl module to u
20          my $node = new Search::Estraier::Node(          my $node = new Search::Estraier::Node(
21                  url => 'http://localhost:1978/node/test',                  url => 'http://localhost:1978/node/test',
22                  user => 'admin',                  user => 'admin',
23                  passwd => 'admin'                  passwd => 'admin',
24                    create => 1,
25                    label => 'Label for node',
26                    croak_on_error => 1,
27          );          );
28    
29          # create document          # create document
# Line 599  sub options { Line 602  sub options {
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/;
# Line 843  or in more verbose form Line 875  or in more verbose form
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    );    );
# Line 855  with following arguments: Line 891  with following arguments:
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
# Line 874  sub new { Line 926  sub new {
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                    eval {
955                            $self->name;
956                    };
957                    if ($@) {
958                            my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#);
959                            croak "can't find node name in '$self->{url}'" unless ($name);
960                            my $label = $self->{label} || $name;
961                            $self->master(
962                                    action => 'nodeadd',
963                                    name => $name,
964                                    label => $label,
965                            ) || croak "can't create node $name ($label)";
966                    }
967            }
968    
969          $self ? return $self : return undef;          $self ? return $self : return undef;
970  }  }
971    
# Line 1270  sub _fetch_doc { Line 1342  sub _fetch_doc {
1342    
1343  sub name {  sub name {
1344          my $self = shift;          my $self = shift;
1345          $self->_set_info unless ($self->{name});          $self->_set_info unless ($self->{inform}->{name});
1346          return $self->{name};          return $self->{inform}->{name};
1347  }  }
1348    
1349    
# Line 1283  sub name { Line 1355  sub name {
1355    
1356  sub label {  sub label {
1357          my $self = shift;          my $self = shift;
1358          $self->_set_info unless ($self->{label});          $self->_set_info unless ($self->{inform}->{label});
1359          return $self->{label};          return $self->{inform}->{label};
1360  }  }
1361    
1362    
# Line 1296  sub label { Line 1368  sub label {
1368    
1369  sub doc_num {  sub doc_num {
1370          my $self = shift;          my $self = shift;
1371          $self->_set_info if ($self->{dnum} < 0);          $self->_set_info if ($self->{inform}->{dnum} < 0);
1372          return $self->{dnum};          return $self->{inform}->{dnum};
1373  }  }
1374    
1375    
# Line 1309  sub doc_num { Line 1381  sub doc_num {
1381    
1382  sub word_num {  sub word_num {
1383          my $self = shift;          my $self = shift;
1384          $self->_set_info if ($self->{wnum} < 0);          $self->_set_info if ($self->{inform}->{wnum} < 0);
1385          return $self->{wnum};          return $self->{inform}->{wnum};
1386  }  }
1387    
1388    
# Line 1322  sub word_num { Line 1394  sub word_num {
1394    
1395  sub size {  sub size {
1396          my $self = shift;          my $self = shift;
1397          $self->_set_info if ($self->{size} < 0);          $self->_set_info if ($self->{inform}->{size} < 0);
1398          return $self->{size};          return $self->{inform}->{size};
1399  }  }
1400    
1401    
# Line 1356  sub search { Line 1428  sub search {
1428          );          );
1429          return if ($rv != 200);          return if ($rv != 200);
1430    
1431          my (@docs, $hints);          my @records     = split /--------\[.*?\]--------(?::END)?\r?\n/, $resbody;
1432            my $hintsText   = splice @records, 0, 2; # starts with empty record
1433          my @lines = split(/\n/, $resbody);          my $hints               = { $hintsText =~ m/^(.*?)\t(.*?)$/gsm };
1434          return unless (@lines);  
1435            # process records
1436          my $border = $lines[0];          my $docs = [];
1437          my $isend = 0;          foreach my $record (@records)
1438          my $lnum = 1;          {
1439                    # split into keys and snippets
1440          while ( $lnum <= $#lines ) {                  my ($keys, $snippet) = $record =~ m/^(.*?)\n\n(.*?)$/s;
1441                  my $line = $lines[$lnum];  
1442                  $lnum++;                  # create document hash
1443                    my $doc                         = { $keys =~ m/^(.*?)=(.*?)$/gsm };
1444                  #warn "## $line\n";                  $doc->{'@keywords'}     = $doc->{keywords};
1445                  if ($line && $line =~ m/^\Q$border\E(:END)*$/) {                  ($doc->{keywords})      = $keys =~ m/^%VECTOR\t(.*?)$/gm;
1446                          $isend = $1;                  $doc->{snippet}         = $snippet;
1447                          last;  
1448                  }                  push @$docs, new Search::Estraier::ResultDocument(
1449                            attrs           => $doc,
1450                  if ($line =~ /\t/) {                          uri             => $doc->{'@uri'},
1451                          my ($k,$v) = split(/\t/, $line, 2);                          snippet         => $snippet,
1452                          $hints->{$k} = $v;                          keywords        => $doc->{'keywords'},
1453                  }                  );
         }  
   
         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$/);  
                 }  
   
1454          }          }
1455    
1456          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 );  
1457  }  }
1458    
1459    
# Line 1486  sub cond_to_query { Line 1502  sub cond_to_query {
1502          push @args, 'wwidth=' . $self->{wwidth};          push @args, 'wwidth=' . $self->{wwidth};
1503          push @args, 'hwidth=' . $self->{hwidth};          push @args, 'hwidth=' . $self->{hwidth};
1504          push @args, 'awidth=' . $self->{awidth};          push @args, 'awidth=' . $self->{awidth};
1505            push @args, 'skip=' . $self->{skip} if ($self->{skip});
1506    
1507          return join('&', @args);          return join('&', @args);
1508  }  }
# Line 1680  Return array of users with admin rights Line 1697  Return array of users with admin rights
1697    
1698  sub admins {  sub admins {
1699          my $self = shift;          my $self = shift;
1700          $self->_set_info unless ($self->{name});          $self->_set_info unless ($self->{inform}->{name});
1701          return $self->{admins};          return $self->{inform}->{admins};
1702  }  }
1703    
1704  =head2 guests  =head2 guests
# Line 1694  Return array of users with guest rights Line 1711  Return array of users with guest rights
1711    
1712  sub guests {  sub guests {
1713          my $self = shift;          my $self = shift;
1714          $self->_set_info unless ($self->{name});          $self->_set_info unless ($self->{inform}->{name});
1715          return $self->{guests};          return $self->{inform}->{guests};
1716  }  }
1717    
1718  =head2 links  =head2 links
# Line 1708  Return array of links for this node Line 1725  Return array of links for this node
1725    
1726  sub links {  sub links {
1727          my $self = shift;          my $self = shift;
1728          $self->_set_info unless ($self->{name});          $self->_set_info unless ($self->{inform}->{name});
1729          return $self->{links};          return $self->{inform}->{links};
1730  }  }
1731    
1732    =head2 master
1733    
1734    Set actions on Hyper Estraier node master (C<estmaster> process)
1735    
1736      $node->master(
1737            action => 'sync'
1738      );
1739    
1740    All available actions are documented in
1741    L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol>
1742    
1743    =cut
1744    
1745    my $estmaster_rest = {
1746            shutdown => {
1747                    status => 202,
1748            },
1749            sync => {
1750                    status => 202,
1751            },
1752            backup => {
1753                    status => 202,
1754            },
1755            userlist => {
1756                    status => 200,
1757                    returns => [ qw/name passwd flags fname misc/ ],
1758            },
1759            useradd => {
1760                    required => [ qw/name passwd flags/ ],
1761                    optional => [ qw/fname misc/ ],
1762                    status => 200,
1763            },
1764            userdel => {
1765                    required => [ qw/name/ ],
1766                    status => 200,
1767            },
1768            nodelist => {
1769                    status => 200,
1770                    returns => [ qw/name label doc_num word_num size/ ],
1771            },
1772            nodeadd => {
1773                    required => [ qw/name/ ],
1774                    optional => [ qw/label/ ],
1775                    status => 200,
1776            },
1777            nodedel => {
1778                    required => [ qw/name/ ],
1779                    status => 200,
1780            },
1781            nodeclr => {
1782                    required => [ qw/name/ ],
1783                    status => 200,
1784            },
1785            nodertt => {
1786                    status => 200,  
1787            },
1788    };
1789    
1790    sub master {
1791            my $self = shift;
1792    
1793            my $args = {@_};
1794    
1795            # have action?
1796            my $action = $args->{action} || croak "need action, available: ",
1797                    join(", ",keys %{ $estmaster_rest });
1798    
1799            # check if action is valid
1800            my $rest = $estmaster_rest->{$action};
1801            croak "action '$action' is not supported, available actions: ",
1802                    join(", ",keys %{ $estmaster_rest }) unless ($rest);
1803    
1804            croak "BUG: action '$action' needs return status" unless ($rest->{status});
1805    
1806            my @args;
1807    
1808            if ($rest->{required} || $rest->{optional}) {
1809    
1810                    map {
1811                            croak "need parametar '$_' for action '$action'" unless ($args->{$_});
1812                            push @args, $_ . '=' . uri_escape( $args->{$_} );
1813                    } ( @{ $rest->{required} } );
1814    
1815                    map {
1816                            push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_});
1817                    } ( @{ $rest->{optional} } );
1818    
1819            }
1820    
1821            my $uri = new URI( $self->{url} );
1822    
1823            my $resbody;
1824    
1825            my $status = $self->shuttle_url(
1826                    'http://' . $uri->host_port . '/master?action=' . $action ,
1827                    'application/x-www-form-urlencoded',
1828                    join('&', @args),
1829                    \$resbody,
1830                    1,
1831            ) or confess "shuttle_url failed";
1832    
1833            if ($status == $rest->{status}) {
1834                    if ($rest->{returns} && wantarray) {
1835    
1836                            my @results;
1837                            my $fields = $#{$rest->{returns}};
1838    
1839                            foreach my $line ( split(/[\r\n]/,$resbody) ) {
1840                                    my @e = split(/\t/, $line, $fields + 1);
1841                                    my $row;
1842                                    foreach my $i ( 0 .. $fields) {
1843                                            $row->{ $rest->{returns}->[$i] } = $e[ $i ];
1844                                    }
1845                                    push @results, $row;
1846                            }
1847    
1848                            return @results;
1849    
1850                    } elsif ($resbody) {
1851                            chomp $resbody;
1852                            return $resbody;
1853                    } else {
1854                            return 0E0;
1855                    }
1856            }
1857    
1858            carp "expected status $rest->{status}, but got $status";
1859            return undef;
1860    }
1861    
1862  =head1 PRIVATE METHODS  =head1 PRIVATE METHODS
1863    
# Line 1741  sub _set_info { Line 1887  sub _set_info {
1887          return if ($rv != 200 || !$resbody);          return if ($rv != 200 || !$resbody);
1888    
1889          my @lines = split(/[\r\n]/,$resbody);          my @lines = split(/[\r\n]/,$resbody);
1890            
1891          ( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) =          $self->{inform} = {};
1892                  split(/\t/, shift @lines, 5);  
1893            ( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum},
1894                    $self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5);
1895    
1896          return $resbody unless (@lines);          return $resbody unless (@lines);
1897    
1898          shift @lines;          shift @lines;
1899    
1900          while(my $admin = shift @lines) {          while(my $admin = shift @lines) {
1901                  push @{$self->{admins}}, $admin;                  push @{$self->{inform}->{admins}}, $admin;
1902          }          }
1903            
1904          while(my $guest = shift @lines) {          while(my $guest = shift @lines) {
1905                  push @{$self->{guests}}, $guest;                  push @{$self->{inform}->{guests}}, $guest;
1906          }          }
1907    
1908          while(my $link = shift @lines) {          while(my $link = shift @lines) {
1909                  push @{$self->{links}}, $link;                  push @{$self->{inform}->{links}}, $link;
1910          }          }
1911    
1912          return $resbody;          return $resbody;
# Line 1781  Hyper Estraier Ruby interface on which t Line 1929  Hyper Estraier Ruby interface on which t
1929    
1930  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>
1931    
1932    Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code
1933    
1934  =head1 COPYRIGHT AND LICENSE  =head1 COPYRIGHT AND LICENSE
1935    

Legend:
Removed from v.108  
changed lines
  Added in v.139

  ViewVC Help
Powered by ViewVC 1.1.26