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();
22 my $xslt_doc = $parser->parse_file( "/home/miker/cvs/OpenILS/app_server/stylesheets/MARC21slim2MODS.xsl" );
23 #my $xslt_doc = $parser->parse_file( "/pines/cvs/ILS/Open-ILS/xsl/MARC21slim2MODS.xsl" );
24 my $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
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)]',
55 $text =~ s/^\s*(.+)\s*$/$1/sgo;
56 $text =~ s/\b(?:the|an?)\b//sgo;
57 $text =~ s/\[.[^\]]+\]//sgo;
58 $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',
72 $text =~ s/^\s*(.+)\s*$/$1/sgo;
73 $text =~ s/,?\s+.*$//sgo;
80 '//mods:mods/mods:relatedItem[@type!="host" and @type!="series"]' => [
83 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="uniform")]',
84 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="translated")]',
85 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="alternative")]',
86 '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and not(@type)]',
87 '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
88 '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
89 '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
90 '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
96 $text =~ s/^\s*(.+)\s*$/$1/sgo;
97 $text =~ s/\b(?:the|an?)\b//sgo;
98 $text =~ s/\[.[^\]]+\]//sgo;
99 $text =~ s/\s*[;\/\.]*$//sgo;
100 $text =~ s/\pM+//gso;
106 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
107 '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
108 '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
109 '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
114 $text =~ s/\s+/ /sgo;
115 $text =~ s/^\s*(.+)\s*$/$1/sgo;
116 $text =~ s/,?\s+.*$//sgo;
117 $text =~ s/\pM+//gso;
125 push @fp_mods_xpath, '//mods:mods/mods:titleInfo' => $fp_mods_xpath[1];
127 sub fingerprint_mods {
134 while ( my $match_xpath = $fp_mods_xpath[$match_index] ) {
135 if ( my @nodes = $mods->findnodes( $match_xpath ) ) {
137 my $block_name_index = 0;
138 my $block_value_index = 1;
139 my $block = $fp_mods_xpath[$block_index];
140 while ( my $part = $$block[$block_value_index] ) {
142 for my $xpath ( @{ $part->{xpath} } ) {
143 $text = $mods->findvalue( $xpath );
147 $log->debug("Found fingerprint text using $$block[$block_name_index] : [$text]", DEBUG);
154 $block_name_index += 2;
155 $block_value_index += 2;
159 $fp_string =~ s/\W+//gso;
160 $log->debug("Fingerprint is [$fp_string]", INFO);;
172 # --------------------------------------------------------------------------------
183 my $create_source_map;
198 my %descriptor_code = (
199 item_type => 'substr($ldr,6,1)',
200 item_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,29,1) : substr($oo8,23,1)',
201 bib_level => 'substr($ldr,7,1)',
202 control_type => 'substr($ldr,8,1)',
203 char_encoding => 'substr($ldr,9,1)',
204 enc_level => 'substr($ldr,17,1)',
205 cat_form => 'substr($ldr,18,1)',
206 pub_status => 'substr($ldr,5,1)',
207 item_lang => 'substr($oo8,35,3)',
208 #lit_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,33,1) : "0"',
209 audience => 'substr($oo8,22,1)',
219 if ($self->api_name =~ /no_map/o) {
223 $in_xact = $self->method_lookup( 'open-ils.storage.transaction.current')
225 $begin = $self->method_lookup( 'open-ils.storage.transaction.begin')
227 $commit = $self->method_lookup( 'open-ils.storage.transaction.commit')
229 $rollback = $self->method_lookup( 'open-ils.storage.transaction.rollback')
231 $sm_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.search.source')
233 $mr_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.search.fingerprint')
235 $mr_update = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.batch.update')
237 $mr_create = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.create')
239 $create_source_map = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.batch.create')
240 unless ($create_source_map);
241 $lookup = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.retrieve')
243 $update_entry = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.update')
244 unless ($update_entry);
245 $rm_old_sm = $self->method_lookup( 'open-ils.storage.direct.metabib.metarecord_source_map.mass_delete')
247 $rm_old_rd = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.mass_delete')
249 $rm_old_fr = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.mass_delete')
251 $rm_old_tr = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.mass_delete')
253 $rm_old_ar = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.mass_delete')
255 $rm_old_sr = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.mass_delete')
257 $rm_old_kr = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.mass_delete')
259 $rm_old_ser = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.mass_delete')
260 unless ($rm_old_ser);
261 $rd_create = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.batch.create')
263 $fr_create = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.batch.create')
265 $$create{title} = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.batch.create')
266 unless ($$create{title});
267 $$create{author} = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.batch.create')
268 unless ($$create{author});
269 $$create{subject} = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.batch.create')
270 unless ($$create{subject});
271 $$create{keyword} = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.batch.create')
272 unless ($$create{keyword});
273 $$create{series} = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.batch.create')
274 unless ($$create{series});
277 my ($outer_xact) = $in_xact->run;
279 unless ($outer_xact) {
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);
301 my $xml = $entry->marc;
302 my $docid = $entry->id;
303 my $marcdoc = $parser->parse_string($xml);
304 my $modsdoc = $mods_sheet->transform($marcdoc);
306 my $mods = $modsdoc->documentElement;
307 $mods->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
309 $entry->fingerprint( fingerprint_mods( $mods ) );
310 push @entry_list, $entry;
313 my ($mr) = $mr_lookup->run( $entry->fingerprint );
315 $mr = new Fieldmapper::metabib::metarecord;
316 $mr->fingerprint( $entry->fingerprint );
317 $mr->master_record( $entry->id );
318 my ($new_mr) = $mr_create->run($mr);
320 unless (defined $mr) {
321 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.create!")
329 my $sm = new Fieldmapper::metabib::metarecord_source_map;
330 $sm->metarecord( $mr->id );
331 $sm->source( $entry->id );
332 push @source_maps, $sm;
335 my $ldr = $marcdoc->documentElement->getChildrenByTagName('leader')->pop->textContent;
336 my $oo8 = $marcdoc->documentElement->findvalue('//*[local-name()="controlfield" and @tag="008"]');
338 my $rd_obj = Fieldmapper::metabib::record_descriptor->new;
339 for my $rd_field ( keys %descriptor_code ) {
340 $rd_obj->$rd_field( eval "$descriptor_code{$rd_field};" );
342 $rd_obj->record( $docid );
343 push @rd_list, $rd_obj;
345 push @mods_data, { $docid => $self->modsdoc_to_values( $mods ) };
347 # step 2: build the KOHA rows
348 my @tmp_list = _marcxml_to_full_rows( $marcdoc );
349 $_->record( $docid ) for (@tmp_list);
350 push @ns_list, @tmp_list;
354 last unless ($self->api_name =~ /batch$/o);
357 $rm_old_rd->run( { record => \@docids } );
358 $rm_old_fr->run( { record => \@docids } );
359 $rm_old_sm->run( { source => \@docids } ) unless ($no_map);
360 $rm_old_tr->run( { source => \@docids } );
361 $rm_old_ar->run( { source => \@docids } );
362 $rm_old_sr->run( { source => \@docids } );
363 $rm_old_kr->run( { source => \@docids } );
364 $rm_old_ser->run( { source => \@docids } );
367 my ($sm) = $create_source_map->run(@source_maps);
368 unless (defined $sm) {
369 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord_source_map.batch.create!")
371 my ($mr) = $mr_update->run(@mr_list);
372 unless (defined $mr) {
373 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.batch.update!")
377 my ($re) = $update_entry->run(@entry_list);
378 unless (defined $re) {
379 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.biblio.record_entry.batch.update!")
382 my ($rd) = $rd_create->run(@rd_list);
383 unless (defined $rd) {
384 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.record_descriptor.batch.create!")
387 my ($fr) = $fr_create->run(@ns_list);
388 unless (defined $fr) {
389 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.full_rec.batch.create!")
392 # step 5: insert the new metadata
393 for my $class ( qw/title author subject keyword series/ ) {
395 for my $doc ( @mods_data ) {
396 my ($did) = keys %$doc;
397 my ($data) = values %$doc;
399 my $fm_constructor = "Fieldmapper::metabib::${class}_field_entry";
400 for my $row ( keys %{ $$data{$class} } ) {
401 next unless (exists $$data{$class}{$row});
402 next unless ($$data{$class}{$row}{value});
403 my $fm_obj = $fm_constructor->new;
404 $fm_obj->value( $$data{$class}{$row}{value} );
405 $fm_obj->field( $$data{$class}{$row}{field_id} );
406 $fm_obj->source( $did );
407 $log->debug("$class entry: ".$fm_obj->source." => ".$fm_obj->field." : ".$fm_obj->value, DEBUG);
409 push @md_list, $fm_obj;
413 my ($cr) = $$create{$class}->run(@md_list);
414 unless (defined $cr) {
415 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.${class}_field_entry.batch.create!")
419 unless ($outer_xact) {
420 my ($c) = $commit->run;
421 unless (defined $c and $c) {
423 throw OpenSRF::EX::PANIC ("Couldn't COMMIT changes!")
429 __PACKAGE__->register_method(
430 api_name => "open-ils.worm.wormize",
435 __PACKAGE__->register_method(
436 api_name => "open-ils.worm.wormize.no_map",
441 __PACKAGE__->register_method(
442 api_name => "open-ils.worm.wormize.batch",
447 __PACKAGE__->register_method(
448 api_name => "open-ils.worm.wormize.no_map.batch",
455 # --------------------------------------------------------------------------------
458 sub _marcxml_to_full_rows {
464 my $root = $marcxml->documentElement;
466 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
467 next unless $tagline;
469 my $ns = new Fieldmapper::metabib::full_rec;
472 my $val = $tagline->textContent;
473 $val =~ s/(\pM)//gso;
479 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
480 next unless $tagline;
482 my $ns = new Fieldmapper::metabib::full_rec;
484 $ns->tag( $tagline->getAttribute( "tag" ) );
485 my $val = $tagline->textContent;
486 $val =~ s/(\pM)//gso;
492 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
493 next unless $tagline;
495 my $tag = $tagline->getAttribute( "tag" );
496 my $ind1 = $tagline->getAttribute( "ind1" );
497 my $ind2 = $tagline->getAttribute( "ind2" );
499 for my $data ( $tagline->childNodes ) {
502 my $ns = new Fieldmapper::metabib::full_rec;
507 $ns->subfield( $data->getAttribute( "code" ) );
508 my $val = $data->textContent;
509 $val =~ s/(\pM)//gso;
510 $ns->value( lc($val) );
518 sub _get_field_value {
520 my( $root, $xpath ) = @_;
524 # grab the set of matching nodes
525 my @nodes = $root->findnodes( $xpath );
526 for my $value (@nodes) {
528 # grab all children of the node
529 my @children = $value->childNodes();
530 for my $child (@children) {
532 # add the childs content to the growing buffer
533 my $content = quotemeta($child->textContent);
534 next if ($string =~ /$content/); # uniquify the values
535 $string .= $child->textContent . " ";
538 $string .= $value->textContent . " ";
541 $string =~ s/(\pM)//gso;
546 sub modsdoc_to_values {
547 my( $self, $mods ) = @_;
549 for my $class (keys %$xpathset) {
550 $data->{$class} = {};
551 for my $type (keys %{$xpathset->{$class}}) {
552 $data->{$class}->{$type} = {};
553 $data->{$class}->{$type}->{value} = _get_field_value( $mods, $xpathset->{$class}->{$type}->{xpath} );
554 $data->{$class}->{$type}->{field_id} = $xpathset->{$class}->{$type}->{id};