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(&BreedingSearch &Z3950Search &Z3950SearchAuth);
43 C4::Breeding : module to add biblios to import_records via
44 the breeding/reservoir API.
48 Z3950Search($pars, $template);
49 ($count, @results) = &BreedingSearch($title,$isbn,$random);
53 This module contains routines related to Koha's Z39.50 search into
54 cataloguing reservoir features.
58 ($count, @results) = &BreedingSearch($title,$isbn,$random);
59 C<$title> contains the title,
60 C<$isbn> contains isbn or issn,
61 C<$random> contains the random seed from a z3950 search.
63 C<$count> is the number of items in C<@results>. C<@results> is an
64 array of references-to-hash; the keys are the items from the C<import_records> and
65 C<import_biblios> tables of the Koha database.
70 my ($search,$isbn,$z3950random) = @_;
71 my $dbh = C4::Context->dbh;
77 # normalise ISBN like at import
78 $isbn = C4::Koha::GetNormalizedISBN($isbn);
80 $query = "SELECT import_record_id, file_name, isbn, title, author
82 JOIN import_records USING (import_record_id)
83 JOIN import_batches USING (import_batch_id)
86 $query .= "z3950random = ?";
90 if (defined($search) && length($search)>0) {
91 $search =~ s/(\s+)/\%/g;
92 $query .= "title like ? OR author like ?";
93 push(@bind,"%$search%", "%$search%");
95 if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
98 if (defined($isbn) && length($isbn)>0) {
99 $query .= "isbn like ?";
100 push(@bind,"$isbn%");
103 $sth = $dbh->prepare($query);
104 $sth->execute(@bind);
105 while (my $data = $sth->fetchrow_hashref) {
106 $results[$count] = $data;
107 # FIXME - hack to reflect difference in name
108 # of columns in old marc_breeding and import_records
109 # There needs to be more separation between column names and
110 # field names used in the templates </soapbox>
111 $data->{'file'} = $data->{'file_name'};
112 $data->{'id'} = $data->{'import_record_id'};
117 return($count, @results);
118 } # sub breedingsearch
123 Z3950Search($pars, $template);
125 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
126 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).
127 This code is used in acqui/z3950_search and cataloging/z3950_search.
128 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
133 my ($pars, $template)= @_;
135 my @id= @{$pars->{id}};
136 my $page= $pars->{page};
137 my $biblionumber= $pars->{biblionumber};
138 my $isbn= $pars->{isbn};
139 my $issn= $pars->{issn};
140 my $title= $pars->{title};
141 my $author= $pars->{author};
142 my $dewey= $pars->{dewey};
143 my $subject= $pars->{subject};
144 my $lccn= $pars->{lccn};
145 my $lccall= $pars->{lccall};
146 my $controlnumber= $pars->{controlnumber};
147 my $srchany= $pars->{srchany};
148 my $stdid= $pars->{stdid};
154 my @breeding_loop = ();
162 my @serverinfo; #replaces former serverhost, servername, encoding
166 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
171 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
175 $query .= " \@attr 1=4 \"$title\" ";
179 $query .= " \@attr 1=1003 \"$author\" ";
183 $query .= " \@attr 1=16 \"$dewey\" ";
187 $query .= " \@attr 1=21 \"$subject\" ";
191 $query .= " \@attr 1=9 $lccn ";
195 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
198 if ($controlnumber) {
199 $query .= " \@attr 1=12 \"$controlnumber\" ";
203 $query .= " \@attr 1=1016 \"$srchany\" ";
207 $query .= " \@attr 1=1007 \"$stdid\" ";
210 for my $i (1..$nterms-1) {
211 $query = "\@and " . $query;
214 my $dbh = C4::Context->dbh;
215 foreach my $servid (@id) {
216 my $sth = $dbh->prepare("select * from z3950servers where id=?");
217 $sth->execute($servid);
218 while (my $server = $sth->fetchrow_hashref) {
219 my $option1= new ZOOM::Options();
220 $option1->option( 'async' => 1 );
221 $option1->option( 'elementSetName', 'F' );
222 $option1->option( 'databaseName', $server->{db} );
223 $option1->option( 'user', $server->{userid} ) if $server->{userid};
224 $option1->option( 'password', $server->{password} ) if $server->{password};
225 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
226 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
227 $oConnection[$s]= create ZOOM::Connection($option1);
228 $oConnection[$s]->connect( $server->{host}, $server->{port} );
229 $serverinfo[$s]->{host}= $server->{host};
230 $serverinfo[$s]->{name}= $server->{name};
231 $serverinfo[$s]->{encd}= $server->{encoding} // "iso-5426";
237 for ( my $z = 0 ; $z < $s ; $z++ ) {
238 $oResult[$z] = $oConnection[$z]->search_pqf($query);
241 while ( $nremaining-- ) {
244 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
245 $event = $oConnection[ $k - 1 ]->last_event();
246 last if $event == ZOOM::Event::ZEND;
251 my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
253 if ($error =~ m/^(10000|10007)$/ ) {
254 push(@errconn, { server => $serverinfo[$k]->{host}, error => $error } );
258 my $numresults = $oResult[$k]->size();
261 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
262 $show_next = 1 if $numresults >= ($page*20);
263 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
264 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
265 if($oResult[$k]->record($i)) {
266 my $res=_handle_one_result($oResult[$k]->record($i), $serverinfo[$k], ++$imported, $biblionumber); #ignores error in sequence numbering
267 push @breeding_loop, $res if $res;
270 push(@breeding_loop,{'server'=>$serverinfo[$k]->{name},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
278 numberpending => $nremaining,
279 current_page => $page,
280 total_pages => $total_pages,
281 show_nextbutton => $show_next?1:0,
282 show_prevbutton => $page!=1,
286 #close result sets and connections
288 $oResult[$_]->destroy();
289 $oConnection[$_]->destroy();
293 foreach my $id (@id) {
294 push @servers, {id => $id};
297 breeding_loop => \@breeding_loop,
298 servers => \@servers,
303 sub _handle_one_result {
304 my ($zoomrec, $servhref, $seq, $bib)= @_;
306 my $raw= $zoomrec->raw();
307 my ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encd}); #ignores charset return values
308 SetUTF8Flag($marcrecord);
310 my $batch_id = GetZ3950BatchId($servhref->{name});
311 my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0, 0);
312 #FIXME passing 0 for z3950random
313 #Will eliminate this unused field in a followup report
314 #Last zero indicates: no update for batch record counts
317 #call to TransformMarcToKoha replaced by next call
318 #we only need six fields from the marc record
321 biblionumber => $bib,
322 server => $servhref->{name},
323 breedingid => $breedingid,
324 }, $marcrecord) if $breedingid;
328 my ($row, $record)=@_;
330 title => 'biblio.title',
331 author => 'biblio.author',
332 isbn =>'biblioitems.isbn',
333 lccn =>'biblioitems.lccn', #LC control number (not call number)
334 edition =>'biblioitems.editionstatement',
335 date => 'biblio.copyrightdate', #MARC21
336 date2 => 'biblioitems.publicationyear', #UNIMARC
338 foreach my $k (keys %fetch) {
339 my ($t, $f)= split '\.', $fetch{$k};
340 $row= C4::Biblio::TransformMarcToKohaOneField($t, $f, $record, $row);
341 $row->{$k}= $row->{$f} if $k ne $f;
343 $row->{date}//= $row->{date2};
344 $row->{isbn}=_isbn_replace($row->{isbn});
350 return unless defined $isbn;
351 $isbn =~ s/ |-|\.//g;
352 $isbn =~ s/\|/ \| /g;
357 =head2 ImportBreedingAuth
359 ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type);
361 ImportBreedingAuth imports MARC records in the reservoir (import_records table).
362 ImportBreedingAuth is based on the ImportBreeding subroutine.
366 sub ImportBreedingAuth {
367 my ($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type) = @_;
368 my @marcarray = split /\x1D/, $marcrecords;
370 my $dbh = C4::Context->dbh;
372 my $batch_id = GetZ3950BatchId($filename);
373 my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
375 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
376 # fields used for import results
379 my $alreadyinfarm = 0;
380 my $notmarcrecord = 0;
382 for (my $i=0;$i<=$#marcarray;$i++) {
383 my ($marcrecord, $charset_result, $charset_errors);
384 ($marcrecord, $charset_result, $charset_errors) =
385 MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
387 # Normalize the record so it doesn't have separated diacritics
388 SetUTF8Flag($marcrecord);
390 if (scalar($marcrecord->fields()) == 0) {
394 $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
396 my $heading_authtype_code;
397 $heading_authtype_code = GuessAuthTypeCode($marcrecord);
400 $controlnumber = $marcrecord->field('001')->data;
402 #Check if the authority record already exists in the database...
403 my ($duplicateauthid,$duplicateauthvalue);
404 if ($marcrecord && $heading_authtype_code) {
405 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority( $marcrecord, $heading_authtype_code);
408 if ($duplicateauthid && $overwrite_auth ne 2) {
409 #If the authority record exists and $overwrite_auth doesn't equal 2, then mark it as already in the DB
412 if ($controlnumber && $heading) {
413 $searchbreeding->execute($controlnumber,$heading);
414 ($breedingid) = $searchbreeding->fetchrow;
416 if ($breedingid && $overwrite_auth eq '0') {
419 if ($breedingid && $overwrite_auth eq '1') {
420 ModAuthorityInBatch($breedingid, $marcrecord);
422 my $import_id = AddAuthToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
423 $breedingid = $import_id;
430 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
433 =head2 Z3950SearchAuth
435 Z3950SearchAuth($pars, $template);
437 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
438 title, uniform title, subject, subjectsubdiv, srchany.
439 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
440 This code is used in cataloging/z3950_auth_search.
441 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
445 sub Z3950SearchAuth {
446 my ($pars, $template)= @_;
448 my $dbh = C4::Context->dbh;
449 my @id= @{$pars->{id}};
450 my $random= $pars->{random};
451 my $page= $pars->{page};
453 my $nameany= $pars->{nameany};
454 my $authorany= $pars->{authorany};
455 my $authorpersonal= $pars->{authorpersonal};
456 my $authorcorp= $pars->{authorcorp};
457 my $authormeetingcon= $pars->{authormeetingcon};
458 my $title= $pars->{title};
459 my $uniformtitle= $pars->{uniformtitle};
460 my $subject= $pars->{subject};
461 my $subjectsubdiv= $pars->{subjectsubdiv};
462 my $srchany= $pars->{srchany};
478 my @breeding_loop = ();
488 $query .= " \@attr 1=1002 \"$nameany\" "; #Any name (this includes personal, corporate, meeting/conference authors, and author names in subject headings)
489 #This attribute is supported by both the Library of Congress and Libraries Australia 08/05/2013
494 $query .= " \@attr 1=1003 \"$authorany\" "; #Author-name (this includes personal, corporate, meeting/conference authors, but not author names in subject headings)
495 #This attribute is not supported by the Library of Congress, but is supported by Libraries Australia 08/05/2013
500 $query .= " \@attr 1=2 \"$authorcorp\" "; #1005 is another valid corporate author attribute...
504 if ($authorpersonal) {
505 $query .= " \@attr 1=1 \"$authorpersonal\" "; #1004 is another valid personal name attribute...
509 if ($authormeetingcon) {
510 $query .= " \@attr 1=3 \"$authormeetingcon\" "; #1006 is another valid meeting/conference name attribute...
515 $query .= " \@attr 1=21 \"$subject\" ";
519 if ($subjectsubdiv) {
520 $query .= " \@attr 1=47 \"$subjectsubdiv\" ";
525 $query .= " \@attr 1=4 \"$title\" "; #This is a regular title search. 1=6 will give just uniform titles
530 $query .= " \@attr 1=6 \"$uniformtitle\" "; #This is the uniform title search
535 $query .= " \@attr 1=1016 \"$srchany\" ";
539 for my $i (1..$nterms-1) {
540 $query = "\@and " . $query;
543 foreach my $servid (@id) {
544 my $sth = $dbh->prepare("select * from z3950servers where id=?");
545 $sth->execute($servid);
546 while ( $server = $sth->fetchrow_hashref ) {
547 my $option1 = new ZOOM::Options();
548 $option1->option( 'async' => 1 );
549 $option1->option( 'elementSetName', 'F' );
550 $option1->option( 'databaseName', $server->{db} );
551 $option1->option( 'user', $server->{userid} ) if $server->{userid};
552 $option1->option( 'password', $server->{password} ) if $server->{password};
553 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
554 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
555 $oConnection[$s] = create ZOOM::Connection($option1);
556 $oConnection[$s]->connect( $server->{host}, $server->{port} );
557 $serverhost[$s] = $server->{host};
558 $servername[$s] = $server->{name};
559 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
565 for ( my $z = 0 ; $z < $s ; $z++ ) {
566 $oResult[$z] = $oConnection[$z]->search_pqf($query);
569 while ( $nremaining-- ) {
572 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
573 $event = $oConnection[ $k - 1 ]->last_event();
574 last if $event == ZOOM::Event::ZEND;
579 my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
581 if ($error =~ m/^(10000|10007)$/ ) {
582 push(@errconn, {'server' => $serverhost[$k]});
586 my $numresults = $oResult[$k]->size();
589 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
590 $show_next = 1 if $numresults >= ($page*20);
591 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
592 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
593 my $rec = $oResult[$k]->record($i);
597 $marcdata = $rec->raw();
599 my ($charset_result, $charset_errors);
600 ($marcrecord, $charset_result, $charset_errors)= MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
603 my $heading_authtype_code;
604 $heading_authtype_code = GuessAuthTypeCode($marcrecord);
605 $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
607 my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
609 $row_data{server} = $servername[$k];
610 $row_data{breedingid} = $breedingid;
611 $row_data{heading} = $heading;
612 $row_data{heading_code} = $heading_authtype_code;
613 push( @breeding_loop, \%row_data );
616 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1});
624 numberpending => $nremaining,
625 current_page => $page,
626 total_pages => $total_pages,
627 show_nextbutton => $show_next?1:0,
628 show_prevbutton => $page!=1,
632 #close result sets and connections
634 $oResult[$_]->destroy();
635 $oConnection[$_]->destroy();
639 foreach my $id (@id) {
640 push @servers, {id => $id};
643 breeding_loop => \@breeding_loop,
644 servers => \@servers,