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 $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef }, { limit => 1 } } )->gather(1);
404 my $max_cn = int($cn->id) + 1000;
406 my $document = $parser->parse_string($xml);
408 my @uris = $self->method_lookup("open-ils.ingest.856_uri.object")->run($bib, $max_cn);
409 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
410 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
411 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
412 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
414 $_->source($bib->id) for (@mXfe);
415 $_->record($bib->id) for (@mfr);
416 $rd->record($bib->id) if ($rd);
418 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd, uri => \@uris };
420 __PACKAGE__->register_method(
421 api_name => "open-ils.ingest.full.biblio.object.readonly",
422 method => "ro_biblio_ingest_single_object",
427 sub ro_biblio_ingest_single_xml {
430 my $xml = OpenILS::Application::Ingest::entityize(shift);
432 my $document = $parser->parse_string($xml);
434 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
435 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
436 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
437 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
439 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd };
441 __PACKAGE__->register_method(
442 api_name => "open-ils.ingest.full.biblio.xml.readonly",
443 method => "ro_biblio_ingest_single_xml",
448 sub ro_biblio_ingest_single_record {
453 OpenILS::Application::Ingest->post_init();
454 my $r = OpenSRF::AppSession
455 ->create('open-ils.cstore')
456 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
459 return undef unless ($r and @$r);
461 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($r->marc);
463 $_->source($rec) for (@{$res->{field_entries}});
464 $_->record($rec) for (@{$res->{full_rec}});
465 $res->{descriptor}->record($rec);
469 __PACKAGE__->register_method(
470 api_name => "open-ils.ingest.full.biblio.record.readonly",
471 method => "ro_biblio_ingest_single_record",
476 sub ro_biblio_ingest_stream_record {
480 OpenILS::Application::Ingest->post_init();
482 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
484 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
486 my $rec = $resp->content;
487 last unless (defined $rec);
489 $log->debug("Running open-ils.ingest.full.biblio.record.readonly ...");
490 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.record.readonly")->run($rec);
492 $_->source($rec) for (@{$res->{field_entries}});
493 $_->record($rec) for (@{$res->{full_rec}});
495 $client->respond( $res );
500 __PACKAGE__->register_method(
501 api_name => "open-ils.ingest.full.biblio.record_stream.readonly",
502 method => "ro_biblio_ingest_stream_record",
507 sub ro_biblio_ingest_stream_xml {
511 OpenILS::Application::Ingest->post_init();
513 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
515 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
517 my $xml = $resp->content;
518 last unless (defined $xml);
520 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
521 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($xml);
523 $client->respond( $res );
528 __PACKAGE__->register_method(
529 api_name => "open-ils.ingest.full.biblio.xml_stream.readonly",
530 method => "ro_biblio_ingest_stream_xml",
535 sub rw_biblio_ingest_stream_import {
539 OpenILS::Application::Ingest->post_init();
541 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
543 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
545 my $bib = $resp->content;
546 last unless (defined $bib);
548 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
549 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($bib->marc);
551 $_->source($bib->id) for (@{$res->{field_entries}});
552 $_->record($bib->id) for (@{$res->{full_rec}});
554 $client->respond( $res );
559 __PACKAGE__->register_method(
560 api_name => "open-ils.ingest.full.biblio.bib_stream.import",
561 method => "rw_biblio_ingest_stream_import",
567 # --------------------------------------------------------------------------------
570 package OpenILS::Application::Ingest::Authority;
571 use base qw/OpenILS::Application::Ingest/;
572 use Unicode::Normalize;
574 sub ro_authority_ingest_single_object {
578 my $xml = OpenILS::Application::Ingest::entityize($bib->marc);
580 my $document = $parser->parse_string($xml);
582 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
584 $_->record($bib->id) for (@mfr);
586 return { full_rec => \@mfr };
588 __PACKAGE__->register_method(
589 api_name => "open-ils.ingest.full.authority.object.readonly",
590 method => "ro_authority_ingest_single_object",
595 sub ro_authority_ingest_single_xml {
598 my $xml = OpenILS::Application::Ingest::entityize(shift);
600 my $document = $parser->parse_string($xml);
602 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
604 return { full_rec => \@mfr };
606 __PACKAGE__->register_method(
607 api_name => "open-ils.ingest.full.authority.xml.readonly",
608 method => "ro_authority_ingest_single_xml",
613 sub ro_authority_ingest_single_record {
618 OpenILS::Application::Ingest->post_init();
619 my $r = OpenSRF::AppSession
620 ->create('open-ils.cstore')
621 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
624 return undef unless ($r and @$r);
626 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($r->marc);
628 $_->record($rec) for (@{$res->{full_rec}});
629 $res->{descriptor}->record($rec);
633 __PACKAGE__->register_method(
634 api_name => "open-ils.ingest.full.authority.record.readonly",
635 method => "ro_authority_ingest_single_record",
640 sub ro_authority_ingest_stream_record {
644 OpenILS::Application::Ingest->post_init();
646 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
648 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
650 my $rec = $resp->content;
651 last unless (defined $rec);
653 $log->debug("Running open-ils.ingest.full.authority.record.readonly ...");
654 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.record.readonly")->run($rec);
656 $_->record($rec) for (@{$res->{full_rec}});
658 $client->respond( $res );
663 __PACKAGE__->register_method(
664 api_name => "open-ils.ingest.full.authority.record_stream.readonly",
665 method => "ro_authority_ingest_stream_record",
670 sub ro_authority_ingest_stream_xml {
674 OpenILS::Application::Ingest->post_init();
676 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
678 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
680 my $xml = $resp->content;
681 last unless (defined $xml);
683 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
684 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($xml);
686 $client->respond( $res );
691 __PACKAGE__->register_method(
692 api_name => "open-ils.ingest.full.authority.xml_stream.readonly",
693 method => "ro_authority_ingest_stream_xml",
698 sub rw_authority_ingest_stream_import {
702 OpenILS::Application::Ingest->post_init();
704 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
706 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
708 my $bib = $resp->content;
709 last unless (defined $bib);
711 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
712 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($bib->marc);
714 $_->record($bib->id) for (@{$res->{full_rec}});
716 $client->respond( $res );
721 __PACKAGE__->register_method(
722 api_name => "open-ils.ingest.full.authority.bib_stream.import",
723 method => "rw_authority_ingest_stream_import",
729 # --------------------------------------------------------------------------------
730 # MARC index extraction
732 package OpenILS::Application::Ingest::XPATH;
733 use base qw/OpenILS::Application::Ingest/;
734 use Unicode::Normalize;
736 # give this an XML documentElement and an XPATH expression
737 sub xpath_to_string {
741 my $ns_prefix = shift;
744 $xml->setNamespace( $ns_uri, $ns_prefix, 1 ) if ($ns_uri && $ns_prefix);
748 # grab the set of matching nodes
749 my @nodes = $xml->findnodes( $xpath );
750 for my $value (@nodes) {
752 # grab all children of the node
753 my @children = $value->childNodes();
754 for my $child (@children) {
756 # add the childs content to the growing buffer
757 my $content = quotemeta($child->textContent);
758 next if ($unique && $string =~ /$content/); # uniquify the values
759 $string .= $child->textContent . " ";
762 $string .= $value->textContent . " ";
766 $string =~ s/(\w+)\/(\w+)/$1 $2/sgo;
767 $string =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
772 sub class_index_string_xml {
778 OpenILS::Application::Ingest->post_init();
779 $xml = $parser->parse_string(OpenILS::Application::Ingest::entityize($xml)) unless (ref $xml);
783 for my $class (@classes) {
784 my $class_constructor = "Fieldmapper::metabib::${class}_field_entry";
785 for my $type ( keys %{ $xpathset->{$class} } ) {
787 my $def = $xpathset->{$class}->{$type};
788 my $sf = $OpenILS::Application::Ingest::supported_formats{$def->{format}};
793 $document = $transform_cache{$def->{format}} || $sf->{xslt}->transform($xml);
794 $transform_cache{$def->{format}} = $document;
797 my $value = xpath_to_string(
798 $document->documentElement => $def->{xpath},
799 $sf->{ns} => $def->{format},
805 $value = NFD($value);
806 $value =~ s/\pM+//sgo;
807 $value =~ s/\pC+//sgo;
808 $value =~ s/\W+$//sgo;
810 $value =~ s/\b\.+\b//sgo;
813 my $fm = $class_constructor->new;
814 $fm->value( $value );
815 $fm->field( $xpathset->{$class}->{$type}->{id} );
816 $client->respond($fm);
821 __PACKAGE__->register_method(
822 api_name => "open-ils.ingest.field_entry.class.xml",
823 method => "class_index_string_xml",
829 sub class_index_string_record {
835 OpenILS::Application::Ingest->post_init();
836 my $r = OpenSRF::AppSession
837 ->create('open-ils.cstore')
838 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
841 return undef unless ($r and @$r);
843 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, @classes)) {
845 $client->respond($fm);
849 __PACKAGE__->register_method(
850 api_name => "open-ils.ingest.field_entry.class.record",
851 method => "class_index_string_record",
857 sub all_index_string_xml {
862 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($xml, keys(%$xpathset))) {
863 $client->respond($fm);
867 __PACKAGE__->register_method(
868 api_name => "open-ils.ingest.extract.field_entry.all.xml",
869 method => "all_index_string_xml",
875 sub all_index_string_record {
880 OpenILS::Application::Ingest->post_init();
881 my $r = OpenSRF::AppSession
882 ->create('open-ils.cstore')
883 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
886 return undef unless ($r and @$r);
888 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, keys(%$xpathset))) {
890 $client->respond($fm);
894 __PACKAGE__->register_method(
895 api_name => "open-ils.ingest.extract.field_entry.all.record",
896 method => "all_index_string_record",
902 # --------------------------------------------------------------------------------
905 package OpenILS::Application::Ingest::FlatMARC;
906 use base qw/OpenILS::Application::Ingest/;
907 use Unicode::Normalize;
910 sub _marcxml_to_full_rows {
913 my $xmltype = shift || 'metabib';
915 my $type = "Fieldmapper::${xmltype}::full_rec";
919 my ($root) = $marcxml->findnodes('//*[local-name()="record"]');
921 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
922 next unless $tagline;
927 my $val = $tagline->textContent;
937 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
938 next unless $tagline;
942 $ns->tag( $tagline->getAttribute( "tag" ) );
943 my $val = $tagline->textContent;
953 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
954 next unless $tagline;
956 my $tag = $tagline->getAttribute( "tag" );
957 my $ind1 = $tagline->getAttribute( "ind1" );
958 my $ind2 = $tagline->getAttribute( "ind2" );
960 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
968 $ns->subfield( $data->getAttribute( "code" ) );
969 my $val = $data->textContent;
974 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
975 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
976 $ns->value( lc($val) );
981 if ($xmltype eq 'metabib' and $tag eq '245') {
984 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
985 next unless ($data and $data->getAttribute( "code" ) eq 'a');
992 $ns->subfield( $data->getAttribute( "code" ) );
993 my $val = substr( $data->textContent, $ind2 );
998 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
999 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1000 $ns->value( lc($val) );
1007 $log->debug("Returning ".scalar(@ns_list)." Fieldmapper nodes from $xmltype xml");
1016 $log->debug("processing [$xml]");
1018 $xml = $parser->parse_string(OpenILS::Application::Ingest::entityize($xml)) unless (ref $xml);
1020 my $type = 'metabib';
1021 $type = 'authority' if ($self->api_name =~ /authority/o);
1023 OpenILS::Application::Ingest->post_init();
1025 $client->respond($_) for (_marcxml_to_full_rows($xml, $type));
1028 __PACKAGE__->register_method(
1029 api_name => "open-ils.ingest.flat_marc.authority.xml",
1030 method => "flat_marc_xml",
1035 __PACKAGE__->register_method(
1036 api_name => "open-ils.ingest.flat_marc.biblio.xml",
1037 method => "flat_marc_xml",
1043 sub flat_marc_record {
1048 my $type = 'biblio';
1049 $type = 'authority' if ($self->api_name =~ /authority/o);
1051 OpenILS::Application::Ingest->post_init();
1052 my $r = OpenSRF::AppSession
1053 ->create('open-ils.cstore')
1054 ->request( "open-ils.cstore.direct.${type}.record_entry.retrieve" => $rec )
1058 return undef unless ($r and $r->marc);
1060 my @rows = $self->method_lookup("open-ils.ingest.flat_marc.$type.xml")->run($r->marc);
1061 for my $row (@rows) {
1062 $client->respond($row);
1063 $log->debug(OpenSRF::Utils::JSON->perl2JSON($row), DEBUG);
1067 __PACKAGE__->register_method(
1068 api_name => "open-ils.ingest.flat_marc.biblio.record_entry",
1069 method => "flat_marc_record",
1074 __PACKAGE__->register_method(
1075 api_name => "open-ils.ingest.flat_marc.authority.record_entry",
1076 method => "flat_marc_record",
1083 # --------------------------------------------------------------------------------
1086 package OpenILS::Application::Ingest::Biblio::URI;
1087 use base qw/OpenILS::Application::Ingest/;
1088 use Unicode::Normalize;
1089 use OpenSRF::EX qw/:try/;
1092 sub _extract_856_uris {
1095 my $marcxml = shift;
1099 my $document = $parser->parse_string($marcxml);
1100 my @nodes = $document->findnodes('//*[local-name()="datafield" and @tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]');
1102 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
1104 for my $node (@nodes) {
1105 # first, is there a URI?
1106 my $href = $node->findvalue('*[local-name()="subfield" and @code="u"]/text()');
1107 next unless ($href);
1109 # now, find the best possible label
1110 my $label = $node->findvalue('*[local-name()="subfield" and @code="y"]/text()');
1111 $label ||= $node->findvalue('*[local-name()="subfield" and @code="3"]/text()');
1115 my $use = $node->findvalue('*[local-name()="subfield" and @code="z"]/text()');
1116 $use ||= $node->findvalue('*[local-name()="subfield" and @code="2"]/text()');
1117 $use ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1119 # moving on to the URI owner
1120 my $owner = $node->findvalue('*[local-name()="subfield" and @code="w"]/text()');
1121 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1122 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="9"]/text()'); # Evergreen special sauce
1124 $owner =~ s/^.*?\((\w+)\).*$/$1/o; # unwrap first paren-enclosed string and then ...
1126 # no owner? skip it :(
1127 next unless ($owner);
1130 ->request( 'open-ils.cstore.direct.actor.org_unit.search' => { shortname => $owner} )
1135 # now we can construct the uri object
1137 ->request( 'open-ils.cstore.direct.asset.uri.search' => { label => $label, href => $href, use_restriction => $use, active => 't' } )
1141 $uri = Fieldmapper::asset::uri->new;
1143 $uri->label($label);
1145 $uri->use_restriction($use);
1148 # see if we need to create a call number
1150 ->request( 'open-ils.cstore.direct.asset.call_number.search' => { owning_lib => $org->id, record => $recid, label => '##URI##' } )
1154 $cn = Fieldmapper::asset::call_number->new;
1156 $cn->id( $$max_cn++ );
1157 $cn->owning_lib( $org->id );
1158 $cn->record( $recid );
1159 $cn->label( '##URI##' );
1162 push @objects, { uri => $uri, call_number => $cn };
1165 $log->debug("Returning ".scalar(@objects)." URI nodes for record $recid");
1169 sub get_uris_record {
1174 OpenILS::Application::Ingest->post_init();
1175 my $r = OpenSRF::AppSession
1176 ->create('open-ils.cstore')
1177 ->request( "open-ils.cstore.direct.biblio.record_entry.retrieve" => $rec )
1180 return undef unless ($r and $r->marc);
1182 $client->respond($_) for (_extract_856_uris($r->id, $r->marc));
1185 __PACKAGE__->register_method(
1186 api_name => "open-ils.ingest.856_uri.record",
1187 method => "get_uris_record",
1193 sub get_uris_object {
1199 return undef unless ($obj and $obj->marc);
1201 $client->respond($_) for (_extract_856_uris($obj->id, $obj->marc, \$max_cn));
1204 __PACKAGE__->register_method(
1205 api_name => "open-ils.ingest.856_uri.object",
1206 method => "get_uris_object",
1213 # --------------------------------------------------------------------------------
1216 package OpenILS::Application::Ingest::Biblio::Fingerprint;
1217 use base qw/OpenILS::Application::Ingest/;
1218 use Unicode::Normalize;
1219 use OpenSRF::EX qw/:try/;
1221 sub biblio_fingerprint_record {
1226 OpenILS::Application::Ingest->post_init();
1228 my $r = OpenSRF::AppSession
1229 ->create('open-ils.cstore')
1230 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1233 return undef unless ($r and $r->marc);
1235 my ($fp) = $self->method_lookup('open-ils.ingest.fingerprint.xml')->run($r->marc);
1236 $log->debug("Returning [$fp] as fingerprint for record $rec", INFO);
1237 $fp->{quality} = int($fp->{quality});
1240 __PACKAGE__->register_method(
1241 api_name => "open-ils.ingest.fingerprint.record",
1242 method => "biblio_fingerprint_record",
1248 sub biblio_fingerprint {
1251 my $xml = OpenILS::Application::Ingest::entityize(shift);
1253 $log->internal("Got MARC [$xml]");
1256 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1257 my $conf = OpenSRF::Utils::SettingsClient->new;
1259 my $libs = $conf->config_value(@pfx, 'script_path');
1260 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_fingerprint');
1261 my $script_libs = (ref($libs)) ? $libs : [$libs];
1263 $log->debug("Loading script $script_file for biblio fingerprinting...");
1265 $fp_script = new OpenILS::Utils::ScriptRunner
1266 ( file => $script_file,
1267 paths => $script_libs,
1268 reset_count => 100 );
1271 $fp_script->insert('environment' => {marc => $xml} => 1);
1273 my $res = $fp_script->run || ($log->error( "Fingerprint script died! $@" ) && return undef);
1274 $log->debug("Script for biblio fingerprinting completed successfully...");
1278 __PACKAGE__->register_method(
1279 api_name => "open-ils.ingest.fingerprint.xml",
1280 method => "biblio_fingerprint",
1286 sub biblio_descriptor {
1289 my $xml = OpenILS::Application::Ingest::entityize(shift);
1291 $log->internal("Got MARC [$xml]");
1294 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1295 my $conf = OpenSRF::Utils::SettingsClient->new;
1297 my $libs = $conf->config_value(@pfx, 'script_path');
1298 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_descriptor');
1299 my $script_libs = (ref($libs)) ? $libs : [$libs];
1301 $log->debug("Loading script $script_file for biblio descriptor extraction...");
1303 $rd_script = new OpenILS::Utils::ScriptRunner
1304 ( file => $script_file,
1305 paths => $script_libs,
1306 reset_count => 100 );
1309 $log->debug("Setting up environment for descriptor extraction script...");
1310 $rd_script->insert('environment.marc' => $xml => 1);
1311 $log->debug("Environment building complete...");
1313 my $res = $rd_script->run || ($log->error( "Descriptor script died! $@" ) && return undef);
1314 $log->debug("Script for biblio descriptor extraction completed successfully");
1316 my $d1 = $res->date1;
1317 if ($d1 && $d1 ne ' ') {
1322 my $d2 = $res->date2;
1323 if ($d2 && $d2 ne ' ') {
1330 __PACKAGE__->register_method(
1331 api_name => "open-ils.ingest.descriptor.xml",
1332 method => "biblio_descriptor",