1 package OpenILS::Utils::ModsParser;
2 use strict; use warnings;
4 use OpenSRF::EX qw/:try/;
7 use Time::HiRes qw(time);
8 use OpenILS::Utils::Fieldmapper;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenSRF::Utils::Logger qw/$logger/;
13 my $parser = XML::LibXML->new();
14 my $xslt = XML::LibXSLT->new();
17 # ----------------------------------------------------------------------------------------
18 # XPATH for extracting info from a MODS doc
19 my $isbn_xpath = "//mods:mods/mods:identifier[\@type='isbn']";
20 my $resource_xpath = "//mods:mods/mods:typeOfResource";
21 my $pub_xpath = "//mods:mods/mods:originInfo//mods:dateIssued[\@encoding='marc']|" .
22 "//mods:mods/mods:originInfo//mods:dateIssued[1]";
23 my $tcn_xpath = "//mods:mods/mods:recordInfo/mods:recordIdentifier";
24 my $publisher_xpath = "//mods:mods/mods:originInfo//mods:publisher[1]";
25 my $edition_xpath = "//mods:mods/mods:originInfo//mods:edition[1]";
26 my $abstract_xpath = "//mods:mods/mods:abstract";
27 my $related_xpath = "";
28 my $online_loc_xpath = "(//mods:location/mods:url|//mods:location/mods:url/\@displayLabel)";
29 my $physical_desc = "(//mods:physicalDescription/mods:form|//mods:physicalDescription/mods:extent|".
30 "//mods:physicalDescription/mods:reformattingQuality|//mods:physicalDescription/mods:internetMediaType|".
31 "//mods:physicalDescription/mods:digitalOrigin)";
32 my $toc_xpath = "//mods:tableOfContents";
38 "//mods:mods/mods:titleInfo[mods:title and (\@type='abreviated')]",
40 "//mods:mods/mods:titleInfo[mods:title and (\@type='translated')]",
42 "//mods:mods/mods:titleInfo[mods:title and (\@type='uniform')]",
44 "//mods:mods/mods:titleInfo[mods:title and not (\@type)]",
46 "//mods:mods/mods:titleInfo",
51 "//mods:mods/mods:name[\@type='corporate']/*[local-name()='namePart']".
52 "[../mods:role/mods:text[text()='creator']][1]",
54 "//mods:mods/mods:name[\@type='personal']/*[local-name()='namePart']".
55 "[../mods:role/mods:text[text()='creator']][1]",
57 "//mods:mods/mods:name[\@type='conference']/*[local-name()='namePart']".
58 "[../mods:role/mods:text[text()='creator']][1]",
60 "//mods:mods/mods:name[\@type='personal']/*[local-name()='namePart']",
62 "//mods:mods/mods:name/*[local-name()='namePart'][1]",
68 "//mods:mods/mods:subject/*[local-name()='geographic' or local-name()='name' or local-name()='temporal' or local-name()='topic']/parent::mods:subject",
71 # "//mods:mods/*[local-name()='subject']/*[local-name()='geographic']",
73 # "//mods:mods/*[local-name()='subject']/*[local-name()='name']",
75 # "//mods:mods/*[local-name()='subject']/*[local-name()='temporal']",
77 # "//mods:mods/*[local-name()='subject']/*[local-name()='topic']",
79 #keyword => { keyword => "//mods:mods/*[not(local-name()='originInfo')]", },
82 series => "//mods:mods/mods:relatedItem[\@type='series']/mods:titleInfo"
85 # ----------------------------------------------------------------------------------------
89 sub new { return bless( {}, shift() ); }
93 my( $self, $mods, $xpath ) = @_;
97 my $root = $mods->documentElement;
98 $root->setNamespace( "http://www.loc.gov/mods/v3", "mods", 1 );
101 # grab the set of matching nodes
102 my @nodes = $root->findnodes( $xpath );
103 for my $value (@nodes) {
105 # grab all children of the node
106 my @children = $value->childNodes();
108 for my $child (@children) {
109 next unless( $child->nodeType != 3 );
111 if($child->childNodes) {
113 for my $c (@{$child->childNodes}){
114 push @a, $c->textContent;
116 push(@child_text, join(' ', @a));
119 push(@child_text, $child->textContent);
124 push(@string, \@child_text);
128 push(@string, $value->textContent );
132 $logger->info("MODS-izing failure: ".shift());
133 $logger->info("Failed MODS xml: ".$root->toString);
134 $logger->info("Failed MODS xpath: $xpath");
140 sub _modsdoc_to_values {
141 my( $self, $mods ) = @_;
143 for my $class (keys %$xpathset) {
144 $data->{$class} = {};
145 for my $type(keys %{$xpathset->{$class}}) {
146 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
147 if( $class eq "subject" ) {
148 push( @{$data->{$class}->{$type}}, @value );
150 $data->{$class}->{$type} = $value[0];
158 sub modsdoc_to_values {
159 my( $self, $mods ) = @_;
163 my $class = "subject";
164 $data->{$class} = {};
165 for my $type(keys %{$xpathset->{$class}}) {
166 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
167 for my $arr (@value) {
168 push( @{$data->{$class}->{$type}}, $arr);
175 $data->{$class} = {};
176 for my $type(keys %{$xpathset->{$class}}) {
177 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
178 for my $arr (@value) {
180 $data->{$class}->{$type} = shift @$arr;
182 my $t = lc($data->{$class}->{$type});
183 if($t and $t =~ /^l[eoa]s|l[ae]|el|the|un[ae]?|an?\s?$/o ) {
184 my $val = shift @$arr || "";
185 $data->{$class}->{$type} .= " $val" if $data->{$class}->{$type};
186 $data->{$class}->{$type} = " $val" unless $data->{$class}->{$type};
190 $data->{$class}->{$type} .= ' : ' if ($data->{$class}->{$type} =~ /\w\s*$/o);
191 $data->{$class}->{$type} .= " $t";
194 $data->{$class}->{$type} = $arr;
201 my $class = "author";
202 $data->{$class} = {};
203 for my $type(keys %{$xpathset->{$class}}) {
204 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
205 $data->{$class}->{$type} = $value[0];
210 my $class = "series";
211 $data->{$class} = {};
212 for my $type(keys %{$xpathset->{$class}}) {
213 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
214 for my $arr (@value) {
216 push(@{$data->{$class}->{$type}}, join(" ", @$arr));
218 push( @{$data->{$class}->{$type}}, $arr );
231 # ---------------------------------------------------------------------------
232 # Grabs the data 'we want' from the MODS doc and returns it in hash form
233 # ---------------------------------------------------------------------------
234 sub mods_values_to_mods_slim {
235 my( $self, $modsperl ) = @_;
242 my $tmp = $modsperl->{title};
245 if(!$tmp) { $title = ""; }
247 ($title = $tmp->{proper}) ||
248 ($title = $tmp->{translated}) ||
249 ($title = $tmp->{abbreviated}) ||
250 ($title = $tmp->{uniform}) ||
251 ($title = $tmp->{any});
254 $tmp = $modsperl->{author};
255 if(!$tmp) { $author = ""; }
257 ($author = $tmp->{personal}) ||
258 ($author = $tmp->{other}) ||
259 ($author = $tmp->{corporate}) ||
260 ($author = $tmp->{conference}) ||
261 ($author = $tmp->{any});
264 $tmp = $modsperl->{subject};
265 if(!$tmp) { $subject = {}; }
267 for my $key( keys %{$tmp}) {
268 push(@$subject, @{$tmp->{$key}}) if ($tmp->{$key});
271 for my $s (@$subject) {
272 if(defined($subh->{$s})) { $subh->{$s->[0]}++ } else { $subh->{$s->[0]} = 1;}
277 $tmp = $modsperl->{'series'};
278 if(!$tmp) { $series = []; }
279 else { $series = $tmp->{'series'}; }
282 return { series => $series, title => $title,
283 author => $author, subject => $subject };
288 # ---------------------------------------------------------------------------
289 # Initializes a MARC -> Unified MODS batch process
290 # ---------------------------------------------------------------------------
292 sub start_mods_batch {
294 my( $self, $master_doc ) = @_;
297 $self->{master_doc} = undef;
302 my $xslt_doc = $parser->parse_file(
303 OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl') . "/MARC21slim2MODS3.xsl");
304 $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
308 my $xmldoc = $parser->parse_string($master_doc);
309 my $mods = $mods_sheet->transform($xmldoc);
311 $self->{master_doc} = $self->modsdoc_to_values( $mods );
312 $self->{master_doc} = $self->mods_values_to_mods_slim( $self->{master_doc} );
314 ($self->{master_doc}->{isbn}) =
315 $self->get_field_value( $mods, $isbn_xpath );
317 $self->{master_doc}->{type_of_resource} =
318 [ $self->get_field_value( $mods, $resource_xpath ) ];
320 ($self->{master_doc}->{tcn}) =
321 $self->get_field_value( $mods, $tcn_xpath );
323 ($self->{master_doc}->{pubdate}) =
324 $self->get_field_value( $mods, $pub_xpath );
326 ($self->{master_doc}->{publisher}) =
327 $self->get_field_value( $mods, $publisher_xpath );
329 ($self->{master_doc}->{edition}) =
330 $self->get_field_value( $mods, $edition_xpath );
334 # ------------------------------
335 # holds an array of [ link, title, link, title, ... ]
336 $self->{master_doc}->{online_loc} = [];
337 push(@{$self->{master_doc}->{online_loc}},
338 $self->get_field_value( $mods, $online_loc_xpath ));
340 ($self->{master_doc}->{synopsis}) =
341 $self->get_field_value( $mods, $abstract_xpath );
343 $self->{master_doc}->{physical_description} = [];
344 push(@{$self->{master_doc}->{physical_description}},
345 $self->get_field_value( $mods, $physical_desc ) );
346 $self->{master_doc}->{physical_description} =
347 join( ' ', @{$self->{master_doc}->{physical_description}});
349 ($self->{master_doc}->{toc}) = $self->get_field_value($mods, $toc_xpath);
355 # ---------------------------------------------------------------------------
356 # Takes a MARCXML string and adds it to the growing MODS doc
357 # ---------------------------------------------------------------------------
358 sub push_mods_batch {
359 my( $self, $marcxml ) = @_;
361 my $xmldoc = $parser->parse_string($marcxml);
362 my $mods = $mods_sheet->transform($xmldoc);
364 my $xmlperl = $self->modsdoc_to_values( $mods );
365 $xmlperl = $self->mods_values_to_mods_slim( $xmlperl );
367 # for backwards compatibility, remove the array part when all is decided
368 if(ref($xmlperl->{subject}) eq 'ARRAY' ) {
369 for my $subject( @{$xmlperl->{subject}} ) {
370 push @{$self->{master_doc}->{subject}}, $subject;
373 for my $subject ( keys %{$xmlperl->{subject}} ) {
374 my $s = $self->{master_doc}->{subject};
375 if(defined($s->{$subject})) { $s->{$subject}++; } else { $s->{$subject} = 1; }
379 push( @{$self->{master_doc}->{type_of_resource}},
380 $self->get_field_value( $mods, $resource_xpath ));
382 if(!($self->{master_doc}->{isbn}) ) {
383 ($self->{master_doc}->{isbn}) =
384 $self->get_field_value( $mods, $isbn_xpath );
389 # ---------------------------------------------------------------------------
390 # Completes a MARC -> Unified MODS batch process and returns the perl hash
391 # ---------------------------------------------------------------------------
392 sub init_virtual_record {
393 my $record = Fieldmapper::metabib::virtual_record->new;
394 $record->subject([]);
395 $record->types_of_resource([]);
396 $record->call_numbers([]);
400 sub finish_mods_batch {
403 return undef unless $self->{master_doc};
405 my $perl = $self->{master_doc};
406 my $record = init_virtual_record();
408 # turn the hash into a fieldmapper object
409 (my $title = $perl->{title}) =~ s/\[.*?\]//og;
410 (my $author = $perl->{author}) =~ s/\(.*?\)//og;
413 for my $s (@{$perl->{series}}) {
414 push @series, (split( /\s*;/, $s ))[0];
417 # uniquify the types of resource
418 my $rtypes = $perl->{type_of_resource};
419 my %hash = map { ($_ => 1) } @$rtypes;
420 $rtypes = [ keys %hash ];
422 $record->title($title);
423 $record->author($author);
425 $record->doc_id($perl->{doc_id});
426 $record->isbn($perl->{isbn});
427 $record->pubdate($perl->{pubdate});
428 $record->publisher($perl->{publisher});
429 $record->tcn($perl->{tcn});
431 $record->edition($perl->{edition});
433 $record->subject($perl->{subject});
434 $record->types_of_resource($rtypes);
435 $record->series(\@series);
437 $record->online_loc($perl->{online_loc});
438 $record->synopsis($perl->{synopsis});
439 $record->physical_description($perl->{physical_description});
440 $record->toc($perl->{toc});
442 $self->{master_doc} = undef;