r1760@llin: dpavlin | 2009-04-20 15:44:39 +0200
[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                         push @{ $self->{_rec} }, $rec if $rec;
86                         warn "## rec = ",dump( $rec ),$/;
87                         my $expect_rec = $#{ $self->{_rec} } + 2;
88                         warn "wrong Ovid record number: $1 != $expect_rec" unless $1 == $expect_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
106         $log->debug("loaded ", $self->size, " records");
107
108         $self ? return $self : return undef;
109 }
110
111 =head2 fetch_rec
112
113 Return record with ID C<$mfn> from database
114
115   my $rec = $input->fetch_rec( $mfn, $filter_coderef );
116
117 =cut
118
119 sub fetch_rec {
120         my $self = shift;
121
122         my ( $mfn, $filter_coderef ) = @_;
123
124         return $self->{_rec}->[$mfn-1];
125 }
126
127
128 =head2 size
129
130 Return number of records in database
131
132   my $size = $input->size;
133
134 =cut
135
136 sub size {
137         my $self = shift;
138         return $#{ $self->{_rec} } + 1;
139 }
140
141 =head1 AUTHOR
142
143 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
144
145 =head1 COPYRIGHT & LICENSE
146
147 Copyright 2008 Dobrica Pavlinusic, All Rights Reserved.
148
149 This program is free software; you can redistribute it and/or modify it
150 under the same terms as Perl itself.
151
152 =cut
153
154 1; # End of WebPAC::Input::Ovid