View | Details | Raw Unified | Return to bug 6536
Collapse All | Expand All

(-)a/C4/Breeding.pm (-2 / +220 lines)
Lines 20-29 package C4::Breeding; Link Here
20
use strict;
20
use strict;
21
use warnings;
21
use warnings;
22
22
23
#use Encode;
24
use MARC::File::USMARC;
25
#use ZOOM; #has been loaded already in Context
26
23
use C4::Biblio;
27
use C4::Biblio;
24
use C4::Koha;
28
use C4::Koha;
25
use C4::Charset;
29
use C4::Charset;
26
use MARC::File::USMARC;
27
use C4::ImportBatch;
30
use C4::ImportBatch;
28
31
29
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Lines 33-39 BEGIN { Link Here
33
    $VERSION = 3.08.01.002;
36
    $VERSION = 3.08.01.002;
34
	require Exporter;
37
	require Exporter;
35
	@ISA = qw(Exporter);
38
	@ISA = qw(Exporter);
36
	@EXPORT = qw(&ImportBreeding &BreedingSearch);
39
    @EXPORT = qw(&ImportBreeding &BreedingSearch Z3950Search);
37
}
40
}
38
41
39
=head1 NAME
42
=head1 NAME
Lines 211-216 sub BreedingSearch { Link Here
211
    return($count, @results);
214
    return($count, @results);
212
} # sub breedingsearch
215
} # sub breedingsearch
213
216
217
218
=head2 Z3950Search
219
220
Z3950Search($pars, $template);
221
222
Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
223
Also it should contain an arrayref id that points to a list of id's of the z3950 targets to be queried (see z3950servers table).
224
This code is used in acqui/z3950_search and cataloging/z3950_search.
225
The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
226
227
=cut
228
229
sub Z3950Search {
230
    my ($pars, $template)= @_;
231
232
    my $server;
233
    my $marcdata;
234
    my @encoding;
235
    my @serverhost;
236
    my @servername;
237
    my @breeding_loop = ();
238
    my $numberpending;
239
240
    my $dbh   = C4::Context->dbh;
241
    my @id = @{$pars->{id}};
242
    my @oConnection;
243
    my @oResult;
244
    my @errconn;
245
    my $s = 0;
246
    my $query = '';
247
    my $nterms;
248
    my $term;
249
    if ($pars->{isbn}) {
250
        $term=$pars->{isbn};
251
        $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
252
        $nterms++;
253
    }
254
    if ($pars->{title}) {
255
        $term=$pars->{title};
256
        utf8::decode($term);
257
        $query .= " \@attr 1=4 \"$term\" ";
258
        $nterms++;
259
    }
260
    if ($pars->{author}) {
261
        $term=$pars->{author};
262
        utf8::decode($term);
263
        $query .= " \@attr 1=1003 \"$term\" ";
264
        $nterms++;
265
    }
266
    if ($pars->{dewey}) {
267
        $term=$pars->{dewey};
268
        $query .= " \@attr 1=16 \"$term\" ";
269
        $nterms++;
270
    }
271
    if ($pars->{subject}) {
272
        $term=$pars->{subject};
273
        utf8::decode($term);
274
        $query .= " \@attr 1=21 \"$term\" ";
275
        $nterms++;
276
    }
277
    if ($pars->{lccall}) {
278
        $term=$pars->{lccall};
279
        $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$term\" ";
280
        $nterms++;
281
    }
282
    if ($pars->{controlnumber}) {
283
        $term=$pars->{controlnumber};
284
        $query .= " \@attr 1=12 \"$term\" ";
285
        $nterms++;
286
    }
287
    if ($pars->{stdid}) {
288
        $term=$pars->{stdid};
289
        $query .= " \@attr 1=1007 \"$term\" ";
290
        $nterms++;
291
    }
292
    if ($pars->{srchany}) {
293
        $term=$pars->{srchany};
294
        $query .= " \@attr 1=1016 \"$term\" ";
295
        $nterms++;
296
    }
297
298
    for my $i (1..$nterms-1) {
299
        $query = "\@and " . $query;
300
    }
301
302
    foreach my $servid (@id) {
303
        my $sth = $dbh->prepare("SELECT * FROM z3950servers WHERE id=? ORDER BY rank, name");
304
        $sth->execute($servid);
305
        while ( $server = $sth->fetchrow_hashref ) {
306
            my $option1      = new ZOOM::Options();
307
            $option1->option('async' => 1);
308
            $option1->option('elementSetName', 'F');
309
            $option1->option('databaseName', $server->{db});
310
            $option1->option('user',         $server->{userid}  ) if $server->{userid};
311
            $option1->option('password',     $server->{password}) if $server->{password};
312
            $option1->option('preferredRecordSyntax', $server->{syntax});
313
            $option1->option( 'timeout', $server->{timeout} ) if ($server->{timeout});
314
            $oConnection[$s] = create ZOOM::Connection($option1)
315
              || $DEBUG
316
              && warn( "" . $oConnection[$s]->errmsg() );
317
            warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
318
            $oConnection[$s]->connect( $server->{host}, $server->{port} )
319
              || $DEBUG
320
              && warn( "" . $oConnection[$s]->errmsg() );
321
            $serverhost[$s] = $server->{host};
322
            $servername[$s] = $server->{name};
323
            $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
324
            $s++;
325
        }    ## while fetch
326
    }    # foreach
327
    my $nremaining  = $s;
328
    my $firstresult = 1;
329
330
    for ( my $z = 0 ; $z < $s ; $z++ ) {
331
        warn "doing the search" if $DEBUG;
332
        $oResult[$z] = $oConnection[$z]->search_pqf($query)
333
          || $DEBUG
334
          && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
335
336
    }
337
338
  AGAIN:
339
    my $k;
340
    my $event;
341
    while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
342
        $event = $oConnection[ $k - 1 ]->last_event();
343
        warn( "connection ", $k - 1, ": event $event (",
344
            ZOOM::event_str($event), ")\n" )
345
          if $DEBUG;
346
        last if $event == ZOOM::Event::ZEND;
347
    }
