parse AND queryies for field combinations from Koha
[Biblio-Z3950.git] / CROSBI.pm
index f65425a..1c9869d 100644 (file)
--- a/CROSBI.pm
+++ b/CROSBI.pm
@@ -6,6 +6,7 @@ use strict;
 use MARC::Record;
 use Data::Dump qw/dump/;
 use DBI;
+use utf8;
 
 use base 'Scraper';
 
@@ -30,15 +31,15 @@ sub diag {
 # @attr 1=1016 any
 
 sub usemap {{
-       4               => '',
-       7               => '',
-       8               => '',
-       1003    => '',
-#      16              => '',
-       21              => '',
-       12              => '',
-#      1007    => '',
-       1016    => '',
+       4               => 'fti_pr:',
+       7               => 'fti_pr:',
+       8               => 'fti_pr:',
+       1003            => 'fti_au:',
+       16              => 'fti_pr:',
+       21              => 'fti_pr:',
+       12              => 'fti_pr:',
+       1007            => 'pti_pr:',
+       1016            => 'fti_au,fti_pr:',
 }};
 
 =for sql
@@ -47,49 +48,62 @@ sub usemap {{
 
 my $dbname = 'bibliografija';
 
-my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", '', '', {AutoCommit => 0});
-
 sub search {
        my ( $self, $query ) = @_;
 
+       utf8::decode( $query );
+       warn "QUERY",dump( $query );
+
        die "need query" unless defined $query;
 
-       my $tsquery = join(' & ', split(/\s+/,$query) );
+       my $table = lc $self->{database};
+       $table =~ s/^crosbi-//g;
 
        my $sql = qq{
 
 select *
-from casopis
+from $table
 inner join rad_ustanova using (id)
 left outer join rad_napomena using (id)
 left outer join rad_projekt using (id)
 left outer join rad_godina using (id)
 left outer join rad_podrucje using (id)
 left outer join url using (id)
-where rad_ustanova.sifra = ? and (
-          fti_au @@ to_tsquery(?)
-       or fti_pr @@ to_tsquery(?)
-)
-
        };
 
-       my $sth = $dbh->prepare( $sql );
+       my @and = ( qq{ rad_ustanova.sifra = ? } );
+       my @exec =  ( 130 ); # FIXME ustanova
 
-warn "XXX SQL = ",$sql;
+       foreach my $and ( split(/ AND /, $query) ) {
 
-#-- and naslov like ?
+               if ( $and =~ s/^(fti_.+):// ) {
+                       my $tsquery = join(' & ', split(/\s+/,$and) );
 
-       $sth->execute(
-               130, # FIXME ustanova
-               $tsquery,
-               $tsquery,
-#              , '%' . $query . '%'
-       );
+                       my @or;
+                       foreach my $f ( split(/,/,$1) ) {
+                               push @or, "$f @@ to_tsquery(?)";
+                               push @exec, $tsquery;
+                       };
+                       push @and, "( " . join(" or ", @or) . ")";
+               } else {
+                       warn "INVALID QUERY [$query]";
+               }
+       }
+       $sql .= "where " . join(" and ", @and);
+
+warn "XXX SQL = ",$sql, dump( @exec );
+
+       my $dbh = DBI->connect_cached("dbi:Pg:dbname=$dbname", '', '', {AutoCommit => 0});
+
+       my $sth = $dbh->prepare( $sql );
+
+       $sth->execute( @exec );
 
-       $self->{_sth} = $sth;
        my $hits = $sth->rows;
 
-       warn "# [$tsquery] $hits hits\n";
+       $self->{_sth} = $sth;
+
+       warn "# [$query] $hits hits\n";
 
        return $self->{hits} = $hits;
 }
@@ -146,7 +160,7 @@ sub next_marc {
        my $id = $row->{id} || die "no id";
 
        my $marc = MARC::Record->new;
-       $marc->encoding('utf-8');
+       $marc->encoding('UTF-8');
 
        my $leader = $marc->leader;
 
@@ -156,12 +170,12 @@ sub next_marc {
 ## LDR 06 - a - language material 
 ## LDR 07 - a - monographic component part 
 
-       $leader =~ s/^(....).../$1naa/;
+       $leader =~ s/^(....)...(.+)/$1naa$2/;
 
 ## LDR 17 - Encoding level ; 7 - minimal level, u - unknown
 ## LDR 18 - i = isbd ; u = unknown
 
-       $leader =~ s/^(.{17})/$1uu/;
+       $leader =~ s/^(.{17})..(.+)/$1uu$2/;
 
        $marc->leader( $leader );
        warn "# leader [$leader]";
@@ -282,8 +296,18 @@ sub next_marc {
                'a' => $row->{title}
        );
 
+       sub page_range {
+               my ( $prefix, $from, $to ) = @_;
+               my $out;
+               if ( $from ) {
+                       $out = $prefix . $from;
+                       $out .= '-' . $to if $to;
+               }
+               return $out;
+       }
+
        $marc->add_fields(300,' ',' ',
-               a => join(' ', $row->{stranica_prva}, $row->{stranica_zadnja}),
+               a => page_range('',$row->{stranica_prva},$row->{stranica_zadnja}),
                f => 'str.'
        );
 
@@ -340,11 +364,10 @@ sub next_marc {
                ) foreach @a;
        }
 
-
        $marc->add_fields(773,'0',' ',
                t => $row->{casopis},
                x => $row->{issn},
-               g => "$row->{volumen}, ($row->{godina}), str. $row->{stranica_prva}-$row->{stranica_zadnja}",
+               g => "$row->{volumen} ($row->{godina}), $row->{broj} ;" . page_range(' str. ',$row->{stranica_prva}, $row->{stranica_zadnja}),
        );
 
        if ( my $file = $row->{datoteka} ) {
@@ -360,17 +383,20 @@ sub next_marc {
                );
        }
 
-       $marc->add_fields(942,' ',' ',
-               c => 'CLA',
-       $row->{status_rada} ? (
+       my @f942 = (
+               c => 'CLA'
+       );
+       if ( $row->{status_rada} ) {
+               push @f942, (
                f => 1,
                g => $row->{status_rada}
-       ) : (),
-       $row->{kategorija} =~ m/Znanstveni/ ? (
-               t => '1.01'
-       ) : $row->{kategorija} =~ m/Strucni/ ? (
-               t => '1.04'
-       ) : (),
+               );
+       }
+       push @f942, t => '1.01' if $row->{kategorija} =~ m/Znanstveni/;
+       push @f942, t => '1.04' if $row->{kategorija} =~ m/Strucni/;
+
+       $marc->add_fields(942,' ',' ',
+               @f942,
                u => '1',
                z => join(' - ', $row->{kategorija}, $row->{vrsta_rada}),
        );