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;
12 my $parser = XML::LibXML->new();
13 my $xslt = XML::LibXSLT->new();
16 # ----------------------------------------------------------------------------------------
17 # XPATH for extracting info from a MODS doc
18 my $isbn_xpath = "//mods:mods/mods:identifier[\@type='isbn']";
19 my $resource_xpath = "//mods:mods/mods:typeOfResource";
20 my $pub_xpath = "//mods:mods/mods:originInfo//mods:dateIssued[\@encoding='marc']|" .
21 "//mods:mods/mods:originInfo//mods:dateIssued[1]";
22 my $tcn_xpath = "//mods:mods/mods:recordInfo/mods:recordIdentifier";
23 my $publisher_xpath = "//mods:mods/mods:originInfo//mods:publisher[1]";
24 my $edition_xpath = "//mods:mods/mods:originInfo//mods:edition[1]";
25 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)";
34 "//mods:mods/mods:titleInfo[mods:title and (\@type='abreviated')]",
36 "//mods:mods/mods:titleInfo[mods:title and (\@type='translated')]",
38 "//mods:mods/mods:titleInfo[mods:title and (\@type='uniform')]",
40 "//mods:mods/mods:titleInfo[mods:title and not (\@type)]",
45 "//mods:mods/mods:name[\@type='corporate']/*[local-name()='namePart']".
46 "[../mods:role/mods:text[text()='creator']][1]",
48 "//mods:mods/mods:name[\@type='personal']/*[local-name()='namePart']".
49 "[../mods:role/mods:text[text()='creator']][1]",
51 "//mods:mods/mods:name[\@type='conference']/*[local-name()='namePart']".
52 "[../mods:role/mods:text[text()='creator']][1]",
54 "//mods:mods/mods:name[\@type='personal']/*[local-name()='namePart']",
60 "//mods:mods/mods:subject/*[local-name()!='geographicCode']/parent::mods:subject",
63 # "//mods:mods/*[local-name()='subject']/*[local-name()='geographic']",
65 # "//mods:mods/*[local-name()='subject']/*[local-name()='name']",
67 # "//mods:mods/*[local-name()='subject']/*[local-name()='temporal']",
69 # "//mods:mods/*[local-name()='subject']/*[local-name()='topic']",
71 #keyword => { keyword => "//mods:mods/*[not(local-name()='originInfo')]", },
74 series => "//mods:mods/mods:relatedItem[\@type='series']/mods:titleInfo"
77 # ----------------------------------------------------------------------------------------
81 sub new { return bless( {}, shift() ); }
85 my( $self, $mods, $xpath ) = @_;
88 my $root = $mods->documentElement;
89 $root->setNamespace( "http://www.loc.gov/mods/v3", "mods", 1 );
91 # grab the set of matching nodes
92 my @nodes = $root->findnodes( $xpath );
93 for my $value (@nodes) {
95 # grab all children of the node
96 my @children = $value->childNodes();
98 for my $child (@children) {
99 next unless( $child->nodeType != 3 );
101 if($child->childNodes) {
103 for my $c (@{$child->childNodes}){
104 push @a, $c->textContent;
106 push(@child_text, join(' ', @a));
109 push(@child_text, $child->textContent);
114 push(@string, \@child_text);
118 push(@string, $value->textContent );
125 sub _modsdoc_to_values {
126 my( $self, $mods ) = @_;
128 for my $class (keys %$xpathset) {
129 $data->{$class} = {};
130 for my $type(keys %{$xpathset->{$class}}) {
131 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
132 if( $class eq "subject" ) {
133 push( @{$data->{$class}->{$type}}, @value );
135 $data->{$class}->{$type} = $value[0];
143 sub modsdoc_to_values {
144 my( $self, $mods ) = @_;
148 my $class = "subject";
149 $data->{$class} = {};
150 for my $type(keys %{$xpathset->{$class}}) {
151 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
152 for my $arr (@value) {
153 push( @{$data->{$class}->{$type}}, $arr);
160 $data->{$class} = {};
161 for my $type(keys %{$xpathset->{$class}}) {
162 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
163 for my $arr (@value) {
165 $data->{$class}->{$type} = join(" ", @$arr);
167 $data->{$class}->{$type} = $arr;
174 my $class = "author";
175 $data->{$class} = {};
176 for my $type(keys %{$xpathset->{$class}}) {
177 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
178 $data->{$class}->{$type} = $value[0];
183 my $class = "series";
184 $data->{$class} = {};
185 for my $type(keys %{$xpathset->{$class}}) {
186 my @value = $self->get_field_value( $mods, $xpathset->{$class}->{$type} );
187 for my $arr (@value) {
189 push(@{$data->{$class}->{$type}}, join(" ", @$arr));
191 push( @{$data->{$class}->{$type}}, $arr );
204 # ---------------------------------------------------------------------------
205 # Grabs the data 'we want' from the MODS doc and returns it in hash form
206 # ---------------------------------------------------------------------------
207 sub mods_values_to_mods_slim {
208 my( $self, $modsperl ) = @_;
215 my $tmp = $modsperl->{title};
218 if(!$tmp) { $title = ""; }
220 ($title = $tmp->{proper}) ||
221 ($title = $tmp->{translated}) ||
222 ($title = $tmp->{abbreviated}) ||
223 ($title = $tmp->{uniform});
226 $tmp = $modsperl->{author};
227 if(!$tmp) { $author = ""; }
229 ($author = $tmp->{personal}) ||
230 ($author = $tmp->{other}) ||
231 ($author = $tmp->{corporate}) ||
232 ($author = $tmp->{conference});
235 $tmp = $modsperl->{subject};
236 if(!$tmp) { $subject = {}; }
238 for my $key( keys %{$tmp}) {
239 push(@$subject, @{$tmp->{$key}}) if ($tmp->{$key});
242 for my $s (@$subject) {
243 if(defined($subh->{$s})) { $subh->{$s->[0]}++ } else { $subh->{$s->[0]} = 1;}
248 $tmp = $modsperl->{'series'};
249 if(!$tmp) { $series = []; }
250 else { $series = $tmp->{'series'}; }
253 return { series => $series, title => $title,
254 author => $author, subject => $subject };
259 # ---------------------------------------------------------------------------
260 # Initializes a MARC -> Unified MODS batch process
261 # ---------------------------------------------------------------------------
263 sub start_mods_batch {
265 my( $self, $master_doc ) = @_;
269 my $xslt_doc = $parser->parse_file(
270 OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl') . "/MARC21slim2MODS3.xsl");
271 $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
275 my $xmldoc = $parser->parse_string($master_doc);
276 my $mods = $mods_sheet->transform($xmldoc);
278 # warn "-" x 100 . "\n";
279 # warn "MODS " . $mods->toString(1) . "\n";
280 # warn "-" x 100 . "\n";
282 $self->{master_doc} = $self->modsdoc_to_values( $mods );
283 $self->{master_doc} = $self->mods_values_to_mods_slim( $self->{master_doc} );
285 ($self->{master_doc}->{isbn}) =
286 $self->get_field_value( $mods, $isbn_xpath );
288 $self->{master_doc}->{type_of_resource} =
289 [ $self->get_field_value( $mods, $resource_xpath ) ];
291 ($self->{master_doc}->{tcn}) =
292 $self->get_field_value( $mods, $tcn_xpath );
294 ($self->{master_doc}->{pubdate}) =
295 $self->get_field_value( $mods, $pub_xpath );
297 ($self->{master_doc}->{publisher}) =
298 $self->get_field_value( $mods, $publisher_xpath );
300 ($self->{master_doc}->{edition}) =
301 $self->get_field_value( $mods, $edition_xpath );
305 # ------------------------------
306 # holds an array of [ link, title, link, title, ... ]
307 $self->{master_doc}->{online_loc} = [];
308 push(@{$self->{master_doc}->{online_loc}},
309 $self->get_field_value( $mods, $online_loc_xpath ));
311 ($self->{master_doc}->{synopsis}) =
312 $self->get_field_value( $mods, $abstract_xpath );
318 # ---------------------------------------------------------------------------
319 # Takes a MARCXML string and adds it to the growing MODS doc
320 # ---------------------------------------------------------------------------
321 sub push_mods_batch {
322 my( $self, $marcxml ) = @_;
324 my $xmldoc = $parser->parse_string($marcxml);
325 my $mods = $mods_sheet->transform($xmldoc);
327 my $xmlperl = $self->modsdoc_to_values( $mods );
328 $xmlperl = $self->mods_values_to_mods_slim( $xmlperl );
330 # for backwards compatibility, remove the array part when all is decided
331 if(ref($xmlperl->{subject}) eq 'ARRAY' ) {
332 for my $subject( @{$xmlperl->{subject}} ) {
333 push @{$self->{master_doc}->{subject}}, $subject;
336 for my $subject ( keys %{$xmlperl->{subject}} ) {
337 my $s = $self->{master_doc}->{subject};
338 if(defined($s->{$subject})) { $s->{$subject}++; } else { $s->{$subject} = 1; }
342 push( @{$self->{master_doc}->{type_of_resource}},
343 $self->get_field_value( $mods, $resource_xpath ));
345 if(!($self->{master_doc}->{isbn}) ) {
346 ($self->{master_doc}->{isbn}) =
347 $self->get_field_value( $mods, $isbn_xpath );
352 # ---------------------------------------------------------------------------
353 # Completes a MARC -> Unified MODS batch process and returns the perl hash
354 # ---------------------------------------------------------------------------
355 sub init_virtual_record {
356 my $record = new Fieldmapper::metabib::virtual_record;
357 $record->subject([]);
358 $record->types_of_resource([]);
359 $record->call_numbers([]);
363 sub finish_mods_batch {
365 my $perl = $self->{master_doc};
366 my $record = init_virtual_record();
368 # turn the hash into a fieldmapper object
369 (my $title = $perl->{title}) =~ s/\[.*?\]//og;
370 (my $author = $perl->{author}) =~ s/\(.*?\)//og;
373 for my $s (@{$perl->{series}}) {
374 push @series, (split( /\s*;/, $s ))[0];
377 # uniquify the types of resource
378 my $rtypes = $perl->{type_of_resource};
379 my %hash = map { ($_ => 1) } @$rtypes;
380 $rtypes = [ keys %hash ];
382 $record->title($title);
383 $record->author($author);
385 $record->doc_id($perl->{doc_id});
386 $record->isbn($perl->{isbn});
387 $record->pubdate($perl->{pubdate});
388 $record->publisher($perl->{publisher});
389 $record->tcn($perl->{tcn});
391 $record->edition($perl->{edition});
393 $record->subject($perl->{subject});
394 $record->types_of_resource($rtypes);
395 $record->series(\@series);
397 $record->online_loc($perl->{online_loc});
398 $record->synopsis($perl->{synopsis});
400 $self->{master_doc} = undef;