3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2013 Prosentient Systems
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::Biblio qw(TransformMarcToKoha);
25 use C4::Koha qw( GetVariationsOfISBN );
26 use C4::Charset qw( MarcToUTF8Record SetUTF8Flag );
27 use MARC::File::USMARC;
29 use C4::ImportBatch qw( GetZ3950BatchId AddBiblioToBatch AddAuthToBatch );
30 use C4::AuthoritiesMarc qw( GuessAuthTypeCode GetAuthorizedHeading );
35 our (@ISA, @EXPORT_OK);
39 @EXPORT_OK = qw(BreedingSearch ImportBreedingAuth Z3950Search Z3950SearchAuth);
44 C4::Breeding : module to add biblios to import_records via
45 the breeding/reservoir API.
49 Z3950Search($pars, $template);
50 ($count, @results) = &BreedingSearch($title,$isbn);
54 This module contains routines related to Koha's Z39.50 search into
55 cataloguing reservoir features.
59 ($count, @results) = &BreedingSearch($term);
60 C<$term> contains the term to search, it will be searched as title,author, or isbn
62 C<$count> is the number of items in C<@results>. C<@results> is an
63 array of references-to-hash; the keys are the items from the C<import_records> and
64 C<import_biblios> tables of the Koha database.
70 my $dbh = C4::Context->dbh;
76 my $authortitle = $term;
77 $authortitle =~ s/(\s+)/\%/g; #Replace spaces with wildcard
78 $authortitle = "%" . $authortitle . "%"; #Add wildcard to start and end of string
79 # normalise ISBN like at import
80 my @isbns = C4::Koha::GetVariationsOfISBN($term);
82 $query = "SELECT import_biblios.import_record_id,
83 import_batches.file_name,
86 import_biblios.author,
87 import_batches.upload_timestamp
89 JOIN import_records USING (import_record_id)
90 JOIN import_batches USING (import_batch_id)
91 WHERE title LIKE ? OR author LIKE ? OR isbn IN (" . join(',',('?') x @isbns) . ")";
92 @bind=( $authortitle, $authortitle, @isbns );
93 $sth = $dbh->prepare($query);
95 while (my $data = $sth->fetchrow_hashref) {
96 $results[$count] = $data;
97 # FIXME - hack to reflect difference in name
98 # of columns in old marc_breeding and import_records
99 # There needs to be more separation between column names and
100 # field names used in the templates </soapbox>
101 $data->{'file'} = $data->{'file_name'};
102 $data->{'id'} = $data->{'import_record_id'};
107 return($count, @results);
108 } # sub breedingsearch
113 Z3950Search($pars, $template);
115 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
116 Also it should contain an arrayref id that points to a list of id's of the z3950 targets to be queried (see z3950servers table).
117 This code is used in acqui/z3950_search and cataloging/z3950_search.
118 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
123 my ($pars, $template)= @_;
125 my @id= @{$pars->{id}};
126 my $page= $pars->{page};
127 my $biblionumber= $pars->{biblionumber};
132 my @breeding_loop = ();
139 my ( $zquery, $squery ) = _bib_build_query( $pars );
141 my $schema = Koha::Database->new()->schema();
142 my $rs = $schema->resultset('Z3950server')->search(
144 { result_class => 'DBIx::Class::ResultClass::HashRefInflator' },
146 my @servers = $rs->all;
147 foreach my $server ( @servers ) {
148 my $server_zquery = $zquery;
149 if(my $attributes = $server->{attributes}){
150 $server_zquery = "$attributes $zquery";
152 $oConnection[$s] = _create_connection( $server );
154 $server->{servertype} eq 'zed'?
155 $oConnection[$s]->search_pqf( $server_zquery ):
156 $oConnection[$s]->search(ZOOM::Query::CQL->new(
157 _translate_query( $server, $squery )));
160 my $xslh = Koha::XSLT::Base->new;
163 while ( $nremaining-- ) {
166 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
167 $event = $oConnection[ $k - 1 ]->last_event();
168 last if $event == ZOOM::Event::ZEND;
173 my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
175 if ($error =~ m/^(10000|10007)$/ ) {
176 push(@errconn, { server => $servers[$k]->{host}, error => $error } );
180 my $numresults = $oResult[$k]->size();
183 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
184 $show_next = 1 if $numresults >= ($page*20);
185 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
186 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
187 if ( $oResult[$k]->record($i) ) {
189 ( $res, $error ) = _handle_one_result( $oResult[$k]->record($i), $servers[$k], ++$imported, $biblionumber, $xslh ); #ignores error in sequence numbering
190 push @breeding_loop, $res if $res;
191 push @errconn, { server => $servers[$k]->{servername}, error => $error, seq => $i+1 } if $error;
194 push @errconn, { 'server' => $servers[$k]->{servername}, error => ( ( $oConnection[$k]->error_x() )[0] ), seq => $i+1 };
202 numberpending => $nremaining,
203 current_page => $page,
204 total_pages => $total_pages,
205 show_nextbutton => $show_next?1:0,
206 show_prevbutton => $page!=1,
210 #close result sets and connections
212 $oResult[$_]->destroy();
213 $oConnection[$_]->destroy();
217 breeding_loop => \@breeding_loop,
218 servers => \@servers,
223 sub _auth_build_query {
227 nameany => '@attr 1=1002 "#term" ',
228 authorany => '@attr 1=1003 "#term" ',
229 authorcorp => '@attr 1=2 "#term" ',
230 authorpersonal => '@attr 1=1 "#term" ',
231 authormeetingcon => '@attr 1=3 "#term" ',
232 subject => '@attr 1=21 "#term" ',
233 subjectsubdiv => '@attr 1=47 "#term" ',
234 title => '@attr 1=4 "#term" ',
235 uniformtitle => '@attr 1=6 "#term" ',
236 srchany => '@attr 1=1016 "#term" ',
237 controlnumber => '@attr 1=12 "#term" ',
240 return _build_query( $pars, $qry_build );
243 sub _bib_build_query {
248 isbn => '@attr 1=7 @attr 5=1 "#term" ',
249 issn => '@attr 1=8 @attr 5=1 "#term" ',
250 title => '@attr 1=4 "#term" ',
251 author => '@attr 1=1003 "#term" ',
252 dewey => '@attr 1=16 "#term" ',
253 subject => '@attr 1=21 "#term" ',
254 lccall => '@attr 1=16 @attr 2=3 @attr 3=1 @attr 4=1 @attr 5=1 '.
255 '@attr 6=1 "#term" ',
256 controlnumber => '@attr 1=12 "#term" ',
257 srchany => '@attr 1=1016 "#term" ',
258 stdid => '@attr 1=1007 "#term" ',
259 publicationyear => '@attr 1=31 "#term" '
262 return _build_query( $pars, $qry_build );
267 my ( $pars, $qry_build ) = @_;
272 foreach my $k ( sort keys %$pars ) {
273 #note that the sort keys forces an identical result under Perl 5.18
274 #one of the unit tests is based on that assumption
275 if( ( my $val=$pars->{$k} ) && $qry_build->{$k} ) {
276 $qry_build->{$k} =~ s/#term/$val/g;
277 $zquery .= $qry_build->{$k};
278 $squery .= "[$k]=\"$val\" and ";
282 $zquery = "\@and " . $zquery for 2..$nterms;
283 $squery =~ s/ and $//;
284 return ( $zquery, $squery );
287 sub _handle_one_result {
288 my ( $zoomrec, $servhref, $seq, $bib, $xslh )= @_;
290 my $raw= $zoomrec->raw();
292 if( $servhref->{servertype} eq 'sru' ) {
293 $marcrecord= MARC::Record->new_from_xml( $raw, 'UTF-8',
294 $servhref->{syntax} );
295 $marcrecord->encoding('UTF-8');
297 ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encoding} // "iso-5426" ); #ignores charset return values
299 SetUTF8Flag($marcrecord);
301 ( $marcrecord, $error ) = _do_xslt_proc($marcrecord, $servhref, $xslh);
303 my $batch_id = GetZ3950BatchId($servhref->{servername});
304 my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0);
305 #Last zero indicates: no update for batch record counts
309 my @kohafields = ('biblio.title','biblio.author','biblioitems.isbn','biblioitems.lccn','biblioitems.editionstatement');
310 push @kohafields, C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
311 $row = C4::Biblio::TransformMarcToKoha({ record => $marcrecord, kohafields => \@kohafields, limit_table => 'no_items' });
312 $row->{date} = $row->{copyrightdate} // $row->{publicationyear};
313 $row->{biblionumber} = $bib;
314 $row->{server} = $servhref->{servername};
315 $row->{breedingid} = $breedingid;
316 $row->{isbn}=_isbn_replace($row->{isbn});
317 $row = _add_custom_field_rowdata($row, $marcrecord);
319 return ( $row, $error );
323 my ( $marc, $server, $xslh ) = @_;
324 return $marc if !$server->{add_xslt};
326 my $htdocs = C4::Context->config('intrahtdocs');
327 my $theme = C4::Context->preference("template"); #staff
328 my $lang = C4::Languages::getlanguage() || 'en';
330 my @files= split ',', $server->{add_xslt};
331 my $xml = $marc->as_xml;
332 foreach my $f ( @files ) {
333 $f =~ s/^\s+//; $f =~ s/\s+$//; next if !$f;
334 $f = C4::XSLT::_get_best_default_xslt_filename(
335 $htdocs, $theme, $lang, $f ) unless $f =~ /^\//;
336 $xml = $xslh->transform( $xml, $f );
337 last if $xslh->err; #skip other files
340 return MARC::Record->new_from_xml($xml, 'UTF-8');
342 return ( $marc, $xslh->err ); #original record in case of errors
346 sub _add_custom_field_rowdata
348 my ( $row, $record ) = @_;
349 my $pref_newtags = C4::Context->preference('AdditionalFieldsInZ3950ResultSearch');
350 my $pref_flavour = C4::Context->preference('MarcFlavour');
352 $pref_newtags =~ s/^\s+|\s+$//g;
353 $pref_newtags =~ s/\h+/ /g;
357 foreach my $field (split /\,/, $pref_newtags) {
358 $field =~ s/^\s+|\s+$//g ; # trim whitespace
359 my ($tag, $subtags) = split(/\$/, $field);
361 if ( $record->field($tag) ) {
364 for my $marcfield ($record->field($tag)) {
367 for my $code (split //, $subtags) {
368 if ( $marcfield->subfield($code) ) {
369 $str .= $marcfield->subfield($code) . ' ';
372 if ( not $str eq '') {
375 } elsif ( $tag == 10 ) {
376 push @content, ( $pref_flavour eq "MARC21" ? $marcfield->data : $marcfield->as_string );
377 } elsif ( $tag < 10 ) {
378 push @content, $marcfield->data();
380 push @content, $marcfield->as_string();
385 $row->{$field} = \@content;
386 push( @addnumberfields, $field );
391 $row->{'addnumberfields'} = \@addnumberfields;
398 return unless defined $isbn;
399 $isbn =~ s/ |-|\.//g;
400 $isbn =~ s/\|/ \| /g;
405 sub _create_connection {
407 my $option1= ZOOM::Options->new();
408 $option1->option( 'async' => 1 );
409 $option1->option( 'elementSetName', 'F' );
410 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
411 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
413 if( $server->{servertype} eq 'sru' ) {
414 foreach( split ',', $server->{sru_options}//'' ) {
415 #first remove surrounding spaces at comma and equals-sign
417 my @temp= split '=', $_, 2;
418 @temp= map { my $c=$_; $c=~s/^\s+|\s+$//g; $c; } @temp;
419 $option1->option( $temp[0] => $temp[1] ) if @temp;
421 } elsif( $server->{servertype} eq 'zed' ) {
422 $option1->option( 'databaseName', $server->{db} );
423 $option1->option( 'user', $server->{userid} ) if $server->{userid};
424 $option1->option( 'password', $server->{password} ) if $server->{password};
426 my $obj= ZOOM::Connection->create($option1);
427 if( $server->{servertype} eq 'sru' ) {
428 my $host= $server->{host};
429 if( $host !~ /^https?:\/\// ) {
430 #Normally, host will not be prefixed by protocol.
431 #In that case we can (safely) assume http.
432 #In case someone prefixed with https, give it a try..
433 $host = 'http://' . $host;
435 $obj->connect( $host.':'.$server->{port}.'/'.$server->{db} );
437 $obj->connect( $server->{host}, $server->{port} );
442 sub _translate_query { #SRU query adjusted per server cf. srufields column
443 my ($server, $query) = @_;
445 #sru_fields is in format title=field,isbn=field,...
446 #if a field doesn't exist, try anywhere or remove [field]=
447 my @parts= split(',', $server->{sru_fields} );
448 my %trans= map { if( /=/ ) { ( $`,$' ) } else { () } } @parts;
449 my $any= $trans{srchany}?$trans{srchany}.'=':'';
452 foreach my $key (keys %trans) {
457 $q=~s/\[$key\]=/$any/g;
460 $q=~s/\[\w+\]=/$any/g; # remove remaining fields (not found in field list)
464 =head2 ImportBreedingAuth
466 ImportBreedingAuth( $marcrecord, $filename, $encoding, $heading );
468 ImportBreedingAuth imports MARC records in the reservoir (import_records table) or returns their id if they already exist.
472 sub ImportBreedingAuth {
473 my ( $marcrecord, $filename, $encoding, $heading ) = @_;
474 my $dbh = C4::Context->dbh;
476 my $batch_id = GetZ3950BatchId($filename);
477 my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
479 my $controlnumber = $marcrecord->field('001')->data;
481 $searchbreeding->execute($controlnumber,$heading);
482 my ($breedingid) = $searchbreeding->fetchrow;
484 return $breedingid if $breedingid;
485 $breedingid = AddAuthToBatch($batch_id, 0, $marcrecord, $encoding);
489 =head2 Z3950SearchAuth
491 Z3950SearchAuth($pars, $template);
493 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
494 title, uniform title, subject, subjectsubdiv, srchany.
495 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
496 This code is used in cataloging/z3950_auth_search.
497 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
501 sub Z3950SearchAuth {
502 my ($pars, $template)= @_;
504 my $dbh = C4::Context->dbh;
505 my @id= @{$pars->{id}};
506 my $page= $pars->{page} // 1;
513 my @breeding_loop = ();
522 my $marcflavour = C4::Context->preference('marcflavour');
523 my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour;
524 my $authid= $pars->{authid};
525 my ( $zquery, $squery ) = _auth_build_query( $pars );
526 foreach my $servid (@id) {
527 my $sth = $dbh->prepare("select * from z3950servers where id=?");
528 $sth->execute($servid);
529 while ( my $server = $sth->fetchrow_hashref ) {
530 $oConnection[$s] = _create_connection( $server );
532 if ( $server->{servertype} eq 'zed' ) {
533 my $server_zquery = $zquery;
534 if ( my $attributes = $server->{attributes} ) {
535 $server_zquery = "$attributes $zquery";
537 $oResult[$s] = $oConnection[$s]->search_pqf( $server_zquery );
540 $oResult[$s] = $oConnection[$s]->search(
541 ZOOM::Query::CQL->new(_translate_query( $server, $squery ))
544 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
545 $servers[$s] = $server;
551 while ( $nremaining-- ) {
554 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
555 $event = $oConnection[ $k - 1 ]->last_event();
556 last if $event == ZOOM::Event::ZEND;
561 my ($error )= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
563 if ($error =~ m/^(10000|10007)$/ ) {
564 push @errconn, { server => $servers[$k]->{host} };
568 my $numresults = $oResult[$k]->size();
571 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
572 $show_next = 1 if $numresults >= ($page*20);
573 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
574 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
575 my $rec = $oResult[$k]->record($i);
577 my $marcdata = $rec->raw();
579 if( $servers[$k]->{servertype} eq 'sru' ) {
580 $marcrecord = eval { MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} ) };
581 if( !$marcrecord || $@ ) {
582 _dump_conversion_error( $servers[$k]->{servername}, $marcdata, $@ );
583 next; # skip this one
586 my ($charset_result, $charset_errors);
587 ( $marcrecord, $charset_result, $charset_errors ) = MarcToUTF8Record( $marcdata, $marc_type, $encoding[$k] );
588 if( !$marcrecord || @$charset_errors ) {
589 _dump_conversion_error( $servers[$k]->{servername}, $marcdata, $charset_result, $charset_errors );
590 next; # skip this one
593 $marcrecord->encoding('UTF-8');
594 SetUTF8Flag($marcrecord);
596 my $heading_authtype_code = GuessAuthTypeCode($marcrecord) or next;
597 my $heading = GetAuthorizedHeading({ record => $marcrecord });
598 my $breedingid = ImportBreedingAuth( $marcrecord, $servers[$k]->{host}, 'UTF-8', $heading );
600 $row_data{server} = $servers[$k]->{'servername'};
601 $row_data{breedingid} = $breedingid;
602 $row_data{heading} = $heading;
603 $row_data{authid} = $authid;
604 $row_data{heading_code} = $heading_authtype_code;
605 push( @breeding_loop, \%row_data );
608 push(@breeding_loop,{'server'=>$servers[$k]->{'servername'},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'authid'=>-1});
616 numberpending => $nremaining,
617 current_page => $page,
618 total_pages => $total_pages,
619 show_nextbutton => $show_next?1:0,
620 show_prevbutton => $page!=1,
624 #close result sets and connections
626 $oResult[$_]->destroy();
627 $oConnection[$_]->destroy();
631 foreach my $id (@id) {
632 push @servers, {id => $id};
635 breeding_loop => \@breeding_loop,
636 servers => \@servers,
641 sub _dump_conversion_error {
642 require Data::Dumper;
643 warn Data::Dumper->new([ 'Z3950SearchAuth conversion error', @_ ])->Indent(0)->Dump;