348
349
    if ( $k != 0 ) {
350
        $k--;
351
        warn $serverhost[$k] if $DEBUG;
352
        my ( $error, $errmsg, $addinfo, $diagset ) =
353
          $oConnection[$k]->error_x();
354
        if ($error) {
355
            if ($error =~ m/^(10000|10007)$/ ) {
356
                push(@errconn, {'server' => $serverhost[$k], 'error' => $error});
357
            }
358
            $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
359
        }
360
        else {
361
            my $numresults = $oResult[$k]->size();
362
            my $i;
363
            my $result = '';
364
            if ( $numresults > 0 ) {
365
                for ($i = 0; $i < (($numresults < 20) ? $numresults : 20); $i++) {
366
                    my $rec = $oResult[$k]->record($i);
367
                    if ($rec) {
368
                        my $marcrecord;
369
                        $marcdata   = $rec->raw();
370
371
                        my ($charset_result, $charset_errors);
372
                        ($marcrecord, $charset_result, $charset_errors) =
373
                          MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
374
                        SetUTF8Flag($marcrecord);
375
####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
376
## In HEAD i change everything to UTF-8
377
# In rel2_2 i am not sure what encoding is so no character conversion is done here
378
##Add necessary encoding changes to here -TG
379
                        my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
380
                        $oldbiblio->{isbn}   =~ s/ |-|\.//g if $oldbiblio->{isbn};
381
                        # pad | and ( with spaces to allow line breaks in the HTML
382
                        $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
383
                        $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
384
385
                        $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
386
                        # pad | and ( with spaces to allow line breaks in the HTML
387
                        $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
388
                        $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
389
                          my (
390
                            $notmarcrecord, $alreadyindb, $alreadyinfarm,
391
                            $imported,      $breedingid
392
                          )
393
                          = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $pars->{random}, 'z3950' );
394
                        my %row_data;
395
                        $row_data{server}       = $servername[$k];
396
                        $row_data{isbn}         = $oldbiblio->{isbn};
397
                        $row_data{lccn}         = $oldbiblio->{lccn};
398
                        $row_data{title}        = $oldbiblio->{title};
399
                        $row_data{author}       = $oldbiblio->{author};
400
                        $row_data{date}         = $oldbiblio->{copyrightdate};
401
                        $row_data{edition}      = $oldbiblio->{editionstatement};
402
                        $row_data{breedingid}   = $breedingid;
403
                        $row_data{biblionumber} = $pars->{biblionumber};
404
                        push( @breeding_loop, \%row_data );
405
406
                    } else {
407
                        push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
408
                    } # $rec
409
                }
410
            }    #$numresults
411
        }
