File: | C4/Breeding.pm |
Coverage: | 24.5% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | |||||
17 | # with Koha; if not, write to the Free Software Foundation, Inc., | |||||
18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |||||
19 | ||||||
20 | 2 2 2 | 572 2 49 | use strict; | |||
21 | 2 2 2 | 8 2 90 | use warnings; | |||
22 | ||||||
23 | 2 2 2 | 207 31 1065 | use C4::Biblio; | |||
24 | 2 2 2 | 30 20 721 | use C4::Koha; | |||
25 | 2 2 2 | 23 12 243 | use C4::Charset; | |||
26 | 2 2 2 | 19 10 97 | use MARC::File::USMARC; | |||
27 | 2 2 2 | 404 25 544 | use C4::ImportBatch; | |||
28 | ||||||
29 | 2 2 2 | 27 16 233 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | |||
30 | ||||||
31 | BEGIN { | |||||
32 | # set the version for version checking | |||||
33 | 2 | 17 | $VERSION = 0.02; | |||
34 | 2 | 18 | require Exporter; | |||
35 | 2 | 34 | @ISA = qw(Exporter); | |||
36 | 2 | 2023 | @EXPORT = qw(&ImportBreeding &BreedingSearch); | |||
37 | } | |||||
38 | ||||||
39 - 71 | =head1 NAME C4::Breeding : module to add biblios to import_records via the breeding/reservoir API. =head1 SYNOPSIS use C4::Scan; &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type); C<$marcrecord> => the MARC::Record C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted. if set to 0 a biblio with the same isbn will be ignored (the previous will be kept) if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN possible in the breeding C<$encoding> => USMARC or UNIMARC. used for char_decoding. If not present, the parameter marcflavour is used instead C<$z3950random> => the random value created during a z3950 search result. =head1 DESCRIPTION ImportBreeding import MARC records in the reservoir (import_records/import_batches tables). the records can be properly encoded or not, we try to reencode them in utf-8 if needed. works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too. =head2 ImportBreeding ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type); TODO description =cut | |||||
72 | ||||||
73 | sub ImportBreeding { | |||||
74 | 0 | my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_; | ||||
75 | 0 | my @marcarray = split /\x1D/, $marcrecords; | ||||
76 | ||||||
77 | 0 | my $dbh = C4::Context->dbh; | ||||
78 | ||||||
79 | 0 | my $batch_id = 0; | ||||
80 | 0 | if ($batch_type eq 'z3950') { | ||||
81 | 0 | $batch_id = GetZ3950BatchId($filename); | ||||
82 | } else { | |||||
83 | # create a new one | |||||
84 | 0 | $batch_id = AddImportBatch('create_new', 'staging', 'batch', $filename, ''); | ||||
85 | } | |||||
86 | 0 | my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?"); | ||||
87 | 0 | my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?"); | ||||
88 | # FIXME -- not sure that this kind of checking is actually needed | |||||
89 | 0 | my $searchbreeding = $dbh->prepare("select import_record_id from import_biblios where isbn=? and title=?"); | ||||
90 | ||||||
91 | # $encoding = C4::Context->preference("marcflavour") unless $encoding; | |||||
92 | # fields used for import results | |||||
93 | 0 | my $imported=0; | ||||
94 | 0 | my $alreadyindb = 0; | ||||
95 | 0 | my $alreadyinfarm = 0; | ||||
96 | 0 | my $notmarcrecord = 0; | ||||
97 | 0 | my $breedingid; | ||||
98 | for (my $i=0;$i<=$#marcarray;$i++) { | |||||
99 | 0 | my ($marcrecord, $charset_result, $charset_errors); | ||||
100 | 0 | ($marcrecord, $charset_result, $charset_errors) = | ||||
101 | MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding); | |||||
102 | ||||||
103 | # warn "$i : $marcarray[$i]"; | |||||
104 | # FIXME - currently this does nothing | |||||
105 | 0 | my @warnings = $marcrecord->warnings(); | ||||
106 | ||||||
107 | 0 | if (scalar($marcrecord->fields()) == 0) { | ||||
108 | 0 | $notmarcrecord++; | ||||
109 | } else { | |||||
110 | 0 | my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,''); | ||||
111 | # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, | |||||
112 | # overwrite or ignore depending on user choice | |||||
113 | # drop every "special" char : spaces, - ... | |||||
114 | 0 | $oldbiblio->{isbn} = C4::Koha::_isbn_cleanup($oldbiblio->{isbn}); # FIXME C4::Koha::_isbn_cleanup should be public | ||||
115 | # search if biblio exists | |||||
116 | 0 | my $biblioitemnumber; | ||||
117 | 0 | if ($oldbiblio->{isbn}) { | ||||
118 | 0 | $searchisbn->execute($oldbiblio->{isbn}); | ||||
119 | 0 | ($biblioitemnumber) = $searchisbn->fetchrow; | ||||
120 | } else { | |||||
121 | 0 | if ($oldbiblio->{issn}) { | ||||
122 | 0 | $searchissn->execute($oldbiblio->{issn}); | ||||
123 | 0 | ($biblioitemnumber) = $searchissn->fetchrow; | ||||
124 | } | |||||
125 | } | |||||
126 | 0 | if ($biblioitemnumber && $overwrite_biblio ne 2) { | ||||
127 | 0 | $alreadyindb++; | ||||
128 | } else { | |||||
129 | # FIXME - in context of batch load, | |||||
130 | # rejecting records because already present in the reservoir | |||||
131 | # not correct in every case. | |||||
132 | # search in breeding farm | |||||
133 | 0 | if ($oldbiblio->{isbn}) { | ||||
134 | 0 | $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title}); | ||||
135 | 0 | ($breedingid) = $searchbreeding->fetchrow; | ||||
136 | } elsif ($oldbiblio->{issn}){ | |||||
137 | 0 | $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title}); | ||||
138 | 0 | ($breedingid) = $searchbreeding->fetchrow; | ||||
139 | } | |||||
140 | 0 | if ($breedingid && $overwrite_biblio eq '0') { | ||||
141 | 0 | $alreadyinfarm++; | ||||
142 | } else { | |||||
143 | 0 | if ($breedingid && $overwrite_biblio eq '1') { | ||||
144 | 0 | ModBiblioInBatch($breedingid, $marcrecord); | ||||
145 | } else { | |||||
146 | 0 | my $import_id = AddBiblioToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random); | ||||
147 | 0 | $breedingid = $import_id; | ||||
148 | } | |||||
149 | 0 | $imported++; | ||||
150 | } | |||||
151 | } | |||||
152 | } | |||||
153 | 0 | } | ||||
154 | 0 | return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid); | ||||
155 | } | |||||
156 | ||||||
157 | ||||||
158 - 169 | =head2 BreedingSearch ($count, @results) = &BreedingSearch($title,$isbn,$random); C<$title> contains the title, C<$isbn> contains isbn or issn, C<$random> contains the random seed from a z3950 search. C<$count> is the number of items in C<@results>. C<@results> is an array of references-to-hash; the keys are the items from the C<import_records> and C<import_biblios> tables of the Koha database. =cut | |||||
170 | ||||||
171 | sub BreedingSearch { | |||||
172 | 0 | my ($search,$isbn,$z3950random) = @_; | ||||
173 | 0 | my $dbh = C4::Context->dbh; | ||||
174 | 0 | my $count = 0; | ||||
175 | 0 | my ($query,@bind); | ||||
176 | 0 | my $sth; | ||||
177 | 0 | my @results; | ||||
178 | ||||||
179 | 0 | $query = "SELECT import_record_id, file_name, isbn, title, author | ||||
180 | FROM import_biblios | |||||
181 | JOIN import_records USING (import_record_id) | |||||
182 | JOIN import_batches USING (import_batch_id) | |||||
183 | WHERE "; | |||||
184 | 0 | if ($z3950random) { | ||||
185 | 0 | $query .= "z3950random = ?"; | ||||
186 | 0 | @bind=($z3950random); | ||||
187 | } else { | |||||
188 | 0 | $search =~ s/(\s+)/\%/g; | ||||
189 | 0 | @bind=(); | ||||
190 | 0 | if ($search) { | ||||
191 | 0 | $query .= "title like ? OR author like ?"; | ||||
192 | 0 | push(@bind,"%$search%", "%$search%"); | ||||
193 | } | |||||
194 | 0 | if ($search && $isbn) { | ||||
195 | 0 | $query .= " and "; | ||||
196 | } | |||||
197 | 0 | if ($isbn) { | ||||
198 | 0 | $query .= "isbn like ?"; | ||||
199 | 0 | push(@bind,"$isbn%"); | ||||
200 | } | |||||
201 | } | |||||
202 | 0 | $sth = $dbh->prepare($query); | ||||
203 | 0 | $sth->execute(@bind); | ||||
204 | 0 | while (my $data = $sth->fetchrow_hashref) { | ||||
205 | 0 | $results[$count] = $data; | ||||
206 | # FIXME - hack to reflect difference in name | |||||
207 | # of columns in old marc_breeding and import_records | |||||
208 | # There needs to be more separation between column names and | |||||
209 | # field names used in the templates </soapbox> | |||||
210 | 0 | $data->{'file'} = $data->{'file_name'}; | ||||
211 | 0 | $data->{'id'} = $data->{'import_record_id'}; | ||||
212 | 0 | $count++; | ||||
213 | } # while | |||||
214 | ||||||
215 | 0 | $sth->finish; | ||||
216 | 0 | return($count, @results); | ||||
217 | } # sub breedingsearch | |||||
218 | ||||||
219 | 1; |