first draft of CROSBI gateway
[Biblio-Z3950.git] / CROSBI.pm
1 package CROSBI;
2
3 use warnings;
4 use strict;
5
6 use MARC::Record;
7 use Data::Dump qw/dump/;
8 use DBI;
9
10 use base 'Scraper';
11
12 my $debug = $ENV{DEBUG} || 0;
13
14 sub diag {
15         warn "# ", @_, $/;
16 }
17
18
19 # Koha Z39.50 query:
20 #
21 # Bib-1 @and @and @and @and @and @and @and @or
22 # @attr 1=4 title 
23 # @attr 1=7 isbn
24 # @attr 1=8 issn 
25 # @attr 1=1003 author 
26 # @attr 1=16 dewey 
27 # @attr 1=21 subject-holding 
28 # @attr 1=12 control-no 
29 # @attr 1=1007 standard-id 
30 # @attr 1=1016 any
31
32 sub usemap {{
33         4               => '',
34         7               => '',
35         8               => '',
36         1003    => '',
37 #       16              => '',
38         21              => '',
39         12              => '',
40 #       1007    => '',
41         1016    => '',
42 }};
43
44 =for sql
45
46 =cut
47
48 my $dbname = 'bibliografija';
49
50 my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", '', '', {AutoCommit => 0});
51
52 sub search {
53         my ( $self, $query ) = @_;
54
55         die "need query" unless defined $query;
56
57         my $tsquery = join(' & ', split(/\s+/,$query) );
58
59         my $sql = qq{
60
61 select *
62 from casopis
63 inner join rad_ustanova using (id)
64 left outer join rad_napomena using (id)
65 left outer join rad_projekt using (id)
66 where sifra = ? and (
67            fti_au @@ to_tsquery(?)
68         or fti_pr @@ to_tsquery(?)
69 )
70
71         };
72
73         my $sth = $dbh->prepare( $sql );
74
75 warn "XXX SQL = ",$sql;
76
77 #-- and naslov like ?
78
79         $sth->execute(
80                 130, # FIXME ustanova
81                 $tsquery,
82                 $tsquery,
83 #               , '%' . $query . '%'
84         );
85
86         $self->{_sth} = $sth;
87         my $hits = $sth->rows;
88
89         warn "# [$tsquery] $hits hits\n";
90
91         return $self->{hits} = $hits;
92 }
93
94
95 sub next_marc {
96         my ($self,$format) = @_;
97
98         $format ||= 'marc';
99
100         my $sth = $self->{_sth} || die "no _sth";
101
102         my $row = $sth->fetchrow_hashref;
103
104         die "no row" unless $row;
105
106         my $id = $row->{id} || die "no id";
107
108         my $marc = MARC::Record->new;
109         $marc->encoding('utf-8');
110
111         my $leader = $marc->leader;
112
113         warn "# leader [$leader]";
114         #$leader =~ s/^(....).../$1na$biblevel/;
115         $marc->leader( $leader );
116
117         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
118
119 # /srv/webpac2/conf/crosbi/2016-12-12/casopis-dbi2marc.pl
120
121         my ( $first_author, $authors ) = split(/ ;\s*/,$row->{autori});
122
123         $marc->add_fields(100,'1',' ','a' => $first_author );
124
125
126
127         my $naslov = $row->{naslov}; # XXX title?
128
129         my $i2 =
130                 $naslov =~ m/^Eine /                            ? 5 :
131                 $naslov =~ m/(Die|Das|Der|Ein|Les|Los|The) /    ? 4 :
132                 $naslov =~ m/^(Um|Un|An|La|Le|Lo|Il) /          ? 3 :
133                 $naslov =~ m/^(A|L) /                           ? 2 :
134                 $naslov =~ m/^L'/                               ? 2 :
135                                                                   0;
136
137         $marc->add_fields(245,'1',$i2,
138                 'a' => $naslov . ' /',
139                 'c' => $row->{autori} . '.',
140         );
141
142         $marc->add_fields(246,'3',' ',
143                 'i' => 'Naslov na engleskom:',
144                 'a' => $row->{title}
145         );
146
147         $marc->add_fields(300,' ',' ',
148                 a => join(' ', $row->{stranica_prva}, $row->{stranica_zadnja}),
149                 f => 'str.'
150         );
151
152         $marc->add_fields(363,' ',' ',
153                 a => $row->{volumen},
154                 b => $row->{broj},
155                 i => $row->{godina},
156         );
157
158 # /data/FF/crosbi/2016-12-12/casopis-rad_napomena.sql
159
160         $marc->add_fields(500,' ',' ',
161                 a => substr($row->{napomena}, 0, 9999), # XXX marc limit for one subfield is 4 digits in dictionary
162         );
163
164         $marc->add_fields(520,' ',' ',
165                 a => substr($row->{sazetak}, 0, 9999)
166         );
167
168 =for later
169         $marc->add_fields(999,' ',' ',
170                 a => $row->{}
171         );
172
173 =cut
174
175 #       diag "# hash ",dump($hash);
176         diag "# marc\n", $marc->as_formatted;
177
178         $self->save_marc( "$id.marc", $marc->as_usmarc );
179
180         return $id;
181
182 }
183
184 1;