412
    }    # if $k !=0
413
    $numberpending = $nremaining - 1;
414
    $template->param(
415
        breeding_loop => \@breeding_loop,
416
        server        => $servername[$k],
417
        numberpending => $numberpending,
418
        biblionumber  => $pars->{biblionumber},
419
        errconn       => \@errconn
420
    );
421
422
    return if $numberpending==0;
423
424
    $firstresult++;
425
426
  MAYBE_AGAIN:
427
    if ( --$nremaining > 0 ) {
428
        goto AGAIN;
429
    }
430
}
431
214
1;
432
1;
215
__END__
433
__END__
216
434
(-)a/acqui/z3950_search.pl (-218 / +25 lines)
Lines 19-37 Link Here
19
# with Koha; if not, write to the Free Software Foundation, Inc.,
19
# with Koha; if not, write to the Free Software Foundation, Inc.,
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
21
22
use warnings;
23
use strict;
22
use strict;
23
use warnings;
24
use CGI;
24
use CGI;
25
25
26
use C4::Auth;
26
use C4::Auth;
27
use C4::Output;
27
use C4::Output;
28
use C4::Biblio;
28
#use C4::Biblio;
29
use C4::Context;
29
use C4::Context;
30
use C4::Breeding;
30
use C4::Breeding;
31
use C4::Koha;
31
use C4::Koha;
32
use C4::Charset;
32
#use C4::Charset;
33
use C4::Bookseller qw/ GetBookSellerFromId /;
33
use C4::Bookseller qw/ GetBookSellerFromId /;
34
use ZOOM;
34
#use ZOOM;
35
35
36
my $input        = new CGI;
36
my $input        = new CGI;
37
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
Lines 47-56 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
47
47
48
48
49
my $dbh          = C4::Context->dbh;
49
my $dbh          = C4::Context->dbh;
50
my $error        = $input->param('error');
50
my $biblionumber = $input->param('biblionumber') || 0;
51
my $biblionumber = $input->param('biblionumber');
51
my $frameworkcode = $input->param('frameworkcode') || '';
52
$biblionumber = 0 unless $biblionumber;
53
my $frameworkcode = $input->param('frameworkcode');
54
my $title         = $input->param('title');
52
my $title         = $input->param('title');
55
my $author        = $input->param('author');
53
my $author        = $input->param('author');
56
my $isbn          = $input->param('isbn');
54
my $isbn          = $input->param('isbn');
Lines 60-93 my $lccall = $input->param('lccall'); Link Here
60
my $subject= $input->param('subject');
58
my $subject= $input->param('subject');
61
my $dewey = $input->param('dewey');
59
my $dewey = $input->param('dewey');
62
my $controlnumber	= $input->param('controlnumber');
60
my $controlnumber	= $input->param('controlnumber');
63
my $op            = $input->param('op');
61
my $op            = $input->param('op')||'';
64
my $booksellerid = $input->param('booksellerid');
62
my $booksellerid = $input->param('booksellerid');
65
my $basketno = $input->param('basketno');
63
my $basketno = $input->param('basketno');
66
my $noconnection;
67
my $attr = '';
68
my $term;
69
my $host;
70
my $server;
71
my $database;
72
my $port;
73
my $marcdata;
74
my @encoding;
75
my @results;
76
my $count;
77
my $toggle;
78
my $record;
79
my $oldbiblio;
80
my $errmsg;
81
my @serverhost;
82
my @servername;
83
my @breeding_loop = ();
84
my $random        = $input->param('random');
85
unless ($random)
86
{    # this var is not useful anymore just kept to keep rel2_2 compatibility
87
    $random = rand(1000000000);
88
}
89
90
my $DEBUG = $ENV{DEBUG} || 0;    # if set to 1, many debug message are send on syslog.
91
64
92
# get framework list
65
# get framework list
93
my $frameworks = getframeworks;
66
my $frameworks = getframeworks;
Lines 130-324 if ( $op ne "do_search" ) { Link Here
130
        biblionumber => $biblionumber,
103
        biblionumber => $biblionumber,
131
    );
