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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use MARC::File::USMARC;
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32 # set the version for version checking
33 $VERSION = 3.07.00.049;
36 @EXPORT = qw(&ImportBreeding &BreedingSearch &Z3950Search);
41 C4::Breeding : module to add biblios to import_records via
42 the breeding/reservoir API.
47 &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type);
49 C<$marcrecord> => the MARC::Record
50 C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
51 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
52 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN
53 possible in the breeding
54 C<$encoding> => USMARC
55 or UNIMARC. used for char_decoding.
56 If not present, the parameter marcflavour is used instead
57 C<$z3950random> => the random value created during a z3950 search result.
61 ImportBreeding import MARC records in the reservoir (import_records/import_batches tables).
62 the records can be properly encoded or not, we try to reencode them in utf-8 if needed.
63 works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too.
67 ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type);
74 my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_;
75 my @marcarray = split /\x1D/, $marcrecords;
77 my $dbh = C4::Context->dbh;
79 my $batch_id = GetZ3950BatchId($filename);
80 my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
81 my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
82 # FIXME -- not sure that this kind of checking is actually needed
83 my $searchbreeding = $dbh->prepare("select import_record_id from import_biblios where isbn=? and title=?");
85 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
86 # fields used for import results
89 my $alreadyinfarm = 0;
90 my $notmarcrecord = 0;
92 for (my $i=0;$i<=$#marcarray;$i++) {
93 my ($marcrecord, $charset_result, $charset_errors);
94 ($marcrecord, $charset_result, $charset_errors) =
95 MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
97 # Normalize the record so it doesn't have separated diacritics
98 SetUTF8Flag($marcrecord);
100 # warn "$i : $marcarray[$i]";
101 # FIXME - currently this does nothing
102 my @warnings = $marcrecord->warnings();
104 if (scalar($marcrecord->fields()) == 0) {
107 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
108 # if isbn found and biblio does not exist, add it. If isbn found and biblio exists,
109 # overwrite or ignore depending on user choice
110 # drop every "special" char : spaces, - ...
111 $oldbiblio->{isbn} = C4::Koha::_isbn_cleanup($oldbiblio->{isbn}); # FIXME C4::Koha::_isbn_cleanup should be public
112 # search if biblio exists
113 my $biblioitemnumber;
114 if ($oldbiblio->{isbn}) {
115 $searchisbn->execute($oldbiblio->{isbn});
116 ($biblioitemnumber) = $searchisbn->fetchrow;
118 if ($oldbiblio->{issn}) {
119 $searchissn->execute($oldbiblio->{issn});
120 ($biblioitemnumber) = $searchissn->fetchrow;
123 if ($biblioitemnumber && $overwrite_biblio ne 2) {
126 # FIXME - in context of batch load,
127 # rejecting records because already present in the reservoir
128 # not correct in every case.
129 # search in breeding farm
130 if ($oldbiblio->{isbn}) {
131 $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
132 ($breedingid) = $searchbreeding->fetchrow;
133 } elsif ($oldbiblio->{issn}){
134 $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
135 ($breedingid) = $searchbreeding->fetchrow;
137 if ($breedingid && $overwrite_biblio eq '0') {
140 if ($breedingid && $overwrite_biblio eq '1') {
141 ModBiblioInBatch($breedingid, $marcrecord);
143 my $import_id = AddBiblioToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
144 $breedingid = $import_id;
151 return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
155 =head2 BreedingSearch
157 ($count, @results) = &BreedingSearch($title,$isbn,$random);
158 C<$title> contains the title,
159 C<$isbn> contains isbn or issn,
160 C<$random> contains the random seed from a z3950 search.
162 C<$count> is the number of items in C<@results>. C<@results> is an
163 array of references-to-hash; the keys are the items from the C<import_records> and
164 C<import_biblios> tables of the Koha database.
169 my ($search,$isbn,$z3950random) = @_;
170 my $dbh = C4::Context->dbh;
176 $query = "SELECT import_record_id, file_name, isbn, title, author
178 JOIN import_records USING (import_record_id)
179 JOIN import_batches USING (import_batch_id)
182 $query .= "z3950random = ?";
183 @bind=($z3950random);
186 if (defined($search) && length($search)>0) {
187 $search =~ s/(\s+)/\%/g;
188 $query .= "title like ? OR author like ?";
189 push(@bind,"%$search%", "%$search%");
191 if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
194 if (defined($isbn) && length($isbn)>0) {
195 $query .= "isbn like ?";
196 push(@bind,"$isbn%");
199 $sth = $dbh->prepare($query);
200 $sth->execute(@bind);
201 while (my $data = $sth->fetchrow_hashref) {
202 $results[$count] = $data;
203 # FIXME - hack to reflect difference in name
204 # of columns in old marc_breeding and import_records
205 # There needs to be more separation between column names and
206 # field names used in the templates </soapbox>
207 $data->{'file'} = $data->{'file_name'};
208 $data->{'id'} = $data->{'import_record_id'};
213 return($count, @results);
214 } # sub breedingsearch
219 Z3950Search($pars, $template);
221 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
222 Also it should contain an arrayref id that points to a list of id's of the z3950 targets to be queried (see z3950servers table).
223 This code is used in acqui/z3950_search and cataloging/z3950_search.
224 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
229 my ($pars, $template)= @_;
231 my $dbh = C4::Context->dbh;
232 my @id= @{$pars->{id}};
233 my $random= $pars->{random};
234 my $page= $pars->{page};
235 my $biblionumber= $pars->{biblionumber};
237 my $isbn= $pars->{isbn};
238 my $issn= $pars->{issn};
239 my $title= $pars->{title};
240 my $author= $pars->{author};
241 my $dewey= $pars->{dewey};
242 my $subject= $pars->{subject};
243 my $lccn= $pars->{lccn};
244 my $lccall= $pars->{lccall};
245 my $controlnumber= $pars->{controlnumber};
267 my @breeding_loop = ();
277 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
282 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
286 utf8::decode($title);
287 $query .= " \@attr 1=4 \"$title\" ";
291 utf8::decode($author);
292 $query .= " \@attr 1=1003 \"$author\" ";
296 $query .= " \@attr 1=16 \"$dewey\" ";
300 utf8::decode($subject);
301 $query .= " \@attr 1=21 \"$subject\" ";
305 $query .= " \@attr 1=9 $lccn ";
309 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
312 if ($controlnumber) {
313 $query .= " \@attr 1=12 \"$controlnumber\" ";
316 for my $i (1..$nterms-1) {
317 $query = "\@and " . $query;
320 foreach my $servid (@id) {
321 my $sth = $dbh->prepare("select * from z3950servers where id=?");
322 $sth->execute($servid);
323 while ( $server = $sth->fetchrow_hashref ) {
324 my $option1 = new ZOOM::Options();
325 $option1->option( 'async' => 1 );
326 $option1->option( 'elementSetName', 'F' );
327 $option1->option( 'databaseName', $server->{db} );
328 $option1->option( 'user', $server->{userid} ) if $server->{userid};
329 $option1->option( 'password', $server->{password} )
330 if $server->{password};
331 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
332 $oConnection[$s] = create ZOOM::Connection($option1);
333 $oConnection[$s]->connect( $server->{host}, $server->{port} );
334 $serverhost[$s] = $server->{host};
335 $servername[$s] = $server->{name};
336 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
343 for ( my $z = 0 ; $z < $s ; $z++ ) {
344 $oResult[$z] = $oConnection[$z]->search_pqf($query);
347 while ( $nremaining-- ) {
350 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
351 $event = $oConnection[ $k - 1 ]->last_event();
352 last if $event == ZOOM::Event::ZEND;
357 my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
359 if ($error =~ m/^(10000|10007)$/ ) {
360 push(@errconn, {'server' => $serverhost[$k]});
364 my $numresults = $oResult[$k]->size();
367 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
368 $show_next = 1 if $numresults >= ($page*20);
369 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
370 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
371 my $rec = $oResult[$k]->record($i);
374 $marcdata = $rec->raw();
375 my ($charset_result, $charset_errors);
376 ($marcrecord, $charset_result, $charset_errors)= MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
377 # Normalize the record so it doesn't have separated diacritics
378 SetUTF8Flag($marcrecord);
379 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
380 $oldbiblio->{isbn} =~ s/ |-|\.//g if $oldbiblio->{isbn};
381 # pad | and ( with spaces to allow line breaks in the HTML
382 $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
383 $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
384 $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
385 # pad | and ( with spaces to allow line breaks in the HTML
386 $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
387 $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
388 my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
390 $row_data{server} = $servername[$k];
391 $row_data{isbn} = $oldbiblio->{isbn};
392 $row_data{lccn} = $oldbiblio->{lccn};
393 $row_data{title} = $oldbiblio->{title};
394 $row_data{author} = $oldbiblio->{author};
395 $row_data{breedingid} = $breedingid;
396 $row_data{biblionumber} = $biblionumber;
397 push( @breeding_loop, \%row_data );
400 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
406 my $numberpending = $nremaining - 1;
409 foreach my $id (@id) {
410 push(@servers,{id => $id});
414 breeding_loop => \@breeding_loop,
415 server => $servername[$k],
416 numberpending => $numberpending,
417 current_page => $page,
418 servers => \@servers,
419 total_pages => $total_pages,
420 show_nextbutton => $show_next?1:0,
421 show_prevbutton => $page!=1,
427 breeding_loop => \@breeding_loop,
428 numberpending => $nremaining > 0 ? $nremaining : 0,