1 package Equinox::Migration::MapDrivenMARCXMLProc;
8 use Equinox::Migration::SubfieldMapper 1.004;
13 Equinox::Migration::MapDrivenMARCXMLProc
21 our $VERSION = '1.002';
25 my @mods = qw( multi bib required );
32 use Equinox::Migration::MapDrivenMARCXMLProc;
40 Takes two required arguments: C<mapfile> (which will be passed along
41 to L<Equinox::Migration::SubfieldMapper> as the basis for its map),
42 and C<marcfile> (the MARC data to be processed).
44 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( mapfile => FILE,
50 my ($class, %args) = @_;
55 # initialize map and taglist
56 die "Argument 'mapfile' must be specified\n" unless ($args{mapfile});
57 $sfmap = Equinox::Migration::SubfieldMapper->new( file => $args{mapfile},
60 # initialize datastore
61 $dstore = DBM::Deep->new( file => "EMMXSSTORAGE.dbmd",
62 data_sector_size => 256 );
63 $dstore->{rcnt} = 0; # next record ptr
64 $dstore->{tags} = $sfmap->tags; # list of all tags
65 $self->{data} = $dstore;
68 die "Argument 'marcfile' must be specified\n" unless ($args{marcfile});
69 if (-r $args{marcfile}) {
70 my $xmltwig = XML::Twig->new( twig_handlers => { record => \&parse_record } );
71 $xmltwig->parsefile( $args{marcfile} );
73 die "Can't open marc file: $!\n";
79 sub DESTROY { unlink "EMMXSSTORAGE.dbmd" }
83 Extracts data from the next record, per the mapping file.
88 my ($twig, $record) = @_;
89 my $crec = {}; # current record
91 my @fields = $record->children;
93 { process_field($f, $crec) }
95 # cleanup memory and increment pointer
99 # check for required fields
101 push @{ $dstore->{recs} }, $crec;
105 my ($field, $crec) = @_;
106 my $tag = $field->{'att'}->{'tag'};
109 unless (defined $tag) {
116 my $sub = $field->first_child('subfield');
117 $crec->{egid} = $sub->text;
120 if ($sfmap->has($tag)) {
121 push @{$crec->{tags}}, { tag => $tag, uni => undef, multi => undef };
122 push @{$crec->{tmap}{$tag}}, (@{$crec->{tags}} - 1);
123 my @subs = $field->children('subfield');
125 { process_subs($tag, $sub, $crec) }
127 # check map to ensure all declared tags and subs have a value
128 my $mods = $sfmap->mods($field);
129 for my $mappedsub ( @{ $sfmap->subfields($tag) } ) {
130 next if $mods->{multi};
131 $crec->{tags}[-1]{uni}{$mappedsub} = ''
132 unless defined $crec->{tags}[-1]{uni}{$mappedsub};
134 for my $mappedtag ( @{ $sfmap->tags }) {
135 $crec->{tmap}{$mappedtag} = undef
136 unless defined $crec->{tmap}{$mappedtag};
142 my ($tag, $sub, $crec) = @_;
143 my $code = $sub->{'att'}->{'code'};
145 # handle unmapped tag/subs
146 return unless ($sfmap->has($tag, $code));
148 # fetch our datafield struct and fieldname
149 my $dataf = $crec->{tags}[-1];
150 my $field = $sfmap->field($tag, $code);
151 $crec->{names}{$tag}{$code} = $field;
154 for my $filter ( @{$sfmap->filters($field)} ) {
155 return if ($sub->text =~ /$filter/i);
157 # handle multi modifier
158 if (my $mods = $sfmap->mods($field)) {
159 if ($mods->{multi}) {
160 push @{$dataf->{multi}{$code}}, $sub->text;
165 # if this were a multi field, it would be handled already. make sure its a singleton
166 die "Multiple occurances of a non-multi field: $tag$code at rec ",
167 ($dstore->{rcnt} + 1),"\n" if (defined $dataf->{uni}{$code});
169 # everything seems okay
170 $dataf->{uni}{$code} = $sub->text;
175 my $mods = $sfmap->mods;
176 my $crec = $dstore->{crec};
178 for my $tag_id (keys %{$mods->{required}}) {
179 for my $code (@{$mods->{required}{$tag_id}}) {
182 for my $tag (@{$crec->{tags}}) {
183 $found = 1 if ($tag->{multi}{($tag_id . $code)});
184 $found = 1 if ($tag->{uni}{$code});
187 die "Required mapping $tag_id$code not found in rec ",$dstore->{rcnt},"\n"
196 Returns current record number (starting from zero)
200 sub recno { my ($self) = @_; return $self->{data}{rcnt} }
204 Returns mapped fieldname when passed a record number, tag, and code
206 my $name = $m->name(3,999,'a');
210 sub name { my ($self, $r, $t, $c) = @_; return $dstore->{recs}[$r]{names}{$t}{$c} };
214 MapDrivenMARCXMLProc implements the following modifiers, and passes
215 them to L<Equinox::Migration::SubfieldMapper>, meaning that specifying
216 any other modifiers in a MDMP map file will cause a fatal error when
221 If a mapping is declared to be C<multi>, then MDMP expects to see more
222 than one instance of that subfield per datafield, and the data is
223 handled accordingly (see L</PARSED RECORDS> below).
225 Occurring zero or one time is legal for a C<multi> mapping.
227 A mapping which is not flagged as C<multi>, but which occurs more than
228 once per datafield will cause a fatal error.
232 By default, if a mapping does not occur in a datafield, processing
233 continues normally. if a mapping has the C<required> modifier,
234 however, it must appear, or a fatal error will occur.
236 =head1 PARSED RECORDS
240 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new(ARGUMENTS);
241 $rec = $m->parse_record;
243 Then C<$rec> will look like:
246 egid => evergreen_record_id,
250 multi => { code => [ val1, val2, ... ] },
251 uni => { code => value, code2 => value2, ... },
255 tmap => { tag_id => [ INDEX_LIST ], tag_id2 => [ INDEX_LIST ], ... }
258 That is, there is an C<egid> key which points to the Evergreen ID of
259 that record, a C<tags> key which points to an arrayref, and a C<tmap>
260 key which points to a hashref.
264 A reference to a list of anonymous hashes, one for each instance of
265 each tag which occurs in the map.
267 Each tag hash holds its own id (e.g. C<998>), and two references to
268 two more hashrefs, C<multi> and C<uni>.
270 The C<multi> hash holds the extracted data for tag/sub mappings which
271 have the C<multiple> modifier on them. The keys in C<multi> subfield
272 codes. The values are arrayrefs containing the content of all
273 instances of that subfield in that instance of that tag. If no tags
274 are defined as C<multi>, it will be C<undef>.
276 The C<uni> hash holds data for tag/sub mappings which occur only once
277 per instance of a tag (but may occur multiple times in a record due to
278 there being multiple instances of that tag in a record). Keys are
279 subfield codes and values are subfield content.
281 All C<uni> subfields occuring in the map are guaranteed to be
282 defined. Sufields which are mapped but do not occur in a particular
283 datafield will be given a value of '' (the null string) in the current
284 record struct. Oppose subfields which are not mapped, which will be
289 A hashref, where each key (a tag id like "650") points to a listref
290 containing the index (or indices) of C<tags> where that tag has
293 The intended use of this is to simplify the processing of data from
294 tags which can appear more than once in a MARC record, like
295 holdings. If your holdings data is in 852, C<tmap->{852}> will be a
296 listref with the indices of C<tags> which hold the data from the 852
299 Complimentarily, C<tmap> prevents data from singular datafields from
300 having to be copied for every instance of a multiple datafield, as it
301 lets you get the data from that record's one instance of whichever
302 field you're looking for.
306 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
310 Please report any bugs or feature requests to the above email address.
314 You can find documentation for this module with the perldoc command.
316 perldoc Equinox::Migration::MapDrivenMARCXMLProc
319 =head1 COPYRIGHT & LICENSE
321 Copyright 2009 Equinox, all rights reserved.
323 This program is free software; you can redistribute it and/or modify it
324 under the same terms as Perl itself.
329 1; # End of Equinox::Migration::MapDrivenMARCXMLProc