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} = "$database NOT FOUND in available databases: " . join(" ", 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,
81 results => [ undef ], # we don't use 0 element
82 database => $database,
84 my $sets = $session->{SETS};
86 if ( defined( $sets->{$setname} ) && !$repl_set ) {
87 $this->{ERR_CODE} = 21;
90 if ( scalar keys %$sets >= $max_result_sets ) {
91 $this->{ERR_CODE} = 112;
92 $this->{ERR_STR} = "Max number is $max_result_sets";
95 $sets->{$setname} = $rs;
96 $this->{HITS} = $session->{HITS} = $hits;
97 $session->{QUERY} = $query;
102 my $session = $this->{HANDLE};
103 my $setname = $this->{SETNAME};
104 my $req_form = $this->{REQ_FORM};
105 my $offset = $this->{OFFSET};
106 my $sets = $session->{SETS};
107 my $hits = $session->{HITS};
111 if ( !defined( $rs = $sets->{$setname} ) ) {
112 $this->{ERR_CODE} = 30;
115 if ( $offset > $hits ) {
116 $this->{ERR_CODE} = 13;
120 $this->{BASENAME} = "HtmlZ3950";
123 $req_form eq Net::Z3950::OID::xml() ? 'xml' :
124 $req_form eq Net::Z3950::OID::unimarc() ? 'unimarc' :
125 $req_form eq Net::Z3950::OID::usmarc() ? 'marc' : # XXX usmarc -> marc
129 warn "ERROR: $req_form format not supported";
130 $this->{ERR_CODE} = 239; ## Unsupported record format
131 $this->{ERR_STR} = $req_form;
135 $this->{REP_FORM} = $req_form;
137 my $from = $rs->{from} || die "no from?";
138 # fetch records up to offset
139 while( $#{ $rs->{results} } < $offset ) {
140 push @{ $rs->{results} }, $from->next_marc;
141 warn "# rs result ", $#{ $rs->{results} },"\n";
144 my $id = $rs->{results}->[$offset] || die "no id for record $offset in ",Dumper( $rs->{results} );
146 my $path = 'marc/' . $rs->{database} . "/$id.$format";
148 warn "ERROR: $path not found";
149 ## Unsupported record format
150 $this->{ERR_CODE} = 239;
151 $this->{ERR_STR} = $req_form;
156 open(my $in, '<', $path) || die "$path: $!";
160 $this->{RECORD} = $marc;
164 if ( $offset == $hits ) {
176 my $z = new Net::Z3950::SimpleServer(
177 INIT => \&InitHandle,
178 SEARCH => \&SearchHandle,
179 FETCH => \&FetchHandle,
180 CLOSE => \&CloseHandle
182 $z->launch_server( $0, @ARGV );
184 package Net::Z3950::RPN::And;
188 return $this->[0]->render() . ' AND ' . $this->[1]->render();
191 package Net::Z3950::RPN::Or;
195 return $this->[0]->render() . ' OR ' . $this->[1]->render();
198 package Net::Z3950::RPN::AndNot;
202 return $this->[0]->render() . ' AND NOT ' . $this->[1]->render();
205 package Net::Z3950::RPN::Term;
207 use Data::Dump qw(dump);
211 my ($this,$usemap) = @_;
213 warn "# render ", dump($this);
214 warn "# usemap ", dump($usemap);
218 foreach my $attr ( @{ $this->{attributes} } ) {
219 my $type = $attr->{attributeType};
220 my $value = $attr->{attributeValue};
221 $attributes->{$type} = $value;
223 if ( defined( my $use = $attributes->{1} ) ) {
224 if ( defined( my $field = $usemap->{$use} ) ) {
228 die { errcode => 114, errstr => $use }; ## Unsupported use attribute
231 if ( defined( my $rel = $attributes->{2} ) )
232 { ## No relation attributes supported
234 die { errcode => 117, errstr => $rel };
237 if ( defined( my $pos = $attributes->{3} ) )
238 { ## No position attributes either
240 die { errcode => 119, errstr => $pos };
243 if ( defined( my $struc = $attributes->{4} ) ) { ## No structure
244 if ( ( $struc != 1 ) && ( $struc != 2 ) ) {
245 die { errcode => 118, errstr => $struc };
248 if ( defined( $attributes->{5} ) ) { ## No truncation
249 die { errcode => 113, errstr => 5 };
251 my $comp = $attributes->{6};
253 if ( defined($comp) && ( $comp >= 2 ) ) {
254 $prefix = "all$prefix= ";
257 $prefix = "$prefix=";
261 my $q = $prefix . $this->{term};