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
32 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35 # set the version for version checking
36 $VERSION = 3.07.00.049;
39 @EXPORT = qw(&BreedingSearch &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,$random);
54 This module contains routines related to Koha's Z39.50 search into
55 cataloguing reservoir features.
59 ($count, @results) = &BreedingSearch($title,$isbn,$random);
60 C<$title> contains the title,
61 C<$isbn> contains isbn or issn,
62 C<$random> contains the random seed from a z3950 search.
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.
71 my ($search,$isbn,$z3950random) = @_;
72 my $dbh = C4::Context->dbh;
78 # normalise ISBN like at import
79 $isbn = C4::Koha::GetNormalizedISBN($isbn);
81 $query = "SELECT import_record_id, file_name, isbn, title, author
83 JOIN import_records USING (import_record_id)
84 JOIN import_batches USING (import_batch_id)
87 $query .= "z3950random = ?";
91 if (defined($search) && length($search)>0) {
92 $search =~ s/(\s+)/\%/g;
93 $query .= "title like ? OR author like ?";
94 push(@bind,"%$search%", "%$search%");
96 if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
99 if (defined($isbn) && length($isbn)>0) {
100 $query .= "isbn like ?";
101 push(@bind,"$isbn%");
104 $sth = $dbh->prepare($query);
105 $sth->execute(@bind);
106 while (my $data = $sth->fetchrow_hashref) {
107 $results[$count] = $data;
108 # FIXME - hack to reflect difference in name
109 # of columns in old marc_breeding and import_records
110 # There needs to be more separation between column names and
111 # field names used in the templates </soapbox>
112 $data->{'file'} = $data->{'file_name'};
113 $data->{'id'} = $data->{'import_record_id'};
118 return($count, @results);
119 } # sub breedingsearch
124 Z3950Search($pars, $template);
126 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
127 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).
128 This code is used in acqui/z3950_search and cataloging/z3950_search.
129 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
134 my ($pars, $template)= @_;
136 my @id= @{$pars->{id}};
137 my $page= $pars->{page};
138 my $biblionumber= $pars->{biblionumber};
143 my @breeding_loop = ();
150 my ( $zquery, $squery ) = _build_query( $pars );
152 my $schema = Koha::Database->new()->schema();
153 my $rs = $schema->resultset('Z3950server')->search(
155 { result_class => 'DBIx::Class::ResultClass::HashRefInflator' },
157 my @servers = $rs->all;
158 foreach my $server ( @servers ) {
159 $oConnection[$s] = _create_connection( $server );
161 $server->{servertype} eq 'zed'?
162 $oConnection[$s]->search_pqf( $zquery ):
163 $oConnection[$s]->search(new ZOOM::Query::CQL(
164 _translate_query( $server, $squery )));
169 while ( $nremaining-- ) {
172 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
173 $event = $oConnection[ $k - 1 ]->last_event();
174 last if $event == ZOOM::Event::ZEND;
179 my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
181 if ($error =~ m/^(10000|10007)$/ ) {
182 push(@errconn, { server => $servers[$k]->{host}, error => $error } );
186 my $numresults = $oResult[$k]->size();
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 my $res=_handle_one_result($oResult[$k]->record($i), $servers[$k], ++$imported, $biblionumber); #ignores error in sequence numbering
195 push @breeding_loop, $res if $res;
198 push(@breeding_loop,{'server'=>$servers[$k]->{servername},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
206 numberpending => $nremaining,
207 current_page => $page,
208 total_pages => $total_pages,
209 show_nextbutton => $show_next?1:0,
210 show_prevbutton => $page!=1,
214 #close result sets and connections
216 $oResult[$_]->destroy();
217 $oConnection[$_]->destroy();
221 breeding_loop => \@breeding_loop,
222 servers => \@servers,
231 isbn => '@attr 1=7 @attr 5=1 "#term" ',
232 issn => '@attr 1=8 @attr 5=1 "#term" ',
233 title => '@attr 1=4 #term ',
234 author => '@attr 1=1003 "#term" ',
235 dewey => '@attr 1=16 "#term" ',
236 subject => '@attr 1=21 "#term" ',
237 lccall => '@attr 1=16 @attr 2=3 @attr 3=1 @attr 4=1 @attr 5=1 '.
238 '@attr 6=1 "#term" ',
239 controlnumber => '@attr 1=12 "#term" ',
240 srchany => '@attr 1=1016 "#term" ',
241 stdid => '@attr 1=1007 "#term" ',
247 foreach my $k ( keys %$pars ) {
248 if( ( my $val=$pars->{$k} ) && $qry_build->{$k} ) {
249 $qry_build->{$k} =~ s/#term/$val/g;
250 $zquery .= $qry_build->{$k};
251 $squery .= "[$k]=\"$val\" and ";
255 $zquery = "\@and " . $zquery for 2..$nterms;
256 $squery =~ s/ and $//;
257 return ( $zquery, $squery );
260 sub _handle_one_result {
261 my ($zoomrec, $servhref, $seq, $bib)= @_;
263 my $raw= $zoomrec->raw();
265 if( $servhref->{servertype} eq 'sru' ) {
266 $marcrecord= MARC::Record->new_from_xml( $raw, 'UTF-8',
267 $servhref->{syntax} );
269 ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encoding} // "iso-5426" ); #ignores charset return values
271 SetUTF8Flag($marcrecord);
273 my $batch_id = GetZ3950BatchId($servhref->{servername});
274 my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0, 0);
275 #FIXME passing 0 for z3950random
276 #Will eliminate this unused field in a followup report
277 #Last zero indicates: no update for batch record counts
280 #call to TransformMarcToKoha replaced by next call
281 #we only need six fields from the marc record
284 biblionumber => $bib,
285 server => $servhref->{servername},
286 breedingid => $breedingid,
287 }, $marcrecord) if $breedingid;
291 my ($row, $record)=@_;
293 title => 'biblio.title',
294 author => 'biblio.author',
295 isbn =>'biblioitems.isbn',
296 lccn =>'biblioitems.lccn', #LC control number (not call number)
297 edition =>'biblioitems.editionstatement',
298 date => 'biblio.copyrightdate', #MARC21
299 date2 => 'biblioitems.publicationyear', #UNIMARC
301 foreach my $k (keys %fetch) {
302 my ($t, $f)= split '\.', $fetch{$k};
303 $row= C4::Biblio::TransformMarcToKohaOneField($t, $f, $record, $row);
304 $row->{$k}= $row->{$f} if $k ne $f;
306 $row->{date}//= $row->{date2};
307 $row->{isbn}=_isbn_replace($row->{isbn});
313 return unless defined $isbn;
314 $isbn =~ s/ |-|\.//g;
315 $isbn =~ s/\|/ \| /g;
320 sub _create_connection {
322 my $option1= new ZOOM::Options();
323 $option1->option( 'async' => 1 );
324 $option1->option( 'elementSetName', 'F' );
325 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
326 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
328 if( $server->{servertype} eq 'sru' ) {
329 foreach( split ',', $server->{sru_options}//'' ) {
331 $option1->option( $temp[0] => $temp[1] ) if @temp;
333 } elsif( $server->{servertype} eq 'zed' ) {
334 $option1->option( 'databaseName', $server->{db} );
335 $option1->option( 'user', $server->{userid} ) if $server->{userid};
336 $option1->option( 'password', $server->{password} ) if $server->{password};
339 my $obj= ZOOM::Connection->create($option1);
340 if( $server->{servertype} eq 'sru' ) {
341 my $host= $server->{host};
342 if( $host !~ /^https?:\/\// ) {
343 #Normally, host will not be prefixed by protocol.
344 #In that case we can (safely) assume http.
345 #In case someone prefixed with https, give it a try..
346 $host = 'http://' . $host;
348 $obj->connect( $host.':'.$server->{port}.'/'.$server->{db} );
350 $obj->connect( $server->{host}, $server->{port} );
355 sub _translate_query { #SRU query adjusted per server cf. srufields column
356 my ($server, $query) = @_;
358 #sru_fields is in format title=field,isbn=field,...
359 #if a field doesn't exist, try anywhere or remove [field]=
360 my @parts= split(',', $server->{sru_fields} );
361 my %trans= map { if( /=/ ) { ( $`,$' ) } else { () } } @parts;
362 my $any= $trans{srchany}?$trans{srchany}.'=':'';
365 foreach my $key (keys %trans) {
370 $q=~s/\[$key\]=/$any/g;
373 $q=~s/\[\w+\]=/$any/g; # remove remaining fields (not found in field list)
377 =head2 ImportBreedingAuth
379 ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type);
381 ImportBreedingAuth imports MARC records in the reservoir (import_records table).
382 ImportBreedingAuth is based on the ImportBreeding subroutine.
386 sub ImportBreedingAuth {
387 my ($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type) = @_;
388 my @marcarray = split /\x1D/, $marcrecords;
390 my $dbh = C4::Context->dbh;
392 my $batch_id = GetZ3950BatchId($filename);
393 my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
395 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
396 # fields used for import results
399 my $alreadyinfarm = 0;
400 my $notmarcrecord = 0;
402 for (my $i=0;$i<=$#marcarray;$i++) {
403 my ($marcrecord, $charset_result, $charset_errors);
404 ($marcrecord, $charset_result, $charset_errors) =
405 MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
407 # Normalize the record so it doesn't have separated diacritics
408 SetUTF8Flag($marcrecord);
410 if (scalar($marcrecord->fields()) == 0) {
414 $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
416 my $heading_authtype_code;
417 $heading_authtype_code = GuessAuthTypeCode($marcrecord);
420 $controlnumber = $marcrecord->field('001')->data;
422 #Check if the authority record already exists in the database...
423 my ($duplicateauthid,$duplicateauthvalue);
424 if ($marcrecord && $heading_authtype_code) {
425 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority( $marcrecord, $heading_authtype_code);
428 if ($duplicateauthid && $overwrite_auth ne 2) {
429 #If the authority record exists and $overwrite_auth doesn't equal 2, then mark it as already in the DB
432 if ($controlnumber && $heading) {
433 $searchbreeding->execute($controlnumber,$heading);
434 ($breedingid) = $searchbreeding->fetchrow;
436 if ($breedingid && $overwrite_auth eq '0') {
439 if ($breedingid && $overwrite_auth eq '1') {
440 ModAuthorityInBatch($breedingid, $marcrecord);
442 my $import_id = AddAuthToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
443 $breedingid = $import_id;
450 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
453 =head2 Z3950SearchAuth
455 Z3950SearchAuth($pars, $template);
457 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
458 title, uniform title, subject, subjectsubdiv, srchany.
459 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
460 This code is used in cataloging/z3950_auth_search.
461 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
465 sub Z3950SearchAuth {
466 my ($pars, $template)= @_;
468 my $dbh = C4::Context->dbh;
469 my @id= @{$pars->{id}};
470 my $random= $pars->{random};
471 my $page= $pars->{page};
473 my $nameany= $pars->{nameany};
474 my $authorany= $pars->{authorany};
475 my $authorpersonal= $pars->{authorpersonal};
476 my $authorcorp= $pars->{authorcorp};
477 my $authormeetingcon= $pars->{authormeetingcon};
478 my $title= $pars->{title};
479 my $uniformtitle= $pars->{uniformtitle};
480 my $subject= $pars->{subject};
481 my $subjectsubdiv= $pars->{subjectsubdiv};
482 my $srchany= $pars->{srchany};
498 my @breeding_loop = ();
508 $query .= " \@attr 1=1002 \"$nameany\" "; #Any name (this includes personal, corporate, meeting/conference authors, and author names in subject headings)
509 #This attribute is supported by both the Library of Congress and Libraries Australia 08/05/2013
514 $query .= " \@attr 1=1003 \"$authorany\" "; #Author-name (this includes personal, corporate, meeting/conference authors, but not author names in subject headings)
515 #This attribute is not supported by the Library of Congress, but is supported by Libraries Australia 08/05/2013
520 $query .= " \@attr 1=2 \"$authorcorp\" "; #1005 is another valid corporate author attribute...
524 if ($authorpersonal) {
525 $query .= " \@attr 1=1 \"$authorpersonal\" "; #1004 is another valid personal name attribute...
529 if ($authormeetingcon) {
530 $query .= " \@attr 1=3 \"$authormeetingcon\" "; #1006 is another valid meeting/conference name attribute...
535 $query .= " \@attr 1=21 \"$subject\" ";
539 if ($subjectsubdiv) {
540 $query .= " \@attr 1=47 \"$subjectsubdiv\" ";
545 $query .= " \@attr 1=4 \"$title\" "; #This is a regular title search. 1=6 will give just uniform titles
550 $query .= " \@attr 1=6 \"$uniformtitle\" "; #This is the uniform title search
555 $query .= " \@attr 1=1016 \"$srchany\" ";
559 for my $i (1..$nterms-1) {
560 $query = "\@and " . $query;
563 foreach my $servid (@id) {
564 my $sth = $dbh->prepare("select * from z3950servers where id=?");
565 $sth->execute($servid);
566 while ( $server = $sth->fetchrow_hashref ) {
567 my $option1 = new ZOOM::Options();
568 $option1->option( 'async' => 1 );
569 $option1->option( 'elementSetName', 'F' );
570 $option1->option( 'databaseName', $server->{db} );
571 $option1->option( 'user', $server->{userid} ) if $server->{userid};
572 $option1->option( 'password', $server->{password} ) if $server->{password};
573 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
574 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
575 $oConnection[$s] = create ZOOM::Connection($option1);
576 $oConnection[$s]->connect( $server->{host}, $server->{port} );
577 $serverhost[$s] = $server->{host};
578 $servername[$s] = $server->{name};
579 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
585 for ( my $z = 0 ; $z < $s ; $z++ ) {
586 $oResult[$z] = $oConnection[$z]->search_pqf($query);
589 while ( $nremaining-- ) {
592 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
593 $event = $oConnection[ $k - 1 ]->last_event();
594 last if $event == ZOOM::Event::ZEND;
599 my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
601 if ($error =~ m/^(10000|10007)$/ ) {
602 push(@errconn, {'server' => $serverhost[$k]});
606 my $numresults = $oResult[$k]->size();
609 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
610 $show_next = 1 if $numresults >= ($page*20);
611 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
612 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
613 my $rec = $oResult[$k]->record($i);
617 $marcdata = $rec->raw();
619 my ($charset_result, $charset_errors);
620 ($marcrecord, $charset_result, $charset_errors)= MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
623 my $heading_authtype_code;
624 $heading_authtype_code = GuessAuthTypeCode($marcrecord);
625 $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
627 my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
629 $row_data{server} = $servername[$k];
630 $row_data{breedingid} = $breedingid;
631 $row_data{heading} = $heading;
632 $row_data{heading_code} = $heading_authtype_code;
633 push( @breeding_loop, \%row_data );
636 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1});
644 numberpending => $nremaining,
645 current_page => $page,
646 total_pages => $total_pages,
647 show_nextbutton => $show_next?1:0,
648 show_prevbutton => $page!=1,
652 #close result sets and connections
654 $oResult[$_]->destroy();
655 $oConnection[$_]->destroy();
659 foreach my $id (@id) {
660 push @servers, {id => $id};
663 breeding_loop => \@breeding_loop,
664 servers => \@servers,