104
    );
132
    output_html_with_http_headers $input, $cookie, $template->output;
105
    output_html_with_http_headers $input, $cookie, $template->output;
133
    exit;
134
}
106
}
135
107
else {
136
    my @id = $input->param('id');
108
    my @id = $input->param('id');
137
109
    if (@id==0) {
138
    if ( not @id ) {
139
        # empty server list -> report and exit
110
        # empty server list -> report and exit
140
        $template->param( emptyserverlist => 1 );
111
        $template->param( emptyserverlist => 1 );
141
        output_html_with_http_headers $input, $cookie, $template->output;
112
        output_html_with_http_headers $input, $cookie, $template->output;
142
        exit;
113
        exit;
143
    }
114
    }
144
115
    my $pars= {
145
    my @oConnection;
116
        random => $input->param('random') || rand(1000000000),
146
    my @oResult;
117
        biblionumber => $biblionumber,
147
    my @errconn;
118
        id => \@id,
148
    my $s = 0;
119
        isbn => $isbn,
149
    my $query;
120
        title => $title,
150
    my $nterms;
121
        author => $author,
151
    if ($isbn || $issn) {
122
        dewey => $dewey,
152
        $term=$isbn if ($isbn);
123
        subject => $subject,
153
        $term=$issn if ($issn);
124
        lccall => $lccall,
154
        $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
125
        controlnumber => $controlnumber,
155
        $nterms++;
126
        stdid => 0,
156
    }
127
        srchany => 0,
157
    if ($title) {
128
    };
158
        utf8::decode($title);
129
    Z3950Search($pars, $template);
159
        $query .= " \@attr 1=4 \"$title\" ";
130
    output_html_with_http_headers $input, $cookie, $template->output;
160
        $nterms++;
161
    }
162
    if ($author) {
163
        utf8::decode($author);
164
        $query .= " \@attr 1=1003 \"$author\" ";
165
        $nterms++;
166
    }
167
    if ($dewey) {
168
        $query .= " \@attr 1=16 \"$dewey\" ";
169
        $nterms++;
170
    }
171
    if ($subject) {
172
        utf8::decode($subject);
173
        $query .= " \@attr 1=21 \"$subject\" ";
174
        $nterms++;
175
    }
176
    if ($lccn) {
177
        $query .= " \@attr 1=9 $lccn ";
178
        $nterms++;
179
    }
180
    if ($lccall) {
181
        $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
182
        $nterms++;
183
    }
184
    if ($controlnumber) {
185
        $query .= " \@attr 1=12 \"$controlnumber\" ";
186
        $nterms++;
187
    }
188
for my $i (1..$nterms-1) {
189
    $query = "\@and " . $query;
190
}
131
}
191
warn "query ".$query  if $DEBUG;
192
193
    foreach my $servid (@id) {
194
        my $sth = $dbh->prepare("select * from z3950servers where id=?");
195
        $sth->execute($servid);
196
        while ( $server = $sth->fetchrow_hashref ) {
197
            warn "serverinfo ".join(':',%$server) if $DEBUG;
198
            my $option1      = new ZOOM::Options();
199
            $option1->option( 'async' => 1 );
200
            $option1->option( 'elementSetName', 'F' );
201
            $option1->option( 'databaseName',   $server->{db} );
202
            $option1->option( 'user', $server->{userid} ) if $server->{userid};
203
            $option1->option( 'password', $server->{password} )
204
              if $server->{password};
205
            $option1->option( 'preferredRecordSyntax', $server->{syntax} );
206
            $oConnection[$s] = create ZOOM::Connection($option1)
207
              || $DEBUG
208
              && warn( "" . $oConnection[$s]->errmsg() );
209
            warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
210
            $oConnection[$s]->connect( $server->{host}, $server->{port} )
211
              || $DEBUG
212
              && warn( "" . $oConnection[$s]->errmsg() );
213
            $serverhost[$s] = $server->{host};
214
            $servername[$s] = $server->{name};
215
            $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
216
            $s++;
217
        }    ## while fetch
218
    }    # foreach
