installer: move base zebra config files
[koha.git] / cataloguing / z3950_search.pl
blob063cf4af5579a7da0dbafa2a6edaa49558164523
1 #!/usr/bin/perl
3 # This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Biblio;
27 use C4::Context;
28 use C4::Breeding;
29 use C4::Koha;
30 use ZOOM;
32 my $input = new CGI;
33 my $dbh = C4::Context->dbh;
34 my $error = $input->param('error');
35 my $biblionumber = $input->param('biblionumber');
36 $biblionumber = 0 unless $biblionumber;
37 my $frameworkcode = $input->param('frameworkcode');
38 my $title = $input->param('title');
39 my $author = $input->param('author');
40 my $isbn = $input->param('isbn');
41 my $issn = $input->param('issn');
42 my $lccn = $input->param('lccn');
43 my $random = $input->param('random');
44 my $op = $input->param('op');
45 my $noconnection;
46 my $numberpending;
47 my $attr = '';
48 my $term;
49 my $host;
50 my $server;
51 my $database;
52 my $port;
53 my $marcdata;
54 my @encoding;
55 my @results;
56 my $count;
57 my $toggle;
58 my $record;
59 my $oldbiblio;
60 my $errmsg;
61 my @serverloop = ();
62 my @serverhost;
63 my @breeding_loop = ();
65 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
67 unless ($random)
68 { # this var is not useful anymore just kept to keep rel2_2 compatibility
69 $random = rand(1000000000);
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
74 template_name => "cataloguing/z3950_search.tmpl",
75 query => $input,
76 type => "intranet",
77 authnotrequired => 1,
78 flagsrequired => { catalogue => 1 },
79 debug => 1,
83 $template->param( frameworkcode => $frameworkcode, );
85 if ( $op ne "do_search" ) {
86 my $sth = $dbh->prepare("select id,host,checked from z3950servers order by host");
87 $sth->execute();
88 my $serverloop = $sth->fetchall_arrayref( {} );
89 $template->param(
90 isbn => $isbn,
91 issn => $issn,
92 lccn => $lccn,
93 title => $title,
94 author => $author,
95 serverloop => $serverloop,
96 opsearch => "search",
97 biblionumber => $biblionumber,
99 output_html_with_http_headers $input, $cookie, $template->output;
101 else {
102 my @id = $input->param('id');
103 my @oConnection;
104 my @oResult;
105 my $s = 0;
107 if ( $isbn || $issn ) {
108 $attr = '1=7';
109 $term = $isbn if ($isbn);
110 $term = $issn if ($issn);
112 elsif ($lccn) {
113 $attr = '1=9';
114 $term = $lccn;
116 elsif ($title) {
117 $attr = '1=4 ';
118 utf8::decode($title);
119 $term = $title;
121 elsif ($author) {
122 $attr = '1=1003';
123 utf8::decode($author);
124 $term = $author;
127 my $query = "\@attr $attr \"$term\"";
128 warn "query " . $query if $DEBUG;
129 foreach my $servid (@id) {
130 my $sth = $dbh->prepare("select * from z3950servers where id=?");
131 $sth->execute($servid);
132 while ( $server = $sth->fetchrow_hashref ) {
133 my $noconnection = 0;
134 my $option1 = new ZOOM::Options();
135 $option1->option( 'async' => 1 );
136 $option1->option( 'elementSetName', 'F' );
137 $option1->option( 'databaseName', $server->{db} );
138 $option1->option( 'user', $server->{userid} ) if $server->{userid};
139 $option1->option( 'password', $server->{password} )
140 if $server->{password};
141 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
142 $oConnection[$s] = create ZOOM::Connection($option1)
143 || $DEBUG
144 && warn( "" . $oConnection[$s]->errmsg() );
145 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
146 $oConnection[$s]->connect( $server->{host}, $server->{port} )
147 || $DEBUG
148 && warn( "" . $oConnection[$s]->errmsg() );
149 $serverhost[$s] = $server->{host};
150 $encoding[$s] = $server->{syntax};
151 $s++;
152 } ## while fetch
153 } # foreach
154 my $nremaining = $s;
155 my $firstresult = 1;
157 for ( my $z = 0 ; $z < $s ; $z++ ) {
158 warn "doing the search" if $DEBUG;
159 $oResult[$z] = $oConnection[$z]->search_pqf($query)
160 || $DEBUG
161 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
163 #$oResult[$z] = $oConnection[$z]->search_pqf($query);
166 AGAIN:
167 my $k;
168 my $event;
169 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
170 $event = $oConnection[ $k - 1 ]->last_event();
171 warn( "connection ", $k - 1, ": event $event (",
172 ZOOM::event_str($event), ")\n" )
173 if $DEBUG;
174 last if $event == ZOOM::Event::ZEND;
177 if ( $k != 0 ) {
178 $k--;
179 warn $serverhost[$k] if $DEBUG;
180 my ( $error, $errmsg, $addinfo, $diagset ) =
181 $oConnection[$k]->error_x();
182 if ($error) {
183 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
184 if $DEBUG;
187 else {
188 my $numresults = $oResult[$k]->size();
189 my $i;
190 my $result = '';
191 if ( $numresults > 0 ) {
192 for (
193 $i = 0 ;
194 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
195 $i++
198 my $rec = $oResult[$k]->record($i);
199 my $marcrecord;
200 $marcdata = $rec->raw();
201 $marcrecord = FixEncoding($marcdata);
202 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
203 ## In HEAD i change everything to UTF-8
204 # In rel2_2 i am not sure what encoding is so no character conversion is done here
205 ##Add necessary encoding changes to here -TG
206 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
207 $oldbiblio->{isbn} =~ s/ |-|\.//g,
208 $oldbiblio->{issn} =~ s/ |-|\.//g,
209 my (
210 $notmarcrecord, $alreadyindb, $alreadyinfarm,
211 $imported, $breedingid
213 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
214 my %row_data;
215 if ( $i % 2 ) {
216 $toggle = 1;
218 else {
219 $toggle = 0;
221 $row_data{toggle} = $toggle;
222 $row_data{server} = $serverhost[$k];
223 $row_data{isbn} = $oldbiblio->{isbn};
224 $row_data{lccn} = $oldbiblio->{lccn};
225 $row_data{title} = $oldbiblio->{title};
226 $row_data{author} = $oldbiblio->{author};
227 $row_data{breedingid} = $breedingid;
228 $row_data{biblionumber} = $biblionumber;
229 push( @breeding_loop, \%row_data );
230 } # upto 5 results
231 } #$numresults
233 } # if $k !=0
234 $numberpending = $nremaining - 1;
235 $template->param(
236 breeding_loop => \@breeding_loop,
237 server => $serverhost[$k],
238 numberpending => $numberpending,
241 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
243 # print $template->output if $firstresult !=1;
244 $firstresult++;
246 MAYBE_AGAIN:
247 if ( --$nremaining > 0 ) {
248 goto AGAIN;
250 } ## if op=search