Merge branch 'translation' of git://git.workbuffer.org/git/koha into master
[koha.git] / acqui / z3950_search.pl
1 #!/usr/bin/perl
2
3 # This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
4 # Copyright 2000-2002 Katipo Communications
5 # Copyright 2010 Catalyst IT
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use warnings;
23 use strict;
24 use CGI;
25
26 use C4::Auth;
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Context;
30 use C4::Breeding;
31 use C4::Koha;
32 use C4::Charset;
33 use ZOOM;
34
35 my $input        = new CGI;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "acqui/z3950_search.tmpl",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 1,
42         flagsrequired   => { acquisition => 'order_manage' },
43         debug           => 1,
44     }
45 );
46
47 my $dbh          = C4::Context->dbh;
48 my $error        = $input->param('error');
49 my $biblionumber = $input->param('biblionumber');
50 $biblionumber = 0 unless $biblionumber;
51 my $frameworkcode = $input->param('frameworkcode');
52 my $title         = $input->param('title');
53 my $author        = $input->param('author');
54 my $isbn          = $input->param('isbn');
55 my $issn          = $input->param('issn');
56 my $lccn          = $input->param('lccn');
57 my $lccall          = $input->param('lccall');
58 my $subject= $input->param('subject');
59 my $dewey = $input->param('dewey');
60 my $op            = $input->param('op');
61 my $booksellerid = $input->param('booksellerid');
62 my $basketno = $input->param('basketno');
63 my $noconnection;
64 my $numberpending;
65 my $attr = '';
66 my $term;
67 my $host;
68 my $server;
69 my $database;
70 my $port;
71 my $marcdata;
72 my @encoding;
73 my @results;
74 my $count;
75 my $toggle;
76 my $record;
77 my $oldbiblio;
78 my $errmsg;
79 my @serverhost;
80 my @servername;
81 my @breeding_loop = ();
82 my $random        = $input->param('random');
83 unless ($random)
84 {    # this var is not useful anymore just kept to keep rel2_2 compatibility
85     $random = rand(1000000000);
86 }
87
88 my $DEBUG = 0;    # if set to 1, many debug message are send on syslog.
89
90 # get framework list
91 my $frameworks = getframeworks;
92 my @frameworkcodeloop;
93 foreach my $thisframeworkcode ( keys %$frameworks ) {
94     my %row = (
95         value         => $thisframeworkcode,
96         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
97     );
98     if ( $row{'value'} eq $frameworkcode){
99         $row{'active'} = 'true';
100     }
101     push @frameworkcodeloop, \%row;
102 }
103
104 $template->param( frameworkcode => $frameworkcode, 
105                                     frameworkcodeloop => \@frameworkcodeloop,
106                                     booksellerid => $booksellerid,
107                                     basketno => $basketno);
108
109
110 if ( $op ne "do_search" ) {
111     my $sth = $dbh->prepare("select id,host,name,checked from z3950servers  order by host");
112     $sth->execute();
113     my $serverloop = $sth->fetchall_arrayref( {} );
114     $template->param(
115         isbn         => $isbn,
116         issn         => $issn,
117         lccn         => $lccn,
118         lccall       => $lccall,
119         title        => $title,
120         author       => $author,
121         serverloop   => $serverloop,
122         opsearch     => "search",
123         biblionumber => $biblionumber,
124     );
125     output_html_with_http_headers $input, $cookie, $template->output;
126 }
127 else {
128     my @id = $input->param('id');
129     my @oConnection;
130     my @oResult;
131     my $s = 0;
132     my $query;
133     my $nterms;
134     if ($isbn || $issn) {
135         $term=$isbn if ($isbn);
136         $term=$issn if ($issn);
137         $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
138         $nterms++;
139     }
140     if ($title) {
141         utf8::decode($title);
142         $query .= " \@attr 1=4 \"$title\" ";
143         $nterms++;
144     }
145     if ($author) {
146         utf8::decode($author);
147         $query .= " \@attr 1=1003 \"$author\" ";
148         $nterms++;
149     }
150     if ($dewey) {
151         $query .= " \@attr 1=16 \"$dewey\" ";
152         $nterms++;
153     }
154     if ($subject) {
155         utf8::decode($subject);
156         $query .= " \@attr 1=21 \"$subject\" ";
157         $nterms++;
158     }
159         if ($lccn) {    
160         $query .= " \@attr 1=9 $lccn ";
161         $nterms++;
162     }
163     if ($lccall) {
164         $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
165         $nterms++;
166     }
167 for my $i (1..$nterms-1) {
168     $query = "\@and " . $query;
169 }
170 warn "query ".$query  if $DEBUG;
171
172     foreach my $servid (@id) {
173         my $sth = $dbh->prepare("select * from z3950servers where id=?");
174         $sth->execute($servid);
175         while ( $server = $sth->fetchrow_hashref ) {
176             warn "serverinfo ".join(':',%$server) if $DEBUG;
177             my $noconnection = 0;
178             my $option1      = new ZOOM::Options();
179             $option1->option( 'async' => 1 );
180             $option1->option( 'elementSetName', 'F' );
181             $option1->option( 'databaseName',   $server->{db} );
182             $option1->option( 'user', $server->{userid} ) if $server->{userid};
183             $option1->option( 'password', $server->{password} )
184               if $server->{password};
185             $option1->option( 'preferredRecordSyntax', $server->{syntax} );
186             $oConnection[$s] = create ZOOM::Connection($option1)
187               || $DEBUG
188               && warn( "" . $oConnection[$s]->errmsg() );
189             warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
190             $oConnection[$s]->connect( $server->{host}, $server->{port} )
191               || $DEBUG
192               && warn( "" . $oConnection[$s]->errmsg() );
193             $serverhost[$s] = $server->{host};
194             $servername[$s] = $server->{name};
195             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
196             $s++;
197         }    ## while fetch
198     }    # foreach
199     my $nremaining  = $s;
200     my $firstresult = 1;
201
202     for ( my $z = 0 ; $z < $s ; $z++ ) {
203         warn "doing the search" if $DEBUG;
204         $oResult[$z] = $oConnection[$z]->search_pqf($query)
205           || $DEBUG
206           && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
207
208         # $oResult[$z] = $oConnection[$z]->search_pqf($query);
209     }
210
211 sub displayresults {
212     my $k;
213     my $event;
214     while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
215         $event = $oConnection[ $k - 1 ]->last_event();
216         warn( "connection ", $k - 1, ": event $event (",
217             ZOOM::event_str($event), ")\n" )
218           if $DEBUG;
219         last if $event == ZOOM::Event::ZEND;
220     }
221
222     if ( $k != 0 ) {
223         $k--;
224         warn $serverhost[$k] if $DEBUG;
225         my ( $error, $errmsg, $addinfo, $diagset ) =
226           $oConnection[$k]->error_x();
227         if ($error) {
228             warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
229               if $DEBUG;
230
231         }
232         else {
233             my $numresults = $oResult[$k]->size();
234             my $i;
235             my $result = '';
236             if ( $numresults > 0 ) {
237                 for (
238                     $i = 0 ;
239                     $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
240                     $i++
241                   )
242                 {
243                     my $rec = $oResult[$k]->record($i);
244                     if ($rec) {
245                         my $marcrecord;
246                         $marcdata   = $rec->raw();
247
248                         my ($charset_result, $charset_errors);
249                         ($marcrecord, $charset_result, $charset_errors) = 
250                           MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
251 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
252 ## In HEAD i change everything to UTF-8
253 # In rel2_2 i am not sure what encoding is so no character conversion is done here
254 ##Add necessary encoding changes to here -TG
255                         my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
256                         $oldbiblio->{isbn}   =~ s/ |-|\.//g if $oldbiblio->{isbn};
257                          $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
258                           my (
259                             $notmarcrecord, $alreadyindb, $alreadyinfarm,
260                             $imported,      $breedingid
261                           )
262                           = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
263                         my %row_data;
264                         if ( $i % 2 ) {
265                             $toggle = 1;
266                         }
267                         else {
268                             $toggle = 0;
269                         }
270                         $row_data{toggle}       = $toggle;
271                         $row_data{server}       = $servername[$k];
272                         $row_data{isbn}         = $oldbiblio->{isbn};
273                         $row_data{lccn}         = $oldbiblio->{lccn};
274                         $row_data{title}        = $oldbiblio->{title};
275                         $row_data{author}       = $oldbiblio->{author};
276                         $row_data{breedingid}   = $breedingid;
277                         $row_data{biblionumber} = $biblionumber;
278                         push( @breeding_loop, \%row_data );
279                             
280                     } else {
281                         push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
282                     } # $rec
283                 }    # upto 5 results
284             }    #$numresults
285         }
286     }    # if $k !=0
287     $numberpending = $nremaining - 1;
288     $template->param(
289         breeding_loop => \@breeding_loop,
290         server        => $servername[$k],
291         numberpending => $numberpending,
292     );
293     output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
294
295     #   print  $template->output  if $firstresult !=1;
296     $firstresult++;
297 }
298 displayresults();
299 while ( --$nremaining > 0 ) {
300         displayresults();
301     }
302 }    ## if op=search