219
    my $nremaining  = $s;
220
    my $firstresult = 1;
221
222
    for ( my $z = 0 ; $z < $s ; $z++ ) {
223
        warn "doing the search" if $DEBUG;
224
        $oResult[$z] = $oConnection[$z]->search_pqf($query)
225
          || $DEBUG
226
          && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
227
228
        # $oResult[$z] = $oConnection[$z]->search_pqf($query);
229
    }
230
231
  warn "# nremaining = $nremaining\n" if $DEBUG;
232
233
  while ( $nremaining-- ) {
234
235
    my $k;
236
    my $event;
237
    while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
238
        $event = $oConnection[ $k - 1 ]->last_event();
239
        warn( "connection ", $k - 1, ": event $event (",
240
            ZOOM::event_str($event), ")\n" )
241
          if $DEBUG;
242
        last if $event == ZOOM::Event::ZEND;
243
    }
244
245
    if ( $k != 0 ) {
246
        $k--;
247
        warn "event from $k server = ",$serverhost[$k] if $DEBUG;
248
        my ( $error, $errmsg, $addinfo, $diagset ) =
249
          $oConnection[$k]->error_x();
250
        if ($error) {
251
            if ($error =~ m/^(10000|10007)$/ ) {
252
                push(@errconn, {'server' => $serverhost[$k]});
253
            }
254
            $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
255
        }
256
        else {
257
            my $numresults = $oResult[$k]->size();
258
            warn "numresults = $numresults" if $DEBUG;
259
            my $i;
260
            my $result = '';
261
            if ( $numresults > 0 ) {
262
                for (
263
                    $i = 0 ;
264
                    $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
265
                    $i++
266
                  )
267
                {
268
                    my $rec = $oResult[$k]->record($i);
269
                    if ($rec) {
270
                        my $marcrecord;
271
                        $marcdata   = $rec->raw();
272
273
                        my ($charset_result, $charset_errors);
274
                        ($marcrecord, $charset_result, $charset_errors) = 
275
                          MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
276
                        SetUTF8Flag($marcrecord);
277
####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
278
## In HEAD i change everything to UTF-8
279
# In rel2_2 i am not sure what encoding is so no character conversion is done here
280
##Add necessary encoding changes to here -TG
281
                        my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
282
                        $oldbiblio->{isbn}   =~ s/ |-|\.//g if $oldbiblio->{isbn};
283
                        # pad | and ( with spaces to allow line breaks in the HTML
284
                        $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
285
                        $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
286
287
                        $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
288
                        # pad | and ( with spaces to allow line breaks in the HTML
289
                        $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
290
                        $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
291
                          my (
292
                            $notmarcrecord, $alreadyindb, $alreadyinfarm,
293
                            $imported,      $breedingid
294
                          )
295
                          = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
296
                        my %row_data;
297
                        $row_data{server}       = $servername[$k];
298
                        $row_data{isbn}         = $oldbiblio->{isbn};
299
                        $row_data{lccn}         = $oldbiblio->{lccn};
300
                        $row_data{title}        = $oldbiblio->{title};
301
                        $row_data{author}       = $oldbiblio->{author};
302
                        $row_data{breedingid}   = $breedingid;
303
                        $row_data{biblionumber} = $biblionumber;
304
                        push( @breeding_loop, \%row_data );
305
306
                    } else {
307
                        push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
308
                    } # $rec
309
                }    # upto 5 results
310
            }    #$numresults
311
        }
312
    }    # if $k !=0
