6 use Net::Z3950::SimpleServer;
10 my $max_records = 3; # XXX configure this
11 my $max_result_sets = 10;
21 $this->{HANDLE} = $session;
22 $this->{IMP_NAME} = "Z39.50 HTML scraping bot";
23 $this->{IMP_VER} = "0.1";
24 $session->{SETS} = {};
32 diag "SearchHandle ",Dumper($this);
34 my $session = $this->{HANDLE};
35 my $rpn = $this->{RPN};
38 eval { $query = $rpn->{query}->render(); };
39 warn "ERROR: $@" if $@;
40 if ( $@ && ref($@) ) { ## Did someone/something report any errors?
41 $this->{ERR_CODE} = $@->{errcode};
42 $this->{ERR_STR} = $@->{errstr};
46 diag "search for $query";
48 my $setname = $this->{SETNAME};
49 my $repl_set = $this->{REPL_SET};
50 diag "SETNAME $setname REPL_SET $repl_set";
52 unless ( $result = COBISS->search( $query ) ) {
53 $this->{ERR_CODE} = 108;
56 my $hits = $COBISS::hits || diag "no results for $query";
57 diag "got $hits hits";
60 upper => $hits < $max_records ? $max_records : $hits,
61 data => $result->{'resultElements'}, # FIXME
63 my $sets = $session->{SETS};
65 if ( defined( $sets->{$setname} ) && !$repl_set ) {
66 $this->{ERR_CODE} = 21;
69 if ( scalar keys %$sets >= $max_result_sets ) {
70 $this->{ERR_CODE} = 112;
71 $this->{ERR_STR} = "Max number is $max_result_sets";
74 $sets->{$setname} = $rs;
75 $this->{HITS} = $session->{HITS} = $hits;
76 $session->{QUERY} = $query;
81 my $session = $this->{HANDLE};
82 my $setname = $this->{SETNAME};
83 my $req_form = $this->{REQ_FORM};
84 my $offset = $this->{OFFSET};
85 my $sets = $session->{SETS};
86 my $hits = $session->{HITS};
92 if ( !defined( $rs = $sets->{$setname} ) ) {
93 $this->{ERR_CODE} = 30;
96 if ( $offset > $hits ) {
97 $this->{ERR_CODE} = 13;
100 $this->{BASENAME} = "HtmlZ3950";
102 # if ( !defined($req_form) || ( $req_form eq &Net::Z3950::OID::xml ) )
105 $this->{REP_FORM} = &Net::Z3950::OID::xml;
106 $this->{RECORD} = '<xml>FIXME: not implementd</xml>';
108 elsif ( $req_form eq &Net::Z3950::OID::unimarc ) { # FIXME convert to usmarc
109 $this->{REP_FORM} = &Net::Z3950::OID::unimarc;
110 $this->{RECORD} = COBISS->fetch_rec('unimarc');
112 elsif ( $req_form eq &Net::Z3950::OID::usmarc ) { # FIXME convert to usmarc
113 $this->{REP_FORM} = &Net::Z3950::OID::usmarc;
114 $this->{RECORD} = COBISS->fetch_rec('usmarc');
116 else { ## Unsupported record format
117 $this->{ERR_CODE} = 239;
118 $this->{ERR_STR} = $req_form;
121 if ( $offset == $hits ) {
133 my $z = new Net::Z3950::SimpleServer(
134 INIT => \&InitHandle,
135 SEARCH => \&SearchHandle,
136 FETCH => \&FetchHandle,
137 CLOSE => \&CloseHandle
139 $z->launch_server( $0, @ARGV );
141 package Net::Z3950::RPN::And;
145 return $this->[0]->render() . ' AND ' . $this->[1]->render();
148 package Net::Z3950::RPN::Or;
152 return $this->[0]->render() . ' OR ' . $this->[1]->render();
155 package Net::Z3950::RPN::AndNot;
159 return $this->[0]->render() . ' AND NOT ' . $this->[1]->render();
162 package Net::Z3950::RPN::Term;
164 use Data::Dump qw(dump);
170 print "render ", dump($this);
174 my $usemap = eval "${from}::usemap;";
176 warn "# $from usermap ",dump($usemap);
180 foreach my $attr ( @{ $this->{attributes} } ) {
181 my $type = $attr->{attributeType};
182 my $value = $attr->{attributeValue};
183 $attributes->{$type} = $value;
185 if ( defined( my $use = $attributes->{1} ) ) {
186 if ( defined( my $field = $usemap->{$use} ) ) {
190 die { errcode => 114, errstr => $use }; ## Unsupported use attribute
193 if ( defined( my $rel = $attributes->{2} ) )
194 { ## No relation attributes supported
196 die { errcode => 117, errstr => $rel };
199 if ( defined( my $pos = $attributes->{3} ) )
200 { ## No position attributes either
202 die { errcode => 119, errstr => $pos };
205 if ( defined( my $struc = $attributes->{4} ) ) { ## No structure
206 if ( ( $struc != 1 ) && ( $struc != 2 ) ) {
207 die { errcode => 118, errstr => $struc };
210 if ( defined( $attributes->{5} ) ) { ## No truncation
211 die { errcode => 113, errstr => 5 };
213 my $comp = $attributes->{6};
215 if ( defined($comp) && ( $comp >= 2 ) ) {
216 $prefix = "all$prefix= ";
219 $prefix = "$prefix=";
223 my $q = $prefix . $this->{term};