Deleted old sample budget sql-data for Ukrainian and Russian
[koha.git] / acqui / z3950_search.pl
blobb0a8b92f25aa00d01f0475da3614bc6defb64621
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
5 # Copyright 2010 Catalyst IT
7 # This file is part of Koha.
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.
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.
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.
22 use warnings;
23 use strict;
24 use CGI;
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;
35 my $input = new CGI;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "acqui/z3950_search.tmpl",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 1,
42 flagsrequired => { acquisition => 'order_manage' },
43 debug => 1,
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 $controlnumber = $input->param('controlnumber');
61 my $op = $input->param('op');
62 my $booksellerid = $input->param('booksellerid');
63 my $basketno = $input->param('basketno');
64 my $noconnection;
65 my $numberpending;
66 my $attr = '';
67 my $term;
68 my $host;
69 my $server;
70 my $database;
71 my $port;
72 my $marcdata;
73 my @encoding;
74 my @results;
75 my $count;
76 my $toggle;
77 my $record;
78 my $oldbiblio;
79 my $errmsg;
80 my @serverhost;
81 my @servername;
82 my @breeding_loop = ();
83 my $random = $input->param('random');
84 unless ($random)
85 { # this var is not useful anymore just kept to keep rel2_2 compatibility
86 $random = rand(1000000000);
89 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
91 # get framework list
92 my $frameworks = getframeworks;
93 my @frameworkcodeloop;
94 foreach my $thisframeworkcode ( keys %$frameworks ) {
95 my %row = (
96 value => $thisframeworkcode,
97 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
99 if ( $row{'value'} eq $frameworkcode){
100 $row{'active'} = 'true';
102 push @frameworkcodeloop, \%row;
105 $template->param( frameworkcode => $frameworkcode,
106 frameworkcodeloop => \@frameworkcodeloop,
107 booksellerid => $booksellerid,
108 basketno => $basketno);
111 if ( $op ne "do_search" ) {
112 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
113 $sth->execute();
114 my $serverloop = $sth->fetchall_arrayref( {} );
115 $template->param(
116 isbn => $isbn,
117 issn => $issn,
118 lccn => $lccn,
119 lccall => $lccall,
120 title => $title,
121 author => $author,
122 controlnumber=> $controlnumber,
123 serverloop => $serverloop,
124 opsearch => "search",
125 biblionumber => $biblionumber,
127 output_html_with_http_headers $input, $cookie, $template->output;
129 else {
130 my @id = $input->param('id');
131 my @oConnection;
132 my @oResult;
133 my $s = 0;
134 my $query;
135 my $nterms;
136 if ($isbn || $issn) {
137 $term=$isbn if ($isbn);
138 $term=$issn if ($issn);
139 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
140 $nterms++;
142 if ($title) {
143 utf8::decode($title);
144 $query .= " \@attr 1=4 \"$title\" ";
145 $nterms++;
147 if ($author) {
148 utf8::decode($author);
149 $query .= " \@attr 1=1003 \"$author\" ";
150 $nterms++;
152 if ($dewey) {
153 $query .= " \@attr 1=16 \"$dewey\" ";
154 $nterms++;
156 if ($subject) {
157 utf8::decode($subject);
158 $query .= " \@attr 1=21 \"$subject\" ";
159 $nterms++;
161 if ($lccn) {
162 $query .= " \@attr 1=9 $lccn ";
163 $nterms++;
165 if ($lccall) {
166 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
167 $nterms++;
169 if ($controlnumber) {
170 $query .= " \@attr 1=12 \"$controlnumber\" ";
171 $nterms++;
173 for my $i (1..$nterms-1) {
174 $query = "\@and " . $query;
176 warn "query ".$query if $DEBUG;
178 foreach my $servid (@id) {
179 my $sth = $dbh->prepare("select * from z3950servers where id=?");
180 $sth->execute($servid);
181 while ( $server = $sth->fetchrow_hashref ) {
182 warn "serverinfo ".join(':',%$server) if $DEBUG;
183 my $noconnection = 0;
184 my $option1 = new ZOOM::Options();
185 $option1->option( 'async' => 1 );
186 $option1->option( 'elementSetName', 'F' );
187 $option1->option( 'databaseName', $server->{db} );
188 $option1->option( 'user', $server->{userid} ) if $server->{userid};
189 $option1->option( 'password', $server->{password} )
190 if $server->{password};
191 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
192 $oConnection[$s] = create ZOOM::Connection($option1)
193 || $DEBUG
194 && warn( "" . $oConnection[$s]->errmsg() );
195 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
196 $oConnection[$s]->connect( $server->{host}, $server->{port} )
197 || $DEBUG
198 && warn( "" . $oConnection[$s]->errmsg() );
199 $serverhost[$s] = $server->{host};
200 $servername[$s] = $server->{name};
201 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
202 $s++;
203 } ## while fetch
204 } # foreach
205 my $nremaining = $s;
206 my $firstresult = 1;
208 for ( my $z = 0 ; $z < $s ; $z++ ) {
209 warn "doing the search" if $DEBUG;
210 $oResult[$z] = $oConnection[$z]->search_pqf($query)
211 || $DEBUG
212 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
214 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
217 sub displayresults {
218 my $k;
219 my $event;
220 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
221 $event = $oConnection[ $k - 1 ]->last_event();
222 warn( "connection ", $k - 1, ": event $event (",
223 ZOOM::event_str($event), ")\n" )
224 if $DEBUG;
225 last if $event == ZOOM::Event::ZEND;
228 if ( $k != 0 ) {
229 $k--;
230 warn $serverhost[$k] if $DEBUG;
231 my ( $error, $errmsg, $addinfo, $diagset ) =
232 $oConnection[$k]->error_x();
233 if ($error) {
234 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
235 if $DEBUG;
238 else {
239 my $numresults = $oResult[$k]->size();
240 my $i;
241 my $result = '';
242 if ( $numresults > 0 ) {
243 for (
244 $i = 0 ;
245 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
246 $i++
249 my $rec = $oResult[$k]->record($i);
250 if ($rec) {
251 my $marcrecord;
252 $marcdata = $rec->raw();
254 my ($charset_result, $charset_errors);
255 ($marcrecord, $charset_result, $charset_errors) =
256 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
257 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
258 ## In HEAD i change everything to UTF-8
259 # In rel2_2 i am not sure what encoding is so no character conversion is done here
260 ##Add necessary encoding changes to here -TG
261 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
262 $oldbiblio->{isbn} =~ s/ |-|\.//g if $oldbiblio->{isbn};
263 # pad | and ( with spaces to allow line breaks in the HTML
264 $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
265 $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
267 $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
268 # pad | and ( with spaces to allow line breaks in the HTML
269 $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
270 $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
271 my (
272 $notmarcrecord, $alreadyindb, $alreadyinfarm,
273 $imported, $breedingid
275 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
276 my %row_data;
277 if ( $i % 2 ) {
278 $toggle = 1;
280 else {
281 $toggle = 0;
283 $row_data{toggle} = $toggle;
284 $row_data{server} = $servername[$k];
285 $row_data{isbn} = $oldbiblio->{isbn};
286 $row_data{lccn} = $oldbiblio->{lccn};
287 $row_data{title} = $oldbiblio->{title};
288 $row_data{author} = $oldbiblio->{author};
289 $row_data{breedingid} = $breedingid;
290 $row_data{biblionumber} = $biblionumber;
291 push( @breeding_loop, \%row_data );
293 } else {
294 push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
295 } # $rec
296 } # upto 5 results
297 } #$numresults
299 } # if $k !=0
300 $numberpending = $nremaining - 1;
301 $template->param(
302 breeding_loop => \@breeding_loop,
303 server => $servername[$k],
304 numberpending => $numberpending,
306 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
308 # print $template->output if $firstresult !=1;
309 $firstresult++;
311 displayresults();
312 while ( --$nremaining > 0 ) {
313 displayresults();
315 } ## if op=search