ae33db0ddf02bf60318cb8aad66d978e41e3d931
[migration-tools.git] / Equinox-Migration / lib / Equinox / Migration / SubfieldMapper.pm
1 package Equinox::Migration::SubfieldMapper;
2
3 use warnings;
4 use strict;
5
6 =head1 NAME
7
8 Equinox::Migration::SubfieldMapper - Generate named-field to MARC tag map from file
9
10 =head1 VERSION
11
12 Version 1.000
13
14 =cut
15
16 our $VERSION = '1.000';
17
18
19 =head1 SYNOPSIS
20
21 Using a file as input, E::M::SM generates a mapping of MARC subfields
22 to arbitrary field names, and provides several access mechanisms to
23 that set.
24
25     use Equinox::Migration::SubfieldMapper;
26     
27     my $stl = Equinox::Migration::SubfieldMapper->new( file => ".txt" );
28     my $tags = $stl->as_hashref;
29
30 or
31
32     my $stl = Equinox::Migration::SubfieldMapper->new( file => ".txt" );
33     if ( $stl->has($foo) ) {
34         # if $foo is an element of $stl's parsed list
35         # do stuff ...
36     }
37
38
39 =head1 ROUTINES
40
41
42 =head2 new
43
44 Takes one optional argument, C<file>. If this is speficied, the tag
45 list will be populated as per that file on instantiation.
46
47 Returns a E::M::STL object.
48
49 =cut
50
51 sub new {
52     my ($class, %args) = @_;
53
54     my $self = bless { conf   => { mods => undef },
55                        fields => {},
56                        tags   => {} }, $class;
57
58     if ($args{file}) {
59         if (-r $args{file}) {
60             $self->{conf}{file} = $args{file};
61             $self->generate;
62         } else {
63             die "Can't open file: $!\n";
64         }
65     }
66
67     if ($args{mods}) {
68         die "Argument 'mods' is wrong type\n"
69           unless (ref $args{mods} eq "ARRAY");
70         for my $mod ( @{$args{mods}} )
71           { $self->{conf}{mods}{$mod} = 1 }
72     }
73
74     return $self;
75 }
76
77 =head2 has
78
79 Ask it whether your mapping has various things, and it'll let you know.
80
81     $sm->has('fieldname')      # is this fieldname mapped?
82     $sm->has(901)              # are there any mappings for this tag?
83     $sm->has(650,'c')          # is this tag/subfield combo mapped?
84     $sm->has('name', 245, 'a') # is this name mapped to 245$a?
85
86 Returns 1 if true, 0 if false.
87
88 FIXME: use named params instead of positional
89
90 =cut
91
92 sub has {
93     my ($self, @chunks) = @_;
94     return undef unless (defined $chunks[0]);
95
96     if ($chunks[0] =~ /^\d/) {
97         if (defined $chunks[1]) {
98             return 1 if ( defined $self->{tags}{$chunks[0]}{$chunks[1]} );
99             return 0;
100         } else {
101             return 1 if ( defined $self->{tags}{$chunks[0]} );
102             return 0;
103         }
104     } else {
105         if (defined $chunks[2]) {
106             return 1 if ( $self->{fields}{$chunks[0]}{tag} eq $chunks[1] and
107                           $self->{fields}{$chunks[0]}{sub} eq $chunks[2] );
108             return 0;
109         } elsif (defined $chunks[1]) {
110             return 1 if ( $self->{fields}{$chunks[0]}{tag} eq $chunks[1] );
111             return 0;
112         } else {
113             return 1 if ( defined $self->{fields}{$chunks[0]} );
114             return 0;
115         }
116     }
117 }
118
119 =head2 field
120
121 Given a tag and subfield code,
122
123     my $fname = $sm->field(945, 'p')
124
125 return the name mapped to them. Returns C<undef> if no mapping exists.
126
127 =cut
128
129 sub field {
130     my ($self, $tag, $sub) = @_;
131     return undef unless (defined $tag and defined $sub);
132     return undef unless $self->has($tag, $sub);
133     return $self->{tags}{$tag}{$sub};
134 }
135
136 =head2 mods
137
138 Returns the modifiers set on a mapping.
139
140     $self->mods('fieldname')
141
142 If there are no modifiers, C<undef> will be returned. Else a listref
143 will be returned.
144
145 =cut
146
147 sub mods {
148     my ($self, $field) = @_;
149     return undef unless $self->has($field);
150     return $self->{fields}{$field}{mods};
151 }
152
153 =head2 filters
154
155 Returns the content filters set on a mapping
156
157     $self->filters('fieldname')
158
159 If there are no filters, C<undef> will be returned. Else a listref
160 will be returned.
161
162 =cut
163
164 sub filters {
165     my ($self, $field) = @_;
166     return undef unless $self->has($field);
167     return $self->{fields}{$field}{filt};
168 }
169
170
171
172 =head2 generate
173
174 Generate initial mapping from file.
175
176 =cut
177
178 sub generate {
179     my ($self, $file) = @_;
180
181     open TAGFILE, '<', $self->{conf}{file};
182     while (<TAGFILE>) {
183         next if m/^#/;
184         next if m/^\s*\n$/;
185
186         chomp;
187         my @tokens = split /\s+/;
188
189         my $map = { mods => [], filt => [] };
190         $map->{field} = shift @tokens;
191         $map->{tag}   = shift @tokens;
192         for my $tok (@tokens) {
193             last if ($tok =~ m/^#/);
194             if ($tok =~ m/^m:/)
195               { push @{$map->{mods}}, $tok }
196             elsif ($tok =~ m/^f:/)
197               { push @{$map->{filt}}, $tok }
198             elsif ($tok =~ m/^[a-z0-9]$/)
199               { $map->{sub} = $tok }
200             else
201               { die "Unknown chunk '$tok' at line $.\n" }
202         }
203         $self->add($map);
204     }
205 }
206
207 =head2 add
208
209 Add new item to mapping. Not usually called directly from user code.
210
211     $sm->add( $map );
212
213 Where C<$map> is a hashref that, at a minimum, looks like
214
215     { field => "value", tag => NNN, sub => X }
216
217 and may also have the key/value pairs
218
219     mods => [ ITEMS ]
220     filt => [ ITEMS ]
221
222 =cut
223
224 sub add {
225     my ($self, $map) = @_;
226
227     # trim the mods and filters
228     my $mods = []; my $filt = [];
229     for my $m (@{$map->{mods}})
230       { $m =~ s/^m://; push @{$mods}, $m }
231     for my $f (@{$map->{filt}})
232       { $f =~ s/^f://; push @{$filt}, $f }
233     $map->{mods} = $mods;
234     $map->{filt} = $filt;
235
236     # check bits for validity
237     $self->validate($map);
238
239     # add data to the fields hash
240     $self->{fields}{ $map->{field} } = { tag => $map->{tag},
241                                          sub => $map->{sub},
242                                          mods => $map->{mods},
243                                          filt => $map->{filt}
244                                        };
245     # and to the tags hash
246     $self->{tags}{ $map->{tag} }{ $map->{sub} } = $map->{field};
247 }
248
249 =head2 validate
250
251 Passed a reference to the hash given to C<add>, validate scans its
252 contents for common errors and dies if there is an issue.
253
254     * field, tag, and sub are required
255     * fieldnames must start with a letter
256     * fieldnames must be unique
257     * tag must be between 0 and 999
258     * subfield code must be a single alphanumeric character
259     * tag+subfield can only be mapped once
260     * if a list of allowable mod values was given in the call to
261       C<new>, any modifiers must be on that list
262
263 =cut
264
265 sub validate {
266     my ($self, $map) = @_;
267
268     $.= 1 unless defined $.;
269
270     die "Required field missing (line $.)\n"
271       unless (defined $map->{field} and defined $map->{tag} and defined $map->{sub});
272
273     die "Fieldnames must start with letter (line $.)\n"
274      unless ($map->{field} =~ /^[a-zA-z]/);
275
276     die "Invalid tag (line $.)\n"
277       if ($map->{tag} =~ /[^\d\-]/ or $map->{tag} < 0 or $map->{tag} > 999);
278
279     die "Invalid subfield code (line $.)\n"
280       if (length $map->{sub} != 1 or $map->{sub} =~ /[^a-zA-Z0-9]/);
281
282     # test mod names if we have a set to check against
283     if (defined $self->{conf}{mods}) {
284         for my $mod ( @{$map->{mods}} ) {
285             die "Modifier '$mod' not allowed\n"
286               unless $self->{conf}{mods}{$mod};
287         }
288     }
289
290     die "Fieldnames must be unique (line $.)\n"
291       if (defined $self->{fields}{$map->{field}});
292
293     die "Subfields cannot be mapped twice (line $.)\n"
294       if (defined $self->{tags}{$map->{tag}}{$map->{sub}});
295
296 }
297
298
299 =head1 AUTHOR
300
301 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
302
303 =head1 BUGS
304
305 Please report any bugs or feature requests to the above email address.
306
307 =head1 SUPPORT
308
309 You can find documentation for this module with the perldoc command.
310
311     perldoc Equinox::Migration::SubfieldMapper
312
313
314 =head1 COPYRIGHT & LICENSE
315
316 Copyright 2009 Equinox, all rights reserved.
317
318 This program is free software; you can redistribute it and/or modify it
319 under the same terms as Perl itself.
320
321
322 =cut
323
324 1; # End of Equinox::Migration::SimpleTagList