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";
29 my $physical_desc = "(//mods:mods/mods:physicalDescription/mods:form|//mods:mods/mods:physicalDescription/mods:extent|".
30 "//mods:mods/mods:physicalDescription/mods:reformattingQuality|//mods:mods/mods:physicalDescription/mods:internetMediaType|".
31 "//mods:mods/mods:physicalDescription/mods:digitalOrigin)";
32 my $toc_xpath = "//mods:tableOfContents";
38 "//mods:mods/mods:titleInfo[mods:title and (\@type='abbreviated')]",
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']".
53 " or ../mods:role/mods:roleTerm[".
55 " and \@authority='marcrelator'".
56 " and text()='creator']".
59 "//mods:mods/mods:name[\@type='personal']/*[local-name()='namePart']".
60 "[../mods:role/mods:text[text()='creator']".
61 " or ../mods:role/mods:roleTerm[".
63 " and \@authority='marcrelator'".
64 " and text()='creator']".
67 "//mods:mods/mods:name[\@type='conference']/*[local-name()='namePart']".
68 "[../mods:role/mods:text[text()='creator']".
69 " or ../mods:role/mods:roleTerm[".
71 " and \@authority='marcrelator'".
72 " and text()='creator']".
75 "//mods:mods/mods:name[\@type='personal']/*[local-name()='namePart']",
77 "//mods:mods/mods:name/*[local-name()='namePart'][1]",
83 "//mods:mods/mods:subject/*[".
84 " local-name()='geographic'".
85 " or local-name()='name'".
86 " or local-name()='temporal'".
87 " or local-name()='topic'".
88 "]/parent::mods:subject",
91 # "//mods:mods/*[local-name()='subject']/*[local-name()='geographic']",
93 # "//mods:mods/*[local-name()='subject']/*[local-name()='name']",
95 # "//mods:mods/*[local-name()='subject']/*[local-name()='temporal']",
97 # "//mods:mods/*[local-name()='subject']/*[local-name()='topic']",
99 #keyword => { keyword => "//mods:mods/*[not(local-name()='originInfo')]", },
102 series => "//mods:mods/mods:relatedItem[\@type='series']/mods:titleInfo"
105 # ----------------------------------------------------------------------------------------
109 sub new { return bless( {}, shift() ); }
111 sub get_field_value {
113 my( $self, $mods, $xpath, $type) = @_;
117 my $root = $mods->documentElement;
118 $root->setNamespace( "http://www.loc.gov/mods/v3", "mods", 1 );
121 # grab the set of matching nodes
122 my @nodes = $root->findnodes( $xpath );
123 for my $value (@nodes) {
125 # grab all children of the node
126 my @children = $value->childNodes();
128 for my $child (@children) {
129 # MODS strips the punctuation from 245abc, which often
130 # results in "title subtitle" rather than "title : subtitle";
131 # this hack gets it back for us
132 if ($type && $type eq 'title' && $child->nodeName =~ m/subTitle/) {
133 push(@child_text, " : ");
135 next unless( $child->nodeType != 3 );
137 if($child->childNodes) {
139 for my $c (@{$child->childNodes}){
140 push @a, $c->textContent;
142 push(@child_text, join(' ', @a));
145 push(@child_text, $child->textContent);
150 push(@string, \@child_text);
154 push(@string, $value->textContent );
158 $logger->info("MODS-izing failure: ".shift());
159 $logger->info("Failed MODS xml: ".$root->toString);
160 $logger->info("Failed MODS xpath: $xpath");
166 sub _modsdoc_to_values {
167 my( $self, $mods ) = @_;
169 for my $class (keys %$xpathset) {
170 $data->{$class} = {};
171 for my $type(keys %{$xpathset->{$class}}) {
172 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
173 if( $class eq "subject" ) {
174 push( @{$data->{$class}->{$type}}, @value );
176 $data->{$class}->{$type} = $value[0];
184 sub modsdoc_to_values {
185 my( $self, $mods ) = @_;
189 my $class = "subject";
190 $data->{$class} = {};
191 for my $type(keys %{$xpathset->{$class}}) {
192 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
193 for my $arr (@value) {
194 push( @{$data->{$class}->{$type}}, $arr);
201 $data->{$class} = {};
202 for my $type(keys %{$xpathset->{$class}}) {
203 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type}, "title" );
204 for my $arr (@value) {
206 $data->{$class}->{$type} = shift @$arr;
208 my $t = lc($data->{$class}->{$type});
209 if($t and $t =~ /^l[eoa]s|l[ae]|el|the|un[ae]?|an?\s?$/o ) {
210 my $val = shift @$arr || "";
211 $data->{$class}->{$type} .= " $val" if $data->{$class}->{$type};
212 $data->{$class}->{$type} = " $val" unless $data->{$class}->{$type};
216 $data->{$class}->{$type} .= " $t";
219 $data->{$class}->{$type} = $arr;
222 $data->{$class}->{$type} =~ s/\s+/ /go if ($data->{$class}->{$type});
227 my $class = "author";
228 $data->{$class} = {};
229 for my $type(keys %{$xpathset->{$class}}) {
230 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
231 $data->{$class}->{$type} = $value[0];
236 my $class = "series";
237 $data->{$class} = {};
238 for my $type(keys %{$xpathset->{$class}}) {
239 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
240 for my $arr (@value) {
242 push(@{$data->{$class}->{$type}}, join(" ", @$arr));
244 push( @{$data->{$class}->{$type}}, $arr );
257 # ---------------------------------------------------------------------------
258 # Grabs the data 'we want' from the MODS doc and returns it in hash form
259 # ---------------------------------------------------------------------------
260 sub mods_values_to_mods_slim {
261 my( $self, $modsperl ) = @_;
268 my $tmp = $modsperl->{title};
271 if(!$tmp) { $title = ""; }
273 ($title = $tmp->{proper}) ||
274 ($title = $tmp->{translated}) ||
275 ($title = $tmp->{abbreviated}) ||
276 ($title = $tmp->{uniform}) ||
277 ($title = $tmp->{any});
280 $tmp = $modsperl->{author};
281 if(!$tmp) { $author = ""; }
283 ($author = $tmp->{personal}) ||
284 ($author = $tmp->{corporate}) ||
285 ($author = $tmp->{conference}) ||
286 ($author = $tmp->{other}) ||
287 ($author = $tmp->{any});
290 $tmp = $modsperl->{subject};
291 if(!$tmp) { $subject = {}; }
293 for my $key( keys %{$tmp}) {
294 push(@$subject, @{$tmp->{$key}}) if ($tmp->{$key});
297 for my $s (@$subject) {
298 if(defined($subh->{$s})) { $subh->{$s->[0]}++ } else { $subh->{$s->[0]} = 1;}
303 $tmp = $modsperl->{'series'};
304 if(!$tmp) { $series = []; }
305 else { $series = $tmp->{'series'}; }
308 return { series => $series, title => $title,
309 author => $author, subject => $subject };
314 # ---------------------------------------------------------------------------
315 # Initializes a MARC -> Unified MODS batch process
316 # ---------------------------------------------------------------------------
318 sub start_mods_batch {
320 my( $self, $master_doc ) = @_;
323 $self->{master_doc} = undef;
328 my $xslt_doc = $parser->parse_file(
329 OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl') . "/MARC21slim2MODS32.xsl");
330 $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
334 my $xmldoc = $parser->parse_string($master_doc);
335 my $mods = $mods_sheet->transform($xmldoc);
337 $self->{master_doc} = $self->modsdoc_to_values( $mods );
338 $self->{master_doc} = $self->mods_values_to_mods_slim( $self->{master_doc} );
340 ($self->{master_doc}->{isbn}) =
341 $self->get_field_value( $mods, $isbn_xpath );
343 $self->{master_doc}->{type_of_resource} =
344 [ $self->get_field_value( $mods, $resource_xpath ) ];
346 ($self->{master_doc}->{tcn}) =
347 $self->get_field_value( $mods, $tcn_xpath );
349 ($self->{master_doc}->{pubdate}) =
350 $self->get_field_value( $mods, $pub_xpath );
352 ($self->{master_doc}->{publisher}) =
353 $self->get_field_value( $mods, $publisher_xpath );
355 ($self->{master_doc}->{edition}) =
356 $self->get_field_value( $mods, $edition_xpath );
360 # ------------------------------
361 # holds an array of [ link, title, link, title, ... ]
362 $self->{master_doc}->{online_loc} = [];
363 for my $url ($mods->findnodes($online_loc_xpath)) {
364 push(@{$self->{master_doc}->{online_loc}}, $url->textContent);
365 push(@{$self->{master_doc}->{online_loc}}, $url->getAttribute('displayLabel') || '');
366 push(@{$self->{master_doc}->{online_loc}}, $url->getAttribute('note') || '');
369 ($self->{master_doc}->{synopsis}) =
370 $self->get_field_value( $mods, $abstract_xpath );
372 $self->{master_doc}->{physical_description} = [];
373 push(@{$self->{master_doc}->{physical_description}},
374 $self->get_field_value( $mods, $physical_desc ) );
375 $self->{master_doc}->{physical_description} =
376 join( ' ', @{$self->{master_doc}->{physical_description}});
378 ($self->{master_doc}->{toc}) = $self->get_field_value($mods, $toc_xpath);
384 # ---------------------------------------------------------------------------
385 # Takes a MARCXML string and adds it to the growing MODS doc
386 # ---------------------------------------------------------------------------
387 sub push_mods_batch {
388 my( $self, $marcxml ) = @_;
390 my $xmldoc = $parser->parse_string($marcxml);
391 my $mods = $mods_sheet->transform($xmldoc);
393 my $xmlperl = $self->modsdoc_to_values( $mods );
394 $xmlperl = $self->mods_values_to_mods_slim( $xmlperl );
396 # for backwards compatibility, remove the array part when all is decided
397 if(ref($xmlperl->{subject}) eq 'ARRAY' ) {
398 for my $subject( @{$xmlperl->{subject}} ) {
399 push @{$self->{master_doc}->{subject}}, $subject;
402 for my $subject ( keys %{$xmlperl->{subject}} ) {
403 my $s = $self->{master_doc}->{subject};
404 if(defined($s->{$subject})) { $s->{$subject}++; } else { $s->{$subject} = 1; }
408 push( @{$self->{master_doc}->{type_of_resource}},
409 $self->get_field_value( $mods, $resource_xpath ));
411 if(!($self->{master_doc}->{isbn}) ) {
412 ($self->{master_doc}->{isbn}) =
413 $self->get_field_value( $mods, $isbn_xpath );
418 # ---------------------------------------------------------------------------
419 # Completes a MARC -> Unified MODS batch process and returns the perl hash
420 # ---------------------------------------------------------------------------
421 sub init_virtual_record {
422 my $record = Fieldmapper::metabib::virtual_record->new;
423 $record->subject([]);
424 $record->types_of_resource([]);
425 $record->call_numbers([]);
429 sub finish_mods_batch {
432 return undef unless $self->{master_doc};
434 my $perl = $self->{master_doc};
435 my $record = init_virtual_record();
437 # turn the hash into a fieldmapper object
438 #(my $title = $perl->{title}) =~ s/\[.*?\]//og;
439 #(my $author = $perl->{author}) =~ s/\(.*?\)//og;
440 my $title = $perl->{title};
441 my $author = $perl->{author};
444 for my $s (@{$perl->{series}}) {
445 push @series, (split( /\s*;/, $s ))[0];
448 # uniquify the types of resource
449 my $rtypes = $perl->{type_of_resource};
450 my %hash = map { ($_ => 1) } @$rtypes;
451 $rtypes = [ keys %hash ];
453 $record->title($title);
454 $record->author($author);
456 $record->doc_id($perl->{doc_id});
457 $record->isbn($perl->{isbn});
458 $record->pubdate($perl->{pubdate});
459 $record->publisher($perl->{publisher});
460 $record->tcn($perl->{tcn});
462 $record->edition($perl->{edition});
464 $record->subject($perl->{subject});
465 $record->types_of_resource($rtypes);
466 $record->series(\@series);
468 $record->online_loc($perl->{online_loc});
469 $record->synopsis($perl->{synopsis});
470 $record->physical_description($perl->{physical_description});
471 $record->toc($perl->{toc});
473 $self->{master_doc} = undef;