Bug 25273: (follow-up) Don't die on unknown authtype
[koha.git] / C4 / Breeding.pm
blob8287cf4b6f9c525b39ac3424f8b0b10908c99b51
1 package C4::Breeding;
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>.
21 use strict;
22 use warnings;
24 use C4::Biblio;
25 use C4::Koha;
26 use C4::Charset;
27 use MARC::File::USMARC;
28 use MARC::Field;
29 use C4::ImportBatch;
30 use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
31 use C4::Languages;
32 use Koha::Database;
33 use Koha::XSLT::Base;
35 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37 BEGIN {
38 require Exporter;
39 @ISA = qw(Exporter);
40 @EXPORT = qw(&BreedingSearch &Z3950Search &Z3950SearchAuth);
43 =head1 NAME
45 C4::Breeding : module to add biblios to import_records via
46 the breeding/reservoir API.
48 =head1 SYNOPSIS
50 Z3950Search($pars, $template);
51 ($count, @results) = &BreedingSearch($title,$isbn);
53 =head1 DESCRIPTION
55 This module contains routines related to Koha's Z39.50 search into
56 cataloguing reservoir features.
58 =head2 BreedingSearch
60 ($count, @results) = &BreedingSearch($title,$isbn);
61 C<$title> contains the title,
62 C<$isbn> contains isbn or issn,
64 C<$count> is the number of items in C<@results>. C<@results> is an
65 array of references-to-hash; the keys are the items from the C<import_records> and
66 C<import_biblios> tables of the Koha database.
68 =cut
70 sub BreedingSearch {
71 my ($search,$isbn) = @_;
72 my $dbh = C4::Context->dbh;
73 my $count = 0;
74 my ($query,@bind);
75 my $sth;
76 my @results;
78 # normalise ISBN like at import
79 $isbn = C4::Koha::GetNormalizedISBN($isbn);
81 $query = "SELECT import_record_id, file_name, isbn, title, author
82 FROM import_biblios
83 JOIN import_records USING (import_record_id)
84 JOIN import_batches USING (import_batch_id)
85 WHERE ";
86 @bind=();
87 if (defined($search) && length($search)>0) {
88 $search =~ s/(\s+)/\%/g;
89 $query .= "title like ? OR author like ?";
90 push(@bind,"%$search%", "%$search%");
92 if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
93 $query .= " and ";
95 if (defined($isbn) && length($isbn)>0) {
96 $query .= "isbn like ?";
97 push(@bind,"$isbn%");
99 $sth = $dbh->prepare($query);
100 $sth->execute(@bind);
101 while (my $data = $sth->fetchrow_hashref) {
102 $results[$count] = $data;
103 # FIXME - hack to reflect difference in name
104 # of columns in old marc_breeding and import_records
105 # There needs to be more separation between column names and
106 # field names used in the templates </soapbox>
107 $data->{'file'} = $data->{'file_name'};
108 $data->{'id'} = $data->{'import_record_id'};
109 $count++;
110 } # while
112 $sth->finish;
113 return($count, @results);
114 } # sub breedingsearch
117 =head2 Z3950Search
119 Z3950Search($pars, $template);
121 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
122 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).
123 This code is used in acqui/z3950_search and cataloging/z3950_search.
124 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
126 =cut
128 sub Z3950Search {
129 my ($pars, $template)= @_;
131 my @id= @{$pars->{id}};
132 my $page= $pars->{page};
133 my $biblionumber= $pars->{biblionumber};
135 my $show_next = 0;
136 my $total_pages = 0;
137 my @results;
138 my @breeding_loop = ();
139 my @oConnection;
140 my @oResult;
141 my @errconn;
142 my $s = 0;
143 my $imported=0;
145 my ( $zquery, $squery ) = _bib_build_query( $pars );
147 my $schema = Koha::Database->new()->schema();
148 my $rs = $schema->resultset('Z3950server')->search(
149 { id => [ @id ] },
150 { result_class => 'DBIx::Class::ResultClass::HashRefInflator' },
152 my @servers = $rs->all;
153 foreach my $server ( @servers ) {
154 my $server_zquery = $zquery;
155 if(my $attributes = $server->{attributes}){
156 $server_zquery = "$attributes $zquery";
158 $oConnection[$s] = _create_connection( $server );
159 $oResult[$s] =
160 $server->{servertype} eq 'zed'?
161 $oConnection[$s]->search_pqf( $server_zquery ):
162 $oConnection[$s]->search(new ZOOM::Query::CQL(
163 _translate_query( $server, $squery )));
164 $s++;
166 my $xslh = Koha::XSLT::Base->new;
168 my $nremaining = $s;
169 while ( $nremaining-- ) {
170 my $k;
171 my $event;
172 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
173 $event = $oConnection[ $k - 1 ]->last_event();
174 last if $event == ZOOM::Event::ZEND;
177 if ( $k != 0 ) {
178 $k--;
179 my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
180 if ($error) {
181 if ($error =~ m/^(10000|10007)$/ ) {
182 push(@errconn, { server => $servers[$k]->{host}, error => $error } );
185 else {
186 my $numresults = $oResult[$k]->size();
187 my $i;
188 my $res;
189 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
190 $show_next = 1 if $numresults >= ($page*20);
191 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
192 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
193 if ( $oResult[$k]->record($i) ) {
194 undef $error;
195 ( $res, $error ) = _handle_one_result( $oResult[$k]->record($i), $servers[$k], ++$imported, $biblionumber, $xslh ); #ignores error in sequence numbering
196 push @breeding_loop, $res if $res;
197 push @errconn, { server => $servers[$k]->{servername}, error => $error, seq => $i+1 } if $error;
199 else {
200 push @errconn, { 'server' => $servers[$k]->{servername}, error => ( ( $oConnection[$k]->error_x() )[0] ), seq => $i+1 };
203 } #if $numresults
205 } # if $k !=0
207 $template->param(
208 numberpending => $nremaining,
209 current_page => $page,
210 total_pages => $total_pages,
211 show_nextbutton => $show_next?1:0,
212 show_prevbutton => $page!=1,
214 } # while nremaining
216 #close result sets and connections
217 foreach(0..$s-1) {
218 $oResult[$_]->destroy();
219 $oConnection[$_]->destroy();
222 $template->param(
223 breeding_loop => \@breeding_loop,
224 servers => \@servers,
225 errconn => \@errconn
229 sub _auth_build_query {
230 my ( $pars ) = @_;
232 my $qry_build = {
233 nameany => '@attr 1=1002 "#term" ',
234 authorany => '@attr 1=1003 "#term" ',
235 authorcorp => '@attr 1=2 "#term" ',
236 authorpersonal => '@attr 1=1 "#term" ',
237 authormeetingcon => '@attr 1=3 "#term" ',
238 subject => '@attr 1=21 "#term" ',
239 subjectsubdiv => '@attr 1=47 "#term" ',
240 title => '@attr 1=4 "#term" ',
241 uniformtitle => '@attr 1=6 "#term" ',
242 srchany => '@attr 1=1016 "#term" ',
243 controlnumber => '@attr 1=12 "#term" ',
246 return _build_query( $pars, $qry_build );
249 sub _bib_build_query {
251 my ( $pars ) = @_;
253 my $qry_build = {
254 isbn => '@attr 1=7 @attr 5=1 "#term" ',
255 issn => '@attr 1=8 @attr 5=1 "#term" ',
256 title => '@attr 1=4 "#term" ',
257 author => '@attr 1=1003 "#term" ',
258 dewey => '@attr 1=16 "#term" ',
259 subject => '@attr 1=21 "#term" ',
260 lccall => '@attr 1=16 @attr 2=3 @attr 3=1 @attr 4=1 @attr 5=1 '.
261 '@attr 6=1 "#term" ',
262 controlnumber => '@attr 1=12 "#term" ',
263 srchany => '@attr 1=1016 "#term" ',
264 stdid => '@attr 1=1007 "#term" ',
265 publicationyear => '@attr 1=31 "#term" '
268 return _build_query( $pars, $qry_build );
271 sub _build_query {
273 my ( $pars, $qry_build ) = @_;
275 my $zquery='';
276 my $squery='';
277 my $nterms=0;
278 foreach my $k ( sort keys %$pars ) {
279 #note that the sort keys forces an identical result under Perl 5.18
280 #one of the unit tests is based on that assumption
281 if( ( my $val=$pars->{$k} ) && $qry_build->{$k} ) {
282 $qry_build->{$k} =~ s/#term/$val/g;
283 $zquery .= $qry_build->{$k};
284 $squery .= "[$k]=\"$val\" and ";
285 $nterms++;
288 $zquery = "\@and " . $zquery for 2..$nterms;
289 $squery =~ s/ and $//;
290 return ( $zquery, $squery );
293 sub _handle_one_result {
294 my ( $zoomrec, $servhref, $seq, $bib, $xslh )= @_;
296 my $raw= $zoomrec->raw();
297 my $marcrecord;
298 if( $servhref->{servertype} eq 'sru' ) {
299 $marcrecord= MARC::Record->new_from_xml( $raw, 'UTF-8',
300 $servhref->{syntax} );
301 $marcrecord->encoding('UTF-8');
302 } else {
303 ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encoding} // "iso-5426" ); #ignores charset return values
305 SetUTF8Flag($marcrecord);
306 my $error;
307 ( $marcrecord, $error ) = _do_xslt_proc($marcrecord, $servhref, $xslh);
309 my $batch_id = GetZ3950BatchId($servhref->{servername});
310 my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0);
311 #Last zero indicates: no update for batch record counts
314 #call to TransformMarcToKoha replaced by next call
315 #we only need six fields from the marc record
316 my $row;
317 $row = _add_rowdata(
319 biblionumber => $bib,
320 server => $servhref->{servername},
321 breedingid => $breedingid,
322 }, $marcrecord) if $breedingid;
323 return ( $row, $error );
326 sub _do_xslt_proc {
327 my ( $marc, $server, $xslh ) = @_;
328 return $marc if !$server->{add_xslt};
330 my $htdocs = C4::Context->config('intrahtdocs');
331 my $theme = C4::Context->preference("template"); #staff
332 my $lang = C4::Languages::getlanguage() || 'en';
334 my @files= split ',', $server->{add_xslt};
335 my $xml = $marc->as_xml;
336 foreach my $f ( @files ) {
337 $f =~ s/^\s+//; $f =~ s/\s+$//; next if !$f;
338 $f = C4::XSLT::_get_best_default_xslt_filename(
339 $htdocs, $theme, $lang, $f ) unless $f =~ /^\//;
340 $xml = $xslh->transform( $xml, $f );
341 last if $xslh->err; #skip other files
343 if( !$xslh->err ) {
344 return MARC::Record->new_from_xml($xml, 'UTF-8');
345 } else {
346 return ( $marc, $xslh->err ); #original record in case of errors
350 sub _add_rowdata {
351 my ($row, $record)=@_;
352 my %fetch= (
353 title => 'biblio.title',
354 author => 'biblio.author',
355 isbn =>'biblioitems.isbn',
356 lccn =>'biblioitems.lccn', #LC control number (not call number)
357 edition =>'biblioitems.editionstatement'
359 $fetch{date} = C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
361 foreach my $k (keys %fetch) {
362 $row->{$k} = C4::Biblio::TransformMarcToKohaOneField( $fetch{$k}, $record );
364 $row->{date}//= $row->{date2};
365 $row->{isbn}=_isbn_replace($row->{isbn});
367 $row = _add_custom_field_rowdata($row, $record);
369 return $row;
372 sub _add_custom_field_rowdata
374 my ( $row, $record ) = @_;
375 my $pref_newtags = C4::Context->preference('AdditionalFieldsInZ3950ResultSearch');
376 my $pref_flavour = C4::Context->preference('MarcFlavour');
378 $pref_newtags =~ s/^\s+|\s+$//g;
379 $pref_newtags =~ s/\h+/ /g;
381 my @addnumberfields;
383 foreach my $field (split /\,/, $pref_newtags) {
384 $field =~ s/^\s+|\s+$//g ; # trim whitespace
385 my ($tag, $subtags) = split(/\$/, $field);
387 if ( $record->field($tag) ) {
388 my @content = ();
390 for my $marcfield ($record->field($tag)) {
391 if ( $subtags ) {
392 my $str = '';
393 for my $code (split //, $subtags) {
394 if ( $marcfield->subfield($code) ) {
395 $str .= $marcfield->subfield($code) . ' ';
398 if ( not $str eq '') {
399 push @content, $str;
401 } elsif ( $tag == 10 ) {
402 push @content, ( $pref_flavour eq "MARC21" ? $marcfield->data : $marcfield->as_string );
403 } elsif ( $tag < 10 ) {
404 push @content, $marcfield->data();
405 } else {
406 push @content, $marcfield->as_string();
410 if ( @content ) {
411 $row->{$field} = \@content;
412 push( @addnumberfields, $field );
417 $row->{'addnumberfields'} = \@addnumberfields;
419 return $row;
422 sub _isbn_replace {
423 my ($isbn) = @_;
424 return unless defined $isbn;
425 $isbn =~ s/ |-|\.//g;
426 $isbn =~ s/\|/ \| /g;
427 $isbn =~ s/\(/ \(/g;
428 return $isbn;
431 sub _create_connection {
432 my ( $server ) = @_;
433 my $option1= new ZOOM::Options();
434 $option1->option( 'async' => 1 );
435 $option1->option( 'elementSetName', 'F' );
436 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
437 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
439 if( $server->{servertype} eq 'sru' ) {
440 foreach( split ',', $server->{sru_options}//'' ) {
441 #first remove surrounding spaces at comma and equals-sign
442 s/^\s+|\s+$//g;
443 my @temp= split '=', $_, 2;
444 @temp= map { my $c=$_; $c=~s/^\s+|\s+$//g; $c; } @temp;
445 $option1->option( $temp[0] => $temp[1] ) if @temp;
447 } elsif( $server->{servertype} eq 'zed' ) {
448 $option1->option( 'databaseName', $server->{db} );
449 $option1->option( 'user', $server->{userid} ) if $server->{userid};
450 $option1->option( 'password', $server->{password} ) if $server->{password};
452 my $obj= ZOOM::Connection->create($option1);
453 if( $server->{servertype} eq 'sru' ) {
454 my $host= $server->{host};
455 if( $host !~ /^https?:\/\// ) {
456 #Normally, host will not be prefixed by protocol.
457 #In that case we can (safely) assume http.
458 #In case someone prefixed with https, give it a try..
459 $host = 'http://' . $host;
461 $obj->connect( $host.':'.$server->{port}.'/'.$server->{db} );
462 } else {
463 $obj->connect( $server->{host}, $server->{port} );
465 return $obj;
468 sub _translate_query { #SRU query adjusted per server cf. srufields column
469 my ($server, $query) = @_;
471 #sru_fields is in format title=field,isbn=field,...
472 #if a field doesn't exist, try anywhere or remove [field]=
473 my @parts= split(',', $server->{sru_fields} );
474 my %trans= map { if( /=/ ) { ( $`,$' ) } else { () } } @parts;
475 my $any= $trans{srchany}?$trans{srchany}.'=':'';
477 my $q=$query;
478 foreach my $key (keys %trans) {
479 my $f=$trans{$key};
480 if( $f ) {
481 $q=~s/\[$key\]/$f/g;
482 } else {
483 $q=~s/\[$key\]=/$any/g;
486 $q=~s/\[\w+\]=/$any/g; # remove remaining fields (not found in field list)
487 return $q;
490 =head2 ImportBreedingAuth
492 ImportBreedingAuth( $marcrecord, $filename, $encoding, $heading );
494 ImportBreedingAuth imports MARC records in the reservoir (import_records table) or returns their id if they already exist.
496 =cut
498 sub ImportBreedingAuth {
499 my ( $marcrecord, $filename, $encoding, $heading ) = @_;
500 my $dbh = C4::Context->dbh;
502 my $batch_id = GetZ3950BatchId($filename);
503 my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
505 my $controlnumber = $marcrecord->field('001')->data;
507 # Normalize the record so it doesn't have separated diacritics
508 SetUTF8Flag($marcrecord);
510 $searchbreeding->execute($controlnumber,$heading);
511 my ($breedingid) = $searchbreeding->fetchrow;
513 return $breedingid if $breedingid;
514 $breedingid = AddAuthToBatch($batch_id, 0, $marcrecord, $encoding);
515 return $breedingid;
518 =head2 Z3950SearchAuth
520 Z3950SearchAuth($pars, $template);
522 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
523 title, uniform title, subject, subjectsubdiv, srchany.
524 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
525 This code is used in cataloging/z3950_auth_search.
526 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
528 =cut
530 sub Z3950SearchAuth {
531 my ($pars, $template)= @_;
533 my $dbh = C4::Context->dbh;
534 my @id= @{$pars->{id}};
535 my $page= $pars->{page};
538 my $show_next = 0;
539 my $total_pages = 0;
540 my @encoding;
541 my @results;
542 my @serverhost;
543 my @breeding_loop = ();
544 my @oConnection;
545 my @oResult;
546 my @errconn;
547 my @servers;
548 my $s = 0;
549 my $query;
550 my $nterms=0;
552 my $marcflavour = C4::Context->preference('marcflavour');
553 my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour;
554 my $authid= $pars->{authid};
555 my ( $zquery, $squery ) = _auth_build_query( $pars );
556 foreach my $servid (@id) {
557 my $sth = $dbh->prepare("select * from z3950servers where id=?");
558 $sth->execute($servid);
559 while ( my $server = $sth->fetchrow_hashref ) {
560 $oConnection[$s] = _create_connection( $server );
562 if ( $server->{servertype} eq 'zed' ) {
563 my $server_zquery = $zquery;
564 if ( my $attributes = $server->{attributes} ) {
565 $server_zquery = "$attributes $zquery";
567 $oResult[$s] = $oConnection[$s]->search_pqf( $server_zquery );
569 else {
570 $oResult[$s] = $oConnection[$s]->search(
571 new ZOOM::Query::CQL(_translate_query( $server, $squery ))
574 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
575 $servers[$s] = $server;
576 $s++;
577 } ## while fetch
578 } # foreach
579 my $nremaining = $s;
581 while ( $nremaining-- ) {
582 my $k;
583 my $event;
584 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
585 $event = $oConnection[ $k - 1 ]->last_event();
586 last if $event == ZOOM::Event::ZEND;
589 if ( $k != 0 ) {
590 $k--;
591 my ($error )= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
592 if ($error) {
593 if ($error =~ m/^(10000|10007)$/ ) {
594 push(@errconn, {'server' => $serverhost[$k]});
597 else {
598 my $numresults = $oResult[$k]->size();
599 my $i;
600 my $result = '';
601 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
602 $show_next = 1 if $numresults >= ($page*20);
603 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
604 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
605 my $rec = $oResult[$k]->record($i);
606 if ($rec) {
607 my $marcrecord;
608 my $marcdata;
609 $marcdata = $rec->raw();
611 my ($charset_result, $charset_errors);
612 if( $servers[$k]->{servertype} eq 'sru' ) {
613 $marcrecord = MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} );
614 $marcrecord->encoding('UTF-8');
615 } else {
616 ( $marcrecord, $charset_result, $charset_errors ) = MarcToUTF8Record( $marcdata, $marc_type, $encoding[$k] );
618 my $heading;
619 my $heading_authtype_code;
620 $heading_authtype_code = GuessAuthTypeCode($marcrecord);
621 $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
623 my $breedingid = ImportBreedingAuth( $marcrecord, $serverhost[$k], $encoding[$k], $heading );
624 my %row_data;
625 $row_data{server} = $servers[$k]->{'servername'};
626 $row_data{breedingid} = $breedingid;
627 $row_data{heading} = $heading;
628 $row_data{authid} = $authid;
629 $row_data{heading_code} = $heading_authtype_code;
630 push( @breeding_loop, \%row_data );
632 else {
633 push(@breeding_loop,{'server'=>$servers[$k]->{'servername'},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'authid'=>-1});
636 } #if $numresults
638 } # if $k !=0
640 $template->param(
641 numberpending => $nremaining,
642 current_page => $page,
643 total_pages => $total_pages,
644 show_nextbutton => $show_next?1:0,
645 show_prevbutton => $page!=1,
647 } # while nremaining
649 #close result sets and connections
650 foreach(0..$s-1) {
651 $oResult[$_]->destroy();
652 $oConnection[$_]->destroy();
655 @servers = ();
656 foreach my $id (@id) {
657 push @servers, {id => $id};
659 $template->param(
660 breeding_loop => \@breeding_loop,
661 servers => \@servers,
662 errconn => \@errconn
667 __END__