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