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::Application::AppUtils;
13 use OpenILS::Utils::ScriptRunner;
14 use OpenILS::Utils::Fieldmapper;
15 use OpenSRF::Utils::JSON;
17 use OpenILS::Utils::Fieldmapper;
21 use Time::HiRes qw(time);
23 our %supported_formats = (
24 mods33 => {ns => 'http://www.loc.gov/mods/v3'},
25 mods32 => {ns => 'http://www.loc.gov/mods/v3'},
26 mods3 => {ns => 'http://www.loc.gov/mods/v3'},
27 mods => {ns => 'http://www.loc.gov/mods/'},
28 marcxml => {ns => 'http://www.loc.gov/MARC21/slim'},
29 srw_dc => {ns => 'info:srw/schema/1/dc-schema'},
30 oai_dc => {ns => 'http://www.openarchives.org/OAI/2.0/oai_dc/'},
31 rdf_dc => {ns => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'},
32 atom => {ns => 'http://www.w3.org/2005/Atom'},
33 rss091 => {ns => 'http://my.netscape.com/rdf/simple/0.9/'},
37 rss10 => {ns => 'http://purl.org/rss/1.0/'},
38 rss11 => {ns => 'http://purl.org/net/rss1.1#'},
43 my $log = 'OpenSRF::Utils::Logger';
45 my $parser = XML::LibXML->new();
46 my $xslt = XML::LibXSLT->new();
56 unless (keys %$xpathset) {
57 $log->debug("Running post_init", DEBUG);
59 my $xsldir = OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl');
61 unless ($supported_formats{mods}{xslt}) {
62 $log->debug("Loading MODS XSLT", DEBUG);
63 my $xslt_doc = $parser->parse_file( $xsldir . "/MARC21slim2MODS.xsl");
64 $supported_formats{mods}{xslt} = $xslt->parse_stylesheet( $xslt_doc );
67 unless ($supported_formats{mods3}{xslt}) {
68 $log->debug("Loading MODS v3 XSLT", DEBUG);
69 my $xslt_doc = $parser->parse_file( $xsldir . "/MARC21slim2MODS3.xsl");
70 $supported_formats{mods3}{xslt} = $xslt->parse_stylesheet( $xslt_doc );
73 unless ($supported_formats{mods32}{xslt}) {
74 $log->debug("Loading MODS v32 XSLT", DEBUG);
75 my $xslt_doc = $parser->parse_file( $xsldir . "/MARC21slim2MODS32.xsl");
76 $supported_formats{mods32}{xslt} = $xslt->parse_stylesheet( $xslt_doc );
79 unless ($supported_formats{mods33}{xslt}) {
80 $log->debug("Loading MODS v33 XSLT", DEBUG);
81 my $xslt_doc = $parser->parse_file( $xsldir . "/MARC21slim2MODS33.xsl");
82 $supported_formats{mods33}{xslt} = $xslt->parse_stylesheet( $xslt_doc );
85 my $req = OpenSRF::AppSession
86 ->create('open-ils.cstore')
88 # XXX testing new metabib field use for faceting
89 #->request( 'open-ils.cstore.direct.config.metabib_field.search.atomic', { id => { '!=' => undef } } )
90 ->request( 'open-ils.cstore.direct.config.metabib_field.search.atomic', { search_field => 't' } )
94 if (ref $req and @$req) {
96 $xpathset->{ $f->field_class }->{ $f->name }->{xpath} = $f->xpath;
97 $xpathset->{ $f->field_class }->{ $f->name }->{id} = $f->id;
98 $xpathset->{ $f->field_class }->{ $f->name }->{format} = $f->format;
99 $log->debug("Loaded XPath from DB: ".$f->field_class." => ".$f->name." : ".$f->xpath, DEBUG);
105 # --------------------------------------------------------------------------------
108 package OpenILS::Application::Ingest::Biblio;
109 use base qw/OpenILS::Application::Ingest/;
110 use Unicode::Normalize;
112 sub rw_biblio_ingest_single_object {
117 my ($blob) = $self->method_lookup("open-ils.ingest.full.biblio.object.readonly")->run($bib);
118 return undef unless ($blob);
120 $bib->fingerprint( $blob->{fingerprint}->{fingerprint} );
121 $bib->quality( $blob->{fingerprint}->{quality} );
123 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
125 my $xact = $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
128 # update uri stuff ...
130 # gather URI call numbers for this record
131 my $uri_cns = $u->{call_number} = $cstore->request(
132 'open-ils.cstore.direct.asset.call_number.id_list.atomic' => { record => $bib->id, label => '##URI##' }
136 # gather the maps for those call numbers
137 my $uri_maps = $u->{call_number} = $cstore->request(
138 'open-ils.cstore.direct.asset.uri_call_number_map.id_list.atomic' => { call_number => $uri_cns }
141 # delete the old maps
142 $cstore->request( 'open-ils.cstore.direct.asset.uri_call_number_map.delete' => $_ )->gather(1) for (@$uri_maps);
144 # and delete the call numbers if there are no more URIs
145 if (!@{ $blob->{uri} }) {
146 $cstore->request( 'open-ils.cstore.direct.asset.call_number.delete' => $_ )->gather(1) for (@$uri_cns);
150 # now, add CNs, URIs and maps
151 my %new_cns_by_owner;
152 my %new_uris_by_owner;
153 for my $u ( @{ $blob->{uri} } ) {
155 my $owner = $u->{call_number}->owning_lib;
157 if ($u->{call_number}->isnew) {
158 if ($new_cns_by_owner{$owner}) {
159 $u->{call_number} = $new_cns_by_owner{$owner};
161 $u->{call_number}->clear_id;
162 $u->{call_number} = $new_cns_by_owner{$owner} = $cstore->request(
163 'open-ils.cstore.direct.asset.call_number.create' => $u->{call_number}
168 if ($u->{uri}->isnew) {
169 if ($new_uris_by_owner{$owner}) {
170 $u->{uri} = $new_uris_by_owner{$owner};
172 $u->{uri} = $new_uris_by_owner{$owner} = $cstore->request(
173 'open-ils.cstore.direct.asset.uri.create' => $u->{uri}
178 # Check for an existing CN-URI map
179 $tmp = $cstore->request(
180 'open-ils.cstore.direct.asset.uri_call_number_map.id_list',
181 { call_number => $u->{call_number}->id, uri => $u->{uri}->id }
186 my $umap = Fieldmapper::asset::uri_call_number_map->new;
187 $umap->uri($u->{uri}->id);
188 $umap->call_number($u->{call_number}->id);
190 $cstore->request( 'open-ils.cstore.direct.asset.uri_call_number_map.create' => $umap )->gather(1);
193 # update full_rec stuff ...
194 $tmp = $cstore->request(
195 'open-ils.cstore.direct.metabib.full_rec.id_list.atomic',
196 { record => $bib->id }
199 $cstore->request( 'open-ils.cstore.direct.metabib.full_rec.delete' => $_ )->gather(1) for (@$tmp);
200 $cstore->request( 'open-ils.cstore.direct.metabib.full_rec.create' => $_ )->gather(1) for (@{ $blob->{full_rec} });
202 # update rec_descriptor stuff ...
203 $tmp = $cstore->request(
204 'open-ils.cstore.direct.metabib.record_descriptor.id_list.atomic',
205 { record => $bib->id }
208 $cstore->request( 'open-ils.cstore.direct.metabib.record_descriptor.delete' => $_ )->gather(1) for (@$tmp);
209 $cstore->request( 'open-ils.cstore.direct.metabib.record_descriptor.create' => $blob->{descriptor} )->gather(1);
211 # deal with classed fields...
212 for my $class ( qw/title author subject keyword series/ ) {
213 $tmp = $cstore->request(
214 "open-ils.cstore.direct.metabib.${class}_field_entry.id_list.atomic",
215 { source => $bib->id }
218 $cstore->request( "open-ils.cstore.direct.metabib.${class}_field_entry.delete" => $_ )->gather(1) for (@$tmp);
220 for my $obj ( @{ $blob->{field_entries} } ) {
221 my $class = $obj->class_name;
222 $class =~ s/^Fieldmapper:://o;
224 $cstore->request( "open-ils.cstore.direct.$class.create" => $obj )->gather(1);
229 $tmp = $cstore->request(
230 'open-ils.cstore.direct.metabib.metarecord_source_map.search.atomic',
231 { source => $bib->id }
234 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord_source_map.delete' => $_->id )->gather(1) for (@$tmp);
237 my $old_mrs = $cstore->request(
238 'open-ils.cstore.direct.metabib.metarecord.search.atomic' => { id => [map { $_->metarecord } @$tmp] }
239 )->gather(1) if (@$tmp);
241 $old_mrs = [] if (!ref($old_mrs));
244 for my $m (@$old_mrs) {
245 if ($m->fingerprint eq $bib->fingerprint) {
248 my $others = $cstore->request(
249 'open-ils.cstore.direct.metabib.metarecord_source_map.id_list.atomic' => { metarecord => $m->id }
254 'open-ils.cstore.direct.metabib.metarecord.delete' => $m->id
264 # Get the matchin MR, if any.
265 $mr = $cstore->request(
266 'open-ils.cstore.direct.metabib.metarecord.search',
267 { fingerprint => $bib->fingerprint }
270 $holds = $cstore->request(
271 'open-ils.cstore.direct.action.hold_request.search.atomic',
272 { hold_type => 'M', target => [ map { $_->id } grep { $_->isdeleted } @$old_mrs ] }
273 )->gather(1) if (@$old_mrs);
276 for my $h (@$holds) {
278 $cstore->request( 'open-ils.cstore.direct.action.hold_request.update' => $h )->gather(1);
285 $mr = new Fieldmapper::metabib::metarecord;
286 $mr->fingerprint( $bib->fingerprint );
287 $mr->master_record( $bib->id );
290 "open-ils.cstore.direct.metabib.metarecord.create",
291 $mr => { quiet => 'true' }
295 for my $h (grep { !$_->ischanged } @$holds) {
297 $cstore->request( 'open-ils.cstore.direct.action.hold_request.update' => $h )->gather(1);
300 my $mrm = $cstore->request(
301 'open-ils.cstore.direct.metabib.metarecord_source_map.search.atomic',
302 { metarecord => $mr->id }
306 my $best = $cstore->request(
307 "open-ils.cstore.direct.biblio.record_entry.search",
308 { id => [ map { $_->source } @$mrm ] },
309 { 'select' => { bre => [ qw/id quality/ ] },
310 order_by => { bre => "quality desc" },
315 if ($best->quality > $bib->quality) {
316 $mr->master_record($best->id);
318 $mr->master_record($bib->id);
321 $mr->master_record($bib->id);
326 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord.update' => $mr )->gather(1);
329 my $mrm = new Fieldmapper::metabib::metarecord_source_map;
330 $mrm->source($bib->id);
331 $mrm->metarecord($mr->id);
333 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord_source_map.create' => $mrm )->gather(1);
334 $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.update' => $bib )->gather(1);
336 $cstore->request( 'open-ils.cstore.json_query.atomic' => { from => [ 'reporter.simple_rec_update', $bib->id ] } )->gather(1);
338 $cstore->request( 'open-ils.cstore.transaction.commit' )->gather(1) || return undef;;
343 __PACKAGE__->register_method(
344 api_name => "open-ils.ingest.full.biblio.object",
345 method => "rw_biblio_ingest_single_object",
350 sub rw_biblio_ingest_single_record {
355 OpenILS::Application::Ingest->post_init();
356 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
357 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
359 my $r = $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )->gather(1);
361 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
364 return undef unless ($r and @$r);
366 return ($self->method_lookup("open-ils.ingest.full.biblio.object")->run($r))[0];
368 __PACKAGE__->register_method(
369 api_name => "open-ils.ingest.full.biblio.record",
370 method => "rw_biblio_ingest_single_record",
375 sub rw_biblio_ingest_record_list {
378 my @rec = ref($_[0]) ? @{ $_[0] } : @_ ;
380 OpenILS::Application::Ingest->post_init();
381 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
382 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
384 my $r = $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.search.atomic' => { id => \@rec } )->gather(1);
386 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
389 return undef unless ($r and @$r);
393 if (($self->method_lookup("open-ils.ingest.full.biblio.object")->run($_))[0]) {
399 __PACKAGE__->register_method(
400 api_name => "open-ils.ingest.full.biblio.record_list",
401 method => "rw_biblio_ingest_record_list",
406 sub ro_biblio_ingest_single_object {
410 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
414 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
417 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
418 $max_cn = int($cn->id) + 1000;
422 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
423 $max_uri = int($cn->id) + 1000;
428 my $document = $parser->parse_string($xml);
430 my @uris = $self->method_lookup("open-ils.ingest.856_uri.object")->run($bib, $max_cn, $max_uri);
431 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
432 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
433 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
434 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
436 $_->source($bib->id) for (@mXfe);
437 $_->record($bib->id) for (@mfr);
438 $rd->record($bib->id) if ($rd);
440 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd, uri => \@uris };
442 __PACKAGE__->register_method(
443 api_name => "open-ils.ingest.full.biblio.object.readonly",
444 method => "ro_biblio_ingest_single_object",
449 sub ro_biblio_ingest_single_xml {
452 my $xml = OpenILS::Application::AppUtils->entityize(shift);
454 my $document = $parser->parse_string($xml);
456 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
457 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
458 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
459 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
461 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd };
463 __PACKAGE__->register_method(
464 api_name => "open-ils.ingest.full.biblio.xml.readonly",
465 method => "ro_biblio_ingest_single_xml",
470 sub ro_biblio_ingest_single_record {
475 OpenILS::Application::Ingest->post_init();
476 my $r = OpenSRF::AppSession
477 ->create('open-ils.cstore')
478 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
481 return undef unless ($r and @$r);
483 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($r->marc);
485 $_->source($rec) for (@{$res->{field_entries}});
486 $_->record($rec) for (@{$res->{full_rec}});
487 $res->{descriptor}->record($rec);
491 __PACKAGE__->register_method(
492 api_name => "open-ils.ingest.full.biblio.record.readonly",
493 method => "ro_biblio_ingest_single_record",
498 sub ro_biblio_ingest_stream_record {
502 OpenILS::Application::Ingest->post_init();
504 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
506 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
508 my $rec = $resp->content;
509 last unless (defined $rec);
511 $log->debug("Running open-ils.ingest.full.biblio.record.readonly ...");
512 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.record.readonly")->run($rec);
514 $_->source($rec) for (@{$res->{field_entries}});
515 $_->record($rec) for (@{$res->{full_rec}});
517 $client->respond( $res );
522 __PACKAGE__->register_method(
523 api_name => "open-ils.ingest.full.biblio.record_stream.readonly",
524 method => "ro_biblio_ingest_stream_record",
529 sub ro_biblio_ingest_stream_xml {
533 OpenILS::Application::Ingest->post_init();
535 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
537 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
539 my $xml = $resp->content;
540 last unless (defined $xml);
542 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
543 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($xml);
545 $client->respond( $res );
550 __PACKAGE__->register_method(
551 api_name => "open-ils.ingest.full.biblio.xml_stream.readonly",
552 method => "ro_biblio_ingest_stream_xml",
557 sub rw_biblio_ingest_stream_import {
561 OpenILS::Application::Ingest->post_init();
563 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
565 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
567 my $bib = $resp->content;
568 last unless (defined $bib);
570 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
571 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($bib->marc);
573 $_->source($bib->id) for (@{$res->{field_entries}});
574 $_->record($bib->id) for (@{$res->{full_rec}});
576 $client->respond( $res );
581 __PACKAGE__->register_method(
582 api_name => "open-ils.ingest.full.biblio.bib_stream.import",
583 method => "rw_biblio_ingest_stream_import",
589 # --------------------------------------------------------------------------------
592 package OpenILS::Application::Ingest::Authority;
593 use base qw/OpenILS::Application::Ingest/;
594 use Unicode::Normalize;
596 sub rw_authority_ingest_single_object {
601 my ($blob) = $self->method_lookup("open-ils.ingest.full.authority.object.readonly")->run($auth);
602 return undef unless ($blob);
604 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
606 my $xact = $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
609 # update full_rec stuff ...
610 $tmp = $cstore->request(
611 'open-ils.cstore.direct.authority.full_rec.id_list.atomic',
612 { record => $auth->id }
615 $cstore->request( 'open-ils.cstore.direct.authority.full_rec.delete' => $_ )->gather(1) for (@$tmp);
616 $cstore->request( 'open-ils.cstore.direct.authority.full_rec.create' => $_ )->gather(1) for (@{ $blob->{full_rec} });
618 # XXX when we start extracting authority descriptors and adding sources ...
620 # update rec_descriptor stuff ...
621 #$tmp = $cstore->request(
622 # 'open-ils.cstore.direct.authority.record_descriptor.id_list.atomic',
623 # { record => $auth->id }
626 #$cstore->request( 'open-ils.cstore.direct.authority.record_descriptor.delete' => $_ )->gather(1) for (@$tmp);
627 #$cstore->request( 'open-ils.cstore.direct.authority.record_descriptor.create' => $blob->{descriptor} )->gather(1);
628 #$cstore->request( 'open-ils.cstore.direct.authority.record_entry.update' => $auth )->gather(1);
630 $cstore->request( 'open-ils.cstore.transaction.commit' )->gather(1) || return undef;;
635 __PACKAGE__->register_method(
636 api_name => "open-ils.ingest.full.authority.object",
637 method => "rw_authority_ingest_single_object",
642 sub rw_authority_ingest_single_record {
647 OpenILS::Application::Ingest->post_init();
648 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
649 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
651 my $r = $cstore->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )->gather(1);
653 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
656 return undef unless ($r and @$r);
658 return ($self->method_lookup("open-ils.ingest.full.authority.object")->run($r))[0];
660 __PACKAGE__->register_method(
661 api_name => "open-ils.ingest.full.authority.record",
662 method => "rw_authority_ingest_single_record",
667 sub ro_authority_ingest_single_object {
671 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
673 my $document = $parser->parse_string($xml);
675 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
677 $_->record($bib->id) for (@mfr);
679 return { full_rec => \@mfr };
681 __PACKAGE__->register_method(
682 api_name => "open-ils.ingest.full.authority.object.readonly",
683 method => "ro_authority_ingest_single_object",
687 __PACKAGE__->register_method(
688 api_name => "open-ils.ingest.full.serial.object.readonly",
689 method => "ro_authority_ingest_single_object",
695 sub ro_authority_ingest_single_xml {
698 my $xml = OpenILS::Application::AppUtils->entityize(shift);
700 my $document = $parser->parse_string($xml);
702 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
704 return { full_rec => \@mfr };
706 __PACKAGE__->register_method(
707 api_name => "open-ils.ingest.full.authority.xml.readonly",
708 method => "ro_authority_ingest_single_xml",
713 sub ro_authority_ingest_single_record {
718 OpenILS::Application::Ingest->post_init();
719 my $r = OpenSRF::AppSession
720 ->create('open-ils.cstore')
721 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
724 return undef unless ($r and @$r);
726 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($r->marc);
728 $_->record($rec) for (@{$res->{full_rec}});
729 $res->{descriptor}->record($rec);
733 __PACKAGE__->register_method(
734 api_name => "open-ils.ingest.full.authority.record.readonly",
735 method => "ro_authority_ingest_single_record",
740 sub ro_authority_ingest_stream_record {
744 OpenILS::Application::Ingest->post_init();
746 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
748 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
750 my $rec = $resp->content;
751 last unless (defined $rec);
753 $log->debug("Running open-ils.ingest.full.authority.record.readonly ...");
754 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.record.readonly")->run($rec);
756 $_->record($rec) for (@{$res->{full_rec}});
758 $client->respond( $res );
763 __PACKAGE__->register_method(
764 api_name => "open-ils.ingest.full.authority.record_stream.readonly",
765 method => "ro_authority_ingest_stream_record",
770 sub ro_authority_ingest_stream_xml {
774 OpenILS::Application::Ingest->post_init();
776 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
778 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
780 my $xml = $resp->content;
781 last unless (defined $xml);
783 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
784 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($xml);
786 $client->respond( $res );
791 __PACKAGE__->register_method(
792 api_name => "open-ils.ingest.full.authority.xml_stream.readonly",
793 method => "ro_authority_ingest_stream_xml",
798 sub rw_authority_ingest_stream_import {
802 OpenILS::Application::Ingest->post_init();
804 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
806 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
808 my $bib = $resp->content;
809 last unless (defined $bib);
811 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
812 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($bib->marc);
814 $_->record($bib->id) for (@{$res->{full_rec}});
816 $client->respond( $res );
821 __PACKAGE__->register_method(
822 api_name => "open-ils.ingest.full.authority.bib_stream.import",
823 method => "rw_authority_ingest_stream_import",
828 # --------------------------------------------------------------------------------
831 package OpenILS::Application::Ingest::Serial;
832 use base qw/OpenILS::Application::Ingest/;
833 use Unicode::Normalize;
835 sub ro_serial_ingest_single_object {
839 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
841 my $document = $parser->parse_string($xml);
843 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.serial.xml")->run($document);
845 $_->record($bib->id) for (@mfr);
847 return { full_rec => \@mfr };
849 __PACKAGE__->register_method(
850 api_name => "open-ils.ingest.full.serial.object.readonly",
851 method => "ro_serial_ingest_single_object",
856 sub ro_serial_ingest_single_xml {
859 my $xml = OpenILS::Application::AppUtils->entityize(shift);
861 my $document = $parser->parse_string($xml);
863 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.serial.xml")->run($document);
865 return { full_rec => \@mfr };
867 __PACKAGE__->register_method(
868 api_name => "open-ils.ingest.full.serial.xml.readonly",
869 method => "ro_serial_ingest_single_xml",
874 sub ro_serial_ingest_single_record {
879 OpenILS::Application::Ingest->post_init();
880 my $r = OpenSRF::AppSession
881 ->create('open-ils.cstore')
882 ->request( 'open-ils.cstore.direct.serial.record_entry.retrieve' => $rec )
885 return undef unless ($r and @$r);
887 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($r->marc);
889 $_->record($rec) for (@{$res->{full_rec}});
890 $res->{descriptor}->record($rec);
894 __PACKAGE__->register_method(
895 api_name => "open-ils.ingest.full.serial.record.readonly",
896 method => "ro_serial_ingest_single_record",
901 sub ro_serial_ingest_stream_record {
905 OpenILS::Application::Ingest->post_init();
907 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
909 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
911 my $rec = $resp->content;
912 last unless (defined $rec);
914 $log->debug("Running open-ils.ingest.full.serial.record.readonly ...");
915 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.record.readonly")->run($rec);
917 $_->record($rec) for (@{$res->{full_rec}});
919 $client->respond( $res );
924 __PACKAGE__->register_method(
925 api_name => "open-ils.ingest.full.serial.record_stream.readonly",
926 method => "ro_serial_ingest_stream_record",
931 sub ro_serial_ingest_stream_xml {
935 OpenILS::Application::Ingest->post_init();
937 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
939 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
941 my $xml = $resp->content;
942 last unless (defined $xml);
944 $log->debug("Running open-ils.ingest.full.serial.xml.readonly ...");
945 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($xml);
947 $client->respond( $res );
952 __PACKAGE__->register_method(
953 api_name => "open-ils.ingest.full.serial.xml_stream.readonly",
954 method => "ro_serial_ingest_stream_xml",
959 sub rw_serial_ingest_stream_import {
963 OpenILS::Application::Ingest->post_init();
965 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
967 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
969 my $bib = $resp->content;
970 last unless (defined $bib);
972 $log->debug("Running open-ils.ingest.full.serial.xml.readonly ...");
973 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($bib->marc);
975 $_->record($bib->id) for (@{$res->{full_rec}});
977 $client->respond( $res );
982 __PACKAGE__->register_method(
983 api_name => "open-ils.ingest.full.serial.bib_stream.import",
984 method => "rw_serial_ingest_stream_import",
990 # --------------------------------------------------------------------------------
991 # MARC index extraction
993 package OpenILS::Application::Ingest::XPATH;
994 use base qw/OpenILS::Application::Ingest/;
995 use Unicode::Normalize;
997 # give this an XML documentElement and an XPATH expression
998 sub xpath_to_string {
1002 my $ns_prefix = shift;
1005 $xml->setNamespace( $ns_uri, $ns_prefix, 1 ) if ($ns_uri && $ns_prefix);
1009 # grab the set of matching nodes
1010 my @nodes = $xml->findnodes( $xpath );
1011 for my $value (@nodes) {
1013 # grab all children of the node
1014 my @children = $value->childNodes();
1015 for my $child (@children) {
1017 # add the childs content to the growing buffer
1018 my $content = quotemeta($child->textContent);
1019 next if ($unique && $string =~ /$content/); # uniquify the values
1020 $string .= $child->textContent . " ";
1023 $string .= $value->textContent . " ";
1027 $string =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1028 $string =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1030 return NFD($string);
1033 sub class_index_string_xml {
1039 OpenILS::Application::Ingest->post_init();
1040 $xml = $parser->parse_string(OpenILS::Application::AppUtils->entityize($xml)) unless (ref $xml);
1042 my %transform_cache;
1044 for my $class (@classes) {
1045 my $class_constructor = "Fieldmapper::metabib::${class}_field_entry";
1046 for my $type ( keys %{ $xpathset->{$class} } ) {
1048 my $def = $xpathset->{$class}->{$type};
1049 my $sf = $OpenILS::Application::Ingest::supported_formats{$def->{format}};
1051 my $document = $xml;
1054 $document = $transform_cache{$def->{format}} || $sf->{xslt}->transform($xml);
1055 $transform_cache{$def->{format}} = $document;
1058 my $value = xpath_to_string(
1059 $document->documentElement => $def->{xpath},
1060 $sf->{ns} => $def->{format},
1066 $value = NFD($value);
1067 $value =~ s/\pM+//sgo;
1068 $value =~ s/\pC+//sgo;
1069 $value =~ s/\W+$//sgo;
1071 $value =~ s/\b\.+\b//sgo;
1072 $value = lc($value);
1074 my $fm = $class_constructor->new;
1075 $fm->value( $value );
1076 $fm->field( $xpathset->{$class}->{$type}->{id} );
1077 $client->respond($fm);
1082 __PACKAGE__->register_method(
1083 api_name => "open-ils.ingest.field_entry.class.xml",
1084 method => "class_index_string_xml",
1090 sub class_index_string_record {
1094 my @classes = shift;
1096 OpenILS::Application::Ingest->post_init();
1097 my $r = OpenSRF::AppSession
1098 ->create('open-ils.cstore')
1099 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
1102 return undef unless ($r and @$r);
1104 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, @classes)) {
1106 $client->respond($fm);
1110 __PACKAGE__->register_method(
1111 api_name => "open-ils.ingest.field_entry.class.record",
1112 method => "class_index_string_record",
1118 sub all_index_string_xml {
1123 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($xml, keys(%$xpathset))) {
1124 $client->respond($fm);
1128 __PACKAGE__->register_method(
1129 api_name => "open-ils.ingest.extract.field_entry.all.xml",
1130 method => "all_index_string_xml",
1136 sub all_index_string_record {
1141 OpenILS::Application::Ingest->post_init();
1142 my $r = OpenSRF::AppSession
1143 ->create('open-ils.cstore')
1144 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1147 return undef unless ($r and @$r);
1149 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, keys(%$xpathset))) {
1151 $client->respond($fm);
1155 __PACKAGE__->register_method(
1156 api_name => "open-ils.ingest.extract.field_entry.all.record",
1157 method => "all_index_string_record",
1163 # --------------------------------------------------------------------------------
1166 package OpenILS::Application::Ingest::FlatMARC;
1167 use base qw/OpenILS::Application::Ingest/;
1168 use Unicode::Normalize;
1171 sub _marcxml_to_full_rows {
1173 my $marcxml = shift;
1174 my $xmltype = shift || 'metabib';
1176 my $type = "Fieldmapper::${xmltype}::full_rec";
1180 my ($root) = $marcxml->findnodes('//*[local-name()="record"]');
1182 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
1183 next unless $tagline;
1185 my $ns = $type->new;
1188 my $val = $tagline->textContent;
1190 $val =~ s/\pM+//sgo;
1191 $val =~ s/\pC+//sgo;
1192 $val =~ s/\W+$//sgo;
1198 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
1199 next unless $tagline;
1201 my $ns = $type->new;
1203 $ns->tag( $tagline->getAttribute( "tag" ) );
1204 my $val = $tagline->textContent;
1206 $val =~ s/\pM+//sgo;
1207 $val =~ s/\pC+//sgo;
1208 $val =~ s/\W+$//sgo;
1214 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
1215 next unless $tagline;
1217 my $tag = $tagline->getAttribute( "tag" );
1218 my $ind1 = $tagline->getAttribute( "ind1" );
1219 my $ind2 = $tagline->getAttribute( "ind2" );
1221 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
1224 my $ns = $type->new;
1229 $ns->subfield( $data->getAttribute( "code" ) );
1230 my $val = $data->textContent;
1232 $val =~ s/\pM+//sgo;
1233 $val =~ s/\pC+//sgo;
1234 $val =~ s/\W+$//sgo;
1235 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1236 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1237 $ns->value( lc($val) );
1242 if ($xmltype eq 'metabib' and $tag eq '245') {
1245 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
1246 next unless ($data and $data->getAttribute( "code" ) eq 'a');
1253 $ns->subfield( $data->getAttribute( "code" ) );
1254 my $val = substr( $data->textContent, $ind2 );
1256 $val =~ s/\pM+//sgo;
1257 $val =~ s/\pC+//sgo;
1258 $val =~ s/\W+$//sgo;
1259 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1260 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1261 $ns->value( lc($val) );
1268 $log->debug("Returning ".scalar(@ns_list)." Fieldmapper nodes from $xmltype xml");
1277 $log->debug("processing [$xml]");
1279 $xml = $parser->parse_string(OpenILS::Application::AppUtils->entityize($xml)) unless (ref $xml);
1281 my $type = 'metabib';
1282 $type = 'authority' if ($self->api_name =~ /authority/o);
1283 $type = 'serial' if ($self->api_name =~ /serial/o);
1285 OpenILS::Application::Ingest->post_init();
1287 $client->respond($_) for (_marcxml_to_full_rows($xml, $type));
1290 __PACKAGE__->register_method(
1291 api_name => "open-ils.ingest.flat_marc.authority.xml",
1292 method => "flat_marc_xml",
1297 __PACKAGE__->register_method(
1298 api_name => "open-ils.ingest.flat_marc.biblio.xml",
1299 method => "flat_marc_xml",
1304 __PACKAGE__->register_method(
1305 api_name => "open-ils.ingest.flat_marc.serial.xml",
1306 method => "flat_marc_xml",
1312 sub flat_marc_record {
1317 my $type = 'biblio';
1318 $type = 'authority' if ($self->api_name =~ /authority/o);
1319 $type = 'serial' if ($self->api_name =~ /serial/o);
1321 OpenILS::Application::Ingest->post_init();
1322 my $r = OpenSRF::AppSession
1323 ->create('open-ils.cstore')
1324 ->request( "open-ils.cstore.direct.${type}.record_entry.retrieve" => $rec )
1328 return undef unless ($r and $r->marc);
1330 my @rows = $self->method_lookup("open-ils.ingest.flat_marc.$type.xml")->run($r->marc);
1331 for my $row (@rows) {
1332 $client->respond($row);
1333 $log->debug(OpenSRF::Utils::JSON->perl2JSON($row), DEBUG);
1337 __PACKAGE__->register_method(
1338 api_name => "open-ils.ingest.flat_marc.biblio.record_entry",
1339 method => "flat_marc_record",
1344 __PACKAGE__->register_method(
1345 api_name => "open-ils.ingest.flat_marc.authority.record_entry",
1346 method => "flat_marc_record",
1351 __PACKAGE__->register_method(
1352 api_name => "open-ils.ingest.flat_marc.serial.record_entry",
1353 method => "flat_marc_record",
1359 # --------------------------------------------------------------------------------
1362 package OpenILS::Application::Ingest::Biblio::URI;
1363 use base qw/OpenILS::Application::Ingest/;
1364 use Unicode::Normalize;
1365 use OpenSRF::EX qw/:try/;
1368 sub _extract_856_uris {
1372 my $max_uri = shift;
1375 my $recid = $rec->id;
1376 my $marcxml = $rec->marc;
1378 my $document = $parser->parse_string($marcxml);
1379 my @nodes = $document->findnodes('//*[local-name()="datafield" and @tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]');
1381 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
1385 for my $node (@nodes) {
1386 # first, is there a URI?
1387 my $href = $node->findvalue('*[local-name()="subfield" and @code="u"]/text()');
1388 next unless ($href);
1390 # now, find the best possible label
1391 my $label = $node->findvalue('*[local-name()="subfield" and @code="y"]/text()');
1392 $label ||= $node->findvalue('*[local-name()="subfield" and @code="3"]/text()');
1396 my $use = $node->findvalue('*[local-name()="subfield" and @code="z"]/text()');
1397 $use ||= $node->findvalue('*[local-name()="subfield" and @code="2"]/text()');
1398 $use ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1400 # moving on to the URI owner
1401 my $owner = $node->findvalue('*[local-name()="subfield" and @code="9"]/text()'); # Evergreen special sauce
1402 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="w"]/text()');
1403 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1405 $owner =~ s/^.*?\((\w+)\).*$/$1/o; # unwrap first paren-enclosed string and then ...
1407 # no owner? skip it :(
1408 next unless ($owner);
1411 ->request( 'open-ils.cstore.direct.actor.org_unit.search' => { shortname => $owner} )
1416 # now we can construct the uri object
1418 ->request( 'open-ils.cstore.direct.asset.uri.search' => { label => $label, href => $href, use_restriction => $use, active => 't' } )
1422 $uri = Fieldmapper::asset::uri->new;
1424 $uri->id( $$max_uri++ );
1425 $uri->label($label);
1428 $uri->use_restriction($use);
1431 # see if we need to create a call number
1432 my $cn = $cn_cache{$org->id};
1433 $cn = $cn->clone if ($cn);
1434 $cn->clear_isnew if ($cn);
1437 ->request( 'open-ils.cstore.direct.asset.call_number.search' => { owning_lib => $org->id, record => $recid, label => '##URI##' } )
1441 $cn = Fieldmapper::asset::call_number->new;
1444 $cn->id( $$max_cn++ );
1445 $cn->owning_lib( $org->id );
1446 $cn->record( $recid );
1447 $cn->create_date( 'now' );
1448 $cn->creator( $rec->creator );
1449 $cn->editor( $rec->editor );
1450 $cn->edit_date( 'now' );
1451 $cn->label( '##URI##' );
1454 $cn_cache{$org->id} = $cn;
1456 push @objects, { uri => $uri, call_number => $cn };
1459 $log->debug("Returning ".scalar(@objects)." URI nodes for record $recid");
1460 $cstore->disconnect;
1464 sub get_uris_record {
1469 OpenILS::Application::Ingest->post_init();
1470 my $r = OpenSRF::AppSession
1471 ->create('open-ils.cstore')
1472 ->request( "open-ils.cstore.direct.biblio.record_entry.retrieve" => $rec )
1475 return undef unless ($r and $r->marc);
1477 $client->respond($_) for (_extract_856_uris($r));
1480 __PACKAGE__->register_method(
1481 api_name => "open-ils.ingest.856_uri.record",
1482 method => "get_uris_record",
1488 sub get_uris_object {
1493 my $max_uri = shift;
1495 return undef unless ($obj and $obj->marc);
1497 $client->respond($_) for (_extract_856_uris($obj, \$max_cn, \$max_uri));
1500 __PACKAGE__->register_method(
1501 api_name => "open-ils.ingest.856_uri.object",
1502 method => "get_uris_object",
1509 # --------------------------------------------------------------------------------
1512 package OpenILS::Application::Ingest::Biblio::Fingerprint;
1513 use base qw/OpenILS::Application::Ingest/;
1514 use Unicode::Normalize;
1515 use OpenSRF::EX qw/:try/;
1517 sub biblio_fingerprint_record {
1522 OpenILS::Application::Ingest->post_init();
1524 my $r = OpenSRF::AppSession
1525 ->create('open-ils.cstore')
1526 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1529 return undef unless ($r and $r->marc);
1531 my ($fp) = $self->method_lookup('open-ils.ingest.fingerprint.xml')->run($r->marc);
1532 $log->debug("Returning [$fp] as fingerprint for record $rec", INFO);
1533 $fp->{quality} = int($fp->{quality});
1536 __PACKAGE__->register_method(
1537 api_name => "open-ils.ingest.fingerprint.record",
1538 method => "biblio_fingerprint_record",
1544 sub biblio_fingerprint {
1547 my $xml = OpenILS::Application::AppUtils->entityize(shift);
1549 $log->internal("Got MARC [$xml]");
1552 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1553 my $conf = OpenSRF::Utils::SettingsClient->new;
1555 my $libs = $conf->config_value(@pfx, 'script_path');
1556 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_fingerprint');
1557 my $script_libs = (ref($libs)) ? $libs : [$libs];
1559 $log->debug("Loading script $script_file for biblio fingerprinting...");
1561 $fp_script = new OpenILS::Utils::ScriptRunner
1562 ( file => $script_file,
1563 paths => $script_libs,
1564 reset_count => 100 );
1567 $fp_script->insert('environment' => {marc => $xml} => 1);
1569 my $res = $fp_script->run || ($log->error( "Fingerprint script died! $@" ) && return undef);
1570 $log->debug("Script for biblio fingerprinting completed successfully...");
1574 __PACKAGE__->register_method(
1575 api_name => "open-ils.ingest.fingerprint.xml",
1576 method => "biblio_fingerprint",
1582 sub biblio_descriptor {
1585 my $xml = OpenILS::Application::AppUtils->entityize(shift);
1587 $log->internal("Got MARC [$xml]");
1590 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1591 my $conf = OpenSRF::Utils::SettingsClient->new;
1593 my $libs = $conf->config_value(@pfx, 'script_path');
1594 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_descriptor');
1595 my $script_libs = (ref($libs)) ? $libs : [$libs];
1597 $log->debug("Loading script $script_file for biblio descriptor extraction...");
1599 $rd_script = new OpenILS::Utils::ScriptRunner
1600 ( file => $script_file,
1601 paths => $script_libs,
1602 reset_count => 100 );
1605 $log->debug("Setting up environment for descriptor extraction script...");
1606 $rd_script->insert('environment.marc' => $xml => 1);
1607 $log->debug("Environment building complete...");
1609 my $res = $rd_script->run || ($log->error( "Descriptor script died! $@" ) && return undef);
1610 $log->debug("Script for biblio descriptor extraction completed successfully");
1612 my $d1 = $res->date1;
1613 if ($d1 && $d1 ne ' ') {
1618 my $d2 = $res->date2;
1619 if ($d2 && $d2 ne ' ') {
1626 __PACKAGE__->register_method(
1627 api_name => "open-ils.ingest.descriptor.xml",
1628 method => "biblio_descriptor",