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
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
23 use MARC
::File
::USMARC
;
27 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29 # set the version for version checking
34 C4::Breeding : module to add biblios to import_records via
35 the breeding/reservoir API.
40 &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type);
42 C<$marcrecord> => the MARC::Record
43 C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
44 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
45 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN
46 possible in the breeding
47 C<$encoding> => USMARC
48 or UNIMARC. used for char_decoding.
49 If not present, the parameter marcflavour is used instead
50 C<$z3950random> => the random value created during a z3950 search result.
54 ImportBreeding import MARC records in the reservoir (import_records/import_batches tables).
55 the records can be properly encoded or not, we try to reencode them in utf-8 if needed.
56 works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too.
57 the FixEncoding sub is in Koha.pm, as it's a general usage sub.
62 @EXPORT = qw(&ImportBreeding &BreedingSearch);
66 ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type);
73 my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_;
74 my @marcarray = split /\x1D/, $marcrecords;
76 my $dbh = C4
::Context
->dbh;
79 if ($batch_type eq 'z3950') {
80 $batch_id = GetZ3950BatchId
($filename);
83 $batch_id = AddImportBatch
('create_new', 'staging', 'batch', $filename, '');
85 my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
86 my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
87 # FIXME -- not sure that this kind of checking is actually needed
88 my $searchbreeding = $dbh->prepare("select import_record_id from import_biblios where isbn=? and title=?");
90 $encoding = C4
::Context
->preference("marcflavour") unless $encoding;
91 # fields used for import results
94 my $alreadyinfarm = 0;
95 my $notmarcrecord = 0;
97 for (my $i=0;$i<=$#marcarray;$i++) {
98 my $marcrecord = FixEncoding
($marcarray[$i]."\x1D");
100 # FIXME - currently this does nothing
101 my @warnings = $marcrecord->warnings();
103 if (scalar($marcrecord->fields()) == 0) {
106 my $oldbiblio = TransformMarcToKoha
($dbh,$marcrecord,'');
107 # if isbn found and biblio does not exist, add it. If isbn found and biblio exists,
108 # overwrite or ignore depending on user choice
109 # drop every "special" char : spaces, - ...
110 $oldbiblio->{isbn
} =~ s/\(.*$//;
111 $oldbiblio->{isbn
} =~ tr/ -_//;
112 $oldbiblio->{isbn
} = uc $oldbiblio->{isbn
};
113 # search if biblio exists
114 my $biblioitemnumber;
115 if ($oldbiblio->{isbn
}) {
116 $searchisbn->execute($oldbiblio->{isbn
});
117 ($biblioitemnumber) = $searchisbn->fetchrow;
119 if ($oldbiblio->{issn
}) {
120 $searchissn->execute($oldbiblio->{issn
});
121 ($biblioitemnumber) = $searchissn->fetchrow;
124 if ($biblioitemnumber && $overwrite_biblio ne 2) {
127 # FIXME - in context of batch load,
128 # rejecting records because already present in the reservoir
129 # not correct in every case.
130 # search in breeding farm
131 if ($oldbiblio->{isbn
}) {
132 $searchbreeding->execute($oldbiblio->{isbn
},$oldbiblio->{title
});
133 ($breedingid) = $searchbreeding->fetchrow;
134 } elsif ($oldbiblio->{issn
}){
135 $searchbreeding->execute($oldbiblio->{issn
},$oldbiblio->{title
});
136 ($breedingid) = $searchbreeding->fetchrow;
138 if ($breedingid && $overwrite_biblio eq '0') {
141 if ($breedingid && $overwrite_biblio eq '1') {
142 ModBiblioInBatch
($breedingid, $marcrecord);
144 my $import_id = AddBiblioToBatch
($batch_id, $imported, $marcrecord, $encoding, $z3950random);
145 $breedingid = $import_id;
152 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
156 =head2 BreedingSearch
158 ($count, @results) = &BreedingSearch($title,$isbn,$random);
159 C<$title> contains the title,
160 C<$isbn> contains isbn or issn,
161 C<$random> contains the random seed from a z3950 search.
163 C<$count> is the number of items in C<@results>. C<@results> is an
164 array of references-to-hash; the keys are the items from the C<import_records> and
165 C<import_biblios> tables of the Koha database.
170 my ($title,$isbn,$z3950random) = @_;
171 my $dbh = C4
::Context
->dbh;
177 $query = "SELECT import_record_id, file_name, isbn, title, author
179 JOIN import_records USING (import_record_id)
180 JOIN import_batches USING (import_batch_id)
183 $query .= "z3950random = ?";
184 @bind=($z3950random);
188 $query .= "title like ?";
189 push(@bind,"$title%");
191 if ($title && $isbn) {
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'};
213 return($count, @results);
214 } # sub breedingsearch
217 END { } # module clean-up code here (global destructor)