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 if ( $@ && ref($@) ) { ## Did someone/something report any errors?
40 $this->{ERR_CODE} = $@->{errcode};
41 $this->{ERR_STR} = $@->{errstr};
45 diag "search for $query";
47 my $setname = $this->{SETNAME};
48 my $repl_set = $this->{REPL_SET};
49 diag "SETNAME $setname REPL_SET $repl_set";
51 unless ( $result = COBISS->search( $query ) ) {
52 $this->{ERR_CODE} = 108;
55 my $hits = $COBISS::hits || diag "no results for $query";
56 diag "got $hits hits";
59 upper => $hits < $max_records ? $max_records : $hits,
60 data => $result->{'resultElements'}, # FIXME
62 my $sets = $session->{SETS};
64 if ( defined( $sets->{$setname} ) && !$repl_set ) {
65 $this->{ERR_CODE} = 21;
68 if ( scalar keys %$sets >= $max_result_sets ) {
69 $this->{ERR_CODE} = 112;
70 $this->{ERR_STR} = "Max number is $max_result_sets";
73 $sets->{$setname} = $rs;
74 $this->{HITS} = $session->{HITS} = $hits;
75 $session->{QUERY} = $query;
80 my $session = $this->{HANDLE};
81 my $setname = $this->{SETNAME};
82 my $req_form = $this->{REQ_FORM};
83 my $offset = $this->{OFFSET};
84 my $sets = $session->{SETS};
85 my $hits = $session->{HITS};
91 if ( !defined( $rs = $sets->{$setname} ) ) {
92 $this->{ERR_CODE} = 30;
95 if ( $offset > $hits ) {
96 $this->{ERR_CODE} = 13;
99 $this->{BASENAME} = "HtmlZ3950";
101 # if ( !defined($req_form) || ( $req_form eq &Net::Z3950::OID::xml ) )
104 $this->{REP_FORM} = &Net::Z3950::OID::xml;
105 $this->{RECORD} = '<xml>FIXME: not implementd</xml>';
107 elsif ( $req_form eq &Net::Z3950::OID::unimarc ) { # FIXME convert to usmarc
108 $this->{REP_FORM} = &Net::Z3950::OID::unimarc;
109 $this->{RECORD} = COBISS->fetch_rec('unimarc');
111 elsif ( $req_form eq &Net::Z3950::OID::usmarc ) { # FIXME convert to usmarc
112 $this->{REP_FORM} = &Net::Z3950::OID::usmarc;
113 $this->{RECORD} = COBISS->fetch_rec('usmarc');
115 else { ## Unsupported record format
116 $this->{ERR_CODE} = 239;
117 $this->{ERR_STR} = $req_form;
120 if ( $offset == $hits ) {
132 my $z = new Net::Z3950::SimpleServer(
133 INIT => \&InitHandle,
134 SEARCH => \&SearchHandle,
135 FETCH => \&FetchHandle,
136 CLOSE => \&CloseHandle
138 $z->launch_server( $0, @ARGV );
140 package Net::Z3950::RPN::And;
144 return $this->[0]->render() . ' AND ' . $this->[1]->render();
147 package Net::Z3950::RPN::Or;
151 return $this->[0]->render() . ' OR ' . $this->[1]->render();
154 package Net::Z3950::RPN::AndNot;
158 return $this->[0]->render() . ' AND NOT ' . $this->[1]->render();
161 package Net::Z3950::RPN::Term;
163 use Data::Dump qw(dump);
169 print "render ", dump($this);
173 my $usemap = eval "${from}::usemap;";
174 warn "# $from usermap ",dump($usemap);
178 foreach my $attr ( @{ $this->{attributes} } ) {
179 my $type = $attr->{attributeType};
180 my $value = $attr->{attributeValue};
181 $attributes->{$type} = $value;
183 if ( defined( my $use = $attributes->{1} ) ) {
184 if ( defined( my $field = $usemap->{$use} ) ) {
188 die { errcode => 114, errstr => $use }; ## Unsupported use attribute
191 if ( defined( my $rel = $attributes->{2} ) )
192 { ## No relation attributes supported
194 die { errcode => 117, errstr => $rel };
197 if ( defined( my $pos = $attributes->{3} ) )
198 { ## No position attributes either
200 die { errcode => 119, errstr => $pos };
203 if ( defined( my $struc = $attributes->{4} ) ) { ## No structure
204 if ( ( $struc != 1 ) && ( $struc != 2 ) ) {
205 die { errcode => 118, errstr => $struc };
208 if ( defined( $attributes->{5} ) ) { ## No truncation
209 die { errcode => 113, errstr => 5 };
211 my $comp = $attributes->{6};
213 if ( defined($comp) && ( $comp >= 2 ) ) {
214 $prefix = "all$prefix= ";
217 $prefix = "$prefix=";
221 my $q = $prefix . $this->{term};