New XML API
[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::Search;
23 use MARC::File::USMARC;
24 use MARC::Record;
25 use Encode;
26 require Exporter;
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::Breeding : script to add a biblio in marc_breeding table.
35
36 =head1 SYNOPSIS
37         &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random);
38
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.
47
48 =head1 DESCRIPTION
49
50 This is for depository of records coming from z3950 or directly imported.
51
52 =cut
53
54 @ISA = qw(Exporter);
55 @EXPORT = qw(&ImportBreeding &BreedingSearch);
56
57 sub  ImportBreeding {
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;
62 my @kohafields;
63 my @values;
64 my @relations;
65 my $sort;
66 my @and_or;
67 my @results;
68 my $count;
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");
71
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
76         my $imported=0;
77         my $alreadyindb = 0;
78         my $alreadyinfarm = 0;
79         my $notmarcrecord = 0;
80         my $breedingid;
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) {
87                         $notmarcrecord++;
88                 } else {
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
98                         my $biblioitemnumber;
99                     if ( !$z3950random){
100                         if ($oldbiblio->{isbn}) {
101                         push @kohafields,"isbn";
102                         push @values,$oldbiblio->{isbn};
103                         push @relations,"";
104                         push @and_or,"";
105                         ($count,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
106                         } else {
107                         push @kohafields,"issn";
108                         push @values,$oldbiblio->{issn};
109                         push @relations,"";
110                         push @and_or,"";
111                         $sort="";
112                         ($count,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
113                         }
114                      }
115                         if ($count>0 && !$z3950random) {
116                                 $alreadyindb++;
117                         } else {
118                                 # search in breeding farm
119                                 
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;
126                                 }
127                                 if ($breedingid && $overwrite_biblio eq 0) {
128                                         $alreadyinfarm++;
129                                 } else {
130                                         my $recoded=MARC::Record->new_from_xml($marcxml,"UTF-8");
131                                         $recoded->encoding('UTF-8');
132                                         
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);
135                                         } else {
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;
139                                         }
140                                         $imported++;
141                                 }
142                         }
143                 }
144         #}
145         return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
146 }
147
148
149 =item BreedingSearch
150
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.
155
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.
158
159 =cut
160
161 sub BreedingSearch {
162         my ($title,$isbn,$z3950random) = @_;
163         my $dbh   = C4::Context->dbh;
164         my $count = 0;
165         my ($query,@bind);
166         my $sth;
167         my @results;
168
169         $query = "Select id,file,isbn,title,author,classification,subclass from marc_breeding where ";
170         if ($z3950random) {
171                 $query .= "z3950random = ?";
172                 @bind=($z3950random);
173         } else {
174             @bind=();
175                 if ($title) {
176                         $query .= "title like ?";
177                         push(@bind,"$title%");
178                 }
179                 if ($title && $isbn) {
180                         $query .= " and ";
181                 }
182                 if ($isbn) {
183                         $query .= "isbn like ?";
184                         push(@bind,"$isbn%");
185                 }
186         }
187         $sth   = $dbh->prepare($query);
188         $sth->execute(@bind);
189         while (my $data = $sth->fetchrow_hashref) {
190                         $results[$count] = $data;
191                         $count++;
192         } # while
193
194         $sth->finish;
195         return($count, @results);
196 } # sub breedingsearch
197
198
199 END { }       # module clean-up code here (global destructor)