Bug 12151: Remove uses of smartmatch operator in Search.pm and opac-search.pl
[koha.git] / C4 / Breeding.pm
1 package C4::Breeding;
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2013 Prosentient Systems
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use C4::Biblio;
25 use C4::Koha;
26 use C4::Charset;
27 use MARC::File::USMARC;
28 use C4::ImportBatch;
29 use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
30
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32
33 BEGIN {
34         # set the version for version checking
35     $VERSION = 3.07.00.049;
36         require Exporter;
37         @ISA = qw(Exporter);
38     @EXPORT = qw(&BreedingSearch &Z3950Search &Z3950SearchAuth);
39 }
40
41 =head1 NAME
42
43 C4::Breeding : module to add biblios to import_records via
44                the breeding/reservoir API.
45
46 =head1 SYNOPSIS
47
48     Z3950Search($pars, $template);
49     ($count, @results) = &BreedingSearch($title,$isbn,$random);
50
51 =head1 DESCRIPTION
52
53 This module contains routines related to Koha's Z39.50 search into
54 cataloguing reservoir features.
55
56 =head2 BreedingSearch
57
58 ($count, @results) = &BreedingSearch($title,$isbn,$random);
59 C<$title> contains the title,
60 C<$isbn> contains isbn or issn,
61 C<$random> contains the random seed from a z3950 search.
62
63 C<$count> is the number of items in C<@results>. C<@results> is an
64 array of references-to-hash; the keys are the items from the C<import_records> and
65 C<import_biblios> tables of the Koha database.
66
67 =cut
68
69 sub BreedingSearch {
70     my ($search,$isbn,$z3950random) = @_;
71     my $dbh   = C4::Context->dbh;
72     my $count = 0;
73     my ($query,@bind);
74     my $sth;
75     my @results;
76
77     # normalise ISBN like at import
78     $isbn = C4::Koha::GetNormalizedISBN($isbn);
79
80     $query = "SELECT import_record_id, file_name, isbn, title, author
81               FROM  import_biblios 
82               JOIN import_records USING (import_record_id)
83               JOIN import_batches USING (import_batch_id)
84               WHERE ";
85     if ($z3950random) {
86         $query .= "z3950random = ?";
87         @bind=($z3950random);
88     } else {
89         @bind=();
90         if (defined($search) && length($search)>0) {
91             $search =~ s/(\s+)/\%/g;
92             $query .= "title like ? OR author like ?";
93             push(@bind,"%$search%", "%$search%");
94         }
95         if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
96             $query .= " and ";
97         }
98         if (defined($isbn) && length($isbn)>0) {
99             $query .= "isbn like ?";
100             push(@bind,"$isbn%");
101         }
102     }
103     $sth   = $dbh->prepare($query);
104     $sth->execute(@bind);
105     while (my $data = $sth->fetchrow_hashref) {
106             $results[$count] = $data;
107             # FIXME - hack to reflect difference in name 
108             # of columns in old marc_breeding and import_records
109             # There needs to be more separation between column names and 
110             # field names used in the templates </soapbox>
111             $data->{'file'} = $data->{'file_name'};
112             $data->{'id'} = $data->{'import_record_id'};
113             $count++;
114     } # while
115
116     $sth->finish;
117     return($count, @results);
118 } # sub breedingsearch
119
120
121 =head2 Z3950Search
122
123 Z3950Search($pars, $template);
124
125 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
126 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).
127 This code is used in acqui/z3950_search and cataloging/z3950_search.
128 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
129
130 =cut
131
132 sub Z3950Search {
133     my ($pars, $template)= @_;
134
135     my @id= @{$pars->{id}};
136     my $page= $pars->{page};
137     my $biblionumber= $pars->{biblionumber};
138     my $isbn= $pars->{isbn};
139     my $issn= $pars->{issn};
140     my $title= $pars->{title};
141     my $author= $pars->{author};
142     my $dewey= $pars->{dewey};
143     my $subject= $pars->{subject};
144     my $lccn= $pars->{lccn};
145     my $lccall= $pars->{lccall};
146     my $controlnumber= $pars->{controlnumber};
147     my $srchany= $pars->{srchany};
148     my $stdid= $pars->{stdid};
149
150     my $show_next       = 0;
151     my $total_pages     = 0;
152     my $term;
153     my @results;
154     my @breeding_loop = ();
155     my @oConnection;
156     my @oResult;
157     my @errconn;
158     my $s = 0;
159     my $query;
160     my $nterms=0;
161     my $imported=0;
162     my @serverinfo; #replaces former serverhost, servername, encoding
163
164     if ($isbn) {
165         $term=$isbn;
166         $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
167         $nterms++;
168     }
169     if ($issn) {
170         $term=$issn;
171         $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
172         $nterms++;
173     }
174     if ($title) {
175         $query .= " \@attr 1=4 \"$title\" ";
176         $nterms++;
177     }
178     if ($author) {
179         $query .= " \@attr 1=1003 \"$author\" ";
180         $nterms++;
181     }
182     if ($dewey) {
183         $query .= " \@attr 1=16 \"$dewey\" ";
184         $nterms++;
185     }
186     if ($subject) {
187         $query .= " \@attr 1=21 \"$subject\" ";
188         $nterms++;
189     }
190     if ($lccn) {
191         $query .= " \@attr 1=9 $lccn ";
192         $nterms++;
193     }
194     if ($lccall) {
195         $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
196         $nterms++;
197     }
198     if ($controlnumber) {
199         $query .= " \@attr 1=12 \"$controlnumber\" ";
200         $nterms++;
201     }
202     if($srchany) {
203         $query .= " \@attr 1=1016 \"$srchany\" ";
204         $nterms++;
205     }
206     if($stdid) {
207         $query .= " \@attr 1=1007 \"$stdid\" ";
208         $nterms++;
209     }
210     for my $i (1..$nterms-1) {
211         $query = "\@and " . $query;
212     }
213
214     my $dbh   = C4::Context->dbh;
215     foreach my $servid (@id) {
216         my $sth = $dbh->prepare("select * from z3950servers where id=?");
217         $sth->execute($servid);
218         while (my $server = $sth->fetchrow_hashref) {
219             my $option1= new ZOOM::Options();
220             $option1->option( 'async' => 1 );
221             $option1->option( 'elementSetName', 'F' );
222             $option1->option( 'databaseName',   $server->{db} );
223             $option1->option( 'user', $server->{userid} ) if $server->{userid};
224             $option1->option( 'password', $server->{password} ) if $server->{password};
225             $option1->option( 'preferredRecordSyntax', $server->{syntax} );
226             $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
227             $oConnection[$s]= create ZOOM::Connection($option1);
228             $oConnection[$s]->connect( $server->{host}, $server->{port} );
229             $serverinfo[$s]->{host}= $server->{host};
230             $serverinfo[$s]->{name}= $server->{name};
231             $serverinfo[$s]->{encd}= $server->{encoding} // "iso-5426";
232             $s++;
233         }    ## while fetch
234     }    # foreach
235     my $nremaining  = $s;
236
237     for ( my $z = 0 ; $z < $s ; $z++ ) {
238         $oResult[$z] = $oConnection[$z]->search_pqf($query);
239     }
240
241     while ( $nremaining-- ) {
242         my $k;
243         my $event;
244         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
245             $event = $oConnection[ $k - 1 ]->last_event();
246             last if $event == ZOOM::Event::ZEND;
247         }
248
249         if ( $k != 0 ) {
250             $k--;
251             my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
252             if ($error) {
253                 if ($error =~ m/^(10000|10007)$/ ) {
254                     push(@errconn, { server => $serverinfo[$k]->{host}, error => $error } );
255                 }
256             }
257             else {
258                 my $numresults = $oResult[$k]->size();
259                 my $i;
260                 my $result = '';
261                 if ( $numresults > 0  and $numresults >= (($page-1)*20)) {
262                     $show_next = 1 if $numresults >= ($page*20);
263                     $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
264                     for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
265                         if($oResult[$k]->record($i)) {
266                             my $res=_handle_one_result($oResult[$k]->record($i), $serverinfo[$k], ++$imported, $biblionumber); #ignores error in sequence numbering
267                             push @breeding_loop, $res if $res;
268                         }
269                         else {
270                             push(@breeding_loop,{'server'=>$serverinfo[$k]->{name},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
271                         }
272                     }
273                 }    #if $numresults
274             }
275         }    # if $k !=0
276
277         $template->param(
278             numberpending => $nremaining,
279             current_page => $page,
280             total_pages => $total_pages,
281             show_nextbutton => $show_next?1:0,
282             show_prevbutton => $page!=1,
283         );
284     } # while nremaining
285
286     #close result sets and connections
287     foreach(0..$s-1) {
288         $oResult[$_]->destroy();
289         $oConnection[$_]->destroy();
290     }
291
292     my @servers = ();
293     foreach my $id (@id) {
294         push @servers, {id => $id};
295     }
296     $template->param(
297         breeding_loop => \@breeding_loop,
298         servers => \@servers,
299         errconn       => \@errconn
300     );
301 }
302
303 sub _handle_one_result {
304     my ($zoomrec, $servhref, $seq, $bib)= @_;
305
306     my $raw= $zoomrec->raw();
307     my ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encd}); #ignores charset return values
308     SetUTF8Flag($marcrecord);
309
310     my $batch_id = GetZ3950BatchId($servhref->{name});
311     my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0, 0);
312         #FIXME passing 0 for z3950random
313         #Will eliminate this unused field in a followup report
314         #Last zero indicates: no update for batch record counts
315
316
317     #call to TransformMarcToKoha replaced by next call
318     #we only need six fields from the marc record
319     return _add_rowdata(
320         {
321             biblionumber => $bib,
322             server       => $servhref->{name},
323             breedingid   => $breedingid,
324         }, $marcrecord) if $breedingid;
325 }
326
327 sub _add_rowdata {
328     my ($row, $record)=@_;
329     my %fetch= (
330         title => 'biblio.title',
331         author => 'biblio.author',
332         isbn =>'biblioitems.isbn',
333         lccn =>'biblioitems.lccn', #LC control number (not call number)
334         edition =>'biblioitems.editionstatement',
335         date => 'biblio.copyrightdate', #MARC21
336         date2 => 'biblioitems.publicationyear', #UNIMARC
337     );
338     foreach my $k (keys %fetch) {
339         my ($t, $f)= split '\.', $fetch{$k};
340         $row= C4::Biblio::TransformMarcToKohaOneField($t, $f, $record, $row);
341         $row->{$k}= $row->{$f} if $k ne $f;
342     }
343     $row->{date}//= $row->{date2};
344     $row->{isbn}=_isbn_replace($row->{isbn});
345     return $row;
346 }
347
348 sub _isbn_replace {
349     my ($isbn) = @_;
350     return unless defined $isbn;
351     $isbn =~ s/ |-|\.//g;
352     $isbn =~ s/\|/ \| /g;
353     $isbn =~ s/\(/ \(/g;
354     return $isbn;
355 }
356
357 =head2 ImportBreedingAuth
358
359 ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type);
360
361     ImportBreedingAuth imports MARC records in the reservoir (import_records table).
362     ImportBreedingAuth is based on the ImportBreeding subroutine.
363
364 =cut
365
366 sub ImportBreedingAuth {
367     my ($marcrecords,$overwrite_auth,$filename,$encoding,$z3950random,$batch_type) = @_;
368     my @marcarray = split /\x1D/, $marcrecords;
369
370     my $dbh = C4::Context->dbh;
371
372     my $batch_id = GetZ3950BatchId($filename);
373     my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
374
375 #     $encoding = C4::Context->preference("marcflavour") unless $encoding;
376     # fields used for import results
377     my $imported=0;
378     my $alreadyindb = 0;
379     my $alreadyinfarm = 0;
380     my $notmarcrecord = 0;
381     my $breedingid;
382     for (my $i=0;$i<=$#marcarray;$i++) {
383         my ($marcrecord, $charset_result, $charset_errors);
384         ($marcrecord, $charset_result, $charset_errors) =
385             MarcToUTF8Record($marcarray[$i]."\x1D", C4::Context->preference("marcflavour"), $encoding);
386
387         # Normalize the record so it doesn't have separated diacritics
388         SetUTF8Flag($marcrecord);
389
390         if (scalar($marcrecord->fields()) == 0) {
391             $notmarcrecord++;
392         } else {
393             my $heading;
394             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
395
396             my $heading_authtype_code;
397             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
398
399             my $controlnumber;
400             $controlnumber = $marcrecord->field('001')->data;
401
402             #Check if the authority record already exists in the database...
403             my ($duplicateauthid,$duplicateauthvalue);
404             if ($marcrecord && $heading_authtype_code) {
405                 ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority( $marcrecord, $heading_authtype_code);
406             }
407
408             if ($duplicateauthid && $overwrite_auth ne 2) {
409                 #If the authority record exists and $overwrite_auth doesn't equal 2, then mark it as already in the DB
410                 $alreadyindb++;
411             } else {
412                 if ($controlnumber && $heading) {
413                     $searchbreeding->execute($controlnumber,$heading);
414                     ($breedingid) = $searchbreeding->fetchrow;
415                 }
416                 if ($breedingid && $overwrite_auth eq '0') {
417                     $alreadyinfarm++;
418                 } else {
419                     if ($breedingid && $overwrite_auth eq '1') {
420                         ModAuthorityInBatch($breedingid, $marcrecord);
421                     } else {
422                         my $import_id = AddAuthToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
423                         $breedingid = $import_id;
424                     }
425                     $imported++;
426                 }
427             }
428         }
429     }
430     return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
431 }
432
433 =head2 Z3950SearchAuth
434
435 Z3950SearchAuth($pars, $template);
436
437 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
438 title, uniform title, subject, subjectsubdiv, srchany.
439 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
440 This code is used in cataloging/z3950_auth_search.
441 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
442
443 =cut
444
445 sub Z3950SearchAuth {
446     my ($pars, $template)= @_;
447
448     my $dbh   = C4::Context->dbh;
449     my @id= @{$pars->{id}};
450     my $random= $pars->{random};
451     my $page= $pars->{page};
452
453     my $nameany= $pars->{nameany};
454     my $authorany= $pars->{authorany};
455     my $authorpersonal= $pars->{authorpersonal};
456     my $authorcorp= $pars->{authorcorp};
457     my $authormeetingcon= $pars->{authormeetingcon};
458     my $title= $pars->{title};
459     my $uniformtitle= $pars->{uniformtitle};
460     my $subject= $pars->{subject};
461     my $subjectsubdiv= $pars->{subjectsubdiv};
462     my $srchany= $pars->{srchany};
463
464     my $show_next       = 0;
465     my $total_pages     = 0;
466     my $attr = '';
467     my $host;
468     my $server;
469     my $database;
470     my $port;
471     my $marcdata;
472     my @encoding;
473     my @results;
474     my $count;
475     my $record;
476     my @serverhost;
477     my @servername;
478     my @breeding_loop = ();
479
480     my @oConnection;
481     my @oResult;
482     my @errconn;
483     my $s = 0;
484     my $query;
485     my $nterms=0;
486
487     if ($nameany) {
488         $query .= " \@attr 1=1002 \"$nameany\" "; #Any name (this includes personal, corporate, meeting/conference authors, and author names in subject headings)
489         #This attribute is supported by both the Library of Congress and Libraries Australia 08/05/2013
490         $nterms++;
491     }
492
493     if ($authorany) {
494         $query .= " \@attr 1=1003 \"$authorany\" "; #Author-name (this includes personal, corporate, meeting/conference authors, but not author names in subject headings)
495         #This attribute is not supported by the Library of Congress, but is supported by Libraries Australia 08/05/2013
496         $nterms++;
497     }
498
499     if ($authorcorp) {
500         $query .= " \@attr 1=2 \"$authorcorp\" "; #1005 is another valid corporate author attribute...
501         $nterms++;
502     }
503
504     if ($authorpersonal) {
505         $query .= " \@attr 1=1 \"$authorpersonal\" "; #1004 is another valid personal name attribute...
506         $nterms++;
507     }
508
509     if ($authormeetingcon) {
510         $query .= " \@attr 1=3 \"$authormeetingcon\" "; #1006 is another valid meeting/conference name attribute...
511         $nterms++;
512     }
513
514     if ($subject) {
515         $query .= " \@attr 1=21 \"$subject\" ";
516         $nterms++;
517     }
518
519     if ($subjectsubdiv) {
520         $query .= " \@attr 1=47 \"$subjectsubdiv\" ";
521         $nterms++;
522     }
523
524     if ($title) {
525         $query .= " \@attr 1=4 \"$title\" "; #This is a regular title search. 1=6 will give just uniform titles
526         $nterms++;
527     }
528
529      if ($uniformtitle) {
530         $query .= " \@attr 1=6 \"$uniformtitle\" "; #This is the uniform title search
531         $nterms++;
532     }
533
534     if($srchany) {
535         $query .= " \@attr 1=1016 \"$srchany\" ";
536         $nterms++;
537     }
538
539     for my $i (1..$nterms-1) {
540         $query = "\@and " . $query;
541     }
542
543     foreach my $servid (@id) {
544         my $sth = $dbh->prepare("select * from z3950servers where id=?");
545         $sth->execute($servid);
546         while ( $server = $sth->fetchrow_hashref ) {
547             my $option1      = new ZOOM::Options();
548             $option1->option( 'async' => 1 );
549             $option1->option( 'elementSetName', 'F' );
550             $option1->option( 'databaseName',   $server->{db} );
551             $option1->option( 'user', $server->{userid} ) if $server->{userid};
552             $option1->option( 'password', $server->{password} ) if $server->{password};
553             $option1->option( 'preferredRecordSyntax', $server->{syntax} );
554             $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
555             $oConnection[$s] = create ZOOM::Connection($option1);
556             $oConnection[$s]->connect( $server->{host}, $server->{port} );
557             $serverhost[$s] = $server->{host};
558             $servername[$s] = $server->{name};
559             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
560             $s++;
561         }    ## while fetch
562     }    # foreach
563     my $nremaining  = $s;
564
565     for ( my $z = 0 ; $z < $s ; $z++ ) {
566         $oResult[$z] = $oConnection[$z]->search_pqf($query);
567     }
568
569     while ( $nremaining-- ) {
570         my $k;
571         my $event;
572         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
573             $event = $oConnection[ $k - 1 ]->last_event();
574             last if $event == ZOOM::Event::ZEND;
575         }
576
577         if ( $k != 0 ) {
578             $k--;
579             my ($error, $errmsg, $addinfo, $diagset)= $oConnection[$k]->error_x();
580             if ($error) {
581                 if ($error =~ m/^(10000|10007)$/ ) {
582                     push(@errconn, {'server' => $serverhost[$k]});
583                 }
584             }
585             else {
586                 my $numresults = $oResult[$k]->size();
587                 my $i;
588                 my $result = '';
589                 if ( $numresults > 0  and $numresults >= (($page-1)*20)) {
590                     $show_next = 1 if $numresults >= ($page*20);
591                     $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
592                     for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
593                         my $rec = $oResult[$k]->record($i);
594                         if ($rec) {
595                             my $marcrecord;
596                             my $marcdata;
597                             $marcdata   = $rec->raw();
598
599                             my ($charset_result, $charset_errors);
600                             ($marcrecord, $charset_result, $charset_errors)= MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
601
602                             my $heading;
603                             my $heading_authtype_code;
604                             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
605                             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
606
607                             my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
608                             my %row_data;
609                             $row_data{server}       = $servername[$k];
610                             $row_data{breedingid}   = $breedingid;
611                             $row_data{heading}      = $heading;
612                             $row_data{heading_code}      = $heading_authtype_code;
613                             push( @breeding_loop, \%row_data );
614                         }
615                         else {
616                             push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1});
617                         }
618                     }
619                 }    #if $numresults
620             }
621         }    # if $k !=0
622
623         $template->param(
624             numberpending => $nremaining,
625             current_page => $page,
626             total_pages => $total_pages,
627             show_nextbutton => $show_next?1:0,
628             show_prevbutton => $page!=1,
629         );
630     } # while nremaining
631
632     #close result sets and connections
633     foreach(0..$s-1) {
634         $oResult[$_]->destroy();
635         $oConnection[$_]->destroy();
636     }
637
638     my @servers = ();
639     foreach my $id (@id) {
640         push @servers, {id => $id};
641     }
642     $template->param(
643         breeding_loop => \@breeding_loop,
644         servers => \@servers,
645         errconn       => \@errconn
646     );
647 }
648
649 1;
650 __END__
651