1 package OpenILS::Application::Ingest;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
8 use OpenSRF::AppSession;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenSRF::Utils::Logger qw/:level/;
12 use OpenILS::Utils::ScriptRunner;
13 use OpenILS::Utils::Fieldmapper;
14 use OpenSRF::Utils::JSON;
16 use OpenILS::Utils::Fieldmapper;
20 use Time::HiRes qw(time);
22 our %supported_formats = (
23 mods32 => {ns => 'http://www.loc.gov/mods/v3'},
24 mods3 => {ns => 'http://www.loc.gov/mods/v3'},
25 mods => {ns => 'http://www.loc.gov/mods/'},
26 marcxml => {ns => 'http://www.loc.gov/MARC21/slim'},
27 srw_dc => {ns => 'info:srw/schema/1/dc-schema'},
28 oai_dc => {ns => 'http://www.openarchives.org/OAI/2.0/oai_dc/'},
29 rdf_dc => {ns => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'},
30 atom => {ns => 'http://www.w3.org/2005/Atom'},
31 rss091 => {ns => 'http://my.netscape.com/rdf/simple/0.9/'},
35 rss10 => {ns => 'http://purl.org/rss/1.0/'},
36 rss11 => {ns => 'http://purl.org/net/rss1.1#'},
41 my $log = 'OpenSRF::Utils::Logger';
43 my $parser = XML::LibXML->new();
44 my $xslt = XML::LibXSLT->new();
54 unless (keys %$xpathset) {
55 $log->debug("Running post_init", DEBUG);
57 my $xsldir = OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl');
59 unless ($supported_formats{mods}{xslt}) {
60 $log->debug("Loading MODS XSLT", DEBUG);
61 my $xslt_doc = $parser->parse_file( $xsldir . "/MARC21slim2MODS.xsl");
62 $supported_formats{mods}{xslt} = $xslt->parse_stylesheet( $xslt_doc );
65 unless ($supported_formats{mods3}{xslt}) {
66 $log->debug("Loading MODS v3 XSLT", DEBUG);
67 my $xslt_doc = $parser->parse_file( $xsldir . "/MARC21slim2MODS3.xsl");
68 $supported_formats{mods3}{xslt} = $xslt->parse_stylesheet( $xslt_doc );
71 unless ($supported_formats{mods32}{xslt}) {
72 $log->debug("Loading MODS v32 XSLT", DEBUG);
73 my $xslt_doc = $parser->parse_file( $xsldir . "/MARC21slim2MODS32.xsl");
74 $supported_formats{mods32}{xslt} = $xslt->parse_stylesheet( $xslt_doc );
77 my $req = OpenSRF::AppSession
78 ->create('open-ils.cstore')
80 # XXX testing new metabib field use for faceting
81 #->request( 'open-ils.cstore.direct.config.metabib_field.search.atomic', { id => { '!=' => undef } } )
82 ->request( 'open-ils.cstore.direct.config.metabib_field.search.atomic', { search_field => 't' } )
86 if (ref $req and @$req) {
88 $xpathset->{ $f->field_class }->{ $f->name }->{xpath} = $f->xpath;
89 $xpathset->{ $f->field_class }->{ $f->name }->{id} = $f->id;
90 $xpathset->{ $f->field_class }->{ $f->name }->{format} = $f->format;
91 $log->debug("Loaded XPath from DB: ".$f->field_class." => ".$f->name." : ".$f->xpath, DEBUG);
102 $stuff = NFD($stuff);
104 $stuff = NFC($stuff);
107 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
111 # --------------------------------------------------------------------------------
114 package OpenILS::Application::Ingest::Biblio;
115 use base qw/OpenILS::Application::Ingest/;
116 use Unicode::Normalize;
118 sub rw_biblio_ingest_single_object {
123 my ($blob) = $self->method_lookup("open-ils.ingest.full.biblio.object.readonly")->run($bib);
124 return undef unless ($blob);
126 $bib->fingerprint( $blob->{fingerprint}->{fingerprint} );
127 $bib->quality( $blob->{fingerprint}->{quality} );
129 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
131 my $xact = $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
134 # update uri stuff ...
136 # gather URI call numbers for this record
137 my $uri_cns = $u->{call_number} = $cstore->request(
138 'open-ils.cstore.direct.asset.call_number.id_list.atomic' => { record => $bib->id, label => '##URI##' }
141 # gather the maps for those call numbers
142 my $uri_maps = $u->{call_number} = $cstore->request(
143 'open-ils.cstore.direct.asset.uri_call_number_map.id_list.atomic' => { call_number => $uri_cns }
146 # delete the old maps
147 $cstore->request( 'open-ils.cstore.direct.asset.uri_call_number_map.delete' => $_ )->gather(1) for (@$uri_maps);
149 # and delete the call numbers if there are no more URIs
150 if (!@{ $blob->{uri} }) {
151 $cstore->request( 'open-ils.cstore.direct.asset.call_number.delete' => $_ )->gather(1) for (@$uri_cns);
154 # now, add CNs, URIs and maps
155 my %new_cns_by_owner;
156 my %new_uris_by_owner;
157 for my $u ( @{ $blob->{uri} } ) {
159 my $owner = $u->{call_number}->owning_lib;
161 if ($u->{call_number}->isnew) {
162 if ($new_cns_by_owner{$owner}) {
163 $u->{call_number} = $new_cns_by_owner{$owner};
165 $u->{call_number}->clear_id;
166 $u->{call_number} = $new_cns_by_owner{$owner} = $cstore->request(
167 'open-ils.cstore.direct.asset.call_number.create' => $u->{call_number}
172 if ($u->{uri}->isnew) {
173 if ($new_uris_by_owner{$owner}) {
174 $u->{uri} = $new_uris_by_owner{$owner};
176 $u->{uri} = $new_uris_by_owner{$owner} = $cstore->request(
177 'open-ils.cstore.direct.asset.uri.create' => $u->{uri}
182 my $umap = Fieldmapper::asset::uri_call_number_map->new;
183 $umap->uri($u->{uri}->id);
184 $umap->call_number($u->{call_number}->id);
186 $cstore->request( 'open-ils.cstore.direct.asset.uri_call_number_map.create' => $umap )->gather(1) if (!$tmp);
189 # update full_rec stuff ...
190 $tmp = $cstore->request(
191 'open-ils.cstore.direct.metabib.full_rec.id_list.atomic',
192 { record => $bib->id }
195 $cstore->request( 'open-ils.cstore.direct.metabib.full_rec.delete' => $_ )->gather(1) for (@$tmp);
196 $cstore->request( 'open-ils.cstore.direct.metabib.full_rec.create' => $_ )->gather(1) for (@{ $blob->{full_rec} });
198 # update rec_descriptor stuff ...
199 $tmp = $cstore->request(
200 'open-ils.cstore.direct.metabib.record_descriptor.id_list.atomic',
201 { record => $bib->id }
204 $cstore->request( 'open-ils.cstore.direct.metabib.record_descriptor.delete' => $_ )->gather(1) for (@$tmp);
205 $cstore->request( 'open-ils.cstore.direct.metabib.record_descriptor.create' => $blob->{descriptor} )->gather(1);
207 # deal with classed fields...
208 for my $class ( qw/title author subject keyword series/ ) {
209 $tmp = $cstore->request(
210 "open-ils.cstore.direct.metabib.${class}_field_entry.id_list.atomic",
211 { source => $bib->id }
214 $cstore->request( "open-ils.cstore.direct.metabib.${class}_field_entry.delete" => $_ )->gather(1) for (@$tmp);
216 for my $obj ( @{ $blob->{field_entries} } ) {
217 my $class = $obj->class_name;
218 $class =~ s/^Fieldmapper:://o;
220 $cstore->request( "open-ils.cstore.direct.$class.create" => $obj )->gather(1);
225 $tmp = $cstore->request(
226 'open-ils.cstore.direct.metabib.metarecord_source_map.search.atomic',
227 { source => $bib->id }
230 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord_source_map.delete' => $_->id )->gather(1) for (@$tmp);
233 my $old_mrs = $cstore->request(
234 'open-ils.cstore.direct.metabib.metarecord.search.atomic' => { id => [map { $_->metarecord } @$tmp] }
235 )->gather(1) if (@$tmp);
237 $old_mrs = [] if (!ref($old_mrs));
240 for my $m (@$old_mrs) {
241 if ($m->fingerprint eq $bib->fingerprint) {
244 my $others = $cstore->request(
245 'open-ils.cstore.direct.metabib.metarecord_source_map.id_list.atomic' => { metarecord => $m->id }
250 'open-ils.cstore.direct.metabib.metarecord.delete' => $m->id
260 # Get the matchin MR, if any.
261 $mr = $cstore->request(
262 'open-ils.cstore.direct.metabib.metarecord.search',
263 { fingerprint => $bib->fingerprint }
266 $holds = $cstore->request(
267 'open-ils.cstore.direct.action.hold_request.search.atomic',
268 { hold_type => 'M', target => [ map { $_->id } grep { $_->isdeleted } @$old_mrs ] }
269 )->gather(1) if (@$old_mrs);
272 for my $h (@$holds) {
274 $cstore->request( 'open-ils.cstore.direct.action.hold_request.update' => $h )->gather(1);
281 $mr = new Fieldmapper::metabib::metarecord;
282 $mr->fingerprint( $bib->fingerprint );
283 $mr->master_record( $bib->id );
286 "open-ils.cstore.direct.metabib.metarecord.create",
287 $mr => { quiet => 'true' }
291 for my $h (grep { !$_->ischanged } @$holds) {
293 $cstore->request( 'open-ils.cstore.direct.action.hold_request.update' => $h )->gather(1);
296 my $mrm = $cstore->request(
297 'open-ils.cstore.direct.metabib.metarecord_source_map.search.atomic',
298 { metarecord => $mr->id }
302 my $best = $cstore->request(
303 "open-ils.cstore.direct.biblio.record_entry.search",
304 { id => [ map { $_->source } @$mrm ] },
305 { 'select' => { bre => [ qw/id quality/ ] },
306 order_by => { bre => "quality desc" },
311 if ($best->quality > $bib->quality) {
312 $mr->master_record($best->id);
314 $mr->master_record($bib->id);
317 $mr->master_record($bib->id);
322 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord.update' => $mr )->gather(1);
325 my $mrm = new Fieldmapper::metabib::metarecord_source_map;
326 $mrm->source($bib->id);
327 $mrm->metarecord($mr->id);
329 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord_source_map.create' => $mrm )->gather(1);
330 $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.update' => $bib )->gather(1);
332 $cstore->request( 'open-ils.cstore.transaction.commit' )->gather(1) || return undef;;
337 __PACKAGE__->register_method(
338 api_name => "open-ils.ingest.full.biblio.object",
339 method => "rw_biblio_ingest_single_object",
344 sub rw_biblio_ingest_single_record {
349 OpenILS::Application::Ingest->post_init();
350 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
351 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
353 my $r = $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )->gather(1);
355 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
358 return undef unless ($r and @$r);
360 return ($self->method_lookup("open-ils.ingest.full.biblio.object")->run($r))[0];
362 __PACKAGE__->register_method(
363 api_name => "open-ils.ingest.full.biblio.record",
364 method => "rw_biblio_ingest_single_record",
369 sub rw_biblio_ingest_record_list {
372 my @rec = ref($_[0]) ? @{ $_[0] } : @_ ;
374 OpenILS::Application::Ingest->post_init();
375 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
376 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
378 my $r = $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.search.atomic' => { id => $rec } )->gather(1);
380 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
383 return undef unless ($r and @$r);
386 $count += ($self->method_lookup("open-ils.ingest.full.biblio.object")->run($_))[0] for (@$r);
390 __PACKAGE__->register_method(
391 api_name => "open-ils.ingest.full.biblio.record_list",
392 method => "rw_biblio_ingest_record_list",
397 sub ro_biblio_ingest_single_object {
401 my $xml = OpenILS::Application::Ingest::entityize($bib->marc);
403 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
404 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1 } )->gather(1);
407 my $max_cn = int($cn->id) + 1000;
409 my $document = $parser->parse_string($xml);
411 my @uris = $self->method_lookup("open-ils.ingest.856_uri.object")->run($bib, $max_cn);
412 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
413 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
414 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
415 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
417 $_->source($bib->id) for (@mXfe);
418 $_->record($bib->id) for (@mfr);
419 $rd->record($bib->id) if ($rd);
421 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd, uri => \@uris };
423 __PACKAGE__->register_method(
424 api_name => "open-ils.ingest.full.biblio.object.readonly",
425 method => "ro_biblio_ingest_single_object",
430 sub ro_biblio_ingest_single_xml {
433 my $xml = OpenILS::Application::Ingest::entityize(shift);
435 my $document = $parser->parse_string($xml);
437 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
438 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
439 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
440 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
442 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd };
444 __PACKAGE__->register_method(
445 api_name => "open-ils.ingest.full.biblio.xml.readonly",
446 method => "ro_biblio_ingest_single_xml",
451 sub ro_biblio_ingest_single_record {
456 OpenILS::Application::Ingest->post_init();
457 my $r = OpenSRF::AppSession
458 ->create('open-ils.cstore')
459 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
462 return undef unless ($r and @$r);
464 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($r->marc);
466 $_->source($rec) for (@{$res->{field_entries}});
467 $_->record($rec) for (@{$res->{full_rec}});
468 $res->{descriptor}->record($rec);
472 __PACKAGE__->register_method(
473 api_name => "open-ils.ingest.full.biblio.record.readonly",
474 method => "ro_biblio_ingest_single_record",
479 sub ro_biblio_ingest_stream_record {
483 OpenILS::Application::Ingest->post_init();
485 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
487 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
489 my $rec = $resp->content;
490 last unless (defined $rec);
492 $log->debug("Running open-ils.ingest.full.biblio.record.readonly ...");
493 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.record.readonly")->run($rec);
495 $_->source($rec) for (@{$res->{field_entries}});
496 $_->record($rec) for (@{$res->{full_rec}});
498 $client->respond( $res );
503 __PACKAGE__->register_method(
504 api_name => "open-ils.ingest.full.biblio.record_stream.readonly",
505 method => "ro_biblio_ingest_stream_record",
510 sub ro_biblio_ingest_stream_xml {
514 OpenILS::Application::Ingest->post_init();
516 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
518 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
520 my $xml = $resp->content;
521 last unless (defined $xml);
523 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
524 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($xml);
526 $client->respond( $res );
531 __PACKAGE__->register_method(
532 api_name => "open-ils.ingest.full.biblio.xml_stream.readonly",
533 method => "ro_biblio_ingest_stream_xml",
538 sub rw_biblio_ingest_stream_import {
542 OpenILS::Application::Ingest->post_init();
544 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
546 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
548 my $bib = $resp->content;
549 last unless (defined $bib);
551 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
552 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($bib->marc);
554 $_->source($bib->id) for (@{$res->{field_entries}});
555 $_->record($bib->id) for (@{$res->{full_rec}});
557 $client->respond( $res );
562 __PACKAGE__->register_method(
563 api_name => "open-ils.ingest.full.biblio.bib_stream.import",
564 method => "rw_biblio_ingest_stream_import",
570 # --------------------------------------------------------------------------------
573 package OpenILS::Application::Ingest::Authority;
574 use base qw/OpenILS::Application::Ingest/;
575 use Unicode::Normalize;
577 sub ro_authority_ingest_single_object {
581 my $xml = OpenILS::Application::Ingest::entityize($bib->marc);
583 my $document = $parser->parse_string($xml);
585 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
587 $_->record($bib->id) for (@mfr);
589 return { full_rec => \@mfr };
591 __PACKAGE__->register_method(
592 api_name => "open-ils.ingest.full.authority.object.readonly",
593 method => "ro_authority_ingest_single_object",
598 sub ro_authority_ingest_single_xml {
601 my $xml = OpenILS::Application::Ingest::entityize(shift);
603 my $document = $parser->parse_string($xml);
605 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
607 return { full_rec => \@mfr };
609 __PACKAGE__->register_method(
610 api_name => "open-ils.ingest.full.authority.xml.readonly",
611 method => "ro_authority_ingest_single_xml",
616 sub ro_authority_ingest_single_record {
621 OpenILS::Application::Ingest->post_init();
622 my $r = OpenSRF::AppSession
623 ->create('open-ils.cstore')
624 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
627 return undef unless ($r and @$r);
629 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($r->marc);
631 $_->record($rec) for (@{$res->{full_rec}});
632 $res->{descriptor}->record($rec);
636 __PACKAGE__->register_method(
637 api_name => "open-ils.ingest.full.authority.record.readonly",
638 method => "ro_authority_ingest_single_record",
643 sub ro_authority_ingest_stream_record {
647 OpenILS::Application::Ingest->post_init();
649 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
651 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
653 my $rec = $resp->content;
654 last unless (defined $rec);
656 $log->debug("Running open-ils.ingest.full.authority.record.readonly ...");
657 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.record.readonly")->run($rec);
659 $_->record($rec) for (@{$res->{full_rec}});
661 $client->respond( $res );
666 __PACKAGE__->register_method(
667 api_name => "open-ils.ingest.full.authority.record_stream.readonly",
668 method => "ro_authority_ingest_stream_record",
673 sub ro_authority_ingest_stream_xml {
677 OpenILS::Application::Ingest->post_init();
679 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
681 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
683 my $xml = $resp->content;
684 last unless (defined $xml);
686 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
687 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($xml);
689 $client->respond( $res );
694 __PACKAGE__->register_method(
695 api_name => "open-ils.ingest.full.authority.xml_stream.readonly",
696 method => "ro_authority_ingest_stream_xml",
701 sub rw_authority_ingest_stream_import {
705 OpenILS::Application::Ingest->post_init();
707 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
709 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
711 my $bib = $resp->content;
712 last unless (defined $bib);
714 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
715 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($bib->marc);
717 $_->record($bib->id) for (@{$res->{full_rec}});
719 $client->respond( $res );
724 __PACKAGE__->register_method(
725 api_name => "open-ils.ingest.full.authority.bib_stream.import",
726 method => "rw_authority_ingest_stream_import",
732 # --------------------------------------------------------------------------------
733 # MARC index extraction
735 package OpenILS::Application::Ingest::XPATH;
736 use base qw/OpenILS::Application::Ingest/;
737 use Unicode::Normalize;
739 # give this an XML documentElement and an XPATH expression
740 sub xpath_to_string {
744 my $ns_prefix = shift;
747 $xml->setNamespace( $ns_uri, $ns_prefix, 1 ) if ($ns_uri && $ns_prefix);
751 # grab the set of matching nodes
752 my @nodes = $xml->findnodes( $xpath );
753 for my $value (@nodes) {
755 # grab all children of the node
756 my @children = $value->childNodes();
757 for my $child (@children) {
759 # add the childs content to the growing buffer
760 my $content = quotemeta($child->textContent);
761 next if ($unique && $string =~ /$content/); # uniquify the values
762 $string .= $child->textContent . " ";
765 $string .= $value->textContent . " ";
769 $string =~ s/(\w+)\/(\w+)/$1 $2/sgo;
770 $string =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
775 sub class_index_string_xml {
781 OpenILS::Application::Ingest->post_init();
782 $xml = $parser->parse_string(OpenILS::Application::Ingest::entityize($xml)) unless (ref $xml);
786 for my $class (@classes) {
787 my $class_constructor = "Fieldmapper::metabib::${class}_field_entry";
788 for my $type ( keys %{ $xpathset->{$class} } ) {
790 my $def = $xpathset->{$class}->{$type};
791 my $sf = $OpenILS::Application::Ingest::supported_formats{$def->{format}};
796 $document = $transform_cache{$def->{format}} || $sf->{xslt}->transform($xml);
797 $transform_cache{$def->{format}} = $document;
800 my $value = xpath_to_string(
801 $document->documentElement => $def->{xpath},
802 $sf->{ns} => $def->{format},
808 $value = NFD($value);
809 $value =~ s/\pM+//sgo;
810 $value =~ s/\pC+//sgo;
811 $value =~ s/\W+$//sgo;
813 $value =~ s/\b\.+\b//sgo;
816 my $fm = $class_constructor->new;
817 $fm->value( $value );
818 $fm->field( $xpathset->{$class}->{$type}->{id} );
819 $client->respond($fm);
824 __PACKAGE__->register_method(
825 api_name => "open-ils.ingest.field_entry.class.xml",
826 method => "class_index_string_xml",
832 sub class_index_string_record {
838 OpenILS::Application::Ingest->post_init();
839 my $r = OpenSRF::AppSession
840 ->create('open-ils.cstore')
841 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
844 return undef unless ($r and @$r);
846 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, @classes)) {
848 $client->respond($fm);
852 __PACKAGE__->register_method(
853 api_name => "open-ils.ingest.field_entry.class.record",
854 method => "class_index_string_record",
860 sub all_index_string_xml {
865 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($xml, keys(%$xpathset))) {
866 $client->respond($fm);
870 __PACKAGE__->register_method(
871 api_name => "open-ils.ingest.extract.field_entry.all.xml",
872 method => "all_index_string_xml",
878 sub all_index_string_record {
883 OpenILS::Application::Ingest->post_init();
884 my $r = OpenSRF::AppSession
885 ->create('open-ils.cstore')
886 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
889 return undef unless ($r and @$r);
891 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, keys(%$xpathset))) {
893 $client->respond($fm);
897 __PACKAGE__->register_method(
898 api_name => "open-ils.ingest.extract.field_entry.all.record",
899 method => "all_index_string_record",
905 # --------------------------------------------------------------------------------
908 package OpenILS::Application::Ingest::FlatMARC;
909 use base qw/OpenILS::Application::Ingest/;
910 use Unicode::Normalize;
913 sub _marcxml_to_full_rows {
916 my $xmltype = shift || 'metabib';
918 my $type = "Fieldmapper::${xmltype}::full_rec";
922 my ($root) = $marcxml->findnodes('//*[local-name()="record"]');
924 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
925 next unless $tagline;
930 my $val = $tagline->textContent;
940 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
941 next unless $tagline;
945 $ns->tag( $tagline->getAttribute( "tag" ) );
946 my $val = $tagline->textContent;
956 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
957 next unless $tagline;
959 my $tag = $tagline->getAttribute( "tag" );
960 my $ind1 = $tagline->getAttribute( "ind1" );
961 my $ind2 = $tagline->getAttribute( "ind2" );
963 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
971 $ns->subfield( $data->getAttribute( "code" ) );
972 my $val = $data->textContent;
977 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
978 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
979 $ns->value( lc($val) );
984 if ($xmltype eq 'metabib' and $tag eq '245') {
987 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
988 next unless ($data and $data->getAttribute( "code" ) eq 'a');
995 $ns->subfield( $data->getAttribute( "code" ) );
996 my $val = substr( $data->textContent, $ind2 );
1000 $val =~ s/\W+$//sgo;
1001 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1002 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1003 $ns->value( lc($val) );
1010 $log->debug("Returning ".scalar(@ns_list)." Fieldmapper nodes from $xmltype xml");
1019 $log->debug("processing [$xml]");
1021 $xml = $parser->parse_string(OpenILS::Application::Ingest::entityize($xml)) unless (ref $xml);
1023 my $type = 'metabib';
1024 $type = 'authority' if ($self->api_name =~ /authority/o);
1026 OpenILS::Application::Ingest->post_init();
1028 $client->respond($_) for (_marcxml_to_full_rows($xml, $type));
1031 __PACKAGE__->register_method(
1032 api_name => "open-ils.ingest.flat_marc.authority.xml",
1033 method => "flat_marc_xml",
1038 __PACKAGE__->register_method(
1039 api_name => "open-ils.ingest.flat_marc.biblio.xml",
1040 method => "flat_marc_xml",
1046 sub flat_marc_record {
1051 my $type = 'biblio';
1052 $type = 'authority' if ($self->api_name =~ /authority/o);
1054 OpenILS::Application::Ingest->post_init();
1055 my $r = OpenSRF::AppSession
1056 ->create('open-ils.cstore')
1057 ->request( "open-ils.cstore.direct.${type}.record_entry.retrieve" => $rec )
1061 return undef unless ($r and $r->marc);
1063 my @rows = $self->method_lookup("open-ils.ingest.flat_marc.$type.xml")->run($r->marc);
1064 for my $row (@rows) {
1065 $client->respond($row);
1066 $log->debug(OpenSRF::Utils::JSON->perl2JSON($row), DEBUG);
1070 __PACKAGE__->register_method(
1071 api_name => "open-ils.ingest.flat_marc.biblio.record_entry",
1072 method => "flat_marc_record",
1077 __PACKAGE__->register_method(
1078 api_name => "open-ils.ingest.flat_marc.authority.record_entry",
1079 method => "flat_marc_record",
1086 # --------------------------------------------------------------------------------
1089 package OpenILS::Application::Ingest::Biblio::URI;
1090 use base qw/OpenILS::Application::Ingest/;
1091 use Unicode::Normalize;
1092 use OpenSRF::EX qw/:try/;
1095 sub _extract_856_uris {
1098 my $marcxml = shift;
1102 my $document = $parser->parse_string($marcxml);
1103 my @nodes = $document->findnodes('//*[local-name()="datafield" and @tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]');
1105 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
1107 for my $node (@nodes) {
1108 # first, is there a URI?
1109 my $href = $node->findvalue('*[local-name()="subfield" and @code="u"]/text()');
1110 next unless ($href);
1112 # now, find the best possible label
1113 my $label = $node->findvalue('*[local-name()="subfield" and @code="y"]/text()');
1114 $label ||= $node->findvalue('*[local-name()="subfield" and @code="3"]/text()');
1118 my $use = $node->findvalue('*[local-name()="subfield" and @code="z"]/text()');
1119 $use ||= $node->findvalue('*[local-name()="subfield" and @code="2"]/text()');
1120 $use ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1122 # moving on to the URI owner
1123 my $owner = $node->findvalue('*[local-name()="subfield" and @code="w"]/text()');
1124 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1125 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="9"]/text()'); # Evergreen special sauce
1127 $owner =~ s/^.*?\((\w+)\).*$/$1/o; # unwrap first paren-enclosed string and then ...
1129 # no owner? skip it :(
1130 next unless ($owner);
1133 ->request( 'open-ils.cstore.direct.actor.org_unit.search' => { shortname => $owner} )
1138 # now we can construct the uri object
1140 ->request( 'open-ils.cstore.direct.asset.uri.search' => { label => $label, href => $href, use_restriction => $use, active => 't' } )
1144 $uri = Fieldmapper::asset::uri->new;
1146 $uri->label($label);
1148 $uri->use_restriction($use);
1151 # see if we need to create a call number
1153 ->request( 'open-ils.cstore.direct.asset.call_number.search' => { owning_lib => $org->id, record => $recid, label => '##URI##' } )
1157 $cn = Fieldmapper::asset::call_number->new;
1159 $cn->id( $$max_cn++ );
1160 $cn->owning_lib( $org->id );
1161 $cn->record( $recid );
1162 $cn->label( '##URI##' );
1165 push @objects, { uri => $uri, call_number => $cn };
1168 $log->debug("Returning ".scalar(@objects)." URI nodes for record $recid");
1172 sub get_uris_record {
1177 OpenILS::Application::Ingest->post_init();
1178 my $r = OpenSRF::AppSession
1179 ->create('open-ils.cstore')
1180 ->request( "open-ils.cstore.direct.biblio.record_entry.retrieve" => $rec )
1183 return undef unless ($r and $r->marc);
1185 $client->respond($_) for (_extract_856_uris($r->id, $r->marc));
1188 __PACKAGE__->register_method(
1189 api_name => "open-ils.ingest.856_uri.record",
1190 method => "get_uris_record",
1196 sub get_uris_object {
1202 return undef unless ($obj and $obj->marc);
1204 $client->respond($_) for (_extract_856_uris($obj->id, $obj->marc, \$max_cn));
1207 __PACKAGE__->register_method(
1208 api_name => "open-ils.ingest.856_uri.object",
1209 method => "get_uris_object",
1216 # --------------------------------------------------------------------------------
1219 package OpenILS::Application::Ingest::Biblio::Fingerprint;
1220 use base qw/OpenILS::Application::Ingest/;
1221 use Unicode::Normalize;
1222 use OpenSRF::EX qw/:try/;
1224 sub biblio_fingerprint_record {
1229 OpenILS::Application::Ingest->post_init();
1231 my $r = OpenSRF::AppSession
1232 ->create('open-ils.cstore')
1233 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1236 return undef unless ($r and $r->marc);
1238 my ($fp) = $self->method_lookup('open-ils.ingest.fingerprint.xml')->run($r->marc);
1239 $log->debug("Returning [$fp] as fingerprint for record $rec", INFO);
1240 $fp->{quality} = int($fp->{quality});
1243 __PACKAGE__->register_method(
1244 api_name => "open-ils.ingest.fingerprint.record",
1245 method => "biblio_fingerprint_record",
1251 sub biblio_fingerprint {
1254 my $xml = OpenILS::Application::Ingest::entityize(shift);
1256 $log->internal("Got MARC [$xml]");
1259 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1260 my $conf = OpenSRF::Utils::SettingsClient->new;
1262 my $libs = $conf->config_value(@pfx, 'script_path');
1263 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_fingerprint');
1264 my $script_libs = (ref($libs)) ? $libs : [$libs];
1266 $log->debug("Loading script $script_file for biblio fingerprinting...");
1268 $fp_script = new OpenILS::Utils::ScriptRunner
1269 ( file => $script_file,
1270 paths => $script_libs,
1271 reset_count => 100 );
1274 $fp_script->insert('environment' => {marc => $xml} => 1);
1276 my $res = $fp_script->run || ($log->error( "Fingerprint script died! $@" ) && return undef);
1277 $log->debug("Script for biblio fingerprinting completed successfully...");
1281 __PACKAGE__->register_method(
1282 api_name => "open-ils.ingest.fingerprint.xml",
1283 method => "biblio_fingerprint",
1289 sub biblio_descriptor {
1292 my $xml = OpenILS::Application::Ingest::entityize(shift);
1294 $log->internal("Got MARC [$xml]");
1297 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1298 my $conf = OpenSRF::Utils::SettingsClient->new;
1300 my $libs = $conf->config_value(@pfx, 'script_path');
1301 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_descriptor');
1302 my $script_libs = (ref($libs)) ? $libs : [$libs];
1304 $log->debug("Loading script $script_file for biblio descriptor extraction...");
1306 $rd_script = new OpenILS::Utils::ScriptRunner
1307 ( file => $script_file,
1308 paths => $script_libs,
1309 reset_count => 100 );
1312 $log->debug("Setting up environment for descriptor extraction script...");
1313 $rd_script->insert('environment.marc' => $xml => 1);
1314 $log->debug("Environment building complete...");
1316 my $res = $rd_script->run || ($log->error( "Descriptor script died! $@" ) && return undef);
1317 $log->debug("Script for biblio descriptor extraction completed successfully");
1319 my $d1 = $res->date1;
1320 if ($d1 && $d1 ne ' ') {
1325 my $d2 = $res->date2;
1326 if ($d2 && $d2 ne ' ') {
1333 __PACKAGE__->register_method(
1334 api_name => "open-ils.ingest.descriptor.xml",
1335 method => "biblio_descriptor",