313
    #   print  $template->output  if $firstresult !=1;
314
    $firstresult++;
315
316
  } # while nremaining
317
318
$template->param(
319
breeding_loop => \@breeding_loop,
320
#server        => $servername[$k],
321
numberpending => $nremaining > 0 ? $nremaining : 0,
322
errconn       => \@errconn
323
);
324
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/cataloguing/z3950_search.pl (-220 / +24 lines)
Lines 19-41 Link Here
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
20
21
use strict;
21
use strict;
22
#use warnings; FIXME - Bug 2505
22
use warnings;
23
use CGI;
23
use CGI;
24
24
25
use C4::Auth;
25
use C4::Auth;
26
use C4::Output;
26
use C4::Output;
27
use C4::Biblio;
27
#use C4::Biblio;
28
use C4::Context;
28
use C4::Context;
29
use C4::Breeding;
29
use C4::Breeding;
30
use C4::Koha;
30
#use C4::Koha;
31
use C4::Charset;
31
#use C4::Charset;
32
use ZOOM;
32
#use ZOOM;
33
33
34
my $input        = new CGI;
34
my $input        = new CGI;
35
my $dbh          = C4::Context->dbh;
35
my $dbh          = C4::Context->dbh;
36
my $error         = $input->param('error');
37
my $biblionumber  = $input->param('biblionumber') || 0;
36
my $biblionumber  = $input->param('biblionumber') || 0;
38
my $frameworkcode = $input->param('frameworkcode');
37
my $frameworkcode = $input->param('frameworkcode') || '';
39
my $title         = $input->param('title');
38
my $title         = $input->param('title');
40
my $author        = $input->param('author');
39
my $author        = $input->param('author');
41
my $isbn          = $input->param('isbn');
40
my $isbn          = $input->param('isbn');
Lines 47-74 my $dewey = $input->param('dewey'); Link Here
47
my $controlnumber	= $input->param('controlnumber');
46
my $controlnumber	= $input->param('controlnumber');
48
my $stdid			= $input->param('stdid');
47
my $stdid			= $input->param('stdid');
49
my $srchany			= $input->param('srchany');
48
my $srchany			= $input->param('srchany');
50
my $random        = $input->param('random') || rand(1000000000); # this var is not useful anymore just kept for rel2_2 compatibility
49
my $op            = $input->param('op') || '';
51
my $op            = $input->param('op');
52
my $numberpending;
53
my $attr = '';
54
my $term;
55
my $host;
56
my $server;
57
my $database;
58
my $port;
59
my $marcdata;
60
my @encoding;
61
my @results;
62
my $count;
63
my $record;
64
my $oldbiblio;
65
my $errmsg;
66
my @serverloop = ();
67
my @serverhost;
68
my @servername;
69
my @breeding_loop = ();
70
71
my $DEBUG = 0;    # if set to 1, many debug message are send on syslog.
72
50
73
my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
51
my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
74
        template_name   => "cataloguing/z3950_search.tmpl",
52
        template_name   => "cataloguing/z3950_search.tmpl",
