unimarc bugfix : the encoding is in field 100 in UNIMARC. when TransformHTMLtoXML...
[koha.git] / C4 / Breeding.pm
1 package C4::Breeding;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Biblio;
22 use C4::Koha;
23 use MARC::File::USMARC;
24 require Exporter;
25
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 =head1 NAME
32
33 C4::Breeding : script to add a biblio in marc_breeding table.
34
35 =head1 SYNOPSIS
36
37     use C4::Scan;
38     &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random);
39
40     C<$marcrecord> => the MARC::Record
41     C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
42                                 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
43                                 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN possible in the breeding
44     C<$encoding> => USMARC
45                         or UNIMARC. used for char_decoding.
46                         If not present, the parameter marcflavour is used instead
47     C<$z3950random> => the random value created during a z3950 search result.
48
49 =head1 DESCRIPTION
50
51     ImportBreeding import MARC records in the reservoir (marc_breeding table).
52     the records can be properly encoded or not, we try to reencode them in utf-8 if needed.
53     works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too.
54     the FixEncoding sub is in Koha.pm, as it's a general usage sub.
55
56 =cut
57
58 @ISA = qw(Exporter);
59 @EXPORT = qw(&ImportBreeding &BreedingSearch);
60
61 sub  ImportBreeding {
62     my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random) = @_;
63     my @marcarray = split /\x1D/, $marcrecords;
64     my $dbh = C4::Context->dbh;
65     my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
66     my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
67     my $searchbreeding = $dbh->prepare("select id from marc_breeding
68 where isbn=? and title=?");
69     my $insertsql = $dbh->prepare("insert into marc_breeding (file,isbn,title,author,marc,encoding,z3950random) values(?,?,?,?,?,?,?)");
70     my $replacesql = $dbh->prepare("update marc_breeding set file=?,isbn=?,title=?,author=?,marc=?,encoding=?,z3950random=? where id=?");
71     $encoding = C4::Context->preference("marcflavour") unless $encoding;
72     # fields used for import results
73     my $imported=0;
74     my $alreadyindb = 0;
75     my $alreadyinfarm = 0;
76     my $notmarcrecord = 0;
77     my $breedingid;
78     for (my $i=0;$i<=$#marcarray;$i++) {
79         my $marcrecord = FixEncoding($marcarray[$i]."\x1D");
80         my @warnings = $marcrecord->warnings();
81         if (scalar($marcrecord->fields()) == 0) {
82             $notmarcrecord++;
83         } else {
84             my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
85             my $isbnlength=10;
86             if($oldbiblio->{isbn}){
87                 $isbnlength = length($oldbiblio->{isbn});
88             }
89             # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, overwrite or ignore depending on user choice
90             # drop every "special" char : spaces, - ...
91             $oldbiblio->{isbn} =~ s/ |-|\.//g,
92             $oldbiblio->{isbn} = substr($oldbiblio->{isbn},0,$isbnlength);
93             $oldbiblio->{issn} =~ s/ |-|\.//g,
94             $oldbiblio->{issn} = substr($oldbiblio->{issn},0,10);
95             # search if biblio exists
96             my $biblioitemnumber;
97             if ($oldbiblio->{isbn}) {
98                 $searchisbn->execute($oldbiblio->{isbn});
99                 ($biblioitemnumber) = $searchisbn->fetchrow;
100             } else {
101                 if ($oldbiblio->{issn}) {
102                                 $searchissn->execute($oldbiblio->{issn});
103                                 ($biblioitemnumber) = $searchissn->fetchrow;
104                                 }
105             }
106             if ($biblioitemnumber) {
107                 $alreadyindb++;
108             } else {
109                 # search in breeding farm
110 #                               my $breedingid;
111                 if ($oldbiblio->{isbn}) {
112                     $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
113                     ($breedingid) = $searchbreeding->fetchrow;
114                 } elsif ($oldbiblio->{issn}){
115                     $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
116                     ($breedingid) = $searchbreeding->fetchrow;
117                 }
118                 if ($breedingid && $overwrite_biblio eq 0) {
119                     $alreadyinfarm++;
120                 } else {
121                     my $recoded;
122                     $recoded = $marcrecord->as_usmarc();
123                     if ($breedingid && $overwrite_biblio eq 1) {
124                         $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,$isbnlength),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random,$breedingid);
125                     } else {
126                         $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,$isbnlength),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random);
127                     $breedingid=$dbh->{'mysql_insertid'};
128                     }
129                     $imported++;
130                 }
131             }
132         }
133     }
134     return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
135 }
136
137
138 =item BreedingSearch
139
140 ($count, @results) = &BreedingSearch($title,$isbn,$random);
141 C<$title> contains the title,
142 C<$isbn> contains isbn or issn,
143 C<$random> contains the random seed from a z3950 search.
144
145 C<$count> is the number of items in C<@results>. C<@results> is an
146 array of references-to-hash; the keys are the items from the C<marc_breeding> table of the Koha database.
147
148 =cut
149
150 sub BreedingSearch {
151     my ($title,$isbn,$z3950random) = @_;
152     my $dbh   = C4::Context->dbh;
153     my $count = 0;
154     my ($query,@bind);
155     my $sth;
156     my @results;
157
158     $query = "Select id,file,isbn,title,author from marc_breeding where ";
159     if ($z3950random) {
160         $query .= "z3950random = ?";
161         @bind=($z3950random);
162     } else {
163         @bind=();
164         if ($title) {
165             $query .= "title like ?";
166             push(@bind,"$title%");
167         }
168         if ($title && $isbn) {
169             $query .= " and ";
170         }
171         if ($isbn) {
172             $query .= "isbn like ?";
173             push(@bind,"$isbn%");
174         }
175     }
176     $sth   = $dbh->prepare($query);
177     $sth->execute(@bind);
178     while (my $data = $sth->fetchrow_hashref) {
179             $results[$count] = $data;
180             $count++;
181     } # while
182
183     $sth->finish;
184     return($count, @results);
185 } # sub breedingsearch
186
187
188 END { }       # module clean-up code here (global destructor)