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 = $COBISS::hits || diag "no results for $query";
53 diag "got $hits hits";
56 upper => $hits < $max_records ? $max_records : $hits,
57 data => $result->{'resultElements'}, # FIXME
59 my $sets = $session->{SETS};
61 if ( defined( $sets->{$setname} ) && !$repl_set ) {
62 $this->{ERR_CODE} = 21;
65 if ( scalar keys %$sets >= $max_result_sets ) {
66 $this->{ERR_CODE} = 112;
67 $this->{ERR_STR} = "Max number is $max_result_sets";
70 $sets->{$setname} = $rs;
71 $this->{HITS} = $session->{HITS} = $hits;
72 $session->{QUERY} = $query;
77 my $session = $this->{HANDLE};
78 my $setname = $this->{SETNAME};
79 my $req_form = $this->{REQ_FORM};
80 my $offset = $this->{OFFSET};
81 my $sets = $session->{SETS};
82 my $hits = $session->{HITS};
88 if ( !defined( $rs = $sets->{$setname} ) ) {
89 $this->{ERR_CODE} = 30;
92 if ( $offset > $hits ) {
93 $this->{ERR_CODE} = 13;
96 $this->{BASENAME} = "HtmlZ3950";
98 # if ( !defined($req_form) || ( $req_form eq &Net::Z3950::OID::xml ) )
101 $this->{REP_FORM} = &Net::Z3950::OID::xml;
102 $this->{RECORD} = '<xml>FIXME: not implementd</xml>';
104 elsif ( $req_form eq &Net::Z3950::OID::unimarc ) { # FIXME convert to usmarc
105 $this->{REP_FORM} = &Net::Z3950::OID::unimarc;
106 $this->{RECORD} = COBISS->fetch_marc;
108 else { ## Unsupported record format
109 $this->{ERR_CODE} = 239;
110 $this->{ERR_STR} = $req_form;
113 if ( $offset == $hits ) {
125 my $z = new Net::Z3950::SimpleServer(
126 INIT => \&InitHandle,
127 SEARCH => \&SearchHandle,
128 FETCH => \&FetchHandle,
129 CLOSE => \&CloseHandle
131 $z->launch_server( $0, @ARGV );
133 package Net::Z3950::RPN::And;
137 return $this->[0]->render() . ' AND ' . $this->[1]->render();
140 package Net::Z3950::RPN::Or;
144 return $this->[0]->render() . ' OR ' . $this->[1]->render();
147 package Net::Z3950::RPN::AndNot;
151 return $this->[0]->render() . ' AND NOT ' . $this->[1]->render();
154 package Net::Z3950::RPN::Term;
161 print "render ", $this;
165 foreach my $attr ( @{ $this->{attributes} } ) {
166 my $type = $attr->{attributeType};
167 my $value = $attr->{attributeValue};
168 $attributes->{$type} = $value;
170 if ( defined( my $use = $attributes->{1} ) ) {
171 if ( defined( my $field = COBISS::usemap($use) ) ) {
175 die { errcode => 114, errstr => $use }; ## Unsupported use attribute
178 if ( defined( my $rel = $attributes->{2} ) )
179 { ## No relation attributes supported
181 die { errcode => 117, errstr => $rel };
184 if ( defined( my $pos = $attributes->{3} ) )
185 { ## No position attributes either
187 die { errcode => 119, errstr => $pos };
190 if ( defined( my $struc = $attributes->{4} ) ) { ## No structure
191 if ( ( $struc != 1 ) && ( $struc != 2 ) ) {
192 die { errcode => 118, errstr => $struc };
195 if ( defined( $attributes->{5} ) ) { ## No truncation
196 die { errcode => 113, errstr => 5 };
198 my $comp = $attributes->{6};
200 if ( defined($comp) && ( $comp >= 2 ) ) {
201 $prefix = "all$prefix= ";
204 $prefix = "$prefix=";
208 my $q = $prefix . $this->{term};