Lines 103-301 if ( $op ne "do_search" ) { Link Here
103
}
81
}
104
else {
82
else {
105
    my @id = $input->param('id');
83
    my @id = $input->param('id');
106
84
    if(@id==0) {
107
    if ( not defined @id ) {
108
        # empty server list -> report and exit
85
        # empty server list -> report and exit
109
        $template->param( emptyserverlist => 1 );
86
        $template->param( emptyserverlist => 1 );
110
        output_html_with_http_headers $input, $cookie, $template->output;
87
        output_html_with_http_headers $input, $cookie, $template->output;
111
        exit;
88
        exit;
112
    }
89
    }
113
90
    my $pars= {
114
    my @oConnection;
91
        random => $input->param('random') || rand(1000000000),
115
    my @oResult;
92
        biblionumber => $biblionumber,
116
    my @errconn;
93
        id => \@id,
117
    my $s = 0;
94
        isbn => $isbn,
118
    my $query = '';
95
        title => $title,
119
    my $nterms;
96
        author => $author,
120
    if ($isbn || $issn) {
97
        dewey => $dewey,
121
        $term=$isbn if ($isbn);
98
        subject => $subject,
122
        $term=$issn if ($issn);
99
        lccall => $lccall,
123
        $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
100
        controlnumber => $controlnumber,
124
        $nterms++;
101
        stdid => $stdid,
125
    }
102
        srchany => $srchany,
126
    if ($title) {
103
    };
127
        utf8::decode($title);
104
    Z3950Search($pars, $template);
128
        $query .= " \@attr 1=4 \"$title\" ";
105
    output_html_with_http_headers $input, $cookie, $template->output;
129
        $nterms++;
130
    }
131
    if ($author) {
132
        utf8::decode($author);
133
        $query .= " \@attr 1=1003 \"$author\" ";
134
        $nterms++;
135
    }
136
    if ($dewey) {
137
        $query .= " \@attr 1=16 \"$dewey\" ";
138
        $nterms++;
139
    }
140
    if ($subject) {
141
        utf8::decode($subject);
142
        $query .= " \@attr 1=21 \"$subject\" ";
143
        $nterms++;
144
    }
145
	if ($lccn) {	
146
        $query .= " \@attr 1=9 $lccn ";
147
        $nterms++;
148
    }
149
    if ($lccall) {
150
        $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
151
        $nterms++;
152
    }
153
    if ($controlnumber) {
154
        $query .= " \@attr 1=12 \"$controlnumber\" ";
155
        $nterms++;
156
    }
157
    if ($stdid) {
158
        $query .= " \@attr 1=1007 \"$stdid\" ";
159
        $nterms++;
160
    }
161
    if ($srchany) {
162
        $query .= " \@attr 1=1016 \"$srchany\" ";
163
        $nterms++;
164
    }
165
for my $i (1..$nterms-1) {
166
    $query = "\@and " . $query;
167
}
168
warn "query ".$query  if $DEBUG;
169
170
    foreach my $servid (@id) {
171
        my $sth = $dbh->prepare("SELECT * FROM z3950servers WHERE id=? ORDER BY rank, name");
172
        $sth->execute($servid);
173
        while ( $server = $sth->fetchrow_hashref ) {
174
            warn "serverinfo ".join(':',%$server) if $DEBUG;
175
            my $option1      = new ZOOM::Options();
176
            $option1->option('async' => 1);
177
            $option1->option('elementSetName', 'F');
178
            $option1->option('databaseName', $server->{db});
179
            $option1->option('user',         $server->{userid}  ) if $server->{userid};
180
            $option1->option('password',     $server->{password}) if $server->{password};
181
            $option1->option('preferredRecordSyntax', $server->{syntax});
182
            $option1->option( 'timeout', $server->{timeout} ) if ($server->{timeout});
183
            $oConnection[$s] = create ZOOM::Connection($option1)
184
              || $DEBUG
185
              && warn( "" . $oConnection[$s]->errmsg() );
186
            warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
187
            $oConnection[$s]->connect( $server->{host}, $server->{port} )
188
              || $DEBUG
189
              && warn( "" . $oConnection[$s]->errmsg() );
190
            $serverhost[$s] = $server->{host};
191
            $servername[$s] = $server->{name};
192
            $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
193
            $s++;
194
        }    ## while fetch
195
    }    # foreach
196
    my $nremaining  = $s;
197
    my $firstresult = 1;
198
199
    for ( my $z = 0 ; $z < $s ; $z++ ) {
200
        warn "doing the search" if $DEBUG;
201
        $oResult[$z] = $oConnection[$z]->search_pqf($query)
202
          || $DEBUG
203
          && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
204
205
        # $oResult[$z] = $oConnection[$z]->search_pqf($query);
206
    }
207
208
  AGAIN:
209
    my $k;
210
    my $event;
211
    while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
212
        $event = $oConnection[ $k - 1 ]->last_event();
213
        warn( "connection ", $k - 1, ": event $event (",
214
            ZOOM::event_str($event), ")\n" )
215
          if $DEBUG;
216
        last if $event == ZOOM::Event::ZEND;
217
    }
218
219
    if ( $k != 0 ) {
220
        $k--;
221
        warn $serverhost[$k] if $DEBUG;
222
        my ( $error, $errmsg, $addinfo, $diagset ) =
223
          $oConnection[$k]->error_x();
224
        if ($error) {
225
            if ($error =~ m/^(10000|10007)$/ ) {
226
                push(@errconn, {'server' => $serverhost[$k], 'error' => $error});
227
            }
228
            $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
229
        }
230
        else {
231
            my $numresults = $oResult[$k]->size();
232
            my $i;
233
            my $result = '';
234
            if ( $numresults > 0 ) {
235
                for ($i = 0; $i < (($numresults < 20) ? $numresults : 20); $i++) {
236
                    my $rec = $oResult[$k]->record($i);
237
                    if ($rec) {
238
                        my $marcrecord;
239
                        $marcdata   = $rec->raw();
240
241
                        my ($charset_result, $charset_errors);
242
                        ($marcrecord, $charset_result, $charset_errors) = 
243
                          MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
244
                        SetUTF8Flag($marcrecord);
245
####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
246
## In HEAD i change everything to UTF-8
247
# In rel2_2 i am not sure what encoding is so no character conversion is done here
248
##Add necessary encoding changes to here -TG
249
                        my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
250
                        $oldbiblio->{isbn}   =~ s/ |-|\.//g if $oldbiblio->{isbn};
251
                        # pad | and ( with spaces to allow line breaks in the HTML
252
                        $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
253
                        $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
254
255
                        $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
256
                        # pad | and ( with spaces to allow line breaks in the HTML
257
                        $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
258
                        $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
259
                          my (
260
                            $notmarcrecord, $alreadyindb, $alreadyinfarm,
261
                            $imported,      $breedingid
262
                          )
263
                          = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
264
                        my %row_data;
265
                        $row_data{server}       = $servername[$k];
266
                        $row_data{isbn}         = $oldbiblio->{isbn};
267
                        $row_data{lccn}         = $oldbiblio->{lccn};
268
                        $row_data{title}        = $oldbiblio->{title};
269
                        $row_data{author}       = $oldbiblio->{author};
270
                        $row_data{date}         = $oldbiblio->{copyrightdate};
271
                        $row_data{edition}      = $oldbiblio->{editionstatement};
272
                        $row_data{breedingid}   = $breedingid;
273
                        $row_data{biblionumber} = $biblionumber;
274
                        push( @breeding_loop, \%row_data );
275
		            
276
                    } else {
277
                        push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
278
                    } # $rec
279
                }
280
            }    #$numresults
281
        }
282
    }    # if $k !=0
283
    $numberpending = $nremaining - 1;
284
    $template->param(
285
        breeding_loop => \@breeding_loop,
286
        server        => $servername[$k],
287
        numberpending => $numberpending,
288
        biblionumber  => $biblionumber,
289
        errconn       => \@errconn
290
    );
291
    
292
    output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
293
294
    #  	print  $template->output  if $firstresult !=1;
295
    $firstresult++;
296
297
  MAYBE_AGAIN:
298
    if ( --$nremaining > 0 ) {
299
        goto AGAIN;
300
    }
301
}    ## if op=search
106
}    ## if op=search
302
- 

Return to bug 6536