-
Notifications
You must be signed in to change notification settings - Fork 4
/
CROSBI.pm
571 lines (434 loc) · 12.6 KB
/
CROSBI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package CROSBI;
use warnings;
use strict;
use MARC::Record;
use Data::Dump qw/dump/;
use DBI;
use utf8;
use Scraper;
use base 'Scraper';
my $debug = $ENV{DEBUG} || 0;
sub diag {
warn "# ", @_, $/;
}
# Koha Z39.50 query:
#
# Bib-1 @and @and @and @and @and @and @and @or
# @attr 1=4 title
# @attr 1=7 isbn
# @attr 1=8 issn
# @attr 1=1003 author
# @attr 1=16 dewey (godina)
# @attr 1=21 subject-holding
# @attr 1=12 control-no
# @attr 1=1007 standard-id
# @attr 1=1016 any
sub usemap {{
4 => 'fti_pr:',
7 => 'fti_pr:',
8 => 'fti_pr:',
1003 => 'fti_au:',
16 => 'fti_pr:',
21 => 'fti_pr:',
12 => 'fti_pr:',
1007 => 'fti_pr:',
1016 => 'fti_au,fti_pr:',
}};
=for sql
=cut
my $dbname = 'bibliografija';
my $pg_user = $ENV{PGUSER} || '';
my $pg_passwd = $ENV{PGPASSWD} || '';
my $pg_host = $ENV{PGHOST} || '';
my @and;
my @exec;
sub search {
my ( $self, $query ) = @_;
utf8::decode( $query );
warn "QUERY",dump( $query );
die "ERROR need query" unless defined $query;
$query =~ s/^\s+//;
$query =~ s/\s+$//;
my $table = lc $self->{database};
$table =~ s/^crosbi-//g;
$self->{_table} = $table;
my $sql = qq{
select
$table.*
,ARRAY( select napomena from rad_napomena where rad_napomena.id = $table.id ) as rad_napomena
,ARRAY( select projekt from rad_projekt where rad_projekt.id = $table.id ) as rad_projekt
,ARRAY( select datum from rad_godina where rad_godina.id = $table.id ) as rad_godina
,ARRAY( select sifra from rad_podrucje where rad_podrucje.id = $table.id ) as rad_podrucje
,ARRAY( select url from url where url.id = $table.id ) as url
from $table
inner join rad_ustanova using (id) -- sifra
};
@and = ( qq{ rad_ustanova.sifra = ? } );
@exec = ( 130 ); # FIXME ustanova
sub parse_fti {
my $query = shift;
warn "## parse_fti [$query]";
my $fti;
if ( $query =~ s/^(fti_.+):\s*// ) {
$fti = $1;
} else {
warn "INVALID QUERY no fti_xxx: [$query]";
}
my $tsquery = join(' & ', split(/\s+/,$query) );
my @or;
foreach my $f ( split(/,/,$fti) ) {
# push @or, "$f @@ to_tsquery('english',?)";
# push @exec, $tsquery;
push @or, "$f @@ to_tsquery('english',?) or $f @@ to_tsquery('croatian',?)";
push @exec, $tsquery, $tsquery;
};
push @and, "( " . join(" or ", @or) . ")";
}
if ( $query =~ / AND / ) {
foreach my $and ( split(/ AND /, $query) ) {
parse_fti $and;
}
} elsif ( $query =~ m/fti_.+:/ ) {
parse_fti $query;
} else { # no " AND " in query
my $tsquery = join(' & ', split(/\s+/,$query) );
# push @and, "( fti_au @@ to_tsquery('english',?) or fti_pr @@ to_tsquery('english',?) )";
# push @exec, $tsquery, $tsquery;
push @and, "( fti_au @@ to_tsquery('english',?) or fti_pr @@ to_tsquery('english',?) or fti_au @@ to_tsquery('croatian',?) or fti_pr @@ to_tsquery('croatian',?) )";
push @exec, $tsquery, $tsquery, $tsquery, $tsquery;
}
$sql .= "where " . join(" and ", @and);
warn "XXX SQL = ",$sql, dump( @exec );
my $dbh = DBI->connect_cached("dbi:Pg:dbname=$dbname;host=$pg_host", $pg_user, $pg_passwd, {AutoCommit => 0});
my $sth = $dbh->prepare( $sql );
$sth->execute( @exec );
my $hits = $sth->rows;
$self->{_sth} = $sth;
warn "# [$query] $hits hits\n";
return $self->{hits} = $hits;
}
my $langrecode008 = {
'bugarski' => 'bul',
'Češki' => 'cze',
'češki' => 'cze',
'ENG' => 'eng',
'Esperanto' => 'epo',
'FRA' => 'fra',
'GER' => 'ger',
'HRV' => 'hrv',
'ITA' => 'ita',
'Japanski' => 'jpn',
'Latinski' => 'lat',
'mađarski' => 'hun',
'Madžarski' => 'hun',
'Makedonski' => 'mac',
'nizozemski' => 'dut',
'Poljski' => 'pol',
'poljski' => 'pol',
'Portugalski' => 'por',
'portugalski' => 'por',
'RUS' => 'rus',
'Rumunjski' => 'rum',
'rumunjski' => 'rum',
'rusinski' => 'sla',
'slovački' => 'slo',
'slovenski' => 'slv',
'SLV' => 'slv',
'SPA' => 'spa',
'Srpski' => 'srp',
'srpski' => 'srp',
'Turski' => 'tur',
'turski' => 'tur',
'ukrajinski' => 'ukr',
'HRV-ENG' => 'mul',
'HRV-GER' => 'mul',
'hrvatsko-francuski' => 'mul',
} ;
sub next_marc {
my ($self,$format) = @_;
$format ||= 'marc';
my $sth = $self->{_sth} || die "ERROR no _sth";
my $row = $sth->fetchrow_hashref;
warn "## row = ",dump($row) if $ENV{DEBUG};
warn "ERROR: no row" unless $row;
my $id = $row->{id} || die "ERROR no id";
my $marc = MARC::Record->new;
$marc->encoding('UTF-8');
my $leader = $marc->leader;
# /srv/webpac2/conf/crosbi/2016-12-12/casopis-dbi2marc.pl
## LDR 05 - n - new
## LDR 06 - a - language material
## LDR 07 - a - monographic component part
$leader =~ s/^(.....)...(.+)/$1naa$2/;
## LDR 17 - Encoding level ; 7 - minimal level, u - unknown
## LDR 18 - i = isbd ; u = unknown
$leader =~ s/^(.{17})..(.+)/$1ui$2/;
$marc->leader( $leader );
warn "# leader [$leader]";
### 008 - All materials
## 008 - 00-05 - Date entered on file
my $f008 = $1 . $2 . $3 if $row->{time_date} =~ m/\d\d(\d\d)-(\d\d)-(\d\d)/;
## 008 06 - Type of date/Publication status
$f008 .= 's';
## 008 07-10 - Date 1
$f008 .= substr( $row->{rad_godina}->[0] ,0,4);
## 008 11-14 - Date 2
#$f008 .= ' ';
$f008 .= ' ' x ( 15 - length($f008) ); # pad to 15 position
## 008 15-17 - Place of publication, production, or execution - ako nema 102, popunjava se s |
$f008 .= 'xx ';
## 008 29 - Conference publication
$f008 .= ' ' x ( 29 - length($f008) );
$f008 .= $self->{_table} eq 'zbornik' ? '1' : '0';
## 008 35-37 - Language
$f008 .= ' ' x ( 35 - length($f008) ); # pad to 35 position
if ( my $lng = $langrecode008->{ $row->{jezik} } ) {
$f008 .= $lng;
} else {
warn "INFO unknown jezik [$row->{jezik}] insert into langrecode008!";
#$f008 .= ' ';
}
$f008 .= ' ' x ( 38 - length($f008) );
## 008 38 - Modified record
$f008 .= '|';
## 008 39 - Cataloging source - d (other)
$f008 .= 'd';
warn "# 008 ",length($f008);
$marc->add_fields('008', $f008); # FIXME - mglavica check
if ( my $doi = $row->{doi} ) {
$marc->add_fields('024','7',' ',
2 => 'doi',
a => $doi,
);
}
### 035$
## marc 035a - System Number
## polje moze sadrzavati slova i razmake
## moguc problem u pretrazivanju ako ima zagrade, kako bi trebalo po standardu
$marc->add_fields('035',' ',' ',
a => join('', '(CROSBI)', $row->{id})
);
### 040
## za sve je isti
$marc->add_fields('040',' ',' ',
'a' => 'HR-ZaFF',
'b' => 'hrv',
'c' => 'HR-ZaFF',
'e' => 'ppiak'
);
### 041 - indikatori
# i1=0 - Item not a translation/does not include a translation
# i1=1 - Item is or includes a translation
# i1=' ' - No information provided
### 041
# ponovljivo potpolje (041a) - marc_repeatable_subfield
# koristi se kad ima vise od jednog jezika, ili kad se radi o prijevodu
$marc->add_fields('041',' ',' ', map {
( a => lc($_) )
} split(/-/, $row->{jezik}));
### 080
### 245 indikatori
## i1 = 0 zza anonimne publikacije, i1 = 1 ako postoji 700 ili 710
## i2 = pretpostavlja se na temelju clana na pocetku naslova i jezika
my ( $first_author, $authors ) = split(/ ;\s*/,$row->{autori});
$marc->add_fields(100,'1',' ','a' => $first_author );
my $naslov = $row->{naslov}; # XXX title?
my $i2 =
$naslov =~ m/^Eine / ? 5 :
$naslov =~ m/(Die|Das|Der|Ein|Les|Los|The) / ? 4 :
$naslov =~ m/^(Um|Un|An|La|Le|Lo|Il) / ? 3 :
$naslov =~ m/^(A|L) / ? 2 :
$naslov =~ m/^L'/ ? 2 :
0;
$marc->add_fields(245,'1',$i2,
'a' => $naslov . ' /',
'c' => $row->{autori} . '.',
);
$marc->add_fields(246,'3',' ',
'i' => 'Naslov na engleskom:',
'a' => $row->{title}
);
sub page_range {
my ( $prefix, $from, $to ) = @_;
my $out;
if ( $from ) {
$out = $prefix . $from;
$out .= '-' . $to if $to;
}
return $out;
}
# fake date for Koha import
#$marc->add_fields(260,' ',' ',
# c => $row->{godina},
#);
$marc->add_fields(300,' ',' ',
a => page_range('',$row->{stranica_prva},$row->{stranica_zadnja}),
f => 'str.'
);
$marc->add_fields(363,' ',' ',
a => $row->{volumen},
b => $row->{broj},
i => $row->{godina},
) if $row->{volumen};
# /data/FF/crosbi/2016-12-12/casopis-rad_napomena.sql
foreach my $napomena ( @{ $row->{rad_napomena} } ) {
$marc->add_fields(500,' ',' ',
a => substr($napomena, 0, 9999), # XXX marc limit for one subfield is 4 digits in dictionary
);
}
$marc->add_fields(520,' ',' ',
a => substr($row->{sazetak}, 0, 9999)
);
if ( $row->{rad_projekt} ) {
$marc->add_fields(536,' ',' ',
a => 'Projekt MZOS',
f => 'projekt',
);
}
$marc->add_fields(546,' ',' ',
a => $row->{jezik}
);
foreach my $v ( @{ $row->{rad_podrucje} } ) {
$marc->add_fields(690,' ',' ',
a => $v,
);
}
$marc->add_fields(693,' ',' ',
a => $row->{kljucne_rijeci},
1 => 'hrv',
2 => 'crosbi',
);
$marc->add_fields(693,' ',' ',
a => $row->{key_words},
1 => 'eng',
2 => 'crosbi',
);
if ( $row->{autori} =~ m/ ; / ) {
my @a = split(/ ; /, $row->{autori});
shift @a; # skip first
$marc->add_fields(700,'1',' ',
a => $_,
4 => 'aut'
) foreach @a;
}
sub combine {
my $out = '';
my $last_delimiter = '';
while(@_) {
my $value = shift @_;
my $delimiter = shift @_;
my ( $before,$after ) = ( '', '' );
( $before, $value, $after ) = @$value if ( ref $value eq 'ARRAY' );
$out .= $last_delimiter . $before . $value . $after if $value;
$last_delimiter = $delimiter || last;
}
warn "### [$out]";
return $out;
}
if ( $self->{_table} =~ m/(casopis|preprint)/ ) {
$marc->add_fields(773,'0',' ',
t => $row->{casopis},
x => $row->{issn},
# g => "$row->{volumen} ($row->{godina}), $row->{broj} ;" . page_range(' str. ',$row->{stranica_prva}, $row->{stranica_zadnja}),
g => combine( $row->{volumen}, ' ', [ '(', $row->{godina}, ')' ], ', ', $row->{broj}, ' ;', page_range(' str. ',$row->{stranica_prva}, $row->{stranica_zadnja}) ),
);
} elsif ( $self->{_table} =~ m/rknjiga/ ) {
# rknjiga-dbi2marc.pl
$marc->add_fields(773,'0',' ',
t => $row->{knjiga},
# d => "$row->{grad} : $row->{nakladnik}, $row->{godina}",
d => combine( $row->{grad}, ' : ', $row->{nakladnik}, ', ', $row->{godina} ),
k => $row->{serija},
h => $row->{ukupno_stranica},
n => $row->{uredink},
z => $row->{isbn},
g => page_range('str. ',$row->{stranica_prva}, $row->{stranica_zadnja}),
);
} elsif ( $self->{_table} =~ m/zbornik/ ) {
# zbornik-dbi2marc.pl
$marc->add_fields(773,'0',' ',
t => $row->{skup},
# d => "$row->{grad} : $row->{nakladnik}, $row->{godina}",
d => combine( $row->{grad}, ' : ', $row->{nakladnik}, ', ', $row->{godina} ),
k => $row->{serija},
h => $row->{ukupno_stranica},
n => $row->{uredink},
z => $row->{isbn},
g => page_range('str. ',$row->{stranica_prva}, $row->{stranica_zadnja}),
);
} elsif ( $self->{_table} =~ m/ostalo/ ) {
$marc->add_fields(773,'0',' ',
t => $row->{izvornik},
);
} else {
die "ERROR: 773 undefined in row ",dump($row);
}
if ( my $file = $row->{datoteka} ) {
$marc->add_fields(856,' ',' ',
u => "http://bib.irb.hr/datoteka/$file",
);
};
$marc->add_fields(856,' ',' ',
u => $row->{openurl},
) if $row->{openurl};
foreach my $url ( @{ $row->{url} } ) {
$marc->add_fields(856,' ',' ',
u => $url,
);
}
my $f942c = {
casopis => 'CLA',
preprint => 'PRE',
rknjiga => 'POG',
zbornik => 'RZB',
};
my @f942 = (
c => $f942c->{ $self->{_table} } || die "ERROR no table $self->{_table} in ".dump($f942c),
) if $self->{_table} !~ m/ostalo/;
if ( $row->{status_rada} ) {
push @f942, (
f => 1,
g => $row->{status_rada}
);
}
if ( $self->{_table} =~ m/(casopis|preprint)/ ) {
if ( $row->{kategorija} =~ m/Znanstveni/ ) {
push @f942, t => '1.01'
} elsif ( $row->{kategorija} =~ m/Strucni/ ) {
push @f942, t => '1.04';
} else {
warn "ERROR kategorija $row->{kategorija}";
}
} elsif ( $self->{_table} =~ m/rknjiga/ ) {
if ( $row->{kategorija} =~ m/Znanstveni/ ) {
push @f942, t => '1.16.1';
} elsif ( $row->{kategorija} =~ m/Pregledni/ ) {
push @f942, t => '1.16.2';
} elsif ( $row->{kategorija} =~ m/Strucni/ ) {
push @f942, t => '1.17';
} else {
warn "ERROR kategorija $row->{kategorija}";
}
} elsif ( $self->{_table} =~ m/zbornik/ ) {
push @f942, v => $row->{vrst_recenzije};
} elsif ( $self->{_table} =~ m/ostalo/ ) {
# XXX
} else {
die "ERROR _table $self->{_table}";
}
$marc->add_fields(942,' ',' ',
@f942,
u => '1',
z => join(' - ', grep { defined $_ } ($row->{kategorija}, $row->{vrst_sudjelovanja}, $row->{vrsta_rada})),
);
=for later
$marc->add_fields(999,' ',' ',
a => $row->{}
);
=cut
# diag "# hash ",dump($hash);
diag "# marc\n", $marc->as_formatted if $ENV{DEBUG};
$self->save_marc( "$id.marc", $marc->as_usmarc );
return $id;
}
1;