Bug 12747: Add extra column in Z3950 search
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 MARC::Field;
29 use C4::ImportBatch;
30 use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
31 use C4::Languages;
32 use Koha::Database;
33 use Koha::XSLT_Handler;
34
35 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36
37 BEGIN {
38         require Exporter;
39         @ISA = qw(Exporter);
40     @EXPORT = qw(&BreedingSearch &Z3950Search &Z3950SearchAuth);
41 }
42
43 =head1 NAME
44
45 C4::Breeding : module to add biblios to import_records via
46                the breeding/reservoir API.
47
48 =head1 SYNOPSIS
49
50     Z3950Search($pars, $template);
51     ($count, @results) = &BreedingSearch($title,$isbn,$random);
52
53 =head1 DESCRIPTION
54
55 This module contains routines related to Koha's Z39.50 search into
56 cataloguing reservoir features.
57
58 =head2 BreedingSearch
59
60 ($count, @results) = &BreedingSearch($title,$isbn,$random);
61 C<$title> contains the title,
62 C<$isbn> contains isbn or issn,
63 C<$random> contains the random seed from a z3950 search.
64
65 C<$count> is the number of items in C<@results>. C<@results> is an
66 array of references-to-hash; the keys are the items from the C<import_records> and
67 C<import_biblios> tables of the Koha database.
68
69 =cut
70
71 sub BreedingSearch {
72     my ($search,$isbn,$z3950random) = @_;
73     my $dbh   = C4::Context->dbh;
74     my $count = 0;
75     my ($query,@bind);
76     my $sth;
77     my @results;
78
79     # normalise ISBN like at import
80     $isbn = C4::Koha::GetNormalizedISBN($isbn);
81
82     $query = "SELECT import_record_id, file_name, isbn, title, author
83               FROM  import_biblios 
84               JOIN import_records USING (import_record_id)
85               JOIN import_batches USING (import_batch_id)
86               WHERE ";
87     if ($z3950random) {
88         $query .= "z3950random = ?";
89         @bind=($z3950random);
90     } else {
91         @bind=();
92         if (defined($search) && length($search)>0) {
93             $search =~ s/(\s+)/\%/g;
94             $query .= "title like ? OR author like ?";
95             push(@bind,"%$search%", "%$search%");
96         }
97         if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
98             $query .= " and ";
99         }
100         if (defined($isbn) && length($isbn)>0) {
101             $query .= "isbn like ?";
102             push(@bind,"$isbn%");
103         }
104     }
105     $sth   = $dbh->prepare($query);
106     $sth->execute(@bind);
107     while (my $data = $sth->fetchrow_hashref) {
108             $results[$count] = $data;
109             # FIXME - hack to reflect difference in name 
110             # of columns in old marc_breeding and import_records
111             # There needs to be more separation between column names and 
112             # field names used in the templates </soapbox>
113             $data->{'file'} = $data->{'file_name'};
114             $data->{'id'} = $data->{'import_record_id'};
115             $count++;
116     } # while
117
118     $sth->finish;
119     return($count, @results);
120 } # sub breedingsearch
121
122
123 =head2 Z3950Search
124
125 Z3950Search($pars, $template);
126
127 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
128 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).
129 This code is used in acqui/z3950_search and cataloging/z3950_search.
130 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
131
132 =cut
133
134 sub Z3950Search {
135     my ($pars, $template)= @_;
136
137     my @id= @{$pars->{id}};
138     my $page= $pars->{page};
139     my $biblionumber= $pars->{biblionumber};
140
141     my $show_next       = 0;
142     my $total_pages     = 0;
143     my @results;
144     my @breeding_loop = ();
145     my @oConnection;
146     my @oResult;
147     my @errconn;
148     my $s = 0;
149     my $imported=0;
150
151     my ( $zquery, $squery ) = _bib_build_query( $pars );
152
153     my $schema = Koha::Database->new()->schema();
154     my $rs = $schema->resultset('Z3950server')->search(
155         { id => [ @id ] },
156         { result_class => 'DBIx::Class::ResultClass::HashRefInflator' },
157     );
158     my @servers = $rs->all;
159     foreach my $server ( @servers ) {
160         $oConnection[$s] = _create_connection( $server );
161         $oResult[$s] =
162             $server->{servertype} eq 'zed'?
163                 $oConnection[$s]->search_pqf( $zquery ):
164                 $oConnection[$s]->search(new ZOOM::Query::CQL(
165                     _translate_query( $server, $squery )));
166         $s++;
167     }
168     my $xslh = Koha::XSLT_Handler->new;
169
170     my $nremaining = $s;
171     while ( $nremaining-- ) {
172         my $k;
173         my $event;
174         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
175             $event = $oConnection[ $k - 1 ]->last_event();
176             last if $event == ZOOM::Event::ZEND;
177         }
178
179         if ( $k != 0 ) {
180             $k--;
181             my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
182             if ($error) {
183                 if ($error =~ m/^(10000|10007)$/ ) {
184                     push(@errconn, { server => $servers[$k]->{host}, error => $error } );
185                 }
186             }
187             else {
188                 my $numresults = $oResult[$k]->size();
189                 my $i;
190                 my $res;
191                 if ( $numresults > 0  and $numresults >= (($page-1)*20)) {
192                     $show_next = 1 if $numresults >= ($page*20);
193                     $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
194                     for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
195                         if ( $oResult[$k]->record($i) ) {
196                             undef $error;
197                             ( $res, $error ) = _handle_one_result( $oResult[$k]->record($i), $servers[$k], ++$imported, $biblionumber, $xslh ); #ignores error in sequence numbering
198                             push @breeding_loop, $res if $res;
199                             push @errconn, { server => $servers[$k]->{servername}, error => $error, seq => $i+1 } if $error;
200                         }
201                         else {
202                             push @errconn, { 'server' => $servers[$k]->{servername}, error => ( ( $oConnection[$k]->error_x() )[0] ), seq => $i+1 };
203                         }
204                     }
205                 }    #if $numresults
206             }
207         }    # if $k !=0
208
209         $template->param(
210             numberpending => $nremaining,
211             current_page => $page,
212             total_pages => $total_pages,
213             show_nextbutton => $show_next?1:0,
214             show_prevbutton => $page!=1,
215         );
216     } # while nremaining
217
218     #close result sets and connections
219     foreach(0..$s-1) {
220         $oResult[$_]->destroy();
221         $oConnection[$_]->destroy();
222     }
223
224     $template->param(
225         breeding_loop => \@breeding_loop,
226         servers => \@servers,
227         errconn       => \@errconn
228     );
229 }
230
231 sub _auth_build_query {
232     my ( $pars ) = @_;
233
234     my $qry_build = {
235         nameany           => '@attr 1=1002 "#term" ',
236         authorany         => '@attr 1=1003 "#term" ',
237         authorcorp        => '@attr 1=2 "#term" ',
238         authorpersonal    => '@attr 1=1 "#term" ',
239         authormeetingcon  => '@attr 1=3 "#term" ',
240         subject           => '@attr 1=21 "#term" ',
241         subjectsubdiv     => '@attr 1=47 "#term" ',
242         title             => '@attr 1=4 "#term" ',
243         uniformtitle      => '@attr 1=6 "#term" ',
244         srchany           => '@attr 1=1016 "#term" ',
245         controlnumber     => '@attr 1=12 "#term" ',
246     };
247
248     return _build_query( $pars, $qry_build );
249 }
250
251 sub _bib_build_query {
252
253     my ( $pars ) = @_;
254
255     my $qry_build = {
256         isbn    => '@attr 1=7 @attr 5=1 "#term" ',
257         issn    => '@attr 1=8 @attr 5=1 "#term" ',
258         title   => '@attr 1=4 "#term" ',
259         author  => '@attr 1=1003 "#term" ',
260         dewey   => '@attr 1=16 "#term" ',
261         subject => '@attr 1=21 "#term" ',
262         lccall  => '@attr 1=16 @attr 2=3 @attr 3=1 @attr 4=1 @attr 5=1 '.
263                    '@attr 6=1 "#term" ',
264         controlnumber => '@attr 1=12 "#term" ',
265         srchany => '@attr 1=1016 "#term" ',
266         stdid   => '@attr 1=1007 "#term" ',
267     };
268
269     return _build_query( $pars, $qry_build );
270 }
271
272 sub _build_query {
273
274     my ( $pars, $qry_build ) = @_;
275
276     my $zquery='';
277     my $squery='';
278     my $nterms=0;
279     foreach my $k ( sort keys %$pars ) {
280     #note that the sort keys forces an identical result under Perl 5.18
281     #one of the unit tests is based on that assumption
282         if( ( my $val=$pars->{$k} ) && $qry_build->{$k} ) {
283             $qry_build->{$k} =~ s/#term/$val/g;
284             $zquery .= $qry_build->{$k};
285             $squery .= "[$k]=\"$val\" and ";
286             $nterms++;
287         }
288     }
289     $zquery = "\@and " . $zquery for 2..$nterms;
290     $squery =~ s/ and $//;
291     return ( $zquery, $squery );
292 }
293
294 sub _handle_one_result {
295     my ( $zoomrec, $servhref, $seq, $bib, $xslh )= @_;
296
297     my $raw= $zoomrec->raw();
298     my $marcrecord;
299     if( $servhref->{servertype} eq 'sru' ) {
300         $marcrecord= MARC::Record->new_from_xml( $raw, 'UTF-8',
301             $servhref->{syntax} );
302     } else {
303         ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encoding} // "iso-5426" ); #ignores charset return values
304     }
305     SetUTF8Flag($marcrecord);
306     my $error;
307     ( $marcrecord, $error ) = _do_xslt_proc($marcrecord, $servhref, $xslh);
308
309     my $batch_id = GetZ3950BatchId($servhref->{servername});
310     my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0, 0);
311         #FIXME passing 0 for z3950random
312         #Will eliminate this unused field in a followup report
313         #Last zero indicates: no update for batch record counts
314
315
316     #call to TransformMarcToKoha replaced by next call
317     #we only need six fields from the marc record
318     my $row;
319     $row = _add_rowdata(
320         {
321             biblionumber => $bib,
322             server       => $servhref->{servername},
323             breedingid   => $breedingid,
324         }, $marcrecord) if $breedingid;
325     return ( $row, $error );
326 }
327
328 sub _do_xslt_proc {
329     my ( $marc, $server, $xslh ) = @_;
330     return $marc if !$server->{add_xslt};
331
332     my $htdocs = C4::Context->config('intrahtdocs');
333     my $theme = C4::Context->preference("template"); #staff
334     my $lang = C4::Languages::getlanguage() || 'en';
335
336     my @files= split ',', $server->{add_xslt};
337     my $xml = $marc->as_xml;
338     foreach my $f ( @files ) {
339         $f =~ s/^\s+//; $f =~ s/\s+$//; next if !$f;
340         $f = C4::XSLT::_get_best_default_xslt_filename(
341             $htdocs, $theme, $lang, $f ) unless $f =~ /^\//;
342         $xml = $xslh->transform( $xml, $f );
343         last if $xslh->err; #skip other files
344     }
345     if( !$xslh->err ) {
346         return MARC::Record->new_from_xml($xml, 'UTF-8');
347     } else {
348         return ( $marc, $xslh->err ); #original record in case of errors
349     }
350 }
351
352 sub _add_rowdata {
353     my ($row, $record)=@_;
354     my %fetch= (
355         title => 'biblio.title',
356         author => 'biblio.author',
357         isbn =>'biblioitems.isbn',
358         lccn =>'biblioitems.lccn', #LC control number (not call number)
359         edition =>'biblioitems.editionstatement'
360     );
361     $fetch{date} = C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
362
363     foreach my $k (keys %fetch) {
364         $row->{$k} = C4::Biblio::TransformMarcToKohaOneField( $fetch{$k}, $record );
365     }
366     $row->{date}//= $row->{date2};
367     $row->{isbn}=_isbn_replace($row->{isbn});
368
369     $row = _add_custom_field_rowdata($row, $record);
370
371     return $row;
372 }
373
374 sub _add_custom_field_rowdata
375 {
376     my ( $row, $record ) = @_;
377     my $pref_newtags = C4::Context->preference('AdditionalFieldsInZ3950ResultSearch');
378
379     $pref_newtags =~ s/^\s+|\s+$//g;
380     $pref_newtags =~ s/\h+/ /g;
381
382     my @addnumberfields;
383
384     foreach my $field (split /\,/, $pref_newtags) {
385         $field =~ s/^\s+|\s+$//g ;  # trim whitespace
386         my ($tag, $subtags) = split(/\$/, $field);
387
388         if ( $record->field($tag) ) {
389             my @content = ();
390
391             for my $marcfield ($record->field($tag)) {
392                 if ( $subtags ) {
393                     my $str = '';
394                     for my $code (split //, $subtags) {
395                         if ( $marcfield->subfield($code) ) {
396                             $str .= $marcfield->subfield($code) . ' ';
397                         }
398                     }
399                     if ( not $str eq '') {
400                         push @content, $str;
401                     }
402                 } elsif ( $tag <= 10 ) {
403                     push @content, $marcfield->data();
404                 } else {
405                     push @content, $marcfield->as_string();
406                 }
407             }
408
409             if ( @content ) {
410                 $row->{$field} = \@content;
411                 push( @addnumberfields, $field );
412             }
413         }
414     }
415
416     $row->{'addnumberfields'} = \@addnumberfields;
417
418     return $row;
419 }
420
421 sub _isbn_replace {
422     my ($isbn) = @_;
423     return unless defined $isbn;
424     $isbn =~ s/ |-|\.//g;
425     $isbn =~ s/\|/ \| /g;
426     $isbn =~ s/\(/ \(/g;
427     return $isbn;
428 }
429
430 sub _create_connection {
431     my ( $server ) = @_;
432     my $option1= new ZOOM::Options();
433     $option1->option( 'async' => 1 );
434     $option1->option( 'elementSetName', 'F' );
435     $option1->option( 'preferredRecordSyntax', $server->{syntax} );
436     $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
437
438     if( $server->{servertype} eq 'sru' ) {
439         foreach( split ',', $server->{sru_options}//'' ) {
440             #first remove surrounding spaces at comma and equals-sign
441             s/^\s+|\s+$//g;
442             my @temp= split '=', $_, 2;
443             @temp= map { my $c=$_; $c=~s/^\s+|\s+$//g; $c; } @temp;
444             $option1->option( $temp[0] => $temp[1] ) if @temp;
445         }
446     } elsif( $server->{servertype} eq 'zed' ) {
447         $option1->option( 'databaseName',   $server->{db} );
448         $option1->option( 'user', $server->{userid} ) if $server->{userid};
449         $option1->option( 'password', $server->{password} ) if $server->{password};
450     }
451     my $obj= ZOOM::Connection->create($option1);
452     if( $server->{servertype} eq 'sru' ) {
453         my $host= $server->{host};
454         if( $host !~ /^https?:\/\// ) {
455             #Normally, host will not be prefixed by protocol.
456             #In that case we can (safely) assume http.
457             #In case someone prefixed with https, give it a try..
458             $host = 'http://' . $host;
459         }
460         $obj->connect( $host.':'.$server->{port}.'/'.$server->{db} );
461     } else {
462         $obj->connect( $server->{host}, $server->{port} );
463     }
464     return $obj;
465 }
466
467 sub _translate_query { #SRU query adjusted per server cf. srufields column
468     my ($server, $query) = @_;
469
470     #sru_fields is in format title=field,isbn=field,...
471     #if a field doesn't exist, try anywhere or remove [field]=
472     my @parts= split(',', $server->{sru_fields} );
473     my %trans= map { if( /=/ ) { ( $`,$' ) } else { () } } @parts;
474     my $any= $trans{srchany}?$trans{srchany}.'=':'';
475
476     my $q=$query;
477     foreach my $key (keys %trans) {
478         my $f=$trans{$key};
479         if( $f ) {
480             $q=~s/\[$key\]/$f/g;
481         } else {
482             $q=~s/\[$key\]=/$any/g;
483         }
484     }
485     $q=~s/\[\w+\]=/$any/g; # remove remaining fields (not found in field list)
486     return $q;
487 }
488
489 =head2 ImportBreedingAuth
490
491 ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random);
492
493     ImportBreedingAuth imports MARC records in the reservoir (import_records table).
494     ImportBreedingAuth is based on the ImportBreeding subroutine.
495
496 =cut
497
498 sub ImportBreedingAuth {
499     my ($marcrecord,$overwrite_auth,$filename,$encoding,$z3950random) = @_;
500     my $dbh = C4::Context->dbh;
501
502     my $batch_id = GetZ3950BatchId($filename);
503     my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
504
505     my $marcflavour = C4::Context->preference('marcflavour');
506     my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour;
507
508     # fields used for import results
509     my $imported=0;
510     my $alreadyindb = 0;
511     my $alreadyinfarm = 0;
512     my $notmarcrecord = 0;
513     my $breedingid;
514
515         # Normalize the record so it doesn't have separated diacritics
516         SetUTF8Flag($marcrecord);
517
518         if (scalar($marcrecord->fields()) == 0) {
519             $notmarcrecord++;
520         } else {
521             my $heading;
522             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
523
524             my $heading_authtype_code;
525             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
526
527             my $controlnumber;
528             $controlnumber = $marcrecord->field('001')->data;
529
530             #Check if the authority record already exists in the database...
531             my ($duplicateauthid,$duplicateauthvalue);
532             if ($marcrecord && $heading_authtype_code) {
533                 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority( $marcrecord, $heading_authtype_code);
534             }
535
536             if ($duplicateauthid && $overwrite_auth ne 2) {
537                 #If the authority record exists and $overwrite_auth doesn't equal 2, then mark it as already in the DB
538                 $alreadyindb++;
539             } else {
540                 if ($controlnumber && $heading) {
541                     $searchbreeding->execute($controlnumber,$heading);
542                     ($breedingid) = $searchbreeding->fetchrow;
543                 }
544                 if ($breedingid && $overwrite_auth eq '0') {
545                     $alreadyinfarm++;
546                 } else {
547                     if ($breedingid && $overwrite_auth eq '1') {
548                         ModAuthorityInBatch($breedingid, $marcrecord);
549                     } else {
550                         my $import_id = AddAuthToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
551                         $breedingid = $import_id;
552                     }
553                     $imported++;
554                 }
555             }
556         }
557     return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
558 }
559
560 =head2 Z3950SearchAuth
561
562 Z3950SearchAuth($pars, $template);
563
564 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
565 title, uniform title, subject, subjectsubdiv, srchany.
566 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
567 This code is used in cataloging/z3950_auth_search.
568 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
569
570 =cut
571
572 sub Z3950SearchAuth {
573     my ($pars, $template)= @_;
574
575     my $dbh   = C4::Context->dbh;
576     my @id= @{$pars->{id}};
577     my $random= $pars->{random};
578     my $page= $pars->{page};
579
580
581     my $show_next       = 0;
582     my $total_pages     = 0;
583     my $attr = '';
584     my $host;
585     my $server;
586     my $database;
587     my $port;
588     my $marcdata;
589     my @encoding;
590     my @results;
591     my $count;
592     my $record;
593     my @serverhost;
594     my @breeding_loop = ();
595
596     my @oConnection;
597     my @oResult;
598     my @errconn;
599     my @servers;
600     my $s = 0;
601     my $query;
602     my $nterms=0;
603
604     my $marcflavour = C4::Context->preference('marcflavour');
605     my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour;
606     my $authid= $pars->{authid};
607     my ( $zquery, $squery ) = _auth_build_query( $pars );
608     foreach my $servid (@id) {
609         my $sth = $dbh->prepare("select * from z3950servers where id=?");
610         $sth->execute($servid);
611         while ( $server = $sth->fetchrow_hashref ) {
612             $oConnection[$s] = _create_connection( $server );
613
614             $oResult[$s] =
615             $server->{servertype} eq 'zed'?
616                 $oConnection[$s]->search_pqf( $zquery ):
617                 $oConnection[$s]->search(new ZOOM::Query::CQL(
618                     _translate_query( $server, $squery )));
619             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
620             $servers[$s] = $server;
621             $s++;
622         }   ## while fetch
623     }    # foreach
624     my $nremaining  = $s;
625
626     while ( $nremaining-- ) {
627         my $k;
628         my $event;
629         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
630             $event = $oConnection[ $k - 1 ]->last_event();
631             last if $event == ZOOM::Event::ZEND;
632         }
633
634         if ( $k != 0 ) {
635             $k--;
636             my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
637             if ($error) {
638                 if ($error =~ m/^(10000|10007)$/ ) {
639                     push(@errconn, {'server' => $serverhost[$k]});
640                 }
641             }
642             else {
643                 my $numresults = $oResult[$k]->size();
644                 my $i;
645                 my $result = '';
646                 if ( $numresults > 0  and $numresults >= (($page-1)*20)) {
647                     $show_next = 1 if $numresults >= ($page*20);
648                     $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
649                     for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
650                         my $rec = $oResult[$k]->record($i);
651                         if ($rec) {
652                             my $marcrecord;
653                             my $marcdata;
654                             $marcdata   = $rec->raw();
655
656                             my ($charset_result, $charset_errors);
657                             if( $servers[$k]->{servertype} eq 'sru' ) {
658                                 $marcrecord = MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} );
659                             } else {
660                                 ( $marcrecord, $charset_result, $charset_errors ) = MarcToUTF8Record( $marcdata, $marc_type, $encoding[$k] );
661                             }
662                             my $heading;
663                             my $heading_authtype_code;
664                             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
665                             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
666
667                             my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth( $marcrecord, 2, $serverhost[$k], $encoding[$k], $random);
668                             my %row_data;
669                             $row_data{server}       = $servers[$k]->{'servername'};
670                             $row_data{breedingid}   = $breedingid;
671                             $row_data{heading}      = $heading;
672                             $row_data{authid}       = $authid;
673                             $row_data{heading_code}      = $heading_authtype_code;
674                             push( @breeding_loop, \%row_data );
675                         }
676                         else {
677                             push(@breeding_loop,{'server'=>$servers[$k]->{'servername'},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'authid'=>-1});
678                         }
679                     }
680                 }    #if $numresults
681             }
682         }    # if $k !=0
683
684         $template->param(
685             numberpending => $nremaining,
686             current_page => $page,
687             total_pages => $total_pages,
688             show_nextbutton => $show_next?1:0,
689             show_prevbutton => $page!=1,
690         );
691     } # while nremaining
692
693     #close result sets and connections
694     foreach(0..$s-1) {
695         $oResult[$_]->destroy();
696         $oConnection[$_]->destroy();
697     }
698
699     @servers = ();
700     foreach my $id (@id) {
701         push @servers, {id => $id};
702     }
703     $template->param(
704         breeding_loop => \@breeding_loop,
705         servers => \@servers,
706         errconn       => \@errconn
707     );
708 }
709
710 1;
711 __END__
712