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
::_isbn_cleanup
($oldbiblio->{isbn
}); # FIXME C4::Koha::_isbn_cleanup should be public
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 $query = "SELECT import_record_id, file_name, isbn, title, author
180 JOIN import_records USING (import_record_id)
181 JOIN import_batches USING (import_batch_id)
184 $query .= "z3950random = ?";
185 @bind=($z3950random);
188 if (defined($search) && length($search)>0) {
189 $search =~ s/(\s+)/\%/g;
190 $query .= "title like ? OR author like ?";
191 push(@bind,"%$search%", "%$search%");
193 if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
196 if (defined($isbn) && length($isbn)>0) {
197 $query .= "isbn like ?";
198 push(@bind,"$isbn%");
201 $sth = $dbh->prepare($query);
202 $sth->execute(@bind);
203 while (my $data = $sth->fetchrow_hashref) {
204 $results[$count] = $data;
205 # FIXME - hack to reflect difference in name
206 # of columns in old marc_breeding and import_records
207 # There needs to be more separation between column names and
208 # field names used in the templates </soapbox>
209 $data->{'file'} = $data->{'file_name'};
210 $data->{'id'} = $data->{'import_record_id'};
215 return($count, @results);
216 } # sub breedingsearch
221 Z3950Search($pars, $template);
223 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
224 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).
225 This code is used in acqui/z3950_search and cataloging/z3950_search.
226 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
231 my ($pars, $template)= @_;
233 my @id= @
{$pars->{id
}};
234 my $page= $pars->{page
};
235 my $biblionumber= $pars->{biblionumber
};
236 my $isbn= $pars->{isbn
};
237 my $issn= $pars->{issn
};
238 my $title= $pars->{title
};
239 my $author= $pars->{author
};
240 my $dewey= $pars->{dewey
};
241 my $subject= $pars->{subject
};
242 my $lccn= $pars->{lccn
};
243 my $lccall= $pars->{lccall
};
244 my $controlnumber= $pars->{controlnumber
};
245 my $srchany= $pars->{srchany
};
246 my $stdid= $pars->{stdid
};
252 my @breeding_loop = ();
260 my @serverinfo; #replaces former serverhost, servername, encoding
264 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
269 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
273 $query .= " \@attr 1=4 \"$title\" ";
277 $query .= " \@attr 1=1003 \"$author\" ";
281 $query .= " \@attr 1=16 \"$dewey\" ";
285 $query .= " \@attr 1=21 \"$subject\" ";
289 $query .= " \@attr 1=9 $lccn ";
293 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
296 if ($controlnumber) {
297 $query .= " \@attr 1=12 \"$controlnumber\" ";
301 $query .= " \@attr 1=1016 \"$srchany\" ";
305 $query .= " \@attr 1=1007 \"$stdid\" ";
308 for my $i (1..$nterms-1) {
309 $query = "\@and " . $query;
312 my $dbh = C4
::Context
->dbh;
313 foreach my $servid (@id) {
314 my $sth = $dbh->prepare("select * from z3950servers where id=?");
315 $sth->execute($servid);
316 while (my $server = $sth->fetchrow_hashref) {
317 my $option1= new ZOOM
::Options
();
318 $option1->option( 'async' => 1 );
319 $option1->option( 'elementSetName', 'F' );
320 $option1->option( 'databaseName', $server->{db
} );
321 $option1->option( 'user', $server->{userid
} ) if $server->{userid
};
322 $option1->option( 'password', $server->{password
} ) if $server->{password
};
323 $option1->option( 'preferredRecordSyntax', $server->{syntax
} );
324 $option1->option( 'timeout', $server->{timeout
} ) if $server->{timeout
};
325 $oConnection[$s]= create ZOOM
::Connection
($option1);
326 $oConnection[$s]->connect( $server->{host
}, $server->{port
} );
327 $serverinfo[$s]->{host
}= $server->{host
};
328 $serverinfo[$s]->{name
}= $server->{name
};
329 $serverinfo[$s]->{encd
}= $server->{encoding
} // "iso-5426";
335 for ( my $z = 0 ; $z < $s ; $z++ ) {
336 $oResult[$z] = $oConnection[$z]->search_pqf($query);
339 while ( $nremaining-- ) {
342 while ( ( $k = ZOOM
::event
( \
@oConnection ) ) != 0 ) {
343 $event = $oConnection[ $k - 1 ]->last_event();
344 last if $event == ZOOM
::Event
::ZEND
;
349 my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
351 if ($error =~ m/^(10000|10007)$/ ) {
352 push(@errconn, { 'server' => $serverinfo[$k]->{host
} } );
356 my $numresults = $oResult[$k]->size();
359 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
360 $show_next = 1 if $numresults >= ($page*20);
361 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
362 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ?
$numresults : ($page*20)); $i++) {
363 if($oResult[$k]->record($i)) {
364 my $res=_handle_one_result
($oResult[$k]->record($i), $serverinfo[$k], ++$imported, $biblionumber); #ignores error in sequence numbering
365 push @breeding_loop, $res if $res;
368 push(@breeding_loop,{'server'=>$serverinfo[$k]->{name
},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
376 numberpending
=> $nremaining,
377 current_page
=> $page,
378 total_pages
=> $total_pages,
379 show_nextbutton
=> $show_next?
1:0,
380 show_prevbutton
=> $page!=1,
384 #close result sets and connections
386 $oResult[$_]->destroy();
387 $oConnection[$_]->destroy();
391 foreach my $id (@id) {
392 push @servers, {id
=> $id};
395 breeding_loop
=> \
@breeding_loop,
396 servers
=> \
@servers,
401 sub _handle_one_result
{
402 my ($zoomrec, $servhref, $seq, $bib)= @_;
404 my $raw= $zoomrec->raw();
405 my ($marcrecord) = MarcToUTF8Record
($raw, C4
::Context
->preference('marcflavour'), $servhref->{encd
}); #ignores charset return values
406 SetUTF8Flag
($marcrecord);
408 #call to ImportBreeding replaced by next two calls for optimization
409 my $batch_id = GetZ3950BatchId
($servhref->{name
});
410 my $breedingid = AddBiblioToBatch
($batch_id, $seq, $marcrecord, 'UTF-8', 0, 0);
411 #FIXME passing 0 for z3950random
412 #Will eliminate this unused field in a followup report
413 #Last zero indicates: no update for batch record counts
416 #call to TransformMarcToKoha replaced by next call
417 #we only need six fields from the marc record
420 biblionumber
=> $bib,
421 server
=> $servhref->{name
},
422 breedingid
=> $breedingid,
423 }, $marcrecord) if $breedingid;
427 my ($row, $record)=@_;
429 title
=> 'biblio.title',
430 author
=> 'biblio.author',
431 isbn
=>'biblioitems.isbn',
432 lccn
=>'biblioitems.lccn', #LC control number (not call number)
433 edition
=>'biblioitems.editionstatement',
434 date
=> 'biblio.copyrightdate', #MARC21
435 date2
=> 'biblioitems.publicationyear', #UNIMARC
437 foreach my $k (keys %fetch) {
438 my ($t, $f)= split '\.', $fetch{$k};
439 $row= C4
::Biblio
::TransformMarcToKohaOneField
($t, $f, $record, $row);
440 $row->{$k}= $row->{$f} if $k ne $f;
442 $row->{date
}//= $row->{date2
};
443 $row->{isbn
}=_isbn_replace
($row->{isbn
});
449 return unless defined $isbn;
450 $isbn =~ s/ |-|\.//g;
451 $isbn =~ s/\|/ \| /g;
456 =head2 ImportBreedingAuth
458 ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type);
460 ImportBreedingAuth imports MARC records in the reservoir (import_records table).
461 ImportBreedingAuth is based on the ImportBreeding subroutine.
465 sub ImportBreedingAuth
{
466 my ($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type) = @_;
467 my @marcarray = split /\x1D/, $marcrecords;
469 my $dbh = C4
::Context
->dbh;
471 my $batch_id = GetZ3950BatchId
($filename);
472 my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
474 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
475 # fields used for import results
478 my $alreadyinfarm = 0;
479 my $notmarcrecord = 0;
481 for (my $i=0;$i<=$#marcarray;$i++) {
482 my ($marcrecord, $charset_result, $charset_errors);
483 ($marcrecord, $charset_result, $charset_errors) =
484 MarcToUTF8Record
($marcarray[$i]."\x1D", C4
::Context
->preference("marcflavour"), $encoding);
486 # Normalize the record so it doesn't have separated diacritics
487 SetUTF8Flag
($marcrecord);
489 if (scalar($marcrecord->fields()) == 0) {
493 $heading = C4
::AuthoritiesMarc
::GetAuthorizedHeading
({ record
=> $marcrecord });
495 my $heading_authtype_code;
496 $heading_authtype_code = GuessAuthTypeCode
($marcrecord);
499 $controlnumber = $marcrecord->field('001')->data;
501 #Check if the authority record already exists in the database...
502 my ($duplicateauthid,$duplicateauthvalue);
503 if ($marcrecord && $heading_authtype_code) {
504 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority
( $marcrecord, $heading_authtype_code);
507 if ($duplicateauthid && $overwrite_auth ne 2) {
508 #If the authority record exists and $overwrite_auth doesn't equal 2, then mark it as already in the DB
511 if ($controlnumber && $heading) {
512 $searchbreeding->execute($controlnumber,$heading);
513 ($breedingid) = $searchbreeding->fetchrow;
515 if ($breedingid && $overwrite_auth eq '0') {
518 if ($breedingid && $overwrite_auth eq '1') {
519 ModAuthorityInBatch
($breedingid, $marcrecord);
521 my $import_id = AddAuthToBatch
($batch_id, $imported, $marcrecord, $encoding, $z3950random);
522 $breedingid = $import_id;
529 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
532 =head2 Z3950SearchAuth
534 Z3950SearchAuth($pars, $template);
536 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
537 title, uniform title, subject, subjectsubdiv, srchany.
538 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
539 This code is used in cataloging/z3950_auth_search.
540 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
544 sub Z3950SearchAuth
{
545 my ($pars, $template)= @_;
547 my $dbh = C4
::Context
->dbh;
548 my @id= @
{$pars->{id
}};
549 my $random= $pars->{random
};
550 my $page= $pars->{page
};
552 my $nameany= $pars->{nameany
};
553 my $authorany= $pars->{authorany
};
554 my $authorpersonal= $pars->{authorpersonal
};
555 my $authorcorp= $pars->{authorcorp
};
556 my $authormeetingcon= $pars->{authormeetingcon
};
557 my $title= $pars->{title
};
558 my $uniformtitle= $pars->{uniformtitle
};
559 my $subject= $pars->{subject
};
560 my $subjectsubdiv= $pars->{subjectsubdiv
};
561 my $srchany= $pars->{srchany
};
577 my @breeding_loop = ();
587 $query .= " \@attr 1=1002 \"$nameany\" "; #Any name (this includes personal, corporate, meeting/conference authors, and author names in subject headings)
588 #This attribute is supported by both the Library of Congress and Libraries Australia 08/05/2013
593 $query .= " \@attr 1=1003 \"$authorany\" "; #Author-name (this includes personal, corporate, meeting/conference authors, but not author names in subject headings)
594 #This attribute is not supported by the Library of Congress, but is supported by Libraries Australia 08/05/2013
599 $query .= " \@attr 1=2 \"$authorcorp\" "; #1005 is another valid corporate author attribute...
603 if ($authorpersonal) {
604 $query .= " \@attr 1=1 \"$authorpersonal\" "; #1004 is another valid personal name attribute...
608 if ($authormeetingcon) {
609 $query .= " \@attr 1=3 \"$authormeetingcon\" "; #1006 is another valid meeting/conference name attribute...
614 $query .= " \@attr 1=21 \"$subject\" ";
618 if ($subjectsubdiv) {
619 $query .= " \@attr 1=47 \"$subjectsubdiv\" ";
624 $query .= " \@attr 1=4 \"$title\" "; #This is a regular title search. 1=6 will give just uniform titles
629 $query .= " \@attr 1=6 \"$uniformtitle\" "; #This is the uniform title search
634 $query .= " \@attr 1=1016 \"$srchany\" ";
638 for my $i (1..$nterms-1) {
639 $query = "\@and " . $query;
642 foreach my $servid (@id) {
643 my $sth = $dbh->prepare("select * from z3950servers where id=?");
644 $sth->execute($servid);
645 while ( $server = $sth->fetchrow_hashref ) {
646 my $option1 = new ZOOM
::Options
();
647 $option1->option( 'async' => 1 );
648 $option1->option( 'elementSetName', 'F' );
649 $option1->option( 'databaseName', $server->{db
} );
650 $option1->option( 'user', $server->{userid
} ) if $server->{userid
};
651 $option1->option( 'password', $server->{password
} ) if $server->{password
};
652 $option1->option( 'preferredRecordSyntax', $server->{syntax
} );
653 $option1->option( 'timeout', $server->{timeout
} ) if $server->{timeout
};
654 $oConnection[$s] = create ZOOM
::Connection
($option1);
655 $oConnection[$s]->connect( $server->{host
}, $server->{port
} );
656 $serverhost[$s] = $server->{host
};
657 $servername[$s] = $server->{name
};
658 $encoding[$s] = ($server->{encoding
}?
$server->{encoding
}:"iso-5426");
664 for ( my $z = 0 ; $z < $s ; $z++ ) {
665 $oResult[$z] = $oConnection[$z]->search_pqf($query);
668 while ( $nremaining-- ) {
671 while ( ( $k = ZOOM
::event
( \
@oConnection ) ) != 0 ) {
672 $event = $oConnection[ $k - 1 ]->last_event();
673 last if $event == ZOOM
::Event
::ZEND
;
678 my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
680 if ($error =~ m/^(10000|10007)$/ ) {
681 push(@errconn, {'server' => $serverhost[$k]});
685 my $numresults = $oResult[$k]->size();
688 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
689 $show_next = 1 if $numresults >= ($page*20);
690 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
691 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ?
$numresults : ($page*20)); $i++) {
692 my $rec = $oResult[$k]->record($i);
696 $marcdata = $rec->raw();
698 my ($charset_result, $charset_errors);
699 ($marcrecord, $charset_result, $charset_errors)= MarcToUTF8Record
($marcdata, C4
::Context
->preference('marcflavour'), $encoding[$k]);
702 my $heading_authtype_code;
703 $heading_authtype_code = GuessAuthTypeCode
($marcrecord);
704 $heading = C4
::AuthoritiesMarc
::GetAuthorizedHeading
({ record
=> $marcrecord });
706 my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth
( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
708 $row_data{server
} = $servername[$k];
709 $row_data{breedingid
} = $breedingid;
710 $row_data{heading
} = $heading;
711 $row_data{heading_code
} = $heading_authtype_code;
712 push( @breeding_loop, \
%row_data );
715 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1});
723 numberpending
=> $nremaining,
724 current_page
=> $page,
725 total_pages
=> $total_pages,
726 show_nextbutton
=> $show_next?
1:0,
727 show_prevbutton
=> $page!=1,
731 #close result sets and connections
733 $oResult[$_]->destroy();
734 $oConnection[$_]->destroy();
738 foreach my $id (@id) {
739 push @servers, {id
=> $id};
742 breeding_loop
=> \
@breeding_loop,
743 servers
=> \
@servers,