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);
405 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
408 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
409 $max_cn = int($cn->id) + 1000;
413 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
414 $max_uri = int($cn->id) + 1000;
419 my $document = $parser->parse_string($xml);
421 my @uris = $self->method_lookup("open-ils.ingest.856_uri.object")->run($bib, $max_cn, $max_uri);
422 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
423 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
424 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
425 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
427 $_->source($bib->id) for (@mXfe);
428 $_->record($bib->id) for (@mfr);
429 $rd->record($bib->id) if ($rd);
431 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd, uri => \@uris };
433 __PACKAGE__->register_method(
434 api_name => "open-ils.ingest.full.biblio.object.readonly",
435 method => "ro_biblio_ingest_single_object",
440 sub ro_biblio_ingest_single_xml {
443 my $xml = OpenILS::Application::Ingest::entityize(shift);
445 my $document = $parser->parse_string($xml);
447 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
448 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
449 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
450 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
452 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd };
454 __PACKAGE__->register_method(
455 api_name => "open-ils.ingest.full.biblio.xml.readonly",
456 method => "ro_biblio_ingest_single_xml",
461 sub ro_biblio_ingest_single_record {
466 OpenILS::Application::Ingest->post_init();
467 my $r = OpenSRF::AppSession
468 ->create('open-ils.cstore')
469 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
472 return undef unless ($r and @$r);
474 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($r->marc);
476 $_->source($rec) for (@{$res->{field_entries}});
477 $_->record($rec) for (@{$res->{full_rec}});
478 $res->{descriptor}->record($rec);
482 __PACKAGE__->register_method(
483 api_name => "open-ils.ingest.full.biblio.record.readonly",
484 method => "ro_biblio_ingest_single_record",
489 sub ro_biblio_ingest_stream_record {
493 OpenILS::Application::Ingest->post_init();
495 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
497 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
499 my $rec = $resp->content;
500 last unless (defined $rec);
502 $log->debug("Running open-ils.ingest.full.biblio.record.readonly ...");
503 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.record.readonly")->run($rec);
505 $_->source($rec) for (@{$res->{field_entries}});
506 $_->record($rec) for (@{$res->{full_rec}});
508 $client->respond( $res );
513 __PACKAGE__->register_method(
514 api_name => "open-ils.ingest.full.biblio.record_stream.readonly",
515 method => "ro_biblio_ingest_stream_record",
520 sub ro_biblio_ingest_stream_xml {
524 OpenILS::Application::Ingest->post_init();
526 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
528 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
530 my $xml = $resp->content;
531 last unless (defined $xml);
533 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
534 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($xml);
536 $client->respond( $res );
541 __PACKAGE__->register_method(
542 api_name => "open-ils.ingest.full.biblio.xml_stream.readonly",
543 method => "ro_biblio_ingest_stream_xml",
548 sub rw_biblio_ingest_stream_import {
552 OpenILS::Application::Ingest->post_init();
554 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
556 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
558 my $bib = $resp->content;
559 last unless (defined $bib);
561 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
562 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($bib->marc);
564 $_->source($bib->id) for (@{$res->{field_entries}});
565 $_->record($bib->id) for (@{$res->{full_rec}});
567 $client->respond( $res );
572 __PACKAGE__->register_method(
573 api_name => "open-ils.ingest.full.biblio.bib_stream.import",
574 method => "rw_biblio_ingest_stream_import",
580 # --------------------------------------------------------------------------------
583 package OpenILS::Application::Ingest::Authority;
584 use base qw/OpenILS::Application::Ingest/;
585 use Unicode::Normalize;
587 sub ro_authority_ingest_single_object {
591 my $xml = OpenILS::Application::Ingest::entityize($bib->marc);
593 my $document = $parser->parse_string($xml);
595 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
597 $_->record($bib->id) for (@mfr);
599 return { full_rec => \@mfr };
601 __PACKAGE__->register_method(
602 api_name => "open-ils.ingest.full.authority.object.readonly",
603 method => "ro_authority_ingest_single_object",
608 sub ro_authority_ingest_single_xml {
611 my $xml = OpenILS::Application::Ingest::entityize(shift);
613 my $document = $parser->parse_string($xml);
615 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
617 return { full_rec => \@mfr };
619 __PACKAGE__->register_method(
620 api_name => "open-ils.ingest.full.authority.xml.readonly",
621 method => "ro_authority_ingest_single_xml",
626 sub ro_authority_ingest_single_record {
631 OpenILS::Application::Ingest->post_init();
632 my $r = OpenSRF::AppSession
633 ->create('open-ils.cstore')
634 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
637 return undef unless ($r and @$r);
639 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($r->marc);
641 $_->record($rec) for (@{$res->{full_rec}});
642 $res->{descriptor}->record($rec);
646 __PACKAGE__->register_method(
647 api_name => "open-ils.ingest.full.authority.record.readonly",
648 method => "ro_authority_ingest_single_record",
653 sub ro_authority_ingest_stream_record {
657 OpenILS::Application::Ingest->post_init();
659 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
661 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
663 my $rec = $resp->content;
664 last unless (defined $rec);
666 $log->debug("Running open-ils.ingest.full.authority.record.readonly ...");
667 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.record.readonly")->run($rec);
669 $_->record($rec) for (@{$res->{full_rec}});
671 $client->respond( $res );
676 __PACKAGE__->register_method(
677 api_name => "open-ils.ingest.full.authority.record_stream.readonly",
678 method => "ro_authority_ingest_stream_record",
683 sub ro_authority_ingest_stream_xml {
687 OpenILS::Application::Ingest->post_init();
689 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
691 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
693 my $xml = $resp->content;
694 last unless (defined $xml);
696 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
697 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($xml);
699 $client->respond( $res );
704 __PACKAGE__->register_method(
705 api_name => "open-ils.ingest.full.authority.xml_stream.readonly",
706 method => "ro_authority_ingest_stream_xml",
711 sub rw_authority_ingest_stream_import {
715 OpenILS::Application::Ingest->post_init();
717 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
719 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
721 my $bib = $resp->content;
722 last unless (defined $bib);
724 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
725 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($bib->marc);
727 $_->record($bib->id) for (@{$res->{full_rec}});
729 $client->respond( $res );
734 __PACKAGE__->register_method(
735 api_name => "open-ils.ingest.full.authority.bib_stream.import",
736 method => "rw_authority_ingest_stream_import",
742 # --------------------------------------------------------------------------------
743 # MARC index extraction
745 package OpenILS::Application::Ingest::XPATH;
746 use base qw/OpenILS::Application::Ingest/;
747 use Unicode::Normalize;
749 # give this an XML documentElement and an XPATH expression
750 sub xpath_to_string {
754 my $ns_prefix = shift;
757 $xml->setNamespace( $ns_uri, $ns_prefix, 1 ) if ($ns_uri && $ns_prefix);
761 # grab the set of matching nodes
762 my @nodes = $xml->findnodes( $xpath );
763 for my $value (@nodes) {
765 # grab all children of the node
766 my @children = $value->childNodes();
767 for my $child (@children) {
769 # add the childs content to the growing buffer
770 my $content = quotemeta($child->textContent);
771 next if ($unique && $string =~ /$content/); # uniquify the values
772 $string .= $child->textContent . " ";
775 $string .= $value->textContent . " ";
779 $string =~ s/(\w+)\/(\w+)/$1 $2/sgo;
780 $string =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
785 sub class_index_string_xml {
791 OpenILS::Application::Ingest->post_init();
792 $xml = $parser->parse_string(OpenILS::Application::Ingest::entityize($xml)) unless (ref $xml);
796 for my $class (@classes) {
797 my $class_constructor = "Fieldmapper::metabib::${class}_field_entry";
798 for my $type ( keys %{ $xpathset->{$class} } ) {
800 my $def = $xpathset->{$class}->{$type};
801 my $sf = $OpenILS::Application::Ingest::supported_formats{$def->{format}};
806 $document = $transform_cache{$def->{format}} || $sf->{xslt}->transform($xml);
807 $transform_cache{$def->{format}} = $document;
810 my $value = xpath_to_string(
811 $document->documentElement => $def->{xpath},
812 $sf->{ns} => $def->{format},
818 $value = NFD($value);
819 $value =~ s/\pM+//sgo;
820 $value =~ s/\pC+//sgo;
821 $value =~ s/\W+$//sgo;
823 $value =~ s/\b\.+\b//sgo;
826 my $fm = $class_constructor->new;
827 $fm->value( $value );
828 $fm->field( $xpathset->{$class}->{$type}->{id} );
829 $client->respond($fm);
834 __PACKAGE__->register_method(
835 api_name => "open-ils.ingest.field_entry.class.xml",
836 method => "class_index_string_xml",
842 sub class_index_string_record {
848 OpenILS::Application::Ingest->post_init();
849 my $r = OpenSRF::AppSession
850 ->create('open-ils.cstore')
851 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
854 return undef unless ($r and @$r);
856 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, @classes)) {
858 $client->respond($fm);
862 __PACKAGE__->register_method(
863 api_name => "open-ils.ingest.field_entry.class.record",
864 method => "class_index_string_record",
870 sub all_index_string_xml {
875 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($xml, keys(%$xpathset))) {
876 $client->respond($fm);
880 __PACKAGE__->register_method(
881 api_name => "open-ils.ingest.extract.field_entry.all.xml",
882 method => "all_index_string_xml",
888 sub all_index_string_record {
893 OpenILS::Application::Ingest->post_init();
894 my $r = OpenSRF::AppSession
895 ->create('open-ils.cstore')
896 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
899 return undef unless ($r and @$r);
901 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, keys(%$xpathset))) {
903 $client->respond($fm);
907 __PACKAGE__->register_method(
908 api_name => "open-ils.ingest.extract.field_entry.all.record",
909 method => "all_index_string_record",
915 # --------------------------------------------------------------------------------
918 package OpenILS::Application::Ingest::FlatMARC;
919 use base qw/OpenILS::Application::Ingest/;
920 use Unicode::Normalize;
923 sub _marcxml_to_full_rows {
926 my $xmltype = shift || 'metabib';
928 my $type = "Fieldmapper::${xmltype}::full_rec";
932 my ($root) = $marcxml->findnodes('//*[local-name()="record"]');
934 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
935 next unless $tagline;
940 my $val = $tagline->textContent;
950 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
951 next unless $tagline;
955 $ns->tag( $tagline->getAttribute( "tag" ) );
956 my $val = $tagline->textContent;
966 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
967 next unless $tagline;
969 my $tag = $tagline->getAttribute( "tag" );
970 my $ind1 = $tagline->getAttribute( "ind1" );
971 my $ind2 = $tagline->getAttribute( "ind2" );
973 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
981 $ns->subfield( $data->getAttribute( "code" ) );
982 my $val = $data->textContent;
987 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
988 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
989 $ns->value( lc($val) );
994 if ($xmltype eq 'metabib' and $tag eq '245') {
997 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
998 next unless ($data and $data->getAttribute( "code" ) eq 'a');
1005 $ns->subfield( $data->getAttribute( "code" ) );
1006 my $val = substr( $data->textContent, $ind2 );
1008 $val =~ s/\pM+//sgo;
1009 $val =~ s/\pC+//sgo;
1010 $val =~ s/\W+$//sgo;
1011 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1012 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1013 $ns->value( lc($val) );
1020 $log->debug("Returning ".scalar(@ns_list)." Fieldmapper nodes from $xmltype xml");
1029 $log->debug("processing [$xml]");
1031 $xml = $parser->parse_string(OpenILS::Application::Ingest::entityize($xml)) unless (ref $xml);
1033 my $type = 'metabib';
1034 $type = 'authority' if ($self->api_name =~ /authority/o);
1036 OpenILS::Application::Ingest->post_init();
1038 $client->respond($_) for (_marcxml_to_full_rows($xml, $type));
1041 __PACKAGE__->register_method(
1042 api_name => "open-ils.ingest.flat_marc.authority.xml",
1043 method => "flat_marc_xml",
1048 __PACKAGE__->register_method(
1049 api_name => "open-ils.ingest.flat_marc.biblio.xml",
1050 method => "flat_marc_xml",
1056 sub flat_marc_record {
1061 my $type = 'biblio';
1062 $type = 'authority' if ($self->api_name =~ /authority/o);
1064 OpenILS::Application::Ingest->post_init();
1065 my $r = OpenSRF::AppSession
1066 ->create('open-ils.cstore')
1067 ->request( "open-ils.cstore.direct.${type}.record_entry.retrieve" => $rec )
1071 return undef unless ($r and $r->marc);
1073 my @rows = $self->method_lookup("open-ils.ingest.flat_marc.$type.xml")->run($r->marc);
1074 for my $row (@rows) {
1075 $client->respond($row);
1076 $log->debug(OpenSRF::Utils::JSON->perl2JSON($row), DEBUG);
1080 __PACKAGE__->register_method(
1081 api_name => "open-ils.ingest.flat_marc.biblio.record_entry",
1082 method => "flat_marc_record",
1087 __PACKAGE__->register_method(
1088 api_name => "open-ils.ingest.flat_marc.authority.record_entry",
1089 method => "flat_marc_record",
1096 # --------------------------------------------------------------------------------
1099 package OpenILS::Application::Ingest::Biblio::URI;
1100 use base qw/OpenILS::Application::Ingest/;
1101 use Unicode::Normalize;
1102 use OpenSRF::EX qw/:try/;
1105 sub _extract_856_uris {
1109 my $max_uri = shift;
1112 my $recid = $rec->id;
1113 my $marcxml = $rec->marc;
1115 my $document = $parser->parse_string($marcxml);
1116 my @nodes = $document->findnodes('//*[local-name()="datafield" and @tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]');
1118 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
1120 for my $node (@nodes) {
1121 # first, is there a URI?
1122 my $href = $node->findvalue('*[local-name()="subfield" and @code="u"]/text()');
1123 next unless ($href);
1125 # now, find the best possible label
1126 my $label = $node->findvalue('*[local-name()="subfield" and @code="y"]/text()');
1127 $label ||= $node->findvalue('*[local-name()="subfield" and @code="3"]/text()');
1131 my $use = $node->findvalue('*[local-name()="subfield" and @code="z"]/text()');
1132 $use ||= $node->findvalue('*[local-name()="subfield" and @code="2"]/text()');
1133 $use ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1135 # moving on to the URI owner
1136 my $owner = $node->findvalue('*[local-name()="subfield" and @code="w"]/text()');
1137 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1138 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="9"]/text()'); # Evergreen special sauce
1140 $owner =~ s/^.*?\((\w+)\).*$/$1/o; # unwrap first paren-enclosed string and then ...
1142 # no owner? skip it :(
1143 next unless ($owner);
1146 ->request( 'open-ils.cstore.direct.actor.org_unit.search' => { shortname => $owner} )
1151 # now we can construct the uri object
1153 ->request( 'open-ils.cstore.direct.asset.uri.search' => { label => $label, href => $href, use_restriction => $use, active => 't' } )
1157 $uri = Fieldmapper::asset::uri->new;
1159 $uri->id( $$max_uri++ );
1160 $uri->label($label);
1163 $uri->use_restriction($use);
1166 # see if we need to create a call number
1168 ->request( 'open-ils.cstore.direct.asset.call_number.search' => { owning_lib => $org->id, record => $recid, label => '##URI##' } )
1172 $c = Fieldmapper::asset::call_number->new;
1175 $cn->id( $$max_cn++ );
1176 $cn->owning_lib( $org->id );
1177 $cn->record( $recid );
1178 $cn->create_date( 'now' );
1179 $cn->creator( $rec->creator );
1180 $cn->editor( $rec->editor );
1181 $cn->edit_date( 'now' );
1182 $cn->label( '##URI##' );
1185 push @objects, { uri => $uri, call_number => $cn };
1188 $log->debug("Returning ".scalar(@objects)." URI nodes for record $recid");
1192 sub get_uris_record {
1197 OpenILS::Application::Ingest->post_init();
1198 my $r = OpenSRF::AppSession
1199 ->create('open-ils.cstore')
1200 ->request( "open-ils.cstore.direct.biblio.record_entry.retrieve" => $rec )
1203 return undef unless ($r and $r->marc);
1205 $client->respond($_) for (_extract_856_uris($r));
1208 __PACKAGE__->register_method(
1209 api_name => "open-ils.ingest.856_uri.record",
1210 method => "get_uris_record",
1216 sub get_uris_object {
1221 my $max_uri = shift;
1223 return undef unless ($obj and $obj->marc);
1225 $client->respond($_) for (_extract_856_uris($obj, \$max_cn, \$max_uri));
1228 __PACKAGE__->register_method(
1229 api_name => "open-ils.ingest.856_uri.object",
1230 method => "get_uris_object",
1237 # --------------------------------------------------------------------------------
1240 package OpenILS::Application::Ingest::Biblio::Fingerprint;
1241 use base qw/OpenILS::Application::Ingest/;
1242 use Unicode::Normalize;
1243 use OpenSRF::EX qw/:try/;
1245 sub biblio_fingerprint_record {
1250 OpenILS::Application::Ingest->post_init();
1252 my $r = OpenSRF::AppSession
1253 ->create('open-ils.cstore')
1254 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1257 return undef unless ($r and $r->marc);
1259 my ($fp) = $self->method_lookup('open-ils.ingest.fingerprint.xml')->run($r->marc);
1260 $log->debug("Returning [$fp] as fingerprint for record $rec", INFO);
1261 $fp->{quality} = int($fp->{quality});
1264 __PACKAGE__->register_method(
1265 api_name => "open-ils.ingest.fingerprint.record",
1266 method => "biblio_fingerprint_record",
1272 sub biblio_fingerprint {
1275 my $xml = OpenILS::Application::Ingest::entityize(shift);
1277 $log->internal("Got MARC [$xml]");
1280 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1281 my $conf = OpenSRF::Utils::SettingsClient->new;
1283 my $libs = $conf->config_value(@pfx, 'script_path');
1284 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_fingerprint');
1285 my $script_libs = (ref($libs)) ? $libs : [$libs];
1287 $log->debug("Loading script $script_file for biblio fingerprinting...");
1289 $fp_script = new OpenILS::Utils::ScriptRunner
1290 ( file => $script_file,
1291 paths => $script_libs,
1292 reset_count => 100 );
1295 $fp_script->insert('environment' => {marc => $xml} => 1);
1297 my $res = $fp_script->run || ($log->error( "Fingerprint script died! $@" ) && return undef);
1298 $log->debug("Script for biblio fingerprinting completed successfully...");
1302 __PACKAGE__->register_method(
1303 api_name => "open-ils.ingest.fingerprint.xml",
1304 method => "biblio_fingerprint",
1310 sub biblio_descriptor {
1313 my $xml = OpenILS::Application::Ingest::entityize(shift);
1315 $log->internal("Got MARC [$xml]");
1318 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1319 my $conf = OpenSRF::Utils::SettingsClient->new;
1321 my $libs = $conf->config_value(@pfx, 'script_path');
1322 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_descriptor');
1323 my $script_libs = (ref($libs)) ? $libs : [$libs];
1325 $log->debug("Loading script $script_file for biblio descriptor extraction...");
1327 $rd_script = new OpenILS::Utils::ScriptRunner
1328 ( file => $script_file,
1329 paths => $script_libs,
1330 reset_count => 100 );
1333 $log->debug("Setting up environment for descriptor extraction script...");
1334 $rd_script->insert('environment.marc' => $xml => 1);
1335 $log->debug("Environment building complete...");
1337 my $res = $rd_script->run || ($log->error( "Descriptor script died! $@" ) && return undef);
1338 $log->debug("Script for biblio descriptor extraction completed successfully");
1340 my $d1 = $res->date1;
1341 if ($d1 && $d1 ne ' ') {
1346 my $d2 = $res->date2;
1347 if ($d2 && $d2 ne ' ') {
1354 __PACKAGE__->register_method(
1355 api_name => "open-ils.ingest.descriptor.xml",
1356 method => "biblio_descriptor",