|
Lines 28-34
use C4::Reserves qw( GetReserveStatus );
Link Here
|
| 28 |
use C4::Search qw(); |
28 |
use C4::Search qw(); |
| 29 |
use Koha::Logger; |
29 |
use Koha::Logger; |
| 30 |
|
30 |
|
| 31 |
use ZOOM; |
31 |
=head1 NAME |
|
|
32 |
|
| 33 |
Koha::Z3950Responder::Session |
| 34 |
|
| 35 |
=head1 SYNOPSIS |
| 36 |
|
| 37 |
An abstract class where backend-specific session modules are derived from. |
| 38 |
Z3950Responder creates one of the child classes depending on the SearchEngine |
| 39 |
preference. |
| 40 |
|
| 41 |
=head1 DESCRIPTION |
| 42 |
|
| 43 |
This class contains common functions for handling searching for and fetching |
| 44 |
of records. It can optionally add item status information to the returned |
| 45 |
records. The backend-specific abstract methods need to be implemented in a |
| 46 |
child class. |
| 47 |
|
| 48 |
=head2 CONSTANTS |
| 49 |
|
| 50 |
OIDs and diagnostic codes used in Z39.50 |
| 51 |
|
| 52 |
=cut |
| 32 |
|
53 |
|
| 33 |
use constant { |
54 |
use constant { |
| 34 |
UNIMARC_OID => '1.2.840.10003.5.1', |
55 |
UNIMARC_OID => '1.2.840.10003.5.1', |
|
Lines 41-50
use constant {
Link Here
|
| 41 |
ERR_PRESENT_OUT_OF_RANGE => 13, |
62 |
ERR_PRESENT_OUT_OF_RANGE => 13, |
| 42 |
ERR_RECORD_TOO_LARGE => 16, |
63 |
ERR_RECORD_TOO_LARGE => 16, |
| 43 |
ERR_NO_SUCH_RESULTSET => 30, |
64 |
ERR_NO_SUCH_RESULTSET => 30, |
| 44 |
ERR_SYNTAX_UNSUPPORTED => 230, |
65 |
ERR_SEARCH_FAILED => 125, |
|
|
66 |
ERR_SYNTAX_UNSUPPORTED => 239, |
| 45 |
ERR_DB_DOES_NOT_EXIST => 235, |
67 |
ERR_DB_DOES_NOT_EXIST => 235, |
| 46 |
}; |
68 |
}; |
| 47 |
|
69 |
|
|
|
70 |
=head1 FUNCTIONS |
| 71 |
|
| 72 |
=head2 INSTANCE METHODS |
| 73 |
|
| 74 |
=head3 new |
| 75 |
|
| 76 |
my $session = $self->new({ |
| 77 |
server => $z3950responder, |
| 78 |
peer => 'PEER NAME' |
| 79 |
}); |
| 80 |
|
| 81 |
Instantiate a Session |
| 82 |
|
| 83 |
=cut |
| 84 |
|
| 48 |
sub new { |
85 |
sub new { |
| 49 |
my ( $class, $args ) = @_; |
86 |
my ( $class, $args ) = @_; |
| 50 |
|
87 |
|
|
Lines 58-246
sub new {
Link Here
|
| 58 |
$self->{logger}->debug_to_screen(); |
95 |
$self->{logger}->debug_to_screen(); |
| 59 |
} |
96 |
} |
| 60 |
|
97 |
|
| 61 |
$self->_log_info("connected"); |
98 |
$self->log_info('connected'); |
| 62 |
|
99 |
|
| 63 |
return $self; |
100 |
return $self; |
| 64 |
} |
101 |
} |
| 65 |
|
102 |
|
| 66 |
sub _log_debug { |
103 |
=head3 search_handler |
| 67 |
my ( $self, $msg ) = @_; |
|
|
| 68 |
$self->{logger}->debug("[$self->{peer}] $msg"); |
| 69 |
} |
| 70 |
|
| 71 |
sub _log_info { |
| 72 |
my ( $self, $msg ) = @_; |
| 73 |
$self->{logger}->info("[$self->{peer}] $msg"); |
| 74 |
} |
| 75 |
|
| 76 |
sub _log_error { |
| 77 |
my ( $self, $msg ) = @_; |
| 78 |
$self->{logger}->error("[$self->{peer}] $msg"); |
| 79 |
} |
| 80 |
|
| 81 |
sub _set_error { |
| 82 |
my ( $self, $args, $code, $msg ) = @_; |
| 83 |
|
| 84 |
( $args->{ERR_CODE}, $args->{ERR_STR} ) = ( $code, $msg ); |
| 85 |
|
| 86 |
$self->_log_error(" returning error $code: $msg"); |
| 87 |
} |
| 88 |
|
| 89 |
sub _set_error_from_zoom { |
| 90 |
my ( $self, $args, $exception ) = @_; |
| 91 |
|
| 92 |
$self->_set_error( $args, ERR_TEMPORARY_ERROR, 'Cannot connect to upstream server' ); |
| 93 |
$self->_log_error( |
| 94 |
"Zebra upstream error: " . |
| 95 |
$exception->message() . " (" . |
| 96 |
$exception->code() . ") " . |
| 97 |
( $exception->addinfo() // '' ) . " " . |
| 98 |
$exception->diagset() |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
# This code originally went through C4::Search::getRecords, but had to use so many escape hatches |
| 103 |
# that it was easier to directly connect to Zebra. |
| 104 |
sub _start_search { |
| 105 |
my ( $self, $args, $in_retry ) = @_; |
| 106 |
|
| 107 |
my $database = $args->{DATABASES}->[0]; |
| 108 |
my ( $connection, $results ); |
| 109 |
|
104 |
|
| 110 |
eval { |
105 |
Callback that is called when a new search is performed |
| 111 |
$connection = C4::Context->Zconn( |
|
|
| 112 |
# We're depending on the caller to have done some validation. |
| 113 |
$database eq 'biblios' ? 'biblioserver' : 'authorityserver', |
| 114 |
0 # No, no async, doesn't really help much for single-server searching |
| 115 |
); |
| 116 |
|
106 |
|
| 117 |
$results = $connection->search_pqf( $args->{QUERY} ); |
107 |
Calls C<start_search> for backend-specific retrieval logic |
| 118 |
|
108 |
|
| 119 |
$self->_log_debug(' retry successful') if ($in_retry); |
109 |
=cut |
| 120 |
}; |
|
|
| 121 |
if ($@) { |
| 122 |
die $@ if ( ref($@) ne 'ZOOM::Exception' ); |
| 123 |
|
| 124 |
if ( $@->diagset() eq 'ZOOM' && $@->code() == 10004 && !$in_retry ) { |
| 125 |
$self->_log_debug(' upstream server lost connection, retrying'); |
| 126 |
return $self->_start_search( $args, 1 ); |
| 127 |
} |
| 128 |
|
| 129 |
$self->_set_error_from_zoom( $args, $@ ); |
| 130 |
$connection = undef; |
| 131 |
} |
| 132 |
|
| 133 |
return ( $connection, $results, $results ? $results->size() : -1 ); |
| 134 |
} |
| 135 |
|
| 136 |
sub _check_fetch { |
| 137 |
my ( $self, $resultset, $args, $offset, $num_records ) = @_; |
| 138 |
|
| 139 |
if ( !defined( $resultset ) ) { |
| 140 |
$self->_set_error( $args, ERR_NO_SUCH_RESULTSET, 'No such resultset' ); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
if ( $offset < 0 || $offset + $num_records > $resultset->{hits} ) { |
| 145 |
$self->_set_error( $args, ERR_PRESENT_OUT_OF_RANGE, 'Present request out of range' ); |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
sub _fetch_record { |
| 153 |
my ( $self, $resultset, $args, $index, $num_to_prefetch ) = @_; |
| 154 |
|
| 155 |
my $record; |
| 156 |
|
| 157 |
eval { |
| 158 |
if ( !$resultset->{results}->record_immediate( $index ) ) { |
| 159 |
my $start = $num_to_prefetch ? int( $index / $num_to_prefetch ) * $num_to_prefetch : $index; |
| 160 |
|
| 161 |
if ( $start + $num_to_prefetch >= $resultset->{results}->size() ) { |
| 162 |
$num_to_prefetch = $resultset->{results}->size() - $start; |
| 163 |
} |
| 164 |
|
| 165 |
$self->_log_debug(" fetch uncached, fetching $num_to_prefetch records starting at $start"); |
| 166 |
|
| 167 |
$resultset->{results}->records( $start, $num_to_prefetch, 0 ); |
| 168 |
} |
| 169 |
|
| 170 |
$record = $resultset->{results}->record_immediate( $index )->raw(); |
| 171 |
}; |
| 172 |
if ($@) { |
| 173 |
die $@ if ( ref($@) ne 'ZOOM::Exception' ); |
| 174 |
$self->_set_error_from_zoom( $args, $@ ); |
| 175 |
return; |
| 176 |
} else { |
| 177 |
return $record; |
| 178 |
} |
| 179 |
} |
| 180 |
|
110 |
|
| 181 |
sub search_handler { |
111 |
sub search_handler { |
| 182 |
# Called when search is first sent. |
|
|
| 183 |
my ( $self, $args ) = @_; |
112 |
my ( $self, $args ) = @_; |
| 184 |
|
113 |
|
| 185 |
my $database = $args->{DATABASES}->[0]; |
114 |
my $database = $args->{DATABASES}->[0]; |
| 186 |
|
115 |
|
| 187 |
if ( $database !~ /^(biblios|authorities)$/ ) { |
116 |
if ( $database ne $Koha::SearchEngine::BIBLIOS_INDEX && $database ne $Koha::SearchEngine::AUTHORITIES_INDEX ) { |
| 188 |
$self->_set_error( $args, ERR_DB_DOES_NOT_EXIST, 'No such database' ); |
117 |
$self->set_error( $args, $self->ERR_DB_DOES_NOT_EXIST, 'No such database' ); |
| 189 |
return; |
118 |
return; |
| 190 |
} |
119 |
} |
| 191 |
|
120 |
|
| 192 |
my $query = $args->{QUERY}; |
121 |
my $query = $args->{QUERY}; |
| 193 |
$self->_log_info("received search for '$query', (RS $args->{SETNAME})"); |
122 |
$self->log_info("received search for '$query', (RS $args->{SETNAME})"); |
| 194 |
|
|
|
| 195 |
my ( $connection, $results, $num_hits ) = $self->_start_search( $args ); |
| 196 |
return unless $connection; |
| 197 |
|
| 198 |
$args->{HITS} = $num_hits; |
| 199 |
my $resultset = $self->{resultsets}->{ $args->{SETNAME} } = { |
| 200 |
database => $database, |
| 201 |
connection => $connection, |
| 202 |
results => $results, |
| 203 |
query => $args->{QUERY}, |
| 204 |
hits => $args->{HITS}, |
| 205 |
}; |
| 206 |
} |
| 207 |
|
123 |
|
| 208 |
sub present_handler { |
124 |
my ($resultset, $hits) = $self->start_search( $args, $self->{server}->{num_to_prefetch} ); |
| 209 |
# Called when a set of records is requested. |
125 |
return unless $resultset; |
| 210 |
my ( $self, $args ) = @_; |
|
|
| 211 |
|
126 |
|
| 212 |
$self->_log_debug("received present for $args->{SETNAME}, $args->{START}+$args->{NUMBER}"); |
127 |
$args->{HITS} = $hits; |
|
|
128 |
$self->{resultsets}->{ $args->{SETNAME} } = $resultset; |
| 129 |
} |
| 213 |
|
130 |
|
| 214 |
my $resultset = $self->{resultsets}->{ $args->{SETNAME} }; |
131 |
=head3 fetch_handler |
| 215 |
# The offset comes across 1-indexed. |
|
|
| 216 |
my $offset = $args->{START} - 1; |
| 217 |
|
132 |
|
| 218 |
return unless $self->_check_fetch( $resultset, $args, $offset, $args->{NUMBER} ); |
133 |
Callback that is called when records are requested |
| 219 |
|
134 |
|
| 220 |
} |
135 |
Calls C<fetch_record> for backend-specific retrieval logic |
|
|
136 |
|
| 137 |
=cut |
| 221 |
|
138 |
|
| 222 |
sub fetch_handler { |
139 |
sub fetch_handler { |
| 223 |
# Called when a given record is requested. |
|
|
| 224 |
my ( $self, $args ) = @_; |
140 |
my ( $self, $args ) = @_; |
| 225 |
my $session = $args->{HANDLE}; |
141 |
|
|
|
142 |
$self->log_debug("received fetch for RS $args->{SETNAME}, record $args->{OFFSET}"); |
| 143 |
|
| 226 |
my $server = $self->{server}; |
144 |
my $server = $self->{server}; |
| 227 |
|
145 |
|
| 228 |
$self->_log_debug("received fetch for $args->{SETNAME}, record $args->{OFFSET}"); |
|
|
| 229 |
my $form_oid = $args->{REQ_FORM} // ''; |
146 |
my $form_oid = $args->{REQ_FORM} // ''; |
| 230 |
my $composition = $args->{COMP} // ''; |
147 |
my $composition = $args->{COMP} // ''; |
| 231 |
$self->_log_debug(" form OID $form_oid, composition $composition"); |
148 |
$self->log_debug(" form OID '$form_oid', composition '$composition'"); |
| 232 |
|
149 |
|
| 233 |
my $resultset = $session->{resultsets}->{ $args->{SETNAME} }; |
150 |
my $resultset = $self->{resultsets}->{ $args->{SETNAME} }; |
| 234 |
# The offset comes across 1-indexed. |
151 |
# The offset comes across 1-indexed. |
| 235 |
my $offset = $args->{OFFSET} - 1; |
152 |
my $offset = $args->{OFFSET} - 1; |
| 236 |
|
153 |
|
| 237 |
return unless $self->_check_fetch( $resultset, $args, $offset, 1 ); |
154 |
return unless $self->check_fetch( $resultset, $args, $offset, 1 ); |
| 238 |
|
155 |
|
| 239 |
$args->{LAST} = 1 if ( $offset == $resultset->{hits} - 1 ); |
156 |
$args->{LAST} = 1 if ( $offset == $resultset->{hits} - 1 ); |
| 240 |
|
157 |
|
| 241 |
my $record = $self->_fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} ); |
158 |
my $record = $self->fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} ); |
| 242 |
return unless $record; |
159 |
return unless $record; |
| 243 |
|
160 |
|
|
|
161 |
# Note that new_record_from_zebra is badly named and works also with Elasticsearch |
| 244 |
$record = C4::Search::new_record_from_zebra( |
162 |
$record = C4::Search::new_record_from_zebra( |
| 245 |
$resultset->{database} eq 'biblios' ? 'biblioserver' : 'authorityserver', |
163 |
$resultset->{database} eq 'biblios' ? 'biblioserver' : 'authorityserver', |
| 246 |
$record |
164 |
$record |
|
Lines 254-269
sub fetch_handler {
Link Here
|
| 254 |
} |
172 |
} |
| 255 |
} |
173 |
} |
| 256 |
|
174 |
|
| 257 |
if ( $form_oid eq MARCXML_OID && $composition eq 'marcxml' ) { |
175 |
if ( $form_oid eq $self->MARCXML_OID && $composition eq 'marcxml' ) { |
| 258 |
$args->{RECORD} = $record->as_xml_record(); |
176 |
$args->{RECORD} = $record->as_xml_record(); |
| 259 |
} elsif ( ( $form_oid eq USMARC_OID || $form_oid eq UNIMARC_OID ) && ( !$composition || $composition eq 'F' ) ) { |
177 |
} elsif ( ( $form_oid eq $self->USMARC_OID || $form_oid eq $self->UNIMARC_OID ) && ( !$composition || $composition eq 'F' ) ) { |
| 260 |
$args->{RECORD} = $record->as_usmarc(); |
178 |
$args->{RECORD} = $record->as_usmarc(); |
| 261 |
} else { |
179 |
} else { |
| 262 |
$self->_set_error( $args, ERR_SYNTAX_UNSUPPORTED, "Unsupported syntax/composition $form_oid/$composition" ); |
180 |
$self->set_error( $args, $self->ERR_SYNTAX_UNSUPPORTED, "Unsupported syntax/composition $form_oid/$composition" ); |
| 263 |
return; |
181 |
return; |
| 264 |
} |
182 |
} |
| 265 |
} |
183 |
} |
| 266 |
|
184 |
|
|
|
185 |
=head3 close_handler |
| 186 |
|
| 187 |
Callback that is called when a session is terminated |
| 188 |
|
| 189 |
=cut |
| 190 |
|
| 191 |
sub close_handler { |
| 192 |
my ( $self, $args ) = @_; |
| 193 |
|
| 194 |
# Override in a child class to add functionality |
| 195 |
} |
| 196 |
|
| 197 |
=head3 start_search |
| 198 |
|
| 199 |
my ($resultset, $hits) = $self->_start_search( $args, $self->{server}->{num_to_prefetch} ); |
| 200 |
|
| 201 |
A backend-specific method for starting a new search |
| 202 |
|
| 203 |
=cut |
| 204 |
|
| 205 |
sub start_search { |
| 206 |
die('Abstract method'); |
| 207 |
} |
| 208 |
|
| 209 |
=head3 check_fetch |
| 210 |
|
| 211 |
$self->check_fetch($resultset, $args, $offset, $num_records); |
| 212 |
|
| 213 |
Check that the fetch request parameters are within bounds of the result set. |
| 214 |
|
| 215 |
=cut |
| 216 |
|
| 217 |
sub check_fetch { |
| 218 |
my ( $self, $resultset, $args, $offset, $num_records ) = @_; |
| 219 |
|
| 220 |
if ( !defined( $resultset ) ) { |
| 221 |
$self->set_error( $args, ERR_NO_SUCH_RESULTSET, 'No such resultset' ); |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
if ( $offset < 0 || $offset + $num_records > $resultset->{hits} ) { |
| 226 |
$self->set_error( $args, ERR_PRESENT_OUT_OF_RANGE, 'Present request out of range' ); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
return 1; |
| 231 |
} |
| 232 |
|
| 233 |
=head3 fetch_record |
| 234 |
|
| 235 |
my $record = $self->_fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} ); |
| 236 |
|
| 237 |
A backend-specific method for fetching a record |
| 238 |
|
| 239 |
=cut |
| 240 |
|
| 241 |
sub fetch_record { |
| 242 |
die('Abstract method'); |
| 243 |
} |
| 244 |
|
| 245 |
=head3 add_item_status |
| 246 |
|
| 247 |
$self->add_item_status( $field ); |
| 248 |
|
| 249 |
Add item status to the given field |
| 250 |
|
| 251 |
=cut |
| 252 |
|
| 267 |
sub add_item_status { |
253 |
sub add_item_status { |
| 268 |
my ( $self, $field ) = @_; |
254 |
my ( $self, $field ) = @_; |
| 269 |
|
255 |
|
|
Lines 318-329
sub add_item_status {
Link Here
|
| 318 |
} |
304 |
} |
| 319 |
} |
305 |
} |
| 320 |
|
306 |
|
| 321 |
sub close_handler { |
|
|
| 322 |
my ( $self, $args ) = @_; |
| 323 |
|
307 |
|
| 324 |
foreach my $resultset ( values %{ $self->{resultsets} } ) { |
308 |
=head3 log_debug |
| 325 |
$resultset->{results}->destroy(); |
309 |
|
| 326 |
} |
310 |
$self->log_debug('Message'); |
|
|
311 |
|
| 312 |
Output a debug message |
| 313 |
|
| 314 |
=cut |
| 315 |
|
| 316 |
sub log_debug { |
| 317 |
my ( $self, $msg ) = @_; |
| 318 |
$self->{logger}->debug("[$self->{peer}] $msg"); |
| 319 |
} |
| 320 |
|
| 321 |
=head3 log_info |
| 322 |
|
| 323 |
$self->log_info('Message'); |
| 324 |
|
| 325 |
Output an info message |
| 326 |
|
| 327 |
=cut |
| 328 |
|
| 329 |
sub log_info { |
| 330 |
my ( $self, $msg ) = @_; |
| 331 |
$self->{logger}->info("[$self->{peer}] $msg"); |
| 332 |
} |
| 333 |
|
| 334 |
=head3 log_error |
| 335 |
|
| 336 |
$self->log_error('Message'); |
| 337 |
|
| 338 |
Output an error message |
| 339 |
|
| 340 |
=cut |
| 341 |
|
| 342 |
sub log_error { |
| 343 |
my ( $self, $msg ) = @_; |
| 344 |
$self->{logger}->error("[$self->{peer}] $msg"); |
| 345 |
} |
| 346 |
|
| 347 |
=head3 set_error |
| 348 |
|
| 349 |
$self->set_error($args, $self->ERR_SEARCH_FAILED, 'Backend connection failed' ); |
| 350 |
|
| 351 |
Set and log an error code and diagnostic message to be returned to the client |
| 352 |
|
| 353 |
=cut |
| 354 |
|
| 355 |
sub set_error { |
| 356 |
my ( $self, $args, $code, $msg ) = @_; |
| 357 |
|
| 358 |
( $args->{ERR_CODE}, $args->{ERR_STR} ) = ( $code, $msg ); |
| 359 |
|
| 360 |
$self->log_error(" returning error $code: $msg"); |
| 327 |
} |
361 |
} |
| 328 |
|
362 |
|
| 329 |
1; |
363 |
1; |