1 package OpenILS::Application::Storage::WORM;
2 use base qw/OpenILS::Application::Storage/;
3 use strict; use warnings;
5 use OpenSRF::EX qw/:try/;
7 use OpenSRF::Utils::SettingsClient;
8 use OpenSRF::Utils::Logger qw/:level/;
9 my $log = 'OpenSRF::Utils::Logger';
11 use OpenILS::Utils::FlatXML;
12 use OpenILS::Utils::Fieldmapper;
17 use Time::HiRes qw(time);
19 my $xml_util = OpenILS::Utils::FlatXML->new();
21 my $parser = XML::LibXML->new();
22 my $xslt = XML::LibXSLT->new();
30 my $meth = __PACKAGE__->method_lookup('open-ils.storage.direct.config.metabib_field.retrieve.all');
31 for my $f ($meth->run) {
32 $xpathset->{ $f->field_class }->{ $f->name }->{xpath} = $f->xpath;
33 $xpathset->{ $f->field_class }->{ $f->name }->{id} = $f->id;
34 $log->debug("Loaded XPath from DB: ".$f->field_class." => ".$f->name." : ".$f->xpath, DEBUG);
38 # --------------------------------------------------------------------------------
42 '//mods:mods/mods:typeOfResource[text()="text"]' => [
45 '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
46 '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
47 '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
48 '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
54 $text =~ s/^\s*(.+)\s*$/$1/sgo;
55 $text =~ s/\b(?:the|an?)\b//sgo;
56 $text =~ s/\[.[^\]]+\]//sgo;
57 $text =~ s/\s*[;\/\.]*$//sgo;
64 '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
65 '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
71 $text =~ s/^\s*(.+)\s*$/$1/sgo;
72 $text =~ s/,?\s+.*$//sgo;
79 '//mods:mods/mods:relatedItem[@type!="host" and @type!="series"]' => [
82 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="uniform")]',
83 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="translated")]',
84 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="alternative")]',
85 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and not(@type)]',
86 '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
87 '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
88 '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
89 '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
95 $text =~ s/^\s*(.+)\s*$/$1/sgo;
96 $text =~ s/\b(?:the|an?)\b//sgo;
97 $text =~ s/\[.[^\]]+\]//sgo;
98 $text =~ s/\s*[;\/\.]*$//sgo;
105 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
106 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
107 '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
108 '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
113 $text =~ s/\s+/ /sgo;
114 $text =~ s/^\s*(.+)\s*$/$1/sgo;
115 $text =~ s/,?\s+.*$//sgo;
116 $text =~ s/\pM+//gso;
124 push @fp_mods_xpath, '//mods:mods/mods:titleInfo' => $fp_mods_xpath[1];
126 sub fingerprint_mods {
133 while ( my $match_xpath = $fp_mods_xpath[$match_index] ) {
134 if ( my @nodes = $mods->findnodes( $match_xpath ) ) {
136 my $block_name_index = 0;
137 my $block_value_index = 1;
138 my $block = $fp_mods_xpath[$block_index];
139 while ( my $part = $$block[$block_value_index] ) {
141 for my $xpath ( @{ $part->{xpath} } ) {
142 $text = $mods->findvalue( $xpath );
146 $log->debug("Found fingerprint text using $$block[$block_name_index] : [$text]", DEBUG);
153 $block_name_index += 2;
154 $block_value_index += 2;
158 $fp_string =~ s/\W+//gso;
159 $log->debug("Fingerprint is [$fp_string]", INFO);;
171 # --------------------------------------------------------------------------------
182 my $create_source_map;
197 my %descriptor_code = (
198 item_type => 'substr($ldr,6,1)',
199 item_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,29,1) : substr($oo8,23,1)',
200 bib_level => 'substr($ldr,7,1)',
201 control_type => 'substr($ldr,8,1)',
202 char_encoding => 'substr($ldr,9,1)',
203 enc_level => 'substr($ldr,17,1)',
204 cat_form => 'substr($ldr,18,1)',
205 pub_status => 'substr($ldr,5,1)',
206 item_lang => 'substr($oo8,35,3)',
207 #lit_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,33,1) : "0"',
208 audience => 'substr($oo8,22,1)',
218 if ($self->api_name =~ /no_map/o) {
222 $in_xact = $self->method_lookup( 'open-ils.storage.transaction.current')
224 $begin = $self->method_lookup( 'open-ils.storage.transaction.begin')
226 $commit = $self->method_lookup( 'open-ils.storage.transaction.commit')
228 $rollback = $self->method_lookup( 'open-ils.storage.transaction.rollback')
230 $sm_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.search.source')
232 $mr_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.search.fingerprint')
234 $mr_update = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.batch.update')
236 $mr_create = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.create')
238 $create_source_map = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.batch.create')
239 unless ($create_source_map);
240 $lookup = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.retrieve')
242 $update_entry = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.update')
243 unless ($update_entry);
244 $rm_old_sm = $self->method_lookup( 'open-ils.storage.direct.metabib.metarecord_source_map.mass_delete')
246 $rm_old_rd = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.mass_delete')
248 $rm_old_fr = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.mass_delete')
250 $rm_old_tr = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.mass_delete')
252 $rm_old_ar = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.mass_delete')
254 $rm_old_sr = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.mass_delete')
256 $rm_old_kr = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.mass_delete')
258 $rm_old_ser = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.mass_delete')
259 unless ($rm_old_ser);
260 $rd_create = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.batch.create')
262 $fr_create = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.batch.create')
264 $$create{title} = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.batch.create')
265 unless ($$create{title});
266 $$create{author} = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.batch.create')
267 unless ($$create{author});
268 $$create{subject} = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.batch.create')
269 unless ($$create{subject});
270 $$create{keyword} = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.batch.create')
271 unless ($$create{keyword});
272 $$create{series} = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.batch.create')
273 unless ($$create{series});
276 my ($outer_xact) = $in_xact->run;
278 unless ($outer_xact) {
279 $log->debug("WoRM isn't inside a transaction, starting one now.", INFO);
280 my ($r) = $begin->run($client);
281 unless (defined $r and $r) {
283 throw OpenSRF::EX::PANIC ("Couldn't BEGIN transaction!")
287 throw OpenSRF::EX::PANIC ("WoRM Couldn't BEGIN transaction!")
297 for my $entry ( $lookup->run(@docids) ) {
298 # step -1: grab the doc from storage
299 next unless ($entry);
302 my $xslt_doc = $parser->parse_file(
303 OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl') . "/MARC21slim2MODS.xsl");
304 $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
307 my $xml = $entry->marc;
308 my $docid = $entry->id;
309 my $marcdoc = $parser->parse_string($xml);
310 my $modsdoc = $mods_sheet->transform($marcdoc);
312 my $mods = $modsdoc->documentElement;
313 $mods->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
315 $entry->fingerprint( fingerprint_mods( $mods ) );
316 push @entry_list, $entry;
318 $log->debug("Fingerprint for Record Entry ".$docid." is [".$entry->fingerprint."]", INFO);
321 my ($mr) = $mr_lookup->run( $entry->fingerprint );
323 $log->debug("No metarecord found for fingerprint [".$entry->fingerprint."]; Creating a new one", INFO);
324 $mr = new Fieldmapper::metabib::metarecord;
325 $mr->fingerprint( $entry->fingerprint );
326 $mr->master_record( $entry->id );
327 my ($new_mr) = $mr_create->run($mr);
329 unless (defined $mr) {
330 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.create!")
333 $log->debug("Retrieved metarecord, id is ".$mr->id, INFO);
338 my $sm = new Fieldmapper::metabib::metarecord_source_map;
339 $sm->metarecord( $mr->id );
340 $sm->source( $entry->id );
341 push @source_maps, $sm;
344 my $ldr = $marcdoc->documentElement->getChildrenByTagName('leader')->pop->textContent;
345 my $oo8 = $marcdoc->documentElement->findvalue('//*[local-name()="controlfield" and @tag="008"]');
347 my $rd_obj = Fieldmapper::metabib::record_descriptor->new;
348 for my $rd_field ( keys %descriptor_code ) {
349 $rd_obj->$rd_field( eval "$descriptor_code{$rd_field};" );
351 $rd_obj->record( $docid );
352 push @rd_list, $rd_obj;
354 push @mods_data, { $docid => $self->modsdoc_to_values( $mods ) };
356 # step 2: build the KOHA rows
357 my @tmp_list = _marcxml_to_full_rows( $marcdoc );
358 $_->record( $docid ) for (@tmp_list);
359 push @ns_list, @tmp_list;
363 last unless ($self->api_name =~ /batch$/o);
366 $rm_old_rd->run( { record => \@docids } );
367 $rm_old_fr->run( { record => \@docids } );
368 $rm_old_sm->run( { source => \@docids } ) unless ($no_map);
369 $rm_old_tr->run( { source => \@docids } );
370 $rm_old_ar->run( { source => \@docids } );
371 $rm_old_sr->run( { source => \@docids } );
372 $rm_old_kr->run( { source => \@docids } );
373 $rm_old_ser->run( { source => \@docids } );
376 my ($sm) = $create_source_map->run(@source_maps);
377 unless (defined $sm) {
378 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord_source_map.batch.create!")
380 my ($mr) = $mr_update->run(@mr_list);
381 unless (defined $mr) {
382 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.batch.update!")
386 my ($re) = $update_entry->run(@entry_list);
387 unless (defined $re) {
388 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.biblio.record_entry.batch.update!")
391 my ($rd) = $rd_create->run(@rd_list);
392 unless (defined $rd) {
393 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.record_descriptor.batch.create!")
396 my ($fr) = $fr_create->run(@ns_list);
397 unless (defined $fr) {
398 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.full_rec.batch.create!")
401 # step 5: insert the new metadata
402 for my $class ( qw/title author subject keyword series/ ) {
404 for my $doc ( @mods_data ) {
405 my ($did) = keys %$doc;
406 my ($data) = values %$doc;
408 my $fm_constructor = "Fieldmapper::metabib::${class}_field_entry";
409 for my $row ( keys %{ $$data{$class} } ) {
410 next unless (exists $$data{$class}{$row});
411 next unless ($$data{$class}{$row}{value});
412 my $fm_obj = $fm_constructor->new;
413 $fm_obj->value( $$data{$class}{$row}{value} );
414 $fm_obj->field( $$data{$class}{$row}{field_id} );
415 $fm_obj->source( $did );
416 $log->debug("$class entry: ".$fm_obj->source." => ".$fm_obj->field." : ".$fm_obj->value, DEBUG);
418 push @md_list, $fm_obj;
422 my ($cr) = $$create{$class}->run(@md_list);
423 unless (defined $cr) {
424 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.${class}_field_entry.batch.create!")
428 unless ($outer_xact) {
429 $log->debug("Commiting transaction started by the WoRM.", INFO);
430 my ($c) = $commit->run;
431 unless (defined $c and $c) {
433 throw OpenSRF::EX::PANIC ("Couldn't COMMIT changes!")
439 __PACKAGE__->register_method(
440 api_name => "open-ils.worm.wormize",
445 __PACKAGE__->register_method(
446 api_name => "open-ils.worm.wormize.no_map",
451 __PACKAGE__->register_method(
452 api_name => "open-ils.worm.wormize.batch",
457 __PACKAGE__->register_method(
458 api_name => "open-ils.worm.wormize.no_map.batch",
465 # --------------------------------------------------------------------------------
468 sub _marcxml_to_full_rows {
474 my $root = $marcxml->documentElement;
476 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
477 next unless $tagline;
479 my $ns = new Fieldmapper::metabib::full_rec;
482 my $val = $tagline->textContent;
483 $val =~ s/(\pM)//gso;
489 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
490 next unless $tagline;
492 my $ns = new Fieldmapper::metabib::full_rec;
494 $ns->tag( $tagline->getAttribute( "tag" ) );
495 my $val = $tagline->textContent;
496 $val =~ s/(\pM)//gso;
502 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
503 next unless $tagline;
505 my $tag = $tagline->getAttribute( "tag" );
506 my $ind1 = $tagline->getAttribute( "ind1" );
507 my $ind2 = $tagline->getAttribute( "ind2" );
509 for my $data ( $tagline->childNodes ) {
512 my $ns = new Fieldmapper::metabib::full_rec;
517 $ns->subfield( $data->getAttribute( "code" ) );
518 my $val = $data->textContent;
519 $val =~ s/(\pM)//gso;
520 $ns->value( lc($val) );
528 sub _get_field_value {
530 my( $root, $xpath ) = @_;
534 # grab the set of matching nodes
535 my @nodes = $root->findnodes( $xpath );
536 for my $value (@nodes) {
538 # grab all children of the node
539 my @children = $value->childNodes();
540 for my $child (@children) {
542 # add the childs content to the growing buffer
543 my $content = quotemeta($child->textContent);
544 next if ($string =~ /$content/); # uniquify the values
545 $string .= $child->textContent . " ";
548 $string .= $value->textContent . " ";
551 $string =~ s/(\pM)//gso;
556 sub modsdoc_to_values {
557 my( $self, $mods ) = @_;
559 for my $class (keys %$xpathset) {
560 $data->{$class} = {};
561 for my $type (keys %{$xpathset->{$class}}) {
562 $data->{$class}->{$type} = {};
563 $data->{$class}->{$type}->{value} = _get_field_value( $mods, $xpathset->{$class}->{$type}->{xpath} );
564 $data->{$class}->{$type}->{field_id} = $xpathset->{$class}->{$type}->{id};