4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.06_1'; |
our $VERSION = '0.08_1'; |
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
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 |
120 |
|
|
121 |
=head1 Search::Estraier::Document |
=head1 Search::Estraier::Document |
122 |
|
|
123 |
This class implements Document which is collection of attributes |
This class implements Document which is single item in Hyper Estraier. |
124 |
(key=value), vectors (also key value) display text and hidden text. |
|
125 |
|
It's is collection of: |
126 |
|
|
127 |
|
=over 4 |
128 |
|
|
129 |
|
=item attributes |
130 |
|
|
131 |
|
C<< 'key' => 'value' >> pairs which can later be used for filtering of results |
132 |
|
|
133 |
|
You can add common filters to C<attrindex> in estmaster's C<_conf> |
134 |
|
file for better performance. See C<attrindex> in |
135 |
|
L<Hyper Estraier P2P Guide|http://hyperestraier.sourceforge.net/nguide-en.html>. |
136 |
|
|
137 |
|
=item vectors |
138 |
|
|
139 |
|
also C<< 'key' => 'value' >> pairs |
140 |
|
|
141 |
|
=item display text |
142 |
|
|
143 |
|
Text which will be used to create searchable corpus of your index and |
144 |
|
included in snippet output. |
145 |
|
|
146 |
|
=item hidden text |
147 |
|
|
148 |
|
Text which will be searchable, but will not be included in snippet. |
149 |
|
|
150 |
|
=back |
151 |
|
|
152 |
=head2 new |
=head2 new |
153 |
|
|
182 |
|
|
183 |
if ($line =~ m/^%VECTOR\t(.+)$/) { |
if ($line =~ m/^%VECTOR\t(.+)$/) { |
184 |
my @fields = split(/\t/, $1); |
my @fields = split(/\t/, $1); |
185 |
for my $i ( 0 .. ($#fields - 1) ) { |
if ($#fields % 2 == 1) { |
186 |
$self->{kwords}->{ $fields[ $i ] } = $fields[ $i + 1 ]; |
$self->{kwords} = { @fields }; |
187 |
$i++; |
} else { |
188 |
|
warn "can't decode $line\n"; |
189 |
} |
} |
190 |
next; |
next; |
191 |
|
} elsif ($line =~ m/^%SCORE\t(.+)$/) { |
192 |
|
$self->{score} = $1; |
193 |
|
next; |
194 |
} elsif ($line =~ m/^%/) { |
} elsif ($line =~ m/^%/) { |
195 |
# What is this? comment? |
# What is this? comment? |
196 |
#warn "$line\n"; |
#warn "$line\n"; |
272 |
push @{ $self->{htexts} }, $self->_s($text); |
push @{ $self->{htexts} }, $self->_s($text); |
273 |
} |
} |
274 |
|
|
275 |
|
=head2 add_vectors |
276 |
|
|
277 |
|
Add a vectors |
278 |
|
|
279 |
|
$doc->add_vector( |
280 |
|
'vector_name' => 42, |
281 |
|
'another' => 12345, |
282 |
|
); |
283 |
|
|
284 |
|
=cut |
285 |
|
|
286 |
|
sub add_vectors { |
287 |
|
my $self = shift; |
288 |
|
return unless (@_); |
289 |
|
|
290 |
|
# this is ugly, but works |
291 |
|
die "add_vector needs HASH as argument" unless ($#_ % 2 == 1); |
292 |
|
|
293 |
|
$self->{kwords} = {@_}; |
294 |
|
} |
295 |
|
|
296 |
|
=head2 set_score |
297 |
|
|
298 |
|
Set the substitute score |
299 |
|
|
300 |
|
$doc->set_score(12345); |
301 |
|
|
302 |
|
=cut |
303 |
|
|
304 |
|
sub set_score { |
305 |
|
my $self = shift; |
306 |
|
my $score = shift; |
307 |
|
return unless (defined($score)); |
308 |
|
$self->{score} = $score; |
309 |
|
} |
310 |
|
|
311 |
|
=head2 score |
312 |
|
|
313 |
|
Get the substitute score |
314 |
|
|
315 |
|
=cut |
316 |
|
|
317 |
|
sub score { |
318 |
|
my $self = shift; |
319 |
|
return -1 unless (defined($self->{score})); |
320 |
|
return $self->{score}; |
321 |
|
} |
322 |
|
|
323 |
=head2 id |
=head2 id |
324 |
|
|
413 |
} |
} |
414 |
|
|
415 |
if ($self->{kwords}) { |
if ($self->{kwords}) { |
416 |
$draft .= '%%VECTOR'; |
$draft .= '%VECTOR'; |
417 |
while (my ($key, $value) = each %{ $self->{kwords} }) { |
while (my ($key, $value) = each %{ $self->{kwords} }) { |
418 |
$draft .= "\t$key\t$value"; |
$draft .= "\t$key\t$value"; |
419 |
} |
} |
420 |
$draft .= "\n"; |
$draft .= "\n"; |
421 |
} |
} |
422 |
|
|
423 |
|
if (defined($self->{score}) && $self->{score} >= 0) { |
424 |
|
$draft .= "%SCORE\t" . $self->{score} . "\n"; |
425 |
|
} |
426 |
|
|
427 |
$draft .= "\n"; |
$draft .= "\n"; |
428 |
|
|
429 |
$draft .= join("\n", @{ $self->{dtexts} }) . "\n" if ($self->{dtexts}); |
$draft .= join("\n", @{ $self->{dtexts} }) . "\n" if ($self->{dtexts}); |
430 |
$draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n" if ($self->{htexts}); |
$draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n" if ($self->{htexts}); |
431 |
|
|
432 |
|
printf("[%s]\n", $draft); |
433 |
|
|
434 |
return $draft; |
return $draft; |
435 |
} |
} |
436 |
|
|
713 |
} |
} |
714 |
|
|
715 |
|
|
716 |
|
=head2 set_distinct |
717 |
|
|
718 |
|
$cond->set_distinct('@author'); |
719 |
|
|
720 |
|
=cut |
721 |
|
|
722 |
|
sub set_distinct { |
723 |
|
my $self = shift; |
724 |
|
$self->{distinct} = shift; |
725 |
|
} |
726 |
|
|
727 |
|
=head2 distinct |
728 |
|
|
729 |
|
Return distinct attribute |
730 |
|
|
731 |
|
print $cond->distinct; |
732 |
|
|
733 |
|
=cut |
734 |
|
|
735 |
|
sub distinct { |
736 |
|
my $self = shift; |
737 |
|
return $self->{distinct}; |
738 |
|
} |
739 |
|
|
740 |
|
=head2 set_mask |
741 |
|
|
742 |
|
Filter out some links when searching. |
743 |
|
|
744 |
|
Argument array of link numbers, starting with 0 (current node). |
745 |
|
|
746 |
|
$cond->set_mask(qw/0 1 4/); |
747 |
|
|
748 |
|
=cut |
749 |
|
|
750 |
|
sub set_mask { |
751 |
|
my $self = shift; |
752 |
|
return unless (@_); |
753 |
|
$self->{mask} = \@_; |
754 |
|
} |
755 |
|
|
756 |
|
|
757 |
package Search::Estraier::ResultDocument; |
package Search::Estraier::ResultDocument; |
758 |
|
|
759 |
use Carp qw/croak/; |
use Carp qw/croak/; |
1000 |
url => 'http://localhost:1978/node/test', |
url => 'http://localhost:1978/node/test', |
1001 |
user => 'admin', |
user => 'admin', |
1002 |
passwd => 'admin' |
passwd => 'admin' |
1003 |
|
create => 1, |
1004 |
|
label => 'optional node label', |
1005 |
debug => 1, |
debug => 1, |
1006 |
croak_on_error => 1 |
croak_on_error => 1 |
1007 |
); |
); |
1022 |
|
|
1023 |
password for authentication |
password for authentication |
1024 |
|
|
1025 |
|
=item create |
1026 |
|
|
1027 |
|
create node if it doesn't exists |
1028 |
|
|
1029 |
|
=item label |
1030 |
|
|
1031 |
|
optional label for new node if C<create> is used |
1032 |
|
|
1033 |
=item debug |
=item debug |
1034 |
|
|
1035 |
dumps a B<lot> of debugging output |
dumps a B<lot> of debugging output |
1073 |
size => -1.0, |
size => -1.0, |
1074 |
}; |
}; |
1075 |
|
|
1076 |
|
if ($self->{create}) { |
1077 |
|
if (! eval { $self->name } || $@) { |
1078 |
|
my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#); |
1079 |
|
croak "can't find node name in '$self->{url}'" unless ($name); |
1080 |
|
my $label = $self->{label} || $name; |
1081 |
|
$self->master( |
1082 |
|
action => 'nodeadd', |
1083 |
|
name => $name, |
1084 |
|
label => $label, |
1085 |
|
) || croak "can't create node $name ($label)"; |
1086 |
|
} |
1087 |
|
} |
1088 |
|
|
1089 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
1090 |
} |
} |
1091 |
|
|
1176 |
|
|
1177 |
$node->put_doc( $document_draft ) or die "can't add document"; |
$node->put_doc( $document_draft ) or die "can't add document"; |
1178 |
|
|
1179 |
Return true on success or false on failture. |
Return true on success or false on failure. |
1180 |
|
|
1181 |
=cut |
=cut |
1182 |
|
|
1184 |
my $self = shift; |
my $self = shift; |
1185 |
my $doc = shift || return; |
my $doc = shift || return; |
1186 |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
1187 |
$self->shuttle_url( $self->{url} . '/put_doc', |
if ($self->shuttle_url( $self->{url} . '/put_doc', |
1188 |
'text/x-estraier-draft', |
'text/x-estraier-draft', |
1189 |
$doc->dump_draft, |
$doc->dump_draft, |
1190 |
undef |
undef |
1191 |
) == 200; |
) == 200) { |
1192 |
|
$self->_clear_info; |
1193 |
|
return 1; |
1194 |
|
} |
1195 |
|
return undef; |
1196 |
} |
} |
1197 |
|
|
1198 |
|
|
1211 |
my $id = shift || return; |
my $id = shift || return; |
1212 |
return unless ($self->{url}); |
return unless ($self->{url}); |
1213 |
croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/); |
croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/); |
1214 |
$self->shuttle_url( $self->{url} . '/out_doc', |
if ($self->shuttle_url( $self->{url} . '/out_doc', |
1215 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1216 |
"id=$id", |
"id=$id", |
1217 |
undef |
undef |
1218 |
) == 200; |
) == 200) { |
1219 |
|
$self->_clear_info; |
1220 |
|
return 1; |
1221 |
|
} |
1222 |
|
return undef; |
1223 |
} |
} |
1224 |
|
|
1225 |
|
|
1237 |
my $self = shift; |
my $self = shift; |
1238 |
my $uri = shift || return; |
my $uri = shift || return; |
1239 |
return unless ($self->{url}); |
return unless ($self->{url}); |
1240 |
$self->shuttle_url( $self->{url} . '/out_doc', |
if ($self->shuttle_url( $self->{url} . '/out_doc', |
1241 |
'application/x-www-form-urlencoded', |
'application/x-www-form-urlencoded', |
1242 |
"uri=" . uri_escape($uri), |
"uri=" . uri_escape($uri), |
1243 |
undef |
undef |
1244 |
) == 200; |
) == 200) { |
1245 |
|
$self->_clear_info; |
1246 |
|
return 1; |
1247 |
|
} |
1248 |
|
return undef; |
1249 |
} |
} |
1250 |
|
|
1251 |
|
|
1263 |
my $self = shift; |
my $self = shift; |
1264 |
my $doc = shift || return; |
my $doc = shift || return; |
1265 |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
return unless ($self->{url} && $doc->isa('Search::Estraier::Document')); |
1266 |
$self->shuttle_url( $self->{url} . '/edit_doc', |
if ($self->shuttle_url( $self->{url} . '/edit_doc', |
1267 |
'text/x-estraier-draft', |
'text/x-estraier-draft', |
1268 |
$doc->dump_draft, |
$doc->dump_draft, |
1269 |
undef |
undef |
1270 |
) == 200; |
) == 200) { |
1271 |
|
$self->_clear_info; |
1272 |
|
return 1; |
1273 |
|
} |
1274 |
|
return undef; |
1275 |
} |
} |
1276 |
|
|
1277 |
|
|
1429 |
$path = '/etch_doc' if ($a->{etch}); |
$path = '/etch_doc' if ($a->{etch}); |
1430 |
|
|
1431 |
if ($a->{id}) { |
if ($a->{id}) { |
1432 |
croak "id must be numberm not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/); |
croak "id must be number not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/); |
1433 |
$arg = 'id=' . $a->{id}; |
$arg = 'id=' . $a->{id}; |
1434 |
} elsif ($a->{uri}) { |
} elsif ($a->{uri}) { |
1435 |
$arg = 'uri=' . uri_escape($a->{uri}); |
$arg = 'uri=' . uri_escape($a->{uri}); |
1638 |
push @args, 'wwidth=' . $self->{wwidth}; |
push @args, 'wwidth=' . $self->{wwidth}; |
1639 |
push @args, 'hwidth=' . $self->{hwidth}; |
push @args, 'hwidth=' . $self->{hwidth}; |
1640 |
push @args, 'awidth=' . $self->{awidth}; |
push @args, 'awidth=' . $self->{awidth}; |
1641 |
push @args, 'skip=' . $self->{skip} if ($self->{skip}); |
push @args, 'skip=' . $cond->{skip} if ($cond->{skip}); |
1642 |
|
|
1643 |
|
if (my $distinct = $cond->distinct) { |
1644 |
|
push @args, 'distinct=' . uri_escape($distinct); |
1645 |
|
} |
1646 |
|
|
1647 |
|
if ($cond->{mask}) { |
1648 |
|
my $mask = 0; |
1649 |
|
map { $mask += ( 2 ** $_ ) } @{ $cond->{mask} }; |
1650 |
|
|
1651 |
|
push @args, 'mask=' . $mask if ($mask); |
1652 |
|
} |
1653 |
|
|
1654 |
return join('&', @args); |
return join('&', @args); |
1655 |
} |
} |
1796 |
croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/); |
croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/); |
1797 |
|
|
1798 |
$self->shuttle_url( $self->{url} . '/_set_user', |
$self->shuttle_url( $self->{url} . '/_set_user', |
1799 |
'text/plain', |
'application/x-www-form-urlencoded', |
1800 |
'name=' . uri_escape($name) . '&mode=' . $mode, |
'name=' . uri_escape($name) . '&mode=' . $mode, |
1801 |
undef |
undef |
1802 |
) == 200; |
) == 200; |
1829 |
undef |
undef |
1830 |
) == 200) { |
) == 200) { |
1831 |
# refresh node info after adding link |
# refresh node info after adding link |
1832 |
$self->_set_info; |
$self->_clear_info; |
1833 |
return 1; |
return 1; |
1834 |
} |
} |
1835 |
|
return undef; |
1836 |
} |
} |
1837 |
|
|
1838 |
=head2 admins |
=head2 admins |
1877 |
return $self->{inform}->{links}; |
return $self->{inform}->{links}; |
1878 |
} |
} |
1879 |
|
|
1880 |
|
=head2 cacheusage |
1881 |
|
|
1882 |
|
Return cache usage for a node |
1883 |
|
|
1884 |
|
my $cache = $node->cacheusage; |
1885 |
|
|
1886 |
|
=cut |
1887 |
|
|
1888 |
|
sub cacheusage { |
1889 |
|
my $self = shift; |
1890 |
|
|
1891 |
|
return unless ($self->{url}); |
1892 |
|
|
1893 |
|
my $resbody; |
1894 |
|
my $rv = $self->shuttle_url( $self->{url} . '/cacheusage', |
1895 |
|
'text/plain', |
1896 |
|
undef, |
1897 |
|
\$resbody, |
1898 |
|
); |
1899 |
|
|
1900 |
|
return if ($rv != 200 || !$resbody); |
1901 |
|
|
1902 |
|
return $resbody; |
1903 |
|
} |
1904 |
|
|
1905 |
=head2 master |
=head2 master |
1906 |
|
|
1907 |
Set actions on Hyper Estraier node master (C<estmaster> process) |
Set actions on Hyper Estraier node master (C<estmaster> process) |
1983 |
map { |
map { |
1984 |
croak "need parametar '$_' for action '$action'" unless ($args->{$_}); |
croak "need parametar '$_' for action '$action'" unless ($args->{$_}); |
1985 |
push @args, $_ . '=' . uri_escape( $args->{$_} ); |
push @args, $_ . '=' . uri_escape( $args->{$_} ); |
1986 |
} ( keys %{ $rest->{required} } ); |
} ( @{ $rest->{required} } ); |
1987 |
|
|
1988 |
map { |
map { |
1989 |
push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); |
push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); |
1990 |
} ( keys %{ $rest->{optional} } ); |
} ( @{ $rest->{optional} } ); |
1991 |
|
|
1992 |
} |
} |
1993 |
|
|
2004 |
) or confess "shuttle_url failed"; |
) or confess "shuttle_url failed"; |
2005 |
|
|
2006 |
if ($status == $rest->{status}) { |
if ($status == $rest->{status}) { |
2007 |
|
|
2008 |
|
# refresh node info after sync |
2009 |
|
$self->_clear_info if ($action eq 'sync' || $action =~ m/^node(?:add|del|clr)$/); |
2010 |
|
|
2011 |
if ($rest->{returns} && wantarray) { |
if ($rest->{returns} && wantarray) { |
2012 |
|
|
2013 |
my @results; |
my @results; |
2025 |
return @results; |
return @results; |
2026 |
|
|
2027 |
} elsif ($resbody) { |
} elsif ($resbody) { |
2028 |
|
chomp $resbody; |
2029 |
return $resbody; |
return $resbody; |
2030 |
} else { |
} else { |
2031 |
return 0E0; |
return 0E0; |
2065 |
|
|
2066 |
my @lines = split(/[\r\n]/,$resbody); |
my @lines = split(/[\r\n]/,$resbody); |
2067 |
|
|
2068 |
$self->{inform} = {}; |
$self->_clear_info; |
2069 |
|
|
2070 |
( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum}, |
( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum}, |
2071 |
$self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5); |
$self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5); |
2090 |
|
|
2091 |
} |
} |
2092 |
|
|
2093 |
|
=head2 _clear_info |
2094 |
|
|
2095 |
|
Clear information for node |
2096 |
|
|
2097 |
|
$node->_clear_info; |
2098 |
|
|
2099 |
|
On next call to C<name>, C<label>, C<doc_num>, C<word_num> or C<size> node |
2100 |
|
info will be fetch again from Hyper Estraier. |
2101 |
|
|
2102 |
|
=cut |
2103 |
|
sub _clear_info { |
2104 |
|
my $self = shift; |
2105 |
|
$self->{inform} = { |
2106 |
|
dnum => -1, |
2107 |
|
wnum => -1, |
2108 |
|
size => -1.0, |
2109 |
|
}; |
2110 |
|
} |
2111 |
|
|
2112 |
### |
### |
2113 |
|
|
2114 |
=head1 EXPORT |
=head1 EXPORT |
2121 |
|
|
2122 |
Hyper Estraier Ruby interface on which this module is based. |
Hyper Estraier Ruby interface on which this module is based. |
2123 |
|
|
2124 |
|
Hyper Estraier now also has pure-perl binding included in distribution. It's |
2125 |
|
a faster way to access databases directly if you are not running |
2126 |
|
C<estmaster> P2P server. |
2127 |
|
|
2128 |
=head1 AUTHOR |
=head1 AUTHOR |
2129 |
|
|
2130 |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |