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 : script to add a biblio in marc_breeding table.
37 &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random);
39 C<$marcrecord> => the MARC::Record
40 C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
41 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
42 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN possible in the breeding
43 C<$encoding> => USMARC
44 or UNIMARC. used for char_decoding.
45 If not present, the parameter marcflavour is used instead
46 C<$z3950random> => the random value created during a z3950 search result.
50 This is for depository of records coming from z3950 or directly imported.
55 @EXPORT = qw(&ImportBreeding &BreedingSearch);
58 my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random) = @_;
59 ## use marc:batch send them in one by one
60 # my @marcarray = split /\x1D/, $marcrecords;
61 my $dbh = C4::Context->dbh;
69 my $searchbreeding = $dbh->prepare("select id from marc_breeding where isbn=? and title=?");
70 my $findbreedingid = $dbh->prepare("select max(id) from marc_breeding");
72 my $insertsql = $dbh->prepare("insert into marc_breeding (file,isbn,title,author,marc,encoding,z3950random,classification,subclass) values(?,?,?,?,?,?,?,?,?)");
73 my $replacesql = $dbh->prepare("update marc_breeding set file=?,isbn=?,title=?,author=?,marc=?,encoding=?,z3950random=?,classification=?,subclass=? where id=?");
74 $encoding = C4::Context->preference("marcflavour") unless $encoding;
75 # fields used for import results
78 my $alreadyinfarm = 0;
79 my $notmarcrecord = 0;
81 # for (my $i=0;$i<=$#marcarray;$i++) {
82 my $marcrecord = MARC::File::USMARC::decode($marcrecords);
83 my $marcxml=$marcrecord->as_xml_record($marcrecord);
84 $marcxml=Encode::encode('utf8',$marcxml);
85 my @warnings = $marcrecord->warnings();
86 if (scalar($marcrecord->fields()) == 0) {
89 my $xmlhash=XML_xml2hash_onerecord($marcxml);
90 my $oldbiblio = XMLmarc2koha_onerecord($dbh,$xmlhash,'biblios');
91 # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, overwrite or ignore depending on user choice
92 # drop every "special" char : spaces, - ...
93 $oldbiblio->{isbn} =~ s/ |-|\.//g,
94 $oldbiblio->{isbn} = substr($oldbiblio->{isbn},0,10);
95 $oldbiblio->{issn} =~ s/ |-|\.//g,
96 $oldbiblio->{issn} = substr($oldbiblio->{issn},0,10);
97 # search if biblio exists
100 if ($oldbiblio->{isbn}) {
101 push @kohafields,"isbn";
102 push @values,$oldbiblio->{isbn};
105 ($count,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
107 push @kohafields,"issn";
108 push @values,$oldbiblio->{issn};
112 ($count,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
115 if ($count>0 && !$z3950random) {
118 # search in breeding farm
120 if ($oldbiblio->{isbn}) {
121 $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
122 ($breedingid) = $searchbreeding->fetchrow;
123 } elsif ($oldbiblio->{issn}){
124 $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
125 ($breedingid) = $searchbreeding->fetchrow;
127 if ($breedingid && $overwrite_biblio eq 0) {
130 my $recoded=MARC::Record->new_from_xml($marcxml,"UTF-8");
131 $recoded->encoding('UTF-8');
133 if ($breedingid && $overwrite_biblio eq 1) {
134 $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded->as_usmarc,$encoding,$z3950random,$oldbiblio->{classification},$oldbiblio->{subclass},$breedingid);
136 $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded->as_usmarc,$encoding,$z3950random,$oldbiblio->{classification},$oldbiblio->{subclass});
137 $findbreedingid->execute;
138 $breedingid=$findbreedingid->fetchrow;
145 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
151 ($count, @results) = &BreedingSearch($title,$isbn,$random);
152 C<$title> contains the title,
153 C<$isbn> contains isbn or issn,
154 C<$random> contains the random seed from a z3950 search.
156 C<$count> is the number of items in C<@results>. C<@results> is an
157 array of references-to-hash; the keys are the items from the C<marc_breeding> table of the Koha database.
162 my ($title,$isbn,$z3950random) = @_;
163 my $dbh = C4::Context->dbh;
169 $query = "Select id,file,isbn,title,author,classification,subclass from marc_breeding where ";
171 $query .= "z3950random = ?";
172 @bind=($z3950random);
176 $query .= "title like ?";
177 push(@bind,"$title%");
179 if ($title && $isbn) {
183 $query .= "isbn like ?";
184 push(@bind,"$isbn%");
187 $sth = $dbh->prepare($query);
188 $sth->execute(@bind);
189 while (my $data = $sth->fetchrow_hashref) {
190 $results[$count] = $data;
195 return($count, @results);
196 } # sub breedingsearch
199 END { } # module clean-up code here (global destructor)