1 package OpenILS::Application::Storage::WORM;
2 use base qw/OpenILS::Application::Storage/;
3 use strict; use warnings;
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenSRF::Utils::Logger qw/:level/;
10 my $log = 'OpenSRF::Utils::Logger';
12 use OpenILS::Utils::FlatXML;
13 use OpenILS::Utils::Fieldmapper;
18 use Time::HiRes qw(time);
20 my $xml_util = OpenILS::Utils::FlatXML->new();
22 my $parser = XML::LibXML->new();
23 my $xslt = XML::LibXSLT->new();
31 my $meth = __PACKAGE__->method_lookup('open-ils.storage.direct.config.metabib_field.retrieve.all');
32 for my $f ($meth->run) {
33 $xpathset->{ $f->field_class }->{ $f->name }->{xpath} = $f->xpath;
34 $xpathset->{ $f->field_class }->{ $f->name }->{id} = $f->id;
35 $log->debug("Loaded XPath from DB: ".$f->field_class." => ".$f->name." : ".$f->xpath, DEBUG);
39 # --------------------------------------------------------------------------------
43 '//mods:mods/mods:typeOfResource[text()="text"]' => [
46 '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
47 '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
48 '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
49 '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
53 $text = lc(NFD($text));
56 $text =~ s/^\s*(.+)\s*$/$1/sgo;
57 $text =~ s/\b(?:the|an?)\b//sgo;
58 $text =~ s/\[.[^\]]+\]//sgo;
59 $text =~ s/\s*[;\/\.]*$//sgo;
65 '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
66 '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
70 $text = lc(NFD($text));
73 $text =~ s/^\s*(.+)\s*$/$1/sgo;
74 $text =~ s/,?\s+.*$//sgo;
81 '//mods:mods/mods:relatedItem[@type!="host" and @type!="series"]' => [
84 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="uniform")]',
85 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="translated")]',
86 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="alternative")]',
87 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and not(@type)]',
88 '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
89 '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
90 '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
91 '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
95 $text = lc(NFD($text));
98 $text =~ s/^\s*(.+)\s*$/$1/sgo;
99 $text =~ s/\b(?:the|an?)\b//sgo;
100 $text =~ s/\[.[^\]]+\]//sgo;
101 $text =~ s/\s*[;\/\.]*$//sgo;
102 $text =~ s/\pM+//gso;
108 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
109 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
110 '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
111 '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
115 $text = lc(NFD($text));
116 $text =~ s/\pM+//gso;
117 $text =~ s/\s+/ /sgo;
118 $text =~ s/^\s*(.+)\s*$/$1/sgo;
119 $text =~ s/,?\s+.*$//sgo;
120 $text =~ s/\pM+//gso;
128 push @fp_mods_xpath, '//mods:mods/mods:titleInfo' => $fp_mods_xpath[1];
130 sub fingerprint_mods {
137 while ( my $match_xpath = $fp_mods_xpath[$match_index] ) {
138 if ( my @nodes = $mods->findnodes( $match_xpath ) ) {
140 my $block_name_index = 0;
141 my $block_value_index = 1;
142 my $block = $fp_mods_xpath[$block_index];
143 while ( my $part = $$block[$block_value_index] ) {
145 for my $xpath ( @{ $part->{xpath} } ) {
146 $text = $mods->findvalue( $xpath );
150 $log->debug("Found fingerprint text using $$block[$block_name_index] : [$text]", DEBUG);
157 $block_name_index += 2;
158 $block_value_index += 2;
162 $fp_string =~ s/\W+//gso;
163 $log->debug("Fingerprint is [$fp_string]", INFO);;
175 # --------------------------------------------------------------------------------
186 my $create_source_map;
201 my %descriptor_code = (
202 item_type => 'substr($ldr,6,1)',
203 item_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,29,1) : substr($oo8,23,1)',
204 bib_level => 'substr($ldr,7,1)',
205 control_type => 'substr($ldr,8,1)',
206 char_encoding => 'substr($ldr,9,1)',
207 enc_level => 'substr($ldr,17,1)',
208 cat_form => 'substr($ldr,18,1)',
209 pub_status => 'substr($ldr,5,1)',
210 item_lang => 'substr($oo8,35,3)',
211 #lit_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,33,1) : "0"',
212 audience => 'substr($oo8,22,1)',
222 if ($self->api_name =~ /no_map/o) {
226 $in_xact = $self->method_lookup( 'open-ils.storage.transaction.current')
228 $begin = $self->method_lookup( 'open-ils.storage.transaction.begin')
230 $commit = $self->method_lookup( 'open-ils.storage.transaction.commit')
232 $rollback = $self->method_lookup( 'open-ils.storage.transaction.rollback')
234 $sm_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.search.source')
236 $mr_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.search.fingerprint')
238 $mr_update = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.batch.update')
240 $mr_create = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.create')
242 $create_source_map = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.batch.create')
243 unless ($create_source_map);
244 $lookup = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.retrieve')
246 $update_entry = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.update')
247 unless ($update_entry);
248 $rm_old_sm = $self->method_lookup( 'open-ils.storage.direct.metabib.metarecord_source_map.mass_delete')
250 $rm_old_rd = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.mass_delete')
252 $rm_old_fr = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.mass_delete')
254 $rm_old_tr = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.mass_delete')
256 $rm_old_ar = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.mass_delete')
258 $rm_old_sr = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.mass_delete')
260 $rm_old_kr = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.mass_delete')
262 $rm_old_ser = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.mass_delete')
263 unless ($rm_old_ser);
264 $rd_create = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.batch.create')
266 $fr_create = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.batch.create')
268 $$create{title} = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.batch.create')
269 unless ($$create{title});
270 $$create{author} = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.batch.create')
271 unless ($$create{author});
272 $$create{subject} = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.batch.create')
273 unless ($$create{subject});
274 $$create{keyword} = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.batch.create')
275 unless ($$create{keyword});
276 $$create{series} = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.batch.create')
277 unless ($$create{series});
280 my ($outer_xact) = $in_xact->run;
282 unless ($outer_xact) {
283 $log->debug("WoRM isn't inside a transaction, starting one now.", INFO);
284 my ($r) = $begin->run($client);
285 unless (defined $r and $r) {
287 throw OpenSRF::EX::PANIC ("Couldn't BEGIN transaction!")
291 throw OpenSRF::EX::PANIC ("WoRM Couldn't BEGIN transaction!")
301 for my $entry ( $lookup->run(@docids) ) {
302 # step -1: grab the doc from storage
303 next unless ($entry);
306 my $xslt_doc = $parser->parse_file(
307 OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl') . "/MARC21slim2MODS.xsl");
308 $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
311 my $xml = $entry->marc;
312 my $docid = $entry->id;
313 my $marcdoc = $parser->parse_string($xml);
314 my $modsdoc = $mods_sheet->transform($marcdoc);
316 my $mods = $modsdoc->documentElement;
317 $mods->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
319 $entry->fingerprint( fingerprint_mods( $mods ) );
320 push @entry_list, $entry;
322 $log->debug("Fingerprint for Record Entry ".$docid." is [".$entry->fingerprint."]", INFO);
325 my ($mr) = $mr_lookup->run( $entry->fingerprint );
327 $log->debug("No metarecord found for fingerprint [".$entry->fingerprint."]; Creating a new one", INFO);
328 $mr = new Fieldmapper::metabib::metarecord;
329 $mr->fingerprint( $entry->fingerprint );
330 $mr->master_record( $entry->id );
331 my ($new_mr) = $mr_create->run($mr);
333 unless (defined $mr) {
334 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.create!")
337 $log->debug("Retrieved metarecord, id is ".$mr->id, INFO);
342 my $sm = new Fieldmapper::metabib::metarecord_source_map;
343 $sm->metarecord( $mr->id );
344 $sm->source( $entry->id );
345 push @source_maps, $sm;
348 my $ldr = $marcdoc->documentElement->getChildrenByTagName('leader')->pop->textContent;
349 my $oo8 = $marcdoc->documentElement->findvalue('//*[local-name()="controlfield" and @tag="008"]');
351 my $rd_obj = Fieldmapper::metabib::record_descriptor->new;
352 for my $rd_field ( keys %descriptor_code ) {
353 $rd_obj->$rd_field( eval "$descriptor_code{$rd_field};" );
355 $rd_obj->record( $docid );
356 push @rd_list, $rd_obj;
358 push @mods_data, { $docid => $self->modsdoc_to_values( $mods ) };
360 # step 2: build the KOHA rows
361 my @tmp_list = _marcxml_to_full_rows( $marcdoc );
362 $_->record( $docid ) for (@tmp_list);
363 push @ns_list, @tmp_list;
367 last unless ($self->api_name =~ /batch$/o);
370 $rm_old_rd->run( { record => \@docids } );
371 $rm_old_fr->run( { record => \@docids } );
372 $rm_old_sm->run( { source => \@docids } ) unless ($no_map);
373 $rm_old_tr->run( { source => \@docids } );
374 $rm_old_ar->run( { source => \@docids } );
375 $rm_old_sr->run( { source => \@docids } );
376 $rm_old_kr->run( { source => \@docids } );
377 $rm_old_ser->run( { source => \@docids } );
380 my ($sm) = $create_source_map->run(@source_maps);
381 unless (defined $sm) {
382 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord_source_map.batch.create!")
384 my ($mr) = $mr_update->run(@mr_list);
385 unless (defined $mr) {
386 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.batch.update!")
390 my ($re) = $update_entry->run(@entry_list);
391 unless (defined $re) {
392 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.biblio.record_entry.batch.update!")
395 my ($rd) = $rd_create->run(@rd_list);
396 unless (defined $rd) {
397 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.record_descriptor.batch.create!")
400 my ($fr) = $fr_create->run(@ns_list);
401 unless (defined $fr) {
402 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.full_rec.batch.create!")
405 # step 5: insert the new metadata
406 for my $class ( qw/title author subject keyword series/ ) {
408 for my $doc ( @mods_data ) {
409 my ($did) = keys %$doc;
410 my ($data) = values %$doc;
412 my $fm_constructor = "Fieldmapper::metabib::${class}_field_entry";
413 for my $row ( keys %{ $$data{$class} } ) {
414 next unless (exists $$data{$class}{$row});
415 next unless ($$data{$class}{$row}{value});
416 my $fm_obj = $fm_constructor->new;
417 $fm_obj->value( $$data{$class}{$row}{value} );
418 $fm_obj->field( $$data{$class}{$row}{field_id} );
419 $fm_obj->source( $did );
420 $log->debug("$class entry: ".$fm_obj->source." => ".$fm_obj->field." : ".$fm_obj->value, DEBUG);
422 push @md_list, $fm_obj;
426 my ($cr) = $$create{$class}->run(@md_list);
427 unless (defined $cr) {
428 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.${class}_field_entry.batch.create!")
432 unless ($outer_xact) {
433 $log->debug("Commiting transaction started by the WoRM.", INFO);
434 my ($c) = $commit->run;
435 unless (defined $c and $c) {
437 throw OpenSRF::EX::PANIC ("Couldn't COMMIT changes!")
443 __PACKAGE__->register_method(
444 api_name => "open-ils.worm.wormize",
449 __PACKAGE__->register_method(
450 api_name => "open-ils.worm.wormize.no_map",
455 __PACKAGE__->register_method(
456 api_name => "open-ils.worm.wormize.batch",
461 __PACKAGE__->register_method(
462 api_name => "open-ils.worm.wormize.no_map.batch",
469 # --------------------------------------------------------------------------------
472 sub _marcxml_to_full_rows {
478 my $root = $marcxml->documentElement;
480 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
481 next unless $tagline;
483 my $ns = new Fieldmapper::metabib::full_rec;
486 my $val = NFD($tagline->textContent);
487 $val =~ s/(\pM+)//gso;
493 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
494 next unless $tagline;
496 my $ns = new Fieldmapper::metabib::full_rec;
498 $ns->tag( $tagline->getAttribute( "tag" ) );
499 my $val = NFD($tagline->textContent);
500 $val =~ s/(\pM+)//gso;
506 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
507 next unless $tagline;
509 my $tag = $tagline->getAttribute( "tag" );
510 my $ind1 = $tagline->getAttribute( "ind1" );
511 my $ind2 = $tagline->getAttribute( "ind2" );
513 for my $data ( $tagline->childNodes ) {
516 my $ns = new Fieldmapper::metabib::full_rec;
521 $ns->subfield( $data->getAttribute( "code" ) );
522 my $val = NFD($data->textContent);
523 $val =~ s/(\pM+)//gso;
524 $ns->value( lc($val) );
532 sub _get_field_value {
534 my( $root, $xpath ) = @_;
538 # grab the set of matching nodes
539 my @nodes = $root->findnodes( $xpath );
540 for my $value (@nodes) {
542 # grab all children of the node
543 my @children = $value->childNodes();
544 for my $child (@children) {
546 # add the childs content to the growing buffer
547 my $content = quotemeta($child->textContent);
548 next if ($string =~ /$content/); # uniquify the values
549 $string .= $child->textContent . " ";
552 $string .= $value->textContent . " ";
555 $string = NFD($string);
556 $string =~ s/(\pM)//gso;
561 sub modsdoc_to_values {
562 my( $self, $mods ) = @_;
564 for my $class (keys %$xpathset) {
565 $data->{$class} = {};
566 for my $type (keys %{$xpathset->{$class}}) {
567 $data->{$class}->{$type} = {};
568 $data->{$class}->{$type}->{value} = _get_field_value( $mods, $xpathset->{$class}->{$type}->{xpath} );
569 $data->{$class}->{$type}->{field_id} = $xpathset->{$class}->{$type}->{id};