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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 use MARC::File::USMARC;
29 use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
34 # set the version for version checking
35 $VERSION = 3.07.00.049;
38 @EXPORT = qw(&ImportBreeding &BreedingSearch &Z3950Search &Z3950SearchAuth);
43 C4::Breeding : module to add biblios to import_records via
44 the breeding/reservoir API.
49 &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type);
51 C<$marcrecord> => the MARC::Record
52 C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
53 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
54 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN
55 possible in the breeding
56 C<$encoding> => USMARC
57 or UNIMARC. used for char_decoding.
58 If not present, the parameter marcflavour is used instead
59 C<$z3950random> => the random value created during a z3950 search result.
63 ImportBreeding import MARC records in the reservoir (import_records/import_batches tables).
64 the records can be properly encoded or not, we try to reencode them in utf-8 if needed.
65 works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too.
69 ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type);
76 my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_;
77 my @marcarray = split /\x1D/, $marcrecords;
79 my $dbh = C4::Context->dbh;
81 my $batch_id = GetZ3950BatchId($filename);
82 my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
83 my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
84 # FIXME -- not sure that this kind of checking is actually needed
85 my $searchbreeding = $dbh->prepare("select import_record_id from import_biblios where isbn=? and title=?");
87 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
88 # fields used for import results
91 my $alreadyinfarm = 0;
92 my $notmarcrecord = 0;
94 for (my $i=0;$i<=$#marcarray;$i++) {
95 my ($marcrecord, $charset_result, $charset_errors);
96 ($marcrecord, $charset_result, $charset_errors) =
97 MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
99 # Normalize the record so it doesn't have separated diacritics
100 SetUTF8Flag($marcrecord);
102 # warn "$i : $marcarray[$i]";
103 # FIXME - currently this does nothing
104 my @warnings = $marcrecord->warnings();
106 if (scalar($marcrecord->fields()) == 0) {
109 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
110 # if isbn found and biblio does not exist, add it. If isbn found and biblio exists,
111 # overwrite or ignore depending on user choice
112 # drop every "special" char : spaces, - ...
113 $oldbiblio->{isbn} = C4::Koha::GetNormalizedISBN($oldbiblio->{isbn});
114 # search if biblio exists
115 my $biblioitemnumber;
116 if ($oldbiblio->{isbn}) {
117 $searchisbn->execute($oldbiblio->{isbn});
118 ($biblioitemnumber) = $searchisbn->fetchrow;
120 if ($oldbiblio->{issn}) {
121 $searchissn->execute($oldbiblio->{issn});
122 ($biblioitemnumber) = $searchissn->fetchrow;
125 if ($biblioitemnumber && $overwrite_biblio ne 2) {
128 # FIXME - in context of batch load,
129 # rejecting records because already present in the reservoir
130 # not correct in every case.
131 # search in breeding farm
132 if ($oldbiblio->{isbn}) {
133 $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
134 ($breedingid) = $searchbreeding->fetchrow;
135 } elsif ($oldbiblio->{issn}){
136 $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
137 ($breedingid) = $searchbreeding->fetchrow;
139 if ($breedingid && $overwrite_biblio eq '0') {
142 if ($breedingid && $overwrite_biblio eq '1') {
143 ModBiblioInBatch($breedingid, $marcrecord);
145 my $import_id = AddBiblioToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
146 $breedingid = $import_id;
153 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
157 =head2 BreedingSearch
159 ($count, @results) = &BreedingSearch($title,$isbn,$random);
160 C<$title> contains the title,
161 C<$isbn> contains isbn or issn,
162 C<$random> contains the random seed from a z3950 search.
164 C<$count> is the number of items in C<@results>. C<@results> is an
165 array of references-to-hash; the keys are the items from the C<import_records> and
166 C<import_biblios> tables of the Koha database.
171 my ($search,$isbn,$z3950random) = @_;
172 my $dbh = C4::Context->dbh;
178 # normalise ISBN like at import
179 $isbn = C4::Koha::GetNormalizedISBN($isbn);
181 $query = "SELECT import_record_id, file_name, isbn, title, author
183 JOIN import_records USING (import_record_id)
184 JOIN import_batches USING (import_batch_id)
187 $query .= "z3950random = ?";
188 @bind=($z3950random);
191 if (defined($search) && length($search)>0) {
192 $search =~ s/(\s+)/\%/g;
193 $query .= "title like ? OR author like ?";
194 push(@bind,"%$search%", "%$search%");
196 if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
199 if (defined($isbn) && length($isbn)>0) {
200 $query .= "isbn like ?";
201 push(@bind,"$isbn%");
204 $sth = $dbh->prepare($query);
205 $sth->execute(@bind);
206 while (my $data = $sth->fetchrow_hashref) {
207 $results[$count] = $data;
208 # FIXME - hack to reflect difference in name
209 # of columns in old marc_breeding and import_records
210 # There needs to be more separation between column names and
211 # field names used in the templates </soapbox>
212 $data->{'file'} = $data->{'file_name'};
213 $data->{'id'} = $data->{'import_record_id'};
218 return($count, @results);
219 } # sub breedingsearch
224 Z3950Search($pars, $template);
226 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
227 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).
228 This code is used in acqui/z3950_search and cataloging/z3950_search.
229 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
234 my ($pars, $template)= @_;
236 my @id= @{$pars->{id}};
237 my $page= $pars->{page};
238 my $biblionumber= $pars->{biblionumber};
239 my $isbn= $pars->{isbn};
240 my $issn= $pars->{issn};
241 my $title= $pars->{title};
242 my $author= $pars->{author};
243 my $dewey= $pars->{dewey};
244 my $subject= $pars->{subject};
245 my $lccn= $pars->{lccn};
246 my $lccall= $pars->{lccall};
247 my $controlnumber= $pars->{controlnumber};
248 my $srchany= $pars->{srchany};
249 my $stdid= $pars->{stdid};
255 my @breeding_loop = ();
263 my @serverinfo; #replaces former serverhost, servername, encoding
267 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
272 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
276 $query .= " \@attr 1=4 \"$title\" ";
280 $query .= " \@attr 1=1003 \"$author\" ";
284 $query .= " \@attr 1=16 \"$dewey\" ";
288 $query .= " \@attr 1=21 \"$subject\" ";
292 $query .= " \@attr 1=9 $lccn ";
296 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
299 if ($controlnumber) {
300 $query .= " \@attr 1=12 \"$controlnumber\" ";
304 $query .= " \@attr 1=1016 \"$srchany\" ";
308 $query .= " \@attr 1=1007 \"$stdid\" ";
311 for my $i (1..$nterms-1) {
312 $query = "\@and " . $query;
315 my $dbh = C4::Context->dbh;
316 foreach my $servid (@id) {
317 my $sth = $dbh->prepare("select * from z3950servers where id=?");
318 $sth->execute($servid);
319 while (my $server = $sth->fetchrow_hashref) {
320 my $option1= new ZOOM::Options();
321 $option1->option( 'async' => 1 );
322 $option1->option( 'elementSetName', 'F' );
323 $option1->option( 'databaseName', $server->{db} );
324 $option1->option( 'user', $server->{userid} ) if $server->{userid};
325 $option1->option( 'password', $server->{password} ) if $server->{password};
326 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
327 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
328 $oConnection[$s]= create ZOOM::Connection($option1);
329 $oConnection[$s]->connect( $server->{host}, $server->{port} );
330 $serverinfo[$s]->{host}= $server->{host};
331 $serverinfo[$s]->{name}= $server->{name};
332 $serverinfo[$s]->{encd}= $server->{encoding} // "iso-5426";
338 for ( my $z = 0 ; $z < $s ; $z++ ) {
339 $oResult[$z] = $oConnection[$z]->search_pqf($query);
342 while ( $nremaining-- ) {
345 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
346 $event = $oConnection[ $k - 1 ]->last_event();
347 last if $event == ZOOM::Event::ZEND;
352 my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
354 if ($error =~ m/^(10000|10007)$/ ) {
355 push(@errconn, { server => $serverinfo[$k]->{host}, error => $error } );
359 my $numresults = $oResult[$k]->size();
362 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
363 $show_next = 1 if $numresults >= ($page*20);
364 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
365 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
366 if($oResult[$k]->record($i)) {
367 my $res=_handle_one_result($oResult[$k]->record($i), $serverinfo[$k], ++$imported, $biblionumber); #ignores error in sequence numbering
368 push @breeding_loop, $res if $res;
371 push(@breeding_loop,{'server'=>$serverinfo[$k]->{name},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
379 numberpending => $nremaining,
380 current_page => $page,
381 total_pages => $total_pages,
382 show_nextbutton => $show_next?1:0,
383 show_prevbutton => $page!=1,
387 #close result sets and connections
389 $oResult[$_]->destroy();
390 $oConnection[$_]->destroy();
394 foreach my $id (@id) {
395 push @servers, {id => $id};
398 breeding_loop => \@breeding_loop,
399 servers => \@servers,
404 sub _handle_one_result {
405 my ($zoomrec, $servhref, $seq, $bib)= @_;
407 my $raw= $zoomrec->raw();
408 my ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encd}); #ignores charset return values
409 SetUTF8Flag($marcrecord);
411 #call to ImportBreeding replaced by next two calls for optimization
412 my $batch_id = GetZ3950BatchId($servhref->{name});
413 my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0, 0);
414 #FIXME passing 0 for z3950random
415 #Will eliminate this unused field in a followup report
416 #Last zero indicates: no update for batch record counts
419 #call to TransformMarcToKoha replaced by next call
420 #we only need six fields from the marc record
423 biblionumber => $bib,
424 server => $servhref->{name},
425 breedingid => $breedingid,
426 }, $marcrecord) if $breedingid;
430 my ($row, $record)=@_;
432 title => 'biblio.title',
433 author => 'biblio.author',
434 isbn =>'biblioitems.isbn',
435 lccn =>'biblioitems.lccn', #LC control number (not call number)
436 edition =>'biblioitems.editionstatement',
437 date => 'biblio.copyrightdate', #MARC21
438 date2 => 'biblioitems.publicationyear', #UNIMARC
440 foreach my $k (keys %fetch) {
441 my ($t, $f)= split '\.', $fetch{$k};
442 $row= C4::Biblio::TransformMarcToKohaOneField($t, $f, $record, $row);
443 $row->{$k}= $row->{$f} if $k ne $f;
445 $row->{date}//= $row->{date2};
446 $row->{isbn}=_isbn_replace($row->{isbn});
452 return unless defined $isbn;
453 $isbn =~ s/ |-|\.//g;
454 $isbn =~ s/\|/ \| /g;
459 =head2 ImportBreedingAuth
461 ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type);
463 ImportBreedingAuth imports MARC records in the reservoir (import_records table).
464 ImportBreedingAuth is based on the ImportBreeding subroutine.
468 sub ImportBreedingAuth {
469 my ($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type) = @_;
470 my @marcarray = split /\x1D/, $marcrecords;
472 my $dbh = C4::Context->dbh;
474 my $batch_id = GetZ3950BatchId($filename);
475 my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
477 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
478 # fields used for import results
481 my $alreadyinfarm = 0;
482 my $notmarcrecord = 0;
484 for (my $i=0;$i<=$#marcarray;$i++) {
485 my ($marcrecord, $charset_result, $charset_errors);
486 ($marcrecord, $charset_result, $charset_errors) =
487 MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
489 # Normalize the record so it doesn't have separated diacritics
490 SetUTF8Flag($marcrecord);
492 if (scalar($marcrecord->fields()) == 0) {
496 $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
498 my $heading_authtype_code;
499 $heading_authtype_code = GuessAuthTypeCode($marcrecord);
502 $controlnumber = $marcrecord->field('001')->data;
504 #Check if the authority record already exists in the database...
505 my ($duplicateauthid,$duplicateauthvalue);
506 if ($marcrecord && $heading_authtype_code) {
507 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority( $marcrecord, $heading_authtype_code);
510 if ($duplicateauthid && $overwrite_auth ne 2) {
511 #If the authority record exists and $overwrite_auth doesn't equal 2, then mark it as already in the DB
514 if ($controlnumber && $heading) {
515 $searchbreeding->execute($controlnumber,$heading);
516 ($breedingid) = $searchbreeding->fetchrow;
518 if ($breedingid && $overwrite_auth eq '0') {
521 if ($breedingid && $overwrite_auth eq '1') {
522 ModAuthorityInBatch($breedingid, $marcrecord);
524 my $import_id = AddAuthToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
525 $breedingid = $import_id;
532 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
535 =head2 Z3950SearchAuth
537 Z3950SearchAuth($pars, $template);
539 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
540 title, uniform title, subject, subjectsubdiv, srchany.
541 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
542 This code is used in cataloging/z3950_auth_search.
543 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
547 sub Z3950SearchAuth {
548 my ($pars, $template)= @_;
550 my $dbh = C4::Context->dbh;
551 my @id= @{$pars->{id}};
552 my $random= $pars->{random};
553 my $page= $pars->{page};
555 my $nameany= $pars->{nameany};
556 my $authorany= $pars->{authorany};
557 my $authorpersonal= $pars->{authorpersonal};
558 my $authorcorp= $pars->{authorcorp};
559 my $authormeetingcon= $pars->{authormeetingcon};
560 my $title= $pars->{title};
561 my $uniformtitle= $pars->{uniformtitle};
562 my $subject= $pars->{subject};
563 my $subjectsubdiv= $pars->{subjectsubdiv};
564 my $srchany= $pars->{srchany};
580 my @breeding_loop = ();
590 $query .= " \@attr 1=1002 \"$nameany\" "; #Any name (this includes personal, corporate, meeting/conference authors, and author names in subject headings)
591 #This attribute is supported by both the Library of Congress and Libraries Australia 08/05/2013
596 $query .= " \@attr 1=1003 \"$authorany\" "; #Author-name (this includes personal, corporate, meeting/conference authors, but not author names in subject headings)
597 #This attribute is not supported by the Library of Congress, but is supported by Libraries Australia 08/05/2013
602 $query .= " \@attr 1=2 \"$authorcorp\" "; #1005 is another valid corporate author attribute...
606 if ($authorpersonal) {
607 $query .= " \@attr 1=1 \"$authorpersonal\" "; #1004 is another valid personal name attribute...
611 if ($authormeetingcon) {
612 $query .= " \@attr 1=3 \"$authormeetingcon\" "; #1006 is another valid meeting/conference name attribute...
617 $query .= " \@attr 1=21 \"$subject\" ";
621 if ($subjectsubdiv) {
622 $query .= " \@attr 1=47 \"$subjectsubdiv\" ";
627 $query .= " \@attr 1=4 \"$title\" "; #This is a regular title search. 1=6 will give just uniform titles
632 $query .= " \@attr 1=6 \"$uniformtitle\" "; #This is the uniform title search
637 $query .= " \@attr 1=1016 \"$srchany\" ";
641 for my $i (1..$nterms-1) {
642 $query = "\@and " . $query;
645 foreach my $servid (@id) {
646 my $sth = $dbh->prepare("select * from z3950servers where id=?");
647 $sth->execute($servid);
648 while ( $server = $sth->fetchrow_hashref ) {
649 my $option1 = new ZOOM::Options();
650 $option1->option( 'async' => 1 );
651 $option1->option( 'elementSetName', 'F' );
652 $option1->option( 'databaseName', $server->{db} );
653 $option1->option( 'user', $server->{userid} ) if $server->{userid};
654 $option1->option( 'password', $server->{password} ) if $server->{password};
655 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
656 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
657 $oConnection[$s] = create ZOOM::Connection($option1);
658 $oConnection[$s]->connect( $server->{host}, $server->{port} );
659 $serverhost[$s] = $server->{host};
660 $servername[$s] = $server->{name};
661 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
667 for ( my $z = 0 ; $z < $s ; $z++ ) {
668 $oResult[$z] = $oConnection[$z]->search_pqf($query);
671 while ( $nremaining-- ) {
674 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
675 $event = $oConnection[ $k - 1 ]->last_event();
676 last if $event == ZOOM::Event::ZEND;
681 my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
683 if ($error =~ m/^(10000|10007)$/ ) {
684 push(@errconn, {'server' => $serverhost[$k]});
688 my $numresults = $oResult[$k]->size();
691 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
692 $show_next = 1 if $numresults >= ($page*20);
693 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
694 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
695 my $rec = $oResult[$k]->record($i);
699 $marcdata = $rec->raw();
701 my ($charset_result, $charset_errors);
702 ($marcrecord, $charset_result, $charset_errors)= MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
705 my $heading_authtype_code;
706 $heading_authtype_code = GuessAuthTypeCode($marcrecord);
707 $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
709 my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
711 $row_data{server} = $servername[$k];
712 $row_data{breedingid} = $breedingid;
713 $row_data{heading} = $heading;
714 $row_data{heading_code} = $heading_authtype_code;
715 push( @breeding_loop, \%row_data );
718 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1});
726 numberpending => $nremaining,
727 current_page => $page,
728 total_pages => $total_pages,
729 show_nextbutton => $show_next?1:0,
730 show_prevbutton => $page!=1,
734 #close result sets and connections
736 $oResult[$_]->destroy();
737 $oConnection[$_]->destroy();
741 foreach my $id (@id) {
742 push @servers, {id => $id};
745 breeding_loop => \@breeding_loop,
746 servers => \@servers,