/[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 160 by dpavlin, Sat Jun 24 15:34:42 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.07_2';
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                    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    
# Line 984  Add a document Line 1053  Add a document
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    
# Line 992  sub put_doc { Line 1061  sub put_doc {
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    
# Line 1015  sub out_doc { Line 1088  sub out_doc {
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    
# Line 1037  sub out_doc_by_uri { Line 1114  sub out_doc_by_uri {
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    
# Line 1059  sub edit_doc { Line 1140  sub edit_doc {
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    
# Line 1270  sub _fetch_doc { Line 1355  sub _fetch_doc {
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    
# Line 1283  sub name { Line 1368  sub name {
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    
# Line 1296  sub label { Line 1381  sub label {
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    
# Line 1309  sub doc_num { Line 1394  sub doc_num {
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    
# Line 1322  sub word_num { Line 1407  sub word_num {
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    
# Line 1356  sub search { Line 1441  sub search {
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                  }                  );
         }  
   
         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$/);  
                 }  
   
1467          }          }
1468    
1469          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 );  
1470  }  }
1471    
1472    
# Line 1486  sub cond_to_query { Line 1515  sub cond_to_query {
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  }  }
# Line 1632  sub set_user { Line 1662  sub set_user {
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;
# Line 1665  sub set_link { Line 1695  sub set_link {
1695                  undef                  undef
1696          ) == 200) {          ) == 200) {
1697                  # refresh node info after adding link                  # refresh node info after adding link
1698                  $self->_set_info;                  $self->_clear_info;
1699                  return 1;                  return 1;
1700          }          }
1701            return undef;
1702  }  }
1703    
1704  =head2 admins  =head2 admins
# Line 1680  Return array of users with admin rights Line 1711  Return array of users with admin rights
1711    
1712  sub admins {  sub admins {
1713          my $self = shift;          my $self = shift;
1714          $self->_set_info unless ($self->{name});          $self->_set_info unless ($self->{inform}->{name});
1715          return $self->{admins};          return $self->{inform}->{admins};
1716  }  }
1717    
1718  =head2 guests  =head2 guests
# Line 1694  Return array of users with guest rights Line 1725  Return array of users with guest rights
1725    
1726  sub guests {  sub guests {
1727          my $self = shift;          my $self = shift;
1728          $self->_set_info unless ($self->{name});          $self->_set_info unless ($self->{inform}->{name});
1729          return $self->{guests};          return $self->{inform}->{guests};
1730  }  }
1731    
1732  =head2 links  =head2 links
# Line 1708  Return array of links for this node Line 1739  Return array of links for this node
1739    
1740  sub links {  sub links {
1741          my $self = shift;          my $self = shift;
1742          $self->_set_info unless ($self->{name});          $self->_set_info unless ($self->{inform}->{name});
1743          return $self->{links};          return $self->{inform}->{links};
1744  }  }
1745    
1746    =head2 cacheusage
1747    
1748    Return cache usage for a node
1749    
1750      my $cache = $node->cacheusage;
1751    
1752    =cut
1753    
1754    sub cacheusage {
1755            my $self = shift;
1756    
1757            return unless ($self->{url});
1758    
1759            my $resbody;
1760            my $rv = $self->shuttle_url( $self->{url} . '/cacheusage',
1761                    'text/plain',
1762                    undef,
1763                    \$resbody,
1764            );
1765    
1766            return if ($rv != 200 || !$resbody);
1767    
1768            return $resbody;
1769    }
1770    
1771    =head2 master
1772    
1773    Set actions on Hyper Estraier node master (C<estmaster> process)
1774    
1775      $node->master(
1776            action => 'sync'
1777      );
1778    
1779    All available actions are documented in
1780    L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol>
1781    
1782    =cut
1783    
1784    my $estmaster_rest = {
1785            shutdown => {
1786                    status => 202,
1787            },
1788            sync => {
1789                    status => 202,
1790            },
1791            backup => {
1792                    status => 202,
1793            },
1794            userlist => {
1795                    status => 200,
1796                    returns => [ qw/name passwd flags fname misc/ ],
1797            },
1798            useradd => {
1799                    required => [ qw/name passwd flags/ ],
1800                    optional => [ qw/fname misc/ ],
1801                    status => 200,
1802            },
1803            userdel => {
1804                    required => [ qw/name/ ],
1805                    status => 200,
1806            },
1807            nodelist => {
1808                    status => 200,
1809                    returns => [ qw/name label doc_num word_num size/ ],
1810            },
1811            nodeadd => {
1812                    required => [ qw/name/ ],
1813                    optional => [ qw/label/ ],
1814                    status => 200,
1815            },
1816            nodedel => {
1817                    required => [ qw/name/ ],
1818                    status => 200,
1819            },
1820            nodeclr => {
1821                    required => [ qw/name/ ],
1822                    status => 200,
1823            },
1824            nodertt => {
1825                    status => 200,  
1826            },
1827    };
1828    
1829    sub master {
1830            my $self = shift;
1831    
1832            my $args = {@_};
1833    
1834            # have action?
1835            my $action = $args->{action} || croak "need action, available: ",
1836                    join(", ",keys %{ $estmaster_rest });
1837    
1838            # check if action is valid
1839            my $rest = $estmaster_rest->{$action};
1840            croak "action '$action' is not supported, available actions: ",
1841                    join(", ",keys %{ $estmaster_rest }) unless ($rest);
1842    
1843            croak "BUG: action '$action' needs return status" unless ($rest->{status});
1844    
1845            my @args;
1846    
1847            if ($rest->{required} || $rest->{optional}) {
1848    
1849                    map {
1850                            croak "need parametar '$_' for action '$action'" unless ($args->{$_});
1851                            push @args, $_ . '=' . uri_escape( $args->{$_} );
1852                    } ( @{ $rest->{required} } );
1853    
1854                    map {
1855                            push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_});
1856                    } ( @{ $rest->{optional} } );
1857    
1858            }
1859    
1860            my $uri = new URI( $self->{url} );
1861    
1862            my $resbody;
1863    
1864            my $status = $self->shuttle_url(
1865                    'http://' . $uri->host_port . '/master?action=' . $action ,
1866                    'application/x-www-form-urlencoded',
1867                    join('&', @args),
1868                    \$resbody,
1869                    1,
1870            ) or confess "shuttle_url failed";
1871    
1872            if ($status == $rest->{status}) {
1873    
1874                    # refresh node info after sync
1875                    $self->_clear_info if ($action eq 'sync' || $action =~ m/^node(?:add|del|clr)$/);
1876    
1877                    if ($rest->{returns} && wantarray) {
1878    
1879                            my @results;
1880                            my $fields = $#{$rest->{returns}};
1881    
1882                            foreach my $line ( split(/[\r\n]/,$resbody) ) {
1883                                    my @e = split(/\t/, $line, $fields + 1);
1884                                    my $row;
1885                                    foreach my $i ( 0 .. $fields) {
1886                                            $row->{ $rest->{returns}->[$i] } = $e[ $i ];
1887                                    }
1888                                    push @results, $row;
1889                            }
1890    
1891                            return @results;
1892    
1893                    } elsif ($resbody) {
1894                            chomp $resbody;
1895                            return $resbody;
1896                    } else {
1897                            return 0E0;
1898                    }
1899            }
1900    
1901            carp "expected status $rest->{status}, but got $status";
1902            return undef;
1903    }
1904    
1905  =head1 PRIVATE METHODS  =head1 PRIVATE METHODS
1906    
# Line 1741  sub _set_info { Line 1930  sub _set_info {
1930          return if ($rv != 200 || !$resbody);          return if ($rv != 200 || !$resbody);
1931    
1932          my @lines = split(/[\r\n]/,$resbody);          my @lines = split(/[\r\n]/,$resbody);
1933            
1934          ( $self->{name}, $self->{label}, $self->{dnum}, $self->{wnum}, $self->{size} ) =          $self->_clear_info;
1935                  split(/\t/, shift @lines, 5);  
1936            ( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum},
1937                    $self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5);
1938    
1939          return $resbody unless (@lines);          return $resbody unless (@lines);
1940    
1941          shift @lines;          shift @lines;
1942    
1943          while(my $admin = shift @lines) {          while(my $admin = shift @lines) {
1944                  push @{$self->{admins}}, $admin;                  push @{$self->{inform}->{admins}}, $admin;
1945          }          }
1946            
1947          while(my $guest = shift @lines) {          while(my $guest = shift @lines) {
1948                  push @{$self->{guests}}, $guest;                  push @{$self->{inform}->{guests}}, $guest;
1949          }          }
1950    
1951          while(my $link = shift @lines) {          while(my $link = shift @lines) {
1952                  push @{$self->{links}}, $link;                  push @{$self->{inform}->{links}}, $link;
1953          }          }
1954    
1955          return $resbody;          return $resbody;
1956    
1957  }  }
1958    
1959    =head2 _clear_info
1960    
1961    Clear information for node
1962    
1963      $node->_clear_info;
1964    
1965    On next call to C<name>, C<label>, C<doc_num>, C<word_num> or C<size> node
1966    info will be fetch again from Hyper Estraier.
1967    
1968    =cut
1969    sub _clear_info {
1970            my $self = shift;
1971            $self->{inform} = {
1972                    dnum => -1,
1973                    wnum => -1,
1974                    size => -1.0,
1975            };
1976    }
1977    
1978  ###  ###
1979    
1980  =head1 EXPORT  =head1 EXPORT
# Line 1781  Hyper Estraier Ruby interface on which t Line 1991  Hyper Estraier Ruby interface on which t
1991    
1992  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>
1993    
1994    Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code
1995    
1996  =head1 COPYRIGHT AND LICENSE  =head1 COPYRIGHT AND LICENSE
1997    

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

  ViewVC Help
Powered by ViewVC 1.1.26