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;
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
33 C4::Breeding : script to add a biblio in marc_breeding table.
38 &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random);
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.
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.
59 @EXPORT = qw(&ImportBreeding &BreedingSearch);
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
75 my $alreadyinfarm = 0;
76 my $notmarcrecord = 0;
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) {
84 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
86 if($oldbiblio->{isbn}){
87 $isbnlength = length($oldbiblio->{isbn});
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
97 if ($oldbiblio->{isbn}) {
98 $searchisbn->execute($oldbiblio->{isbn});
99 ($biblioitemnumber) = $searchisbn->fetchrow;
101 if ($oldbiblio->{issn}) {
102 $searchissn->execute($oldbiblio->{issn});
103 ($biblioitemnumber) = $searchissn->fetchrow;
106 if ($biblioitemnumber) {
109 # search in breeding farm
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;
118 if ($breedingid && $overwrite_biblio eq 0) {
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);
126 $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,$isbnlength),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random);
127 $breedingid=$dbh->{'mysql_insertid'};
134 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
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.
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.
151 my ($title,$isbn,$z3950random) = @_;
152 my $dbh = C4::Context->dbh;
158 $query = "Select id,file,isbn,title,author from marc_breeding where ";
160 $query .= "z3950random = ?";
161 @bind=($z3950random);
165 $query .= "title like ?";
166 push(@bind,"$title%");
168 if ($title && $isbn) {
172 $query .= "isbn like ?";
173 push(@bind,"$isbn%");
176 $sth = $dbh->prepare($query);
177 $sth->execute(@bind);
178 while (my $data = $sth->fetchrow_hashref) {
179 $results[$count] = $data;
184 return($count, @results);
185 } # sub breedingsearch
188 END { } # module clean-up code here (global destructor)