1ac19677f9a1309dbde06391d5e0af6889141da1
[webpac2] / lib / WebPAC / Input / Ovid.pm
1 package WebPAC::Input::Ovid;
2
3 use warnings;
4 use strict;
5
6 use lib 'lib';
7 use WebPAC::Input;
8 use base qw/WebPAC::Common/;
9
10 use Data::Dump qw/dump/;
11
12 =head1 NAME
13
14 WebPAC::Input::Ovid - support for Ovid citation export
15
16 =head1 VERSION
17
18 Version 0.01
19
20 =cut
21
22 our $VERSION = '0.01';
23
24 =head1 SYNOPSIS
25
26 Open file in Ovid citation export fromat
27
28  my $input = new WebPAC::Input::Ovid(
29         path => '/path/to/ovid-cites.txt',
30  );
31
32 =head1 FUNCTIONS
33
34 =head2 new
35
36 Returns new low-level input API object
37
38   my $input = new WebPAC::Input::Ovid(
39         path => '/path/to/ebsco.txt',
40         filter => sub {
41                 my ($l,$field_nr) = @_;
42                 # do something with $l which is line of input file
43                 return $l;
44         },
45   }
46
47 Options:
48
49 =over 4
50
51 =item path
52
53 path to Ovid export file
54
55 =back
56
57 =cut
58
59 sub new {
60         my $class = shift;
61         my $self = {@_};
62         bless($self, $class);
63
64         my $arg = {@_};
65
66         my $log = $self->_get_logger();
67
68         open( my $fh, '<', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
69
70         $log->info("reading '$arg->{path}'");
71
72         my $rec;
73         my $size = 0;
74         my $tag;
75
76         $self->{_rec} = [];
77
78         while( my $line = <$fh> ) {
79                 $line =~ s{[\r\n]+$}{};
80                 next if $line eq '';
81
82                 warn "<< $line\n";
83
84                 if ( $line =~ m/^<(\d+)>$/ ) {
85                         my $expect_rec = $#{ $self->{_rec} } + 2;
86                         warn "wrong Ovid record number: $1 != $expect_rec" unless $1 == $expect_rec;
87                         push @{ $self->{_rec} }, $rec if $rec;
88                         warn "## rec = ",dump( $rec ),$/;
89                         $rec = { '000' => $1 };
90                 } elsif ( $line =~ /^(\w.+)/ ) {
91                         $tag = $1;
92                         warn "++ $tag\n";
93                 } elsif ( $line =~ /^\s\s(.+)/ ) {
94                         my $v = $1;
95                         $v =~ s{[\s\.]+$}{};
96                         $rec->{$tag} = $v;
97                 } else {
98                         warn "### skip: '$line'\n";
99                 }
100
101         }
102
103         # save last rec
104         push @{ $self->{_rec} }, $rec if $rec;
105         $self->{size} = $#{ $self->{_rec} } + 1;
106
107         $log->debug("loaded ", $self->size, " records");
108
109         $self ? return $self : return undef;
110 }
111
112 =head2 fetch_rec
113
114 Return record with ID C<$mfn> from database
115
116   my $rec = $input->fetch_rec( $mfn, $filter_coderef );
117
118 =cut
119
120 sub fetch_rec {
121         my $self = shift;
122
123         my ( $mfn, $filter_coderef ) = @_;
124
125         return $self->{_rec}->[$mfn-1];
126 }
127
128
129 =head2 size
130
131 Return number of records in database
132
133   my $size = $input->size;
134
135 =cut
136
137 sub size {
138         my $self = shift;
139         return $self->{size};
140 }
141
142 =head1 AUTHOR
143
144 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
145
146 =head1 COPYRIGHT & LICENSE
147
148 Copyright 2008 Dobrica Pavlinusic, All Rights Reserved.
149
150 This program is free software; you can redistribute it and/or modify it
151 under the same terms as Perl itself.
152
153 =cut
154
155 1; # End of WebPAC::Input::Ovid