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::Logger qw/:level/;
8 my $log = 'OpenSRF::Utils::Logger';
10 use OpenILS::Utils::FlatXML;
11 use OpenILS::Utils::Fieldmapper;
16 use Time::HiRes qw(time);
18 my $xml_util = OpenILS::Utils::FlatXML->new();
20 my $parser = XML::LibXML->new();
21 my $xslt = XML::LibXSLT->new();
29 my $meth = __PACKAGE__->method_lookup('open-ils.storage.direct.config.metabib_field.retrieve.all');
30 for my $f ($meth->run) {
31 $xpathset->{ $f->field_class }->{ $f->name }->{xpath} = $f->xpath;
32 $xpathset->{ $f->field_class }->{ $f->name }->{id} = $f->id;
33 $log->debug("Loaded XPath from DB: ".$f->field_class." => ".$f->name." : ".$f->xpath, DEBUG);
37 # --------------------------------------------------------------------------------
41 '//mods:mods/mods:typeOfResource[text()="text"]' => [
44 '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
45 '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
46 '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
47 '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
53 $text =~ s/^\s*(.+)\s*$/$1/sgo;
54 $text =~ s/\b(?:the|an?)\b//sgo;
55 $text =~ s/\[.[^\]]+\]//sgo;
56 $text =~ s/\s*[;\/\.]*$//sgo;
63 '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
64 '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
70 $text =~ s/^\s*(.+)\s*$/$1/sgo;
71 $text =~ s/,?\s+.*$//sgo;
78 '//mods:mods/mods:relatedItem[@type!="host" and @type!="series"]' => [
81 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="uniform")]',
82 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="translated")]',
83 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="alternative")]',
84 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and not(@type)]',
85 '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
86 '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
87 '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
88 '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
94 $text =~ s/^\s*(.+)\s*$/$1/sgo;
95 $text =~ s/\b(?:the|an?)\b//sgo;
96 $text =~ s/\[.[^\]]+\]//sgo;
97 $text =~ s/\s*[;\/\.]*$//sgo;
104 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
105 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
106 '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
107 '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
112 $text =~ s/\s+/ /sgo;
113 $text =~ s/^\s*(.+)\s*$/$1/sgo;
114 $text =~ s/,?\s+.*$//sgo;
115 $text =~ s/\pM+//gso;
123 push @fp_mods_xpath, '//mods:mods/mods:titleInfo' => $fp_mods_xpath[1];
125 sub fingerprint_mods {
132 while ( my $match_xpath = $fp_mods_xpath[$match_index] ) {
133 if ( my @nodes = $mods->findnodes( $match_xpath ) ) {
135 my $block_name_index = 0;
136 my $block_value_index = 1;
137 my $block = $fp_mods_xpath[$block_index];
138 while ( my $part = $$block[$block_value_index] ) {
140 for my $xpath ( @{ $part->{xpath} } ) {
141 $text = $mods->findvalue( $xpath );
145 $log->debug("Found fingerprint text using $$block[$block_name_index] : [$text]", DEBUG);
152 $block_name_index += 2;
153 $block_value_index += 2;
157 $fp_string =~ s/\W+//gso;
158 $log->debug("Fingerprint is [$fp_string]", INFO);;
170 # --------------------------------------------------------------------------------
181 my $create_source_map;
196 my %descriptor_code = (
197 item_type => 'substr($ldr,6,1)',
198 item_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,29,1) : substr($oo8,23,1)',
199 bib_level => 'substr($ldr,7,1)',
200 control_type => 'substr($ldr,8,1)',
201 char_encoding => 'substr($ldr,9,1)',
202 enc_level => 'substr($ldr,17,1)',
203 cat_form => 'substr($ldr,18,1)',
204 pub_status => 'substr($ldr,5,1)',
205 item_lang => 'substr($oo8,35,3)',
206 #lit_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,33,1) : "0"',
207 audience => 'substr($oo8,22,1)',
217 if ($self->api_name =~ /no_map/o) {
221 $in_xact = $self->method_lookup( 'open-ils.storage.transaction.current')
223 $begin = $self->method_lookup( 'open-ils.storage.transaction.begin')
225 $commit = $self->method_lookup( 'open-ils.storage.transaction.commit')
227 $rollback = $self->method_lookup( 'open-ils.storage.transaction.rollback')
229 $sm_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.search.source')
231 $mr_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.search.fingerprint')
233 $mr_update = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.batch.update')
235 $mr_create = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.create')
237 $create_source_map = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.batch.create')
238 unless ($create_source_map);
239 $lookup = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.retrieve')
241 $update_entry = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.update')
242 unless ($update_entry);
243 $rm_old_sm = $self->method_lookup( 'open-ils.storage.direct.metabib.metarecord_source_map.mass_delete')
245 $rm_old_rd = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.mass_delete')
247 $rm_old_fr = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.mass_delete')
249 $rm_old_tr = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.mass_delete')
251 $rm_old_ar = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.mass_delete')
253 $rm_old_sr = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.mass_delete')
255 $rm_old_kr = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.mass_delete')
257 $rm_old_ser = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.mass_delete')
258 unless ($rm_old_ser);
259 $rd_create = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.batch.create')
261 $fr_create = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.batch.create')
263 $$create{title} = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.batch.create')
264 unless ($$create{title});
265 $$create{author} = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.batch.create')
266 unless ($$create{author});
267 $$create{subject} = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.batch.create')
268 unless ($$create{subject});
269 $$create{keyword} = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.batch.create')
270 unless ($$create{keyword});
271 $$create{series} = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.batch.create')
272 unless ($$create{series});
275 my ($outer_xact) = $in_xact->run;
277 unless ($outer_xact) {
278 $log->debug("WoRM isn't inside a transaction, starting one now.", INFO);
279 my ($r) = $begin->run($client);
280 unless (defined $r and $r) {
282 throw OpenSRF::EX::PANIC ("Couldn't BEGIN transaction!")
286 throw OpenSRF::EX::PANIC ("WoRM Couldn't BEGIN transaction!")
296 for my $entry ( $lookup->run(@docids) ) {
297 # step -1: grab the doc from storage
298 next unless ($entry);
301 my $xslt_doc = $parser->parse_file(
302 OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl') . "/MARC21slim2MODS.xsl");
303 $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
306 my $xml = $entry->marc;
307 my $docid = $entry->id;
308 my $marcdoc = $parser->parse_string($xml);
309 my $modsdoc = $mods_sheet->transform($marcdoc);
311 my $mods = $modsdoc->documentElement;
312 $mods->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
314 $entry->fingerprint( fingerprint_mods( $mods ) );
315 push @entry_list, $entry;
317 $log->debug("Fingerprint for Record Entry ".$docid." is [".$entry->fingerprint."]", INFO);
320 my ($mr) = $mr_lookup->run( $entry->fingerprint );
322 $log->debug("No metarecord found for fingerprint [".$entry->fingerprint."]; Creating a new one", INFO);
323 $mr = new Fieldmapper::metabib::metarecord;
324 $mr->fingerprint( $entry->fingerprint );
325 $mr->master_record( $entry->id );
326 my ($new_mr) = $mr_create->run($mr);
328 unless (defined $mr) {
329 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.create!")
332 $log->debug("Retrieved metarecord, id is ".$mr->id, INFO);
337 my $sm = new Fieldmapper::metabib::metarecord_source_map;
338 $sm->metarecord( $mr->id );
339 $sm->source( $entry->id );
340 push @source_maps, $sm;
343 my $ldr = $marcdoc->documentElement->getChildrenByTagName('leader')->pop->textContent;
344 my $oo8 = $marcdoc->documentElement->findvalue('//*[local-name()="controlfield" and @tag="008"]');
346 my $rd_obj = Fieldmapper::metabib::record_descriptor->new;
347 for my $rd_field ( keys %descriptor_code ) {
348 $rd_obj->$rd_field( eval "$descriptor_code{$rd_field};" );
350 $rd_obj->record( $docid );
351 push @rd_list, $rd_obj;
353 push @mods_data, { $docid => $self->modsdoc_to_values( $mods ) };
355 # step 2: build the KOHA rows
356 my @tmp_list = _marcxml_to_full_rows( $marcdoc );
357 $_->record( $docid ) for (@tmp_list);
358 push @ns_list, @tmp_list;
362 last unless ($self->api_name =~ /batch$/o);
365 $rm_old_rd->run( { record => \@docids } );
366 $rm_old_fr->run( { record => \@docids } );
367 $rm_old_sm->run( { source => \@docids } ) unless ($no_map);
368 $rm_old_tr->run( { source => \@docids } );
369 $rm_old_ar->run( { source => \@docids } );
370 $rm_old_sr->run( { source => \@docids } );
371 $rm_old_kr->run( { source => \@docids } );
372 $rm_old_ser->run( { source => \@docids } );
375 my ($sm) = $create_source_map->run(@source_maps);
376 unless (defined $sm) {
377 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord_source_map.batch.create!")
379 my ($mr) = $mr_update->run(@mr_list);
380 unless (defined $mr) {
381 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.batch.update!")
385 my ($re) = $update_entry->run(@entry_list);
386 unless (defined $re) {
387 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.biblio.record_entry.batch.update!")
390 my ($rd) = $rd_create->run(@rd_list);
391 unless (defined $rd) {
392 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.record_descriptor.batch.create!")
395 my ($fr) = $fr_create->run(@ns_list);
396 unless (defined $fr) {
397 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.full_rec.batch.create!")
400 # step 5: insert the new metadata
401 for my $class ( qw/title author subject keyword series/ ) {
403 for my $doc ( @mods_data ) {
404 my ($did) = keys %$doc;
405 my ($data) = values %$doc;
407 my $fm_constructor = "Fieldmapper::metabib::${class}_field_entry";
408 for my $row ( keys %{ $$data{$class} } ) {
409 next unless (exists $$data{$class}{$row});
410 next unless ($$data{$class}{$row}{value});
411 my $fm_obj = $fm_constructor->new;
412 $fm_obj->value( $$data{$class}{$row}{value} );
413 $fm_obj->field( $$data{$class}{$row}{field_id} );
414 $fm_obj->source( $did );
415 $log->debug("$class entry: ".$fm_obj->source." => ".$fm_obj->field." : ".$fm_obj->value, DEBUG);
417 push @md_list, $fm_obj;
421 my ($cr) = $$create{$class}->run(@md_list);
422 unless (defined $cr) {
423 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.${class}_field_entry.batch.create!")
427 unless ($outer_xact) {
428 $log->debug("Commiting transaction started by the WoRM.", INFO);
429 my ($c) = $commit->run;
430 unless (defined $c and $c) {
432 throw OpenSRF::EX::PANIC ("Couldn't COMMIT changes!")
438 __PACKAGE__->register_method(
439 api_name => "open-ils.worm.wormize",
444 __PACKAGE__->register_method(
445 api_name => "open-ils.worm.wormize.no_map",
450 __PACKAGE__->register_method(
451 api_name => "open-ils.worm.wormize.batch",
456 __PACKAGE__->register_method(
457 api_name => "open-ils.worm.wormize.no_map.batch",
464 # --------------------------------------------------------------------------------
467 sub _marcxml_to_full_rows {
473 my $root = $marcxml->documentElement;
475 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
476 next unless $tagline;
478 my $ns = new Fieldmapper::metabib::full_rec;
481 my $val = $tagline->textContent;
482 $val =~ s/(\pM)//gso;
488 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
489 next unless $tagline;
491 my $ns = new Fieldmapper::metabib::full_rec;
493 $ns->tag( $tagline->getAttribute( "tag" ) );
494 my $val = $tagline->textContent;
495 $val =~ s/(\pM)//gso;
501 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
502 next unless $tagline;
504 my $tag = $tagline->getAttribute( "tag" );
505 my $ind1 = $tagline->getAttribute( "ind1" );
506 my $ind2 = $tagline->getAttribute( "ind2" );
508 for my $data ( $tagline->childNodes ) {
511 my $ns = new Fieldmapper::metabib::full_rec;
516 $ns->subfield( $data->getAttribute( "code" ) );
517 my $val = $data->textContent;
518 $val =~ s/(\pM)//gso;
519 $ns->value( lc($val) );
527 sub _get_field_value {
529 my( $root, $xpath ) = @_;
533 # grab the set of matching nodes
534 my @nodes = $root->findnodes( $xpath );
535 for my $value (@nodes) {
537 # grab all children of the node
538 my @children = $value->childNodes();
539 for my $child (@children) {
541 # add the childs content to the growing buffer
542 my $content = quotemeta($child->textContent);
543 next if ($string =~ /$content/); # uniquify the values
544 $string .= $child->textContent . " ";
547 $string .= $value->textContent . " ";
550 $string =~ s/(\pM)//gso;
555 sub modsdoc_to_values {
556 my( $self, $mods ) = @_;
558 for my $class (keys %$xpathset) {
559 $data->{$class} = {};
560 for my $type (keys %{$xpathset->{$class}}) {
561 $data->{$class}->{$type} = {};
562 $data->{$class}->{$type}->{value} = _get_field_value( $mods, $xpathset->{$class}->{$type}->{xpath} );
563 $data->{$class}->{$type}->{field_id} = $xpathset->{$class}->{$type}->{id};