Field weighting applied to ranked searches. A new facets table in mysql db
[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                         my $facets;
100                     if ( !$z3950random){
101                         if ($oldbiblio->{isbn}) {
102                         push @kohafields,"isbn";
103                         push @values,$oldbiblio->{isbn};
104                         push @relations,"";
105                         push @and_or,"";
106                         
107                         ($count,$facets,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
108                         } else {
109                         push @kohafields,"issn";
110                         push @values,$oldbiblio->{issn};
111                         push @relations,"";
112                         push @and_or,"";
113                         $sort="";
114                         ($count,$facets,@results)=ZEBRAsearch_kohafields(\@kohafields,\@values,\@relations);
115                         }
116                      }
117                         if ($count>0 && !$z3950random) {
118                                 $alreadyindb++;
119                         } else {
120                                 # search in breeding farm
121                                 
122                                 if ($oldbiblio->{isbn}) {
123                                         $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
124                                         ($breedingid) = $searchbreeding->fetchrow;
125                                 } elsif ($oldbiblio->{issn}){
126                                         $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
127                                         ($breedingid) = $searchbreeding->fetchrow;
128                                 }
129                                 if ($breedingid && $overwrite_biblio eq 0) {
130                                         $alreadyinfarm++;
131                                 } else {
132                                         my $recoded=MARC::Record->new_from_xml($marcxml,"UTF-8");
133                                         $recoded->encoding('UTF-8');
134                                         
135                                         if ($breedingid && $overwrite_biblio eq 1) {
136                                                 $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded->as_usmarc,$encoding,$z3950random,$oldbiblio->{classification},$oldbiblio->{subclass},$breedingid);
137                                         } else {
138                                                 $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded->as_usmarc,$encoding,$z3950random,$oldbiblio->{classification},$oldbiblio->{subclass});
139                                         $findbreedingid->execute;
140                                         $breedingid=$findbreedingid->fetchrow;
141                                         }
142                                         $imported++;
143                                 }
144                         }
145                 }
146         #}
147         return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
148 }
149
150
151 =item BreedingSearch
152
153   ($count, @results) = &BreedingSearch($title,$isbn,$random);
154 C<$title> contains the title,
155 C<$isbn> contains isbn or issn,
156 C<$random> contains the random seed from a z3950 search.
157
158 C<$count> is the number of items in C<@results>. C<@results> is an
159 array of references-to-hash; the keys are the items from the C<marc_breeding> table of the Koha database.
160
161 =cut
162
163 sub BreedingSearch {
164         my ($title,$isbn,$z3950random) = @_;
165         my $dbh   = C4::Context->dbh;
166         my $count = 0;
167         my ($query,@bind);
168         my $sth;
169         my @results;
170
171         $query = "Select id,file,isbn,title,author,classification,subclass from marc_breeding where ";
172         if ($z3950random) {
173                 $query .= "z3950random = ?";
174                 @bind=($z3950random);
175         } else {
176             @bind=();
177                 if ($title) {
178                         $query .= "title like ?";
179                         push(@bind,"$title%");
180                 }
181                 if ($title && $isbn) {
182                         $query .= " and ";
183                 }
184                 if ($isbn) {
185                         $query .= "isbn like ?";
186                         push(@bind,"$isbn%");
187                 }
188         }
189         $sth   = $dbh->prepare($query);
190         $sth->execute(@bind);
191         while (my $data = $sth->fetchrow_hashref) {
192                         $results[$count] = $data;
193                         $count++;
194         } # while
195
196         $sth->finish;
197         return($count, @results);
198 } # sub breedingsearch
199
200
201 END { }       # module clean-up code here (global destructor)