3 use Net::Z3950::SimpleServer;
8 my $max_records = 3; # XXX configure this
9 my $max_result_sets = 10;
19 $this->{HANDLE} = $session;
20 $this->{IMP_NAME} = "Z39.50 HTML scraping bot";
21 $this->{IMP_VER} = "0.1";
22 $session->{SETS} = {};
30 diag "SearchHandle ",Dumper($this);
32 my $session = $this->{HANDLE};
33 my $rpn = $this->{RPN};
36 eval { $query = $rpn->{query}->render(); };
37 if ( $@ && ref($@) ) { ## Did someone/something report any errors?
38 $this->{ERR_CODE} = $@->{errcode};
39 $this->{ERR_STR} = $@->{errstr};
43 diag "search for $query";
45 my $setname = $this->{SETNAME};
46 my $repl_set = $this->{REPL_SET};
48 unless ( $result = COBISS->search( $query ) ) {
49 $this->{ERR_CODE} = 108;
52 my $hits = $CROBISS::hits || diag "no results for $query";
55 upper => $hits < $max_records ? $max_records : $hits,
56 data => $result->{'resultElements'}, # FIXME
58 my $sets = $session->{SETS};
60 if ( defined( $sets->{$setname} ) && !$repl_set ) {
61 $this->{ERR_CODE} = 21;
64 if ( scalar keys %$sets >= $max_result_sets ) {
65 $this->{ERR_CODE} = 112;
66 $this->{ERR_STR} = "Max number is $max_result_sets";
69 $sets->{$setname} = $rs;
70 $this->{HITS} = $session->{HITS} = $hits;
71 $session->{QUERY} = $query;
76 my $session = $this->{HANDLE};
77 my $setname = $this->{SETNAME};
78 my $req_form = $this->{REQ_FORM};
79 my $offset = $this->{OFFSET};
80 my $sets = $session->{SETS};
81 my $hits = $session->{HITS};
87 if ( !defined( $rs = $sets->{$setname} ) ) {
88 $this->{ERR_CODE} = 30;
91 if ( $offset > $hits ) {
92 $this->{ERR_CODE} = 13;
95 $this->{BASENAME} = "HtmlZ3950";
97 # if ( !defined($req_form) || ( $req_form eq &Net::Z3950::OID::xml ) )
100 $this->{REP_FORM} = &Net::Z3950::OID::xml;
101 $this->{RECORD} = '<xml>FIXME: not implementd</xml>';
103 elsif ( $req_form eq &Net::Z3950::OID::unimarc ) { # FIXME convert to usmarc
104 $this->{REP_FORM} = &Net::Z3950::OID::unimarc;
105 $this->{RECORD} = COBISS->fetch_marc;
107 else { ## Unsupported record format
108 $this->{ERR_CODE} = 239;
109 $this->{ERR_STR} = $req_form;
112 if ( $offset == $hits ) {
124 my $z = new Net::Z3950::SimpleServer(
125 INIT => \&InitHandle,
126 SEARCH => \&SearchHandle,
127 FETCH => \&FetchHandle,
128 CLOSE => \&CloseHandle
130 $z->launch_server( $0, @ARGV );
132 package Net::Z3950::RPN::And;
136 return $this->[0]->render() . ' AND ' . $this->[1]->render();
139 package Net::Z3950::RPN::Or;
143 return $this->[0]->render() . ' OR ' . $this->[1]->render();
146 package Net::Z3950::RPN::AndNot;
150 return $this->[0]->render() . ' AND NOT ' . $this->[1]->render();
153 package Net::Z3950::RPN::Term;
160 print "render ", $this;
164 foreach my $attr ( @{ $this->{attributes} } ) {
165 my $type = $attr->{attributeType};
166 my $value = $attr->{attributeValue};
167 $attributes->{$type} = $value;
169 if ( defined( my $use = $attributes->{1} ) ) {
170 if ( defined( my $field = COBISS::usemap($use) ) ) {
174 die { errcode => 114, errstr => $use }; ## Unsupported use attribute
177 if ( defined( my $rel = $attributes->{2} ) )
178 { ## No relation attributes supported
180 die { errcode => 117, errstr => $rel };
183 if ( defined( my $pos = $attributes->{3} ) )
184 { ## No position attributes either
186 die { errcode => 119, errstr => $pos };
189 if ( defined( my $struc = $attributes->{4} ) ) { ## No structure
190 if ( ( $struc != 1 ) && ( $struc != 2 ) ) {
191 die { errcode => 118, errstr => $struc };
194 if ( defined( $attributes->{5} ) ) { ## No truncation
195 die { errcode => 113, errstr => 5 };
197 my $comp = $attributes->{6};
199 if ( defined($comp) && ( $comp >= 2 ) ) {
200 $prefix = "all$prefix= ";
203 $prefix = "$prefix=";
207 my $q = $prefix . $this->{term};