Bug 9238 - make fund pull down say 'select fund', not 'select budget'
[koha.git] / C4 / Breeding.pm
blob9527f0fa873aedbc4e006c5c8b07c54cce138aa6
1 package C4::Breeding;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
23 use C4::Biblio;
24 use C4::Koha;
25 use C4::Charset;
26 use MARC::File::USMARC;
27 use C4::ImportBatch;
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31 BEGIN {
32 # set the version for version checking
33 $VERSION = 3.07.00.049;
34 require Exporter;
35 @ISA = qw(Exporter);
36 @EXPORT = qw(&ImportBreeding &BreedingSearch &Z3950Search);
39 =head1 NAME
41 C4::Breeding : module to add biblios to import_records via
42 the breeding/reservoir API.
44 =head1 SYNOPSIS
46 use C4::Scan;
47 &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type);
49 C<$marcrecord> => the MARC::Record
50 C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
51 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
52 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN
53 possible in the breeding
54 C<$encoding> => USMARC
55 or UNIMARC. used for char_decoding.
56 If not present, the parameter marcflavour is used instead
57 C<$z3950random> => the random value created during a z3950 search result.
59 =head1 DESCRIPTION
61 ImportBreeding import MARC records in the reservoir (import_records/import_batches tables).
62 the records can be properly encoded or not, we try to reencode them in utf-8 if needed.
63 works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too.
65 =head2 ImportBreeding
67 ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type);
69 TODO description
71 =cut
73 sub ImportBreeding {
74 my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_;
75 my @marcarray = split /\x1D/, $marcrecords;
77 my $dbh = C4::Context->dbh;
79 my $batch_id = GetZ3950BatchId($filename);
80 my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
81 my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
82 # FIXME -- not sure that this kind of checking is actually needed
83 my $searchbreeding = $dbh->prepare("select import_record_id from import_biblios where isbn=? and title=?");
85 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
86 # fields used for import results
87 my $imported=0;
88 my $alreadyindb = 0;
89 my $alreadyinfarm = 0;
90 my $notmarcrecord = 0;
91 my $breedingid;
92 for (my $i=0;$i<=$#marcarray;$i++) {
93 my ($marcrecord, $charset_result, $charset_errors);
94 ($marcrecord, $charset_result, $charset_errors) =
95 MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
97 # Normalize the record so it doesn't have separated diacritics
98 SetUTF8Flag($marcrecord);
100 # warn "$i : $marcarray[$i]";
101 # FIXME - currently this does nothing
102 my @warnings = $marcrecord->warnings();
104 if (scalar($marcrecord->fields()) == 0) {
105 $notmarcrecord++;
106 } else {
107 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
108 # if isbn found and biblio does not exist, add it. If isbn found and biblio exists,
109 # overwrite or ignore depending on user choice
110 # drop every "special" char : spaces, - ...
111 $oldbiblio->{isbn} = C4::Koha::_isbn_cleanup($oldbiblio->{isbn}); # FIXME C4::Koha::_isbn_cleanup should be public
112 # search if biblio exists
113 my $biblioitemnumber;
114 if ($oldbiblio->{isbn}) {
115 $searchisbn->execute($oldbiblio->{isbn});
116 ($biblioitemnumber) = $searchisbn->fetchrow;
117 } else {
118 if ($oldbiblio->{issn}) {
119 $searchissn->execute($oldbiblio->{issn});
120 ($biblioitemnumber) = $searchissn->fetchrow;
123 if ($biblioitemnumber && $overwrite_biblio ne 2) {
124 $alreadyindb++;
125 } else {
126 # FIXME - in context of batch load,
127 # rejecting records because already present in the reservoir
128 # not correct in every case.
129 # search in breeding farm
130 if ($oldbiblio->{isbn}) {
131 $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
132 ($breedingid) = $searchbreeding->fetchrow;
133 } elsif ($oldbiblio->{issn}){
134 $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
135 ($breedingid) = $searchbreeding->fetchrow;
137 if ($breedingid && $overwrite_biblio eq '0') {
138 $alreadyinfarm++;
139 } else {
140 if ($breedingid && $overwrite_biblio eq '1') {
141 ModBiblioInBatch($breedingid, $marcrecord);
142 } else {
143 my $import_id = AddBiblioToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
144 $breedingid = $import_id;
146 $imported++;
151 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
155 =head2 BreedingSearch
157 ($count, @results) = &BreedingSearch($title,$isbn,$random);
158 C<$title> contains the title,
159 C<$isbn> contains isbn or issn,
160 C<$random> contains the random seed from a z3950 search.
162 C<$count> is the number of items in C<@results>. C<@results> is an
163 array of references-to-hash; the keys are the items from the C<import_records> and
164 C<import_biblios> tables of the Koha database.
166 =cut
168 sub BreedingSearch {
169 my ($search,$isbn,$z3950random) = @_;
170 my $dbh = C4::Context->dbh;
171 my $count = 0;
172 my ($query,@bind);
173 my $sth;
174 my @results;
176 $query = "SELECT import_record_id, file_name, isbn, title, author
177 FROM import_biblios
178 JOIN import_records USING (import_record_id)
179 JOIN import_batches USING (import_batch_id)
180 WHERE ";
181 if ($z3950random) {
182 $query .= "z3950random = ?";
183 @bind=($z3950random);
184 } else {
185 @bind=();
186 if (defined($search) && length($search)>0) {
187 $search =~ s/(\s+)/\%/g;
188 $query .= "title like ? OR author like ?";
189 push(@bind,"%$search%", "%$search%");
191 if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
192 $query .= " and ";
194 if (defined($isbn) && length($isbn)>0) {
195 $query .= "isbn like ?";
196 push(@bind,"$isbn%");
199 $sth = $dbh->prepare($query);
200 $sth->execute(@bind);
201 while (my $data = $sth->fetchrow_hashref) {
202 $results[$count] = $data;
203 # FIXME - hack to reflect difference in name
204 # of columns in old marc_breeding and import_records
205 # There needs to be more separation between column names and
206 # field names used in the templates </soapbox>
207 $data->{'file'} = $data->{'file_name'};
208 $data->{'id'} = $data->{'import_record_id'};
209 $count++;
210 } # while
212 $sth->finish;
213 return($count, @results);
214 } # sub breedingsearch
217 =head2 Z3950Search
219 Z3950Search($pars, $template);
221 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
222 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).
223 This code is used in acqui/z3950_search and cataloging/z3950_search.
224 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
226 =cut
228 sub Z3950Search {
229 my ($pars, $template)= @_;
231 my @id= @{$pars->{id}};
232 my $page= $pars->{page};
233 my $biblionumber= $pars->{biblionumber};
234 my $isbn= $pars->{isbn};
235 my $issn= $pars->{issn};
236 my $title= $pars->{title};
237 my $author= $pars->{author};
238 my $dewey= $pars->{dewey};
239 my $subject= $pars->{subject};
240 my $lccn= $pars->{lccn};
241 my $lccall= $pars->{lccall};
242 my $controlnumber= $pars->{controlnumber};
243 my $srchany= $pars->{srchany};
244 my $stdid= $pars->{stdid};
246 my $show_next = 0;
247 my $total_pages = 0;
248 my $term;
249 my @results;
250 my @breeding_loop = ();
251 my @oConnection;
252 my @oResult;
253 my @errconn;
254 my $s = 0;
255 my $query;
256 my $nterms=0;
257 my $imported=0;
258 my @serverinfo; #replaces former serverhost, servername, encoding
260 if ($isbn) {
261 $term=$isbn;
262 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
263 $nterms++;
265 if ($issn) {
266 $term=$issn;
267 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
268 $nterms++;
270 if ($title) {
271 $query .= " \@attr 1=4 \"$title\" ";
272 $nterms++;
274 if ($author) {
275 $query .= " \@attr 1=1003 \"$author\" ";
276 $nterms++;
278 if ($dewey) {
279 $query .= " \@attr 1=16 \"$dewey\" ";
280 $nterms++;
282 if ($subject) {
283 $query .= " \@attr 1=21 \"$subject\" ";
284 $nterms++;
286 if ($lccn) {
287 $query .= " \@attr 1=9 $lccn ";
288 $nterms++;
290 if ($lccall) {
291 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
292 $nterms++;
294 if ($controlnumber) {
295 $query .= " \@attr 1=12 \"$controlnumber\" ";
296 $nterms++;
298 if($srchany) {
299 $query .= " \@attr 1=1016 \"$srchany\" ";
300 $nterms++;
302 if($stdid) {
303 $query .= " \@attr 1=1007 \"$stdid\" ";
304 $nterms++;
306 for my $i (1..$nterms-1) {
307 $query = "\@and " . $query;
310 my $dbh = C4::Context->dbh;
311 foreach my $servid (@id) {
312 my $sth = $dbh->prepare("select * from z3950servers where id=?");
313 $sth->execute($servid);
314 while (my $server = $sth->fetchrow_hashref) {
315 my $option1= new ZOOM::Options();
316 $option1->option( 'async' => 1 );
317 $option1->option( 'elementSetName', 'F' );
318 $option1->option( 'databaseName', $server->{db} );
319 $option1->option( 'user', $server->{userid} ) if $server->{userid};
320 $option1->option( 'password', $server->{password} ) if $server->{password};
321 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
322 $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
323 $oConnection[$s]= create ZOOM::Connection($option1);
324 $oConnection[$s]->connect( $server->{host}, $server->{port} );
325 $serverinfo[$s]->{host}= $server->{host};
326 $serverinfo[$s]->{name}= $server->{name};
327 $serverinfo[$s]->{encd}= $server->{encoding} // "iso-5426";
328 $s++;
329 } ## while fetch
330 } # foreach
331 my $nremaining = $s;
333 for ( my $z = 0 ; $z < $s ; $z++ ) {
334 $oResult[$z] = $oConnection[$z]->search_pqf($query);
337 while ( $nremaining-- ) {
338 my $k;
339 my $event;
340 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
341 $event = $oConnection[ $k - 1 ]->last_event();
342 last if $event == ZOOM::Event::ZEND;
345 if ( $k != 0 ) {
346 $k--;
347 my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
348 if ($error) {
349 if ($error =~ m/^(10000|10007)$/ ) {
350 push(@errconn, { 'server' => $serverinfo[$k]->{host} } );
353 else {
354 my $numresults = $oResult[$k]->size();
355 my $i;
356 my $result = '';
357 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
358 $show_next = 1 if $numresults >= ($page*20);
359 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
360 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
361 if($oResult[$k]->record($i)) {
362 my $res=_handle_one_result($oResult[$k]->record($i), $serverinfo[$k], ++$imported, $biblionumber); #ignores error in sequence numbering
363 push @breeding_loop, $res if $res;
365 else {
366 push(@breeding_loop,{'server'=>$serverinfo[$k]->{name},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
369 } #if $numresults
371 } # if $k !=0
373 $template->param(
374 numberpending => $nremaining,
375 current_page => $page,
376 total_pages => $total_pages,
377 show_nextbutton => $show_next?1:0,
378 show_prevbutton => $page!=1,
380 } # while nremaining
382 #close result sets and connections
383 foreach(0..$s-1) {
384 $oResult[$_]->destroy();
385 $oConnection[$_]->destroy();
388 my @servers = ();
389 foreach my $id (@id) {
390 push @servers, {id => $id};
392 $template->param(
393 breeding_loop => \@breeding_loop,
394 servers => \@servers,
395 errconn => \@errconn
399 sub _handle_one_result {
400 my ($zoomrec, $servhref, $seq, $bib)= @_;
402 my $raw= $zoomrec->raw();
403 my ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encd}); #ignores charset return values
404 SetUTF8Flag($marcrecord);
406 #call to ImportBreeding replaced by next two calls for optimization
407 my $batch_id = GetZ3950BatchId($servhref->{name});
408 my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0, 0);
409 #FIXME passing 0 for z3950random
410 #Will eliminate this unused field in a followup report
411 #Last zero indicates: no update for batch record counts
414 #call to TransformMarcToKoha replaced by next call
415 #we only need six fields from the marc record
416 return _add_rowdata(
418 biblionumber => $bib,
419 server => $servhref->{name},
420 breedingid => $breedingid,
421 }, $marcrecord) if $breedingid;
424 sub _add_rowdata {
425 my ($row, $record)=@_;
426 my %fetch= (
427 title => 'biblio.title',
428 author => 'biblio.author',
429 isbn =>'biblioitems.isbn',
430 lccn =>'biblioitems.lccn', #LC control number (not call number)
431 edition =>'biblioitems.editionstatement',
432 date => 'biblio.copyrightdate', #MARC21
433 date2 => 'biblioitems.publicationyear', #UNIMARC
435 foreach my $k (keys %fetch) {
436 my ($t, $f)= split '\.', $fetch{$k};
437 $row= C4::Biblio::TransformMarcToKohaOneField($t, $f, $record, $row);
438 $row->{$k}= $row->{$f} if $k ne $f;
440 $row->{date}//= $row->{date2};
441 $row->{isbn}=_isbn_replace($row->{isbn});
442 return $row;
445 sub _isbn_replace {
446 my ($isbn) = @_;
447 return unless defined $isbn;
448 $isbn =~ s/ |-|\.//g;
449 $isbn =~ s/\|/ \| /g;
450 $isbn =~ s/\(/ \(/g;
451 return $isbn;
455 __END__