better error messages
[webpac2] / lib / WebPAC / Input / MARC.pm
1 package WebPAC::Input::MARC;
2
3 use warnings;
4 use strict;
5
6 use MARC::Fast;
7 use base qw/WebPAC::Common/;
8 use Carp qw/confess/;
9
10 =head1 NAME
11
12 WebPAC::Input::MARC - support for MARC database files
13
14 =head1 VERSION
15
16 Version 0.09
17
18 =cut
19
20 our $VERSION = '0.09';
21
22
23 =head1 SYNOPSIS
24
25 Open USMARC, Unimarc or any other file format that has same internal
26 structure using C<MARC::Fast>.
27
28  my $marc = new WebPAC::Input::MARC(
29         path => '/path/to/marc.iso'
30  );
31
32 =head1 FUNCTIONS
33
34 =head2 new
35
36 Returns new low-level input API object
37
38   my $marc = new WebPAC::Input::MARC(
39         path => '/path/to/marc.iso',
40   }
41
42 =cut
43
44 sub new {
45         my $class = shift;
46         my $self = {@_};
47         bless($self, $class);
48
49         my $arg = {@_};
50
51         my $log = $self->_get_logger();
52
53         $log->info("opening MARC database '$arg->{path}'");
54
55         die "no filter support any more!" if $arg->{filter};
56
57         my $db = new MARC::Fast(
58                 marcdb => $arg->{path},
59                 hash_filter => sub {
60                         my ( $l, $nr ) = @_;
61                         Encode::decode( 'utf-8', $l );
62                 },
63         );
64         my $db_size = $db->count - 1;   # FIXME
65
66         $self->{_marc_size} = $db_size;
67         $self->{_marc_db} = $db;
68
69         $self ? return $self : return undef;
70 }
71
72 =head2 fetch_rec
73
74 Return record with ID C<$mfn> from database
75
76   my $rec = $self->fetch_rec( $mfn );
77
78 =cut
79
80 sub fetch_rec {
81         my $self = shift;
82
83         my $mfn = shift;
84
85         if ($mfn > $self->{_marc_size}) {
86                 $self->_get_logger()->warn("seek beyond database size $self->{_marc_size} to $mfn");
87         } else {
88                 my $marc = $self->{_marc_db} || confess "no _marc_db?";
89                 my $row = $marc->to_hash($mfn, include_subfields => 1);
90                 push @{$row->{'000'}}, $mfn;
91                 push @{$row->{'leader'}}, $marc->last_leader;
92                 return $row;
93         }
94 }
95
96 =head2 dump_ascii
97
98 Return ASCII dump of record with ID C<$mfn> from database
99
100   print $self->dump_ascii( $mfn );
101
102 =cut
103
104 sub dump_ascii {
105         my $self = shift;
106
107         my $mfn = shift;
108         return $self->{_marc_db}->to_ascii($mfn);
109 }
110
111 =head2 size
112
113 Return number of records in database
114
115   my $size = $isis->size;
116
117 =cut
118
119 sub size {
120         my $self = shift;
121         return $self->{_marc_size};
122 }
123
124
125 =head1 AUTHOR
126
127 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
128
129 =head1 COPYRIGHT & LICENSE
130
131 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
132
133 This program is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself.
135
136 =cut
137
138 1; # End of WebPAC::Input::MARC