6 use Net::Z3950::SimpleServer;
19 my $max_records = 3; # XXX configure this
20 my $max_result_sets = 10;
30 $this->{HANDLE} = $session;
31 $this->{IMP_NAME} = "Biblio Z39.50";
32 $this->{IMP_VER} = "0.2";
33 $session->{SETS} = {};
39 diag "SearchHandle ",Dumper($this);
41 my $session = $this->{HANDLE};
42 my $rpn = $this->{RPN};
45 my $database = uc $this->{DATABASES}->[0];
46 my $module = $databases->{$database};
47 if ( ! defined $module ) {
48 $this->{ERR_CODE} = 108;
49 warn $this->{ERR_STR} = "unknown database $database - available: " . keys %$databases;
53 my $from = $module->new( $database );
55 diag "using $module for $database ", Dumper( $from );
57 eval { $query = $rpn->{query}->render( $from->usemap ); };
58 warn "ERROR: $@" if $@;
59 if ( $@ && ref($@) ) { ## Did someone/something report any errors?
60 $this->{ERR_CODE} = $@->{errcode};
61 $this->{ERR_STR} = $@->{errstr};
65 diag "search for $query";
67 my $setname = $this->{SETNAME};
68 my $repl_set = $this->{REPL_SET};
69 diag "SETNAME $setname REPL_SET $repl_set";
71 unless ( $hits = $from->search( $query ) ) {
72 $this->{ERR_CODE} = 108;
75 diag "got $hits hits";
78 upper => $hits < $max_records ? $max_records : $hits,
82 my $sets = $session->{SETS};
84 if ( defined( $sets->{$setname} ) && !$repl_set ) {
85 $this->{ERR_CODE} = 21;
88 if ( scalar keys %$sets >= $max_result_sets ) {
89 $this->{ERR_CODE} = 112;
90 $this->{ERR_STR} = "Max number is $max_result_sets";
93 $sets->{$setname} = $rs;
94 $this->{HITS} = $session->{HITS} = $hits;
95 $session->{QUERY} = $query;
100 my $session = $this->{HANDLE};
101 my $setname = $this->{SETNAME};
102 my $req_form = $this->{REQ_FORM};
103 my $offset = $this->{OFFSET};
104 my $sets = $session->{SETS};
105 my $hits = $session->{HITS};
109 if ( !defined( $rs = $sets->{$setname} ) ) {
110 $this->{ERR_CODE} = 30;
113 if ( $offset > $hits ) {
114 $this->{ERR_CODE} = 13;
118 my $from = $rs->{from} || die "no from?";
120 $this->{BASENAME} = "HtmlZ3950";
122 # if ( !defined($req_form) || ( $req_form eq &Net::Z3950::OID::xml ) )
125 $this->{REP_FORM} = &Net::Z3950::OID::xml;
126 $this->{RECORD} = '<xml>FIXME: not implementd</xml>';
128 elsif ( $req_form eq &Net::Z3950::OID::unimarc ) { # FIXME convert to usmarc
129 $this->{REP_FORM} = &Net::Z3950::OID::unimarc;
130 $this->{RECORD} = $from->next_marc('unimarc');
132 elsif ( $req_form eq &Net::Z3950::OID::usmarc ) { # FIXME convert to usmarc
133 $this->{REP_FORM} = &Net::Z3950::OID::usmarc;
134 $this->{RECORD} = $from->next_marc('usmarc');
136 else { ## Unsupported record format
137 $this->{ERR_CODE} = 239;
138 $this->{ERR_STR} = $req_form;
141 if ( $offset == $hits ) {
153 my $z = new Net::Z3950::SimpleServer(
154 INIT => \&InitHandle,
155 SEARCH => \&SearchHandle,
156 FETCH => \&FetchHandle,
157 CLOSE => \&CloseHandle
159 $z->launch_server( $0, @ARGV );
161 package Net::Z3950::RPN::And;
165 return $this->[0]->render() . ' AND ' . $this->[1]->render();
168 package Net::Z3950::RPN::Or;
172 return $this->[0]->render() . ' OR ' . $this->[1]->render();
175 package Net::Z3950::RPN::AndNot;
179 return $this->[0]->render() . ' AND NOT ' . $this->[1]->render();
182 package Net::Z3950::RPN::Term;
184 use Data::Dump qw(dump);
188 my ($this,$usemap) = @_;
190 warn "# render ", dump($this);
191 warn "# usemap ", dump($usemap);
195 foreach my $attr ( @{ $this->{attributes} } ) {
196 my $type = $attr->{attributeType};
197 my $value = $attr->{attributeValue};
198 $attributes->{$type} = $value;
200 if ( defined( my $use = $attributes->{1} ) ) {
201 if ( defined( my $field = $usemap->{$use} ) ) {
205 die { errcode => 114, errstr => $use }; ## Unsupported use attribute
208 if ( defined( my $rel = $attributes->{2} ) )
209 { ## No relation attributes supported
211 die { errcode => 117, errstr => $rel };
214 if ( defined( my $pos = $attributes->{3} ) )
215 { ## No position attributes either
217 die { errcode => 119, errstr => $pos };
220 if ( defined( my $struc = $attributes->{4} ) ) { ## No structure
221 if ( ( $struc != 1 ) && ( $struc != 2 ) ) {
222 die { errcode => 118, errstr => $struc };
225 if ( defined( $attributes->{5} ) ) { ## No truncation
226 die { errcode => 113, errstr => 5 };
228 my $comp = $attributes->{6};
230 if ( defined($comp) && ( $comp >= 2 ) ) {
231 $prefix = "all$prefix= ";
234 $prefix = "$prefix=";
238 my $q = $prefix . $this->{term};