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
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.
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
38 template_name
=> "acqui/z3950_search.tmpl",
42 flagsrequired
=> { acquisition
=> 'order_manage' },
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');
82 my @breeding_loop = ();
83 my $random = $input->param('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.
92 my $frameworks = getframeworks
;
93 my @frameworkcodeloop;
94 foreach my $thisframeworkcode ( keys %$frameworks ) {
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");
114 my $serverloop = $sth->fetchall_arrayref( {} );
122 controlnumber
=> $controlnumber,
123 serverloop
=> $serverloop,
124 opsearch
=> "search",
125 biblionumber
=> $biblionumber,
127 output_html_with_http_headers
$input, $cookie, $template->output;
130 my @id = $input->param('id');
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\" ";
143 utf8
::decode
($title);
144 $query .= " \@attr 1=4 \"$title\" ";
148 utf8
::decode
($author);
149 $query .= " \@attr 1=1003 \"$author\" ";
153 $query .= " \@attr 1=16 \"$dewey\" ";
157 utf8
::decode
($subject);
158 $query .= " \@attr 1=21 \"$subject\" ";
162 $query .= " \@attr 1=9 $lccn ";
166 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
169 if ($controlnumber) {
170 $query .= " \@attr 1=12 \"$controlnumber\" ";
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)
194 && warn( "" . $oConnection[$s]->errmsg() );
195 warn( "server data", $server->{name
}, $server->{port
} ) if $DEBUG;
196 $oConnection[$s]->connect( $server->{host
}, $server->{port
} )
198 && warn( "" . $oConnection[$s]->errmsg() );
199 $serverhost[$s] = $server->{host
};
200 $servername[$s] = $server->{name
};
201 $encoding[$s] = ($server->{encoding
}?
$server->{encoding
}:"iso-5426");
208 for ( my $z = 0 ; $z < $s ; $z++ ) {
209 warn "doing the search" if $DEBUG;
210 $oResult[$z] = $oConnection[$z]->search_pqf($query)
212 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
214 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
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" )
225 last if $event == ZOOM
::Event
::ZEND
;
230 warn $serverhost[$k] if $DEBUG;
231 my ( $error, $errmsg, $addinfo, $diagset ) =
232 $oConnection[$k]->error_x();
234 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
239 my $numresults = $oResult[$k]->size();
242 if ( $numresults > 0 ) {
245 $i < ( ( $numresults < 20 ) ?
($numresults) : (20) ) ;
249 my $rec = $oResult[$k]->record($i);
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
};
272 $notmarcrecord, $alreadyindb, $alreadyinfarm,
273 $imported, $breedingid
275 = ImportBreeding
( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
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 );
294 push(@breeding_loop,{'toggle'=>($i % 2)?
1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
300 $numberpending = $nremaining - 1;
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;
312 while ( --$nremaining > 0 ) {