Bug 11254: make reservoir search normalize ISBNs
[koha.git] / C4 / Breeding.pm
1 package C4::Breeding;
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2013 Prosentient Systems
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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.
20
21 use strict;
22 use warnings;
23
24 use C4::Biblio;
25 use C4::Koha;
26 use C4::Charset;
27 use MARC::File::USMARC;
28 use C4::ImportBatch;
29 use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
30
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32
33 BEGIN {
34         # set the version for version checking
35     $VERSION = 3.07.00.049;
36         require Exporter;
37         @ISA = qw(Exporter);
38     @EXPORT = qw(&ImportBreeding &BreedingSearch &Z3950Search &Z3950SearchAuth);
39 }
40
41 =head1 NAME
42
43 C4::Breeding : module to add biblios to import_records via
44                the breeding/reservoir API.
45
46 =head1 SYNOPSIS
47
48     use C4::Scan;
49     &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type);
50
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.
60
61 =head1 DESCRIPTION
62
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.
66
67 =head2 ImportBreeding
68
69         ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type);
70
71         TODO description
72
73 =cut
74
75 sub ImportBreeding {
76     my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_;
77     my @marcarray = split /\x1D/, $marcrecords;
78     
79     my $dbh = C4::Context->dbh;
80     
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=?");
86     
87 #     $encoding = C4::Context->preference("marcflavour") unless $encoding;
88     # fields used for import results
89     my $imported=0;
90     my $alreadyindb = 0;
91     my $alreadyinfarm = 0;
92     my $notmarcrecord = 0;
93     my $breedingid;
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);
98         
99         # Normalize the record so it doesn't have separated diacritics
100         SetUTF8Flag($marcrecord);
101
102 #         warn "$i : $marcarray[$i]";
103         # FIXME - currently this does nothing 
104         my @warnings = $marcrecord->warnings();
105         
106         if (scalar($marcrecord->fields()) == 0) {
107             $notmarcrecord++;
108         } else {
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;
119             } else {
120                 if ($oldbiblio->{issn}) {
121                     $searchissn->execute($oldbiblio->{issn});
122                         ($biblioitemnumber) = $searchissn->fetchrow;
123                 }
124             }
125             if ($biblioitemnumber && $overwrite_biblio ne 2) {
126                 $alreadyindb++;
127             } else {
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;
138                 }
139                 if ($breedingid && $overwrite_biblio eq '0') {
140                     $alreadyinfarm++;
141                 } else {
142                     if ($breedingid && $overwrite_biblio eq '1') {
143                         ModBiblioInBatch($breedingid, $marcrecord);
144                     } else {
145                         my $import_id = AddBiblioToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
146                         $breedingid = $import_id;
147                     }
148                     $imported++;
149                 }
150             }
151         }
152     }
153     return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
154 }
155
156
157 =head2 BreedingSearch
158
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.
163
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.
167
168 =cut
169
170 sub BreedingSearch {
171     my ($search,$isbn,$z3950random) = @_;
172     my $dbh   = C4::Context->dbh;
173     my $count = 0;
174     my ($query,@bind);
175     my $sth;
176     my @results;
177
178     # normalise ISBN like at import
179     $isbn = C4::Koha::GetNormalizedISBN($isbn);
180
181     $query = "SELECT import_record_id, file_name, isbn, title, author
182               FROM  import_biblios 
183               JOIN import_records USING (import_record_id)
184               JOIN import_batches USING (import_batch_id)
185               WHERE ";
186     if ($z3950random) {
187         $query .= "z3950random = ?";
188         @bind=($z3950random);
189     } else {
190         @bind=();
191         if (defined($search) && length($search)>0) {
192             $search =~ s/(\s+)/\%/g;
193             $query .= "title like ? OR author like ?";
194             push(@bind,"%$search%", "%$search%");
195         }
196         if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
197             $query .= " and ";
198         }
199         if (defined($isbn) && length($isbn)>0) {
200             $query .= "isbn like ?";
201             push(@bind,"$isbn%");
202         }
203     }
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'};
214             $count++;
215     } # while
216
217     $sth->finish;
218     return($count, @results);
219 } # sub breedingsearch
220
221
222 =head2 Z3950Search
223
224 Z3950Search($pars, $template);
225
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.
230
231 =cut
232
233 sub Z3950Search {
234     my ($pars, $template)= @_;
235
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};
250
251     my $show_next       = 0;
252     my $total_pages     = 0;
253     my $term;
254     my @results;
255     my @breeding_loop = ();
256     my @oConnection;
257     my @oResult;
258     my @errconn;
259     my $s = 0;
260     my $query;
261     my $nterms=0;
262     my $imported=0;
263     my @serverinfo; #replaces former serverhost, servername, encoding
264
265     if ($isbn) {
266         $term=$isbn;
267         $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
268         $nterms++;
269     }
270     if ($issn) {
271         $term=$issn;
272         $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
273         $nterms++;
274     }
275     if ($title) {
276         $query .= " \@attr 1=4 \"$title\" ";
277         $nterms++;
278     }
279     if ($author) {
280         $query .= " \@attr 1=1003 \"$author\" ";
281         $nterms++;
282     }
283     if ($dewey) {
284         $query .= " \@attr 1=16 \"$dewey\" ";
285         $nterms++;
286     }
287     if ($subject) {
288         $query .= " \@attr 1=21 \"$subject\" ";
289         $nterms++;
290     }
291     if ($lccn) {
292         $query .= " \@attr 1=9 $lccn ";
293         $nterms++;
294     }
295     if ($lccall) {
296         $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
297         $nterms++;
298     }
299     if ($controlnumber) {
300         $query .= " \@attr 1=12 \"$controlnumber\" ";
301         $nterms++;
302     }
303     if($srchany) {
304         $query .= " \@attr 1=1016 \"$srchany\" ";
305         $nterms++;
306     }
307     if($stdid) {
308         $query .= " \@attr 1=1007 \"$stdid\" ";
309         $nterms++;
310     }
311     for my $i (1..$nterms-1) {
312         $query = "\@and " . $query;
313     }
314
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";
333             $s++;
334         }    ## while fetch
335     }    # foreach
336     my $nremaining  = $s;
337
338     for ( my $z = 0 ; $z < $s ; $z++ ) {
339         $oResult[$z] = $oConnection[$z]->search_pqf($query);
340     }
341
342     while ( $nremaining-- ) {
343         my $k;
344         my $event;
345         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
346             $event = $oConnection[ $k - 1 ]->last_event();
347             last if $event == ZOOM::Event::ZEND;
348         }
349
350         if ( $k != 0 ) {
351             $k--;
352             my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
353             if ($error) {
354                 if ($error =~ m/^(10000|10007)$/ ) {
355                     push(@errconn, { server => $serverinfo[$k]->{host}, error => $error } );
356                 }
357             }
358             else {
359                 my $numresults = $oResult[$k]->size();
360                 my $i;
361                 my $result = '';
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;
369                         }
370                         else {
371                             push(@breeding_loop,{'server'=>$serverinfo[$k]->{name},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
372                         }
373                     }
374                 }    #if $numresults
375             }
376         }    # if $k !=0
377
378         $template->param(
379             numberpending => $nremaining,
380             current_page => $page,
381             total_pages => $total_pages,
382             show_nextbutton => $show_next?1:0,
383             show_prevbutton => $page!=1,
384         );
385     } # while nremaining
386
387     #close result sets and connections
388     foreach(0..$s-1) {
389         $oResult[$_]->destroy();
390         $oConnection[$_]->destroy();
391     }
392
393     my @servers = ();
394     foreach my $id (@id) {
395         push @servers, {id => $id};
396     }
397     $template->param(
398         breeding_loop => \@breeding_loop,
399         servers => \@servers,
400         errconn       => \@errconn
401     );
402 }
403
404 sub _handle_one_result {
405     my ($zoomrec, $servhref, $seq, $bib)= @_;
406
407     my $raw= $zoomrec->raw();
408     my ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encd}); #ignores charset return values
409     SetUTF8Flag($marcrecord);
410
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
417
418
419     #call to TransformMarcToKoha replaced by next call
420     #we only need six fields from the marc record
421     return _add_rowdata(
422         {
423             biblionumber => $bib,
424             server       => $servhref->{name},
425             breedingid   => $breedingid,
426         }, $marcrecord) if $breedingid;
427 }
428
429 sub _add_rowdata {
430     my ($row, $record)=@_;
431     my %fetch= (
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
439     );
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;
444     }
445     $row->{date}//= $row->{date2};
446     $row->{isbn}=_isbn_replace($row->{isbn});
447     return $row;
448 }
449
450 sub _isbn_replace {
451     my ($isbn) = @_;
452     return unless defined $isbn;
453     $isbn =~ s/ |-|\.//g;
454     $isbn =~ s/\|/ \| /g;
455     $isbn =~ s/\(/ \(/g;
456     return $isbn;
457 }
458
459 =head2 ImportBreedingAuth
460
461 ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type);
462
463     ImportBreedingAuth imports MARC records in the reservoir (import_records table).
464     ImportBreedingAuth is based on the ImportBreeding subroutine.
465
466 =cut
467
468 sub ImportBreedingAuth {
469     my ($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type) = @_;
470     my @marcarray = split /\x1D/, $marcrecords;
471
472     my $dbh = C4::Context->dbh;
473
474     my $batch_id = GetZ3950BatchId($filename);
475     my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
476
477 #     $encoding = C4::Context->preference("marcflavour") unless $encoding;
478     # fields used for import results
479     my $imported=0;
480     my $alreadyindb = 0;
481     my $alreadyinfarm = 0;
482     my $notmarcrecord = 0;
483     my $breedingid;
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);
488
489         # Normalize the record so it doesn't have separated diacritics
490         SetUTF8Flag($marcrecord);
491
492         if (scalar($marcrecord->fields()) == 0) {
493             $notmarcrecord++;
494         } else {
495             my $heading;
496             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
497
498             my $heading_authtype_code;
499             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
500
501             my $controlnumber;
502             $controlnumber = $marcrecord->field('001')->data;
503
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);
508             }
509
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
512                 $alreadyindb++;
513             } else {
514                 if ($controlnumber && $heading) {
515                     $searchbreeding->execute($controlnumber,$heading);
516                     ($breedingid) = $searchbreeding->fetchrow;
517                 }
518                 if ($breedingid && $overwrite_auth eq '0') {
519                     $alreadyinfarm++;
520                 } else {
521                     if ($breedingid && $overwrite_auth eq '1') {
522                         ModAuthorityInBatch($breedingid, $marcrecord);
523                     } else {
524                         my $import_id = AddAuthToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
525                         $breedingid = $import_id;
526                     }
527                     $imported++;
528                 }
529             }
530         }
531     }
532     return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
533 }
534
535 =head2 Z3950SearchAuth
536
537 Z3950SearchAuth($pars, $template);
538
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.
544
545 =cut
546
547 sub Z3950SearchAuth {
548     my ($pars, $template)= @_;
549
550     my $dbh   = C4::Context->dbh;
551     my @id= @{$pars->{id}};
552     my $random= $pars->{random};
553     my $page= $pars->{page};
554
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};
565
566     my $show_next       = 0;
567     my $total_pages     = 0;
568     my $attr = '';
569     my $host;
570     my $server;
571     my $database;
572     my $port;
573     my $marcdata;
574     my @encoding;
575     my @results;
576     my $count;
577     my $record;
578     my @serverhost;
579     my @servername;
580     my @breeding_loop = ();
581
582     my @oConnection;
583     my @oResult;
584     my @errconn;
585     my $s = 0;
586     my $query;
587     my $nterms=0;
588
589     if ($nameany) {
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
592         $nterms++;
593     }
594
595     if ($authorany) {
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
598         $nterms++;
599     }
600
601     if ($authorcorp) {
602         $query .= " \@attr 1=2 \"$authorcorp\" "; #1005 is another valid corporate author attribute...
603         $nterms++;
604     }
605
606     if ($authorpersonal) {
607         $query .= " \@attr 1=1 \"$authorpersonal\" "; #1004 is another valid personal name attribute...
608         $nterms++;
609     }
610
611     if ($authormeetingcon) {
612         $query .= " \@attr 1=3 \"$authormeetingcon\" "; #1006 is another valid meeting/conference name attribute...
613         $nterms++;
614     }
615
616     if ($subject) {
617         $query .= " \@attr 1=21 \"$subject\" ";
618         $nterms++;
619     }
620
621     if ($subjectsubdiv) {
622         $query .= " \@attr 1=47 \"$subjectsubdiv\" ";
623         $nterms++;
624     }
625
626     if ($title) {
627         $query .= " \@attr 1=4 \"$title\" "; #This is a regular title search. 1=6 will give just uniform titles
628         $nterms++;
629     }
630
631      if ($uniformtitle) {
632         $query .= " \@attr 1=6 \"$uniformtitle\" "; #This is the uniform title search
633         $nterms++;
634     }
635
636     if($srchany) {
637         $query .= " \@attr 1=1016 \"$srchany\" ";
638         $nterms++;
639     }
640
641     for my $i (1..$nterms-1) {
642         $query = "\@and " . $query;
643     }
644
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");
662             $s++;
663         }    ## while fetch
664     }    # foreach
665     my $nremaining  = $s;
666
667     for ( my $z = 0 ; $z < $s ; $z++ ) {
668         $oResult[$z] = $oConnection[$z]->search_pqf($query);
669     }
670
671     while ( $nremaining-- ) {
672         my $k;
673         my $event;
674         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
675             $event = $oConnection[ $k - 1 ]->last_event();
676             last if $event == ZOOM::Event::ZEND;
677         }
678
679         if ( $k != 0 ) {
680             $k--;
681             my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
682             if ($error) {
683                 if ($error =~ m/^(10000|10007)$/ ) {
684                     push(@errconn, {'server' => $serverhost[$k]});
685                 }
686             }
687             else {
688                 my $numresults = $oResult[$k]->size();
689                 my $i;
690                 my $result = '';
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);
696                         if ($rec) {
697                             my $marcrecord;
698                             my $marcdata;
699                             $marcdata   = $rec->raw();
700
701                             my ($charset_result, $charset_errors);
702                             ($marcrecord, $charset_result, $charset_errors)= MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
703
704                             my $heading;
705                             my $heading_authtype_code;
706                             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
707                             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
708
709                             my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
710                             my %row_data;
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 );
716                         }
717                         else {
718                             push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1});
719                         }
720                     }
721                 }    #if $numresults
722             }
723         }    # if $k !=0
724
725         $template->param(
726             numberpending => $nremaining,
727             current_page => $page,
728             total_pages => $total_pages,
729             show_nextbutton => $show_next?1:0,
730             show_prevbutton => $page!=1,
731         );
732     } # while nremaining
733
734     #close result sets and connections
735     foreach(0..$s-1) {
736         $oResult[$_]->destroy();
737         $oConnection[$_]->destroy();
738     }
739
740     my @servers = ();
741     foreach my $id (@id) {
742         push @servers, {id => $id};
743     }
744     $template->param(
745         breeding_loop => \@breeding_loop,
746         servers => \@servers,
747         errconn       => \@errconn
748     );
749 }
750
751 1;
752 __END__
753