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);
63 ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random);
70 my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random) = @_;
71 my @marcarray = split /\x1D/, $marcrecords;
73 my $dbh = C4::Context->dbh;
74 my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
75 my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
76 my $searchbreeding = $dbh->prepare("select id from marc_breeding where isbn=? and title=?");
77 my $insertsql = $dbh->prepare("insert into marc_breeding (file,isbn,title,author,marc,encoding,z3950random) values(?,?,?,?,?,?,?)");
78 my $replacesql = $dbh->prepare("update marc_breeding set file=?,isbn=?,title=?,author=?,marc=?,encoding=?,z3950random=? where id=?");
80 $encoding = C4::Context->preference("marcflavour") unless $encoding;
81 # fields used for import results
84 my $alreadyinfarm = 0;
85 my $notmarcrecord = 0;
87 for (my $i=0;$i<=$#marcarray;$i++) {
88 my $marcrecord = FixEncoding($marcarray[$i]."\x1D");
90 my @warnings = $marcrecord->warnings();
92 if (scalar($marcrecord->fields()) == 0) {
95 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
97 if($oldbiblio->{isbn}){
98 $isbnlength = length($oldbiblio->{isbn});
100 # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, overwrite or ignore depending on user choice
101 # drop every "special" char : spaces, - ...
102 $oldbiblio->{isbn} =~ s/ |-|\.//g,
103 $oldbiblio->{isbn} = substr($oldbiblio->{isbn},0,$isbnlength);
104 $oldbiblio->{issn} =~ s/ |-|\.//g,
105 $oldbiblio->{issn} = substr($oldbiblio->{issn},0,10);
106 # search if biblio exists
107 my $biblioitemnumber;
108 if ($oldbiblio->{isbn}) {
109 $searchisbn->execute($oldbiblio->{isbn});
110 ($biblioitemnumber) = $searchisbn->fetchrow;
112 if ($oldbiblio->{issn}) {
113 $searchissn->execute($oldbiblio->{issn});
114 ($biblioitemnumber) = $searchissn->fetchrow;
117 if ($biblioitemnumber) {
120 # search in breeding farm
121 if ($oldbiblio->{isbn}) {
122 $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
123 ($breedingid) = $searchbreeding->fetchrow;
124 } elsif ($oldbiblio->{issn}){
125 $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
126 ($breedingid) = $searchbreeding->fetchrow;
128 if ($breedingid && $overwrite_biblio eq '0') {
132 $recoded = $marcrecord->as_usmarc();
133 if ($breedingid && $overwrite_biblio eq '1') {
134 $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,$isbnlength),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random,$breedingid);
136 $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,$isbnlength),$oldbiblio->{title},$oldbiblio->{author},$recoded,$encoding,$z3950random);
137 $breedingid=$dbh->{'mysql_insertid'};
144 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
148 =head2 BreedingSearch
150 ($count, @results) = &BreedingSearch($title,$isbn,$random);
151 C<$title> contains the title,
152 C<$isbn> contains isbn or issn,
153 C<$random> contains the random seed from a z3950 search.
155 C<$count> is the number of items in C<@results>. C<@results> is an
156 array of references-to-hash; the keys are the items from the C<marc_breeding> table of the Koha database.
161 my ($title,$isbn,$z3950random) = @_;
162 my $dbh = C4::Context->dbh;
168 $query = "Select id,file,isbn,title,author from marc_breeding where ";
170 $query .= "z3950random = ?";
171 @bind=($z3950random);
175 $query .= "title like ?";
176 push(@bind,"$title%");
178 if ($title && $isbn) {
182 $query .= "isbn like ?";
183 push(@bind,"$isbn%");
186 $sth = $dbh->prepare($query);
187 $sth->execute(@bind);
188 while (my $data = $sth->fetchrow_hashref) {
189 $results[$count] = $data;
194 return($count, @results);
195 } # sub breedingsearch
198 END { } # module clean-up code here (global destructor)