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 my $umap = Fieldmapper::asset::uri_call_number_map->new;
179 $umap->uri($u->{uri}->id);
180 $umap->call_number($u->{call_number}->id);
182 $cstore->request( 'open-ils.cstore.direct.asset.uri_call_number_map.create' => $umap )->gather(1) if (!$tmp);
185 # update full_rec stuff ...
186 $tmp = $cstore->request(
187 'open-ils.cstore.direct.metabib.full_rec.id_list.atomic',
188 { record => $bib->id }
191 $cstore->request( 'open-ils.cstore.direct.metabib.full_rec.delete' => $_ )->gather(1) for (@$tmp);
192 $cstore->request( 'open-ils.cstore.direct.metabib.full_rec.create' => $_ )->gather(1) for (@{ $blob->{full_rec} });
194 # update rec_descriptor stuff ...
195 $tmp = $cstore->request(
196 'open-ils.cstore.direct.metabib.record_descriptor.id_list.atomic',
197 { record => $bib->id }
200 $cstore->request( 'open-ils.cstore.direct.metabib.record_descriptor.delete' => $_ )->gather(1) for (@$tmp);
201 $cstore->request( 'open-ils.cstore.direct.metabib.record_descriptor.create' => $blob->{descriptor} )->gather(1);
203 # deal with classed fields...
204 for my $class ( qw/title author subject keyword series/ ) {
205 $tmp = $cstore->request(
206 "open-ils.cstore.direct.metabib.${class}_field_entry.id_list.atomic",
207 { source => $bib->id }
210 $cstore->request( "open-ils.cstore.direct.metabib.${class}_field_entry.delete" => $_ )->gather(1) for (@$tmp);
212 for my $obj ( @{ $blob->{field_entries} } ) {
213 my $class = $obj->class_name;
214 $class =~ s/^Fieldmapper:://o;
216 $cstore->request( "open-ils.cstore.direct.$class.create" => $obj )->gather(1);
221 $tmp = $cstore->request(
222 'open-ils.cstore.direct.metabib.metarecord_source_map.search.atomic',
223 { source => $bib->id }
226 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord_source_map.delete' => $_->id )->gather(1) for (@$tmp);
229 my $old_mrs = $cstore->request(
230 'open-ils.cstore.direct.metabib.metarecord.search.atomic' => { id => [map { $_->metarecord } @$tmp] }
231 )->gather(1) if (@$tmp);
233 $old_mrs = [] if (!ref($old_mrs));
236 for my $m (@$old_mrs) {
237 if ($m->fingerprint eq $bib->fingerprint) {
240 my $others = $cstore->request(
241 'open-ils.cstore.direct.metabib.metarecord_source_map.id_list.atomic' => { metarecord => $m->id }
246 'open-ils.cstore.direct.metabib.metarecord.delete' => $m->id
256 # Get the matchin MR, if any.
257 $mr = $cstore->request(
258 'open-ils.cstore.direct.metabib.metarecord.search',
259 { fingerprint => $bib->fingerprint }
262 $holds = $cstore->request(
263 'open-ils.cstore.direct.action.hold_request.search.atomic',
264 { hold_type => 'M', target => [ map { $_->id } grep { $_->isdeleted } @$old_mrs ] }
265 )->gather(1) if (@$old_mrs);
268 for my $h (@$holds) {
270 $cstore->request( 'open-ils.cstore.direct.action.hold_request.update' => $h )->gather(1);
277 $mr = new Fieldmapper::metabib::metarecord;
278 $mr->fingerprint( $bib->fingerprint );
279 $mr->master_record( $bib->id );
282 "open-ils.cstore.direct.metabib.metarecord.create",
283 $mr => { quiet => 'true' }
287 for my $h (grep { !$_->ischanged } @$holds) {
289 $cstore->request( 'open-ils.cstore.direct.action.hold_request.update' => $h )->gather(1);
292 my $mrm = $cstore->request(
293 'open-ils.cstore.direct.metabib.metarecord_source_map.search.atomic',
294 { metarecord => $mr->id }
298 my $best = $cstore->request(
299 "open-ils.cstore.direct.biblio.record_entry.search",
300 { id => [ map { $_->source } @$mrm ] },
301 { 'select' => { bre => [ qw/id quality/ ] },
302 order_by => { bre => "quality desc" },
307 if ($best->quality > $bib->quality) {
308 $mr->master_record($best->id);
310 $mr->master_record($bib->id);
313 $mr->master_record($bib->id);
318 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord.update' => $mr )->gather(1);
321 my $mrm = new Fieldmapper::metabib::metarecord_source_map;
322 $mrm->source($bib->id);
323 $mrm->metarecord($mr->id);
325 $cstore->request( 'open-ils.cstore.direct.metabib.metarecord_source_map.create' => $mrm )->gather(1);
326 $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.update' => $bib )->gather(1);
328 $cstore->request( 'open-ils.cstore.transaction.commit' )->gather(1) || return undef;;
333 __PACKAGE__->register_method(
334 api_name => "open-ils.ingest.full.biblio.object",
335 method => "rw_biblio_ingest_single_object",
340 sub rw_biblio_ingest_single_record {
345 OpenILS::Application::Ingest->post_init();
346 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
347 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
349 my $r = $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )->gather(1);
351 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
354 return undef unless ($r and @$r);
356 return ($self->method_lookup("open-ils.ingest.full.biblio.object")->run($r))[0];
358 __PACKAGE__->register_method(
359 api_name => "open-ils.ingest.full.biblio.record",
360 method => "rw_biblio_ingest_single_record",
365 sub rw_biblio_ingest_record_list {
368 my @rec = ref($_[0]) ? @{ $_[0] } : @_ ;
370 OpenILS::Application::Ingest->post_init();
371 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
372 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
374 my $r = $cstore->request( 'open-ils.cstore.direct.biblio.record_entry.search.atomic' => { id => \@rec } )->gather(1);
376 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
379 return undef unless ($r and @$r);
383 if (($self->method_lookup("open-ils.ingest.full.biblio.object")->run($_))[0]) {
389 __PACKAGE__->register_method(
390 api_name => "open-ils.ingest.full.biblio.record_list",
391 method => "rw_biblio_ingest_record_list",
396 sub ro_biblio_ingest_single_object {
400 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
404 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
407 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
408 $max_cn = int($cn->id) + 1000;
412 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
413 $max_uri = int($cn->id) + 1000;
418 my $document = $parser->parse_string($xml);
420 my @uris = $self->method_lookup("open-ils.ingest.856_uri.object")->run($bib, $max_cn, $max_uri);
421 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
422 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
423 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
424 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
426 $_->source($bib->id) for (@mXfe);
427 $_->record($bib->id) for (@mfr);
428 $rd->record($bib->id) if ($rd);
430 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd, uri => \@uris };
432 __PACKAGE__->register_method(
433 api_name => "open-ils.ingest.full.biblio.object.readonly",
434 method => "ro_biblio_ingest_single_object",
439 sub ro_biblio_ingest_single_xml {
442 my $xml = OpenILS::Application::AppUtils->entityize(shift);
444 my $document = $parser->parse_string($xml);
446 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
447 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
448 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
449 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
451 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd };
453 __PACKAGE__->register_method(
454 api_name => "open-ils.ingest.full.biblio.xml.readonly",
455 method => "ro_biblio_ingest_single_xml",
460 sub ro_biblio_ingest_single_record {
465 OpenILS::Application::Ingest->post_init();
466 my $r = OpenSRF::AppSession
467 ->create('open-ils.cstore')
468 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
471 return undef unless ($r and @$r);
473 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($r->marc);
475 $_->source($rec) for (@{$res->{field_entries}});
476 $_->record($rec) for (@{$res->{full_rec}});
477 $res->{descriptor}->record($rec);
481 __PACKAGE__->register_method(
482 api_name => "open-ils.ingest.full.biblio.record.readonly",
483 method => "ro_biblio_ingest_single_record",
488 sub ro_biblio_ingest_stream_record {
492 OpenILS::Application::Ingest->post_init();
494 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
496 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
498 my $rec = $resp->content;
499 last unless (defined $rec);
501 $log->debug("Running open-ils.ingest.full.biblio.record.readonly ...");
502 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.record.readonly")->run($rec);
504 $_->source($rec) for (@{$res->{field_entries}});
505 $_->record($rec) for (@{$res->{full_rec}});
507 $client->respond( $res );
512 __PACKAGE__->register_method(
513 api_name => "open-ils.ingest.full.biblio.record_stream.readonly",
514 method => "ro_biblio_ingest_stream_record",
519 sub ro_biblio_ingest_stream_xml {
523 OpenILS::Application::Ingest->post_init();
525 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
527 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
529 my $xml = $resp->content;
530 last unless (defined $xml);
532 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
533 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($xml);
535 $client->respond( $res );
540 __PACKAGE__->register_method(
541 api_name => "open-ils.ingest.full.biblio.xml_stream.readonly",
542 method => "ro_biblio_ingest_stream_xml",
547 sub rw_biblio_ingest_stream_import {
551 OpenILS::Application::Ingest->post_init();
553 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
555 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
557 my $bib = $resp->content;
558 last unless (defined $bib);
560 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
561 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($bib->marc);
563 $_->source($bib->id) for (@{$res->{field_entries}});
564 $_->record($bib->id) for (@{$res->{full_rec}});
566 $client->respond( $res );
571 __PACKAGE__->register_method(
572 api_name => "open-ils.ingest.full.biblio.bib_stream.import",
573 method => "rw_biblio_ingest_stream_import",
579 # --------------------------------------------------------------------------------
582 package OpenILS::Application::Ingest::Authority;
583 use base qw/OpenILS::Application::Ingest/;
584 use Unicode::Normalize;
586 sub rw_authority_ingest_single_object {
591 my ($blob) = $self->method_lookup("open-ils.ingest.full.authority.object.readonly")->run($auth);
592 return undef unless ($blob);
594 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
596 my $xact = $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
599 # update full_rec stuff ...
600 $tmp = $cstore->request(
601 'open-ils.cstore.direct.authority.full_rec.id_list.atomic',
602 { record => $auth->id }
605 $cstore->request( 'open-ils.cstore.direct.authority.full_rec.delete' => $_ )->gather(1) for (@$tmp);
606 $cstore->request( 'open-ils.cstore.direct.authority.full_rec.create' => $_ )->gather(1) for (@{ $blob->{full_rec} });
608 # XXX when we start extracting authority descriptors and adding sources ...
610 # update rec_descriptor stuff ...
611 #$tmp = $cstore->request(
612 # 'open-ils.cstore.direct.authority.record_descriptor.id_list.atomic',
613 # { record => $auth->id }
616 #$cstore->request( 'open-ils.cstore.direct.authority.record_descriptor.delete' => $_ )->gather(1) for (@$tmp);
617 #$cstore->request( 'open-ils.cstore.direct.authority.record_descriptor.create' => $blob->{descriptor} )->gather(1);
618 #$cstore->request( 'open-ils.cstore.direct.authority.record_entry.update' => $auth )->gather(1);
620 $cstore->request( 'open-ils.cstore.transaction.commit' )->gather(1) || return undef;;
625 __PACKAGE__->register_method(
626 api_name => "open-ils.ingest.full.authority.object",
627 method => "rw_authority_ingest_single_object",
632 sub rw_authority_ingest_single_record {
637 OpenILS::Application::Ingest->post_init();
638 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
639 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
641 my $r = $cstore->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )->gather(1);
643 $cstore->request('open-ils.cstore.transaction.rollback')->gather(1);
646 return undef unless ($r and @$r);
648 return ($self->method_lookup("open-ils.ingest.full.authority.object")->run($r))[0];
650 __PACKAGE__->register_method(
651 api_name => "open-ils.ingest.full.authority.record",
652 method => "rw_authority_ingest_single_record",
657 sub ro_authority_ingest_single_object {
661 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
663 my $document = $parser->parse_string($xml);
665 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
667 $_->record($bib->id) for (@mfr);
669 return { full_rec => \@mfr };
671 __PACKAGE__->register_method(
672 api_name => "open-ils.ingest.full.authority.object.readonly",
673 method => "ro_authority_ingest_single_object",
677 __PACKAGE__->register_method(
678 api_name => "open-ils.ingest.full.serial.object.readonly",
679 method => "ro_authority_ingest_single_object",
685 sub ro_authority_ingest_single_xml {
688 my $xml = OpenILS::Application::AppUtils->entityize(shift);
690 my $document = $parser->parse_string($xml);
692 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
694 return { full_rec => \@mfr };
696 __PACKAGE__->register_method(
697 api_name => "open-ils.ingest.full.authority.xml.readonly",
698 method => "ro_authority_ingest_single_xml",
703 sub ro_authority_ingest_single_record {
708 OpenILS::Application::Ingest->post_init();
709 my $r = OpenSRF::AppSession
710 ->create('open-ils.cstore')
711 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
714 return undef unless ($r and @$r);
716 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($r->marc);
718 $_->record($rec) for (@{$res->{full_rec}});
719 $res->{descriptor}->record($rec);
723 __PACKAGE__->register_method(
724 api_name => "open-ils.ingest.full.authority.record.readonly",
725 method => "ro_authority_ingest_single_record",
730 sub ro_authority_ingest_stream_record {
734 OpenILS::Application::Ingest->post_init();
736 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
738 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
740 my $rec = $resp->content;
741 last unless (defined $rec);
743 $log->debug("Running open-ils.ingest.full.authority.record.readonly ...");
744 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.record.readonly")->run($rec);
746 $_->record($rec) for (@{$res->{full_rec}});
748 $client->respond( $res );
753 __PACKAGE__->register_method(
754 api_name => "open-ils.ingest.full.authority.record_stream.readonly",
755 method => "ro_authority_ingest_stream_record",
760 sub ro_authority_ingest_stream_xml {
764 OpenILS::Application::Ingest->post_init();
766 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
768 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
770 my $xml = $resp->content;
771 last unless (defined $xml);
773 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
774 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($xml);
776 $client->respond( $res );
781 __PACKAGE__->register_method(
782 api_name => "open-ils.ingest.full.authority.xml_stream.readonly",
783 method => "ro_authority_ingest_stream_xml",
788 sub rw_authority_ingest_stream_import {
792 OpenILS::Application::Ingest->post_init();
794 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
796 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
798 my $bib = $resp->content;
799 last unless (defined $bib);
801 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
802 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($bib->marc);
804 $_->record($bib->id) for (@{$res->{full_rec}});
806 $client->respond( $res );
811 __PACKAGE__->register_method(
812 api_name => "open-ils.ingest.full.authority.bib_stream.import",
813 method => "rw_authority_ingest_stream_import",
818 # --------------------------------------------------------------------------------
821 package OpenILS::Application::Ingest::Serial;
822 use base qw/OpenILS::Application::Ingest/;
823 use Unicode::Normalize;
825 sub ro_serial_ingest_single_object {
829 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
831 my $document = $parser->parse_string($xml);
833 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.serial.xml")->run($document);
835 $_->record($bib->id) for (@mfr);
837 return { full_rec => \@mfr };
839 __PACKAGE__->register_method(
840 api_name => "open-ils.ingest.full.serial.object.readonly",
841 method => "ro_serial_ingest_single_object",
846 sub ro_serial_ingest_single_xml {
849 my $xml = OpenILS::Application::AppUtils->entityize(shift);
851 my $document = $parser->parse_string($xml);
853 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.serial.xml")->run($document);
855 return { full_rec => \@mfr };
857 __PACKAGE__->register_method(
858 api_name => "open-ils.ingest.full.serial.xml.readonly",
859 method => "ro_serial_ingest_single_xml",
864 sub ro_serial_ingest_single_record {
869 OpenILS::Application::Ingest->post_init();
870 my $r = OpenSRF::AppSession
871 ->create('open-ils.cstore')
872 ->request( 'open-ils.cstore.direct.serial.record_entry.retrieve' => $rec )
875 return undef unless ($r and @$r);
877 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($r->marc);
879 $_->record($rec) for (@{$res->{full_rec}});
880 $res->{descriptor}->record($rec);
884 __PACKAGE__->register_method(
885 api_name => "open-ils.ingest.full.serial.record.readonly",
886 method => "ro_serial_ingest_single_record",
891 sub ro_serial_ingest_stream_record {
895 OpenILS::Application::Ingest->post_init();
897 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
899 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
901 my $rec = $resp->content;
902 last unless (defined $rec);
904 $log->debug("Running open-ils.ingest.full.serial.record.readonly ...");
905 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.record.readonly")->run($rec);
907 $_->record($rec) for (@{$res->{full_rec}});
909 $client->respond( $res );
914 __PACKAGE__->register_method(
915 api_name => "open-ils.ingest.full.serial.record_stream.readonly",
916 method => "ro_serial_ingest_stream_record",
921 sub ro_serial_ingest_stream_xml {
925 OpenILS::Application::Ingest->post_init();
927 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
929 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
931 my $xml = $resp->content;
932 last unless (defined $xml);
934 $log->debug("Running open-ils.ingest.full.serial.xml.readonly ...");
935 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($xml);
937 $client->respond( $res );
942 __PACKAGE__->register_method(
943 api_name => "open-ils.ingest.full.serial.xml_stream.readonly",
944 method => "ro_serial_ingest_stream_xml",
949 sub rw_serial_ingest_stream_import {
953 OpenILS::Application::Ingest->post_init();
955 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
957 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
959 my $bib = $resp->content;
960 last unless (defined $bib);
962 $log->debug("Running open-ils.ingest.full.serial.xml.readonly ...");
963 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($bib->marc);
965 $_->record($bib->id) for (@{$res->{full_rec}});
967 $client->respond( $res );
972 __PACKAGE__->register_method(
973 api_name => "open-ils.ingest.full.serial.bib_stream.import",
974 method => "rw_serial_ingest_stream_import",
980 # --------------------------------------------------------------------------------
981 # MARC index extraction
983 package OpenILS::Application::Ingest::XPATH;
984 use base qw/OpenILS::Application::Ingest/;
985 use Unicode::Normalize;
987 # give this an XML documentElement and an XPATH expression
988 sub xpath_to_string {
992 my $ns_prefix = shift;
995 $xml->setNamespace( $ns_uri, $ns_prefix, 1 ) if ($ns_uri && $ns_prefix);
999 # grab the set of matching nodes
1000 my @nodes = $xml->findnodes( $xpath );
1001 for my $value (@nodes) {
1003 # grab all children of the node
1004 my @children = $value->childNodes();
1005 for my $child (@children) {
1007 # add the childs content to the growing buffer
1008 my $content = quotemeta($child->textContent);
1009 next if ($unique && $string =~ /$content/); # uniquify the values
1010 $string .= $child->textContent . " ";
1013 $string .= $value->textContent . " ";
1017 $string =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1018 $string =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1020 return NFD($string);
1023 sub class_index_string_xml {
1029 OpenILS::Application::Ingest->post_init();
1030 $xml = $parser->parse_string(OpenILS::Application::AppUtils->entityize($xml)) unless (ref $xml);
1032 my %transform_cache;
1034 for my $class (@classes) {
1035 my $class_constructor = "Fieldmapper::metabib::${class}_field_entry";
1036 for my $type ( keys %{ $xpathset->{$class} } ) {
1038 my $def = $xpathset->{$class}->{$type};
1039 my $sf = $OpenILS::Application::Ingest::supported_formats{$def->{format}};
1041 my $document = $xml;
1044 $document = $transform_cache{$def->{format}} || $sf->{xslt}->transform($xml);
1045 $transform_cache{$def->{format}} = $document;
1048 my $value = xpath_to_string(
1049 $document->documentElement => $def->{xpath},
1050 $sf->{ns} => $def->{format},
1056 $value = NFD($value);
1057 $value =~ s/\pM+//sgo;
1058 $value =~ s/\pC+//sgo;
1059 $value =~ s/\W+$//sgo;
1061 $value =~ s/\b\.+\b//sgo;
1062 $value = lc($value);
1064 my $fm = $class_constructor->new;
1065 $fm->value( $value );
1066 $fm->field( $xpathset->{$class}->{$type}->{id} );
1067 $client->respond($fm);
1072 __PACKAGE__->register_method(
1073 api_name => "open-ils.ingest.field_entry.class.xml",
1074 method => "class_index_string_xml",
1080 sub class_index_string_record {
1084 my @classes = shift;
1086 OpenILS::Application::Ingest->post_init();
1087 my $r = OpenSRF::AppSession
1088 ->create('open-ils.cstore')
1089 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
1092 return undef unless ($r and @$r);
1094 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, @classes)) {
1096 $client->respond($fm);
1100 __PACKAGE__->register_method(
1101 api_name => "open-ils.ingest.field_entry.class.record",
1102 method => "class_index_string_record",
1108 sub all_index_string_xml {
1113 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($xml, keys(%$xpathset))) {
1114 $client->respond($fm);
1118 __PACKAGE__->register_method(
1119 api_name => "open-ils.ingest.extract.field_entry.all.xml",
1120 method => "all_index_string_xml",
1126 sub all_index_string_record {
1131 OpenILS::Application::Ingest->post_init();
1132 my $r = OpenSRF::AppSession
1133 ->create('open-ils.cstore')
1134 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1137 return undef unless ($r and @$r);
1139 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, keys(%$xpathset))) {
1141 $client->respond($fm);
1145 __PACKAGE__->register_method(
1146 api_name => "open-ils.ingest.extract.field_entry.all.record",
1147 method => "all_index_string_record",
1153 # --------------------------------------------------------------------------------
1156 package OpenILS::Application::Ingest::FlatMARC;
1157 use base qw/OpenILS::Application::Ingest/;
1158 use Unicode::Normalize;
1161 sub _marcxml_to_full_rows {
1163 my $marcxml = shift;
1164 my $xmltype = shift || 'metabib';
1166 my $type = "Fieldmapper::${xmltype}::full_rec";
1170 my ($root) = $marcxml->findnodes('//*[local-name()="record"]');
1172 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
1173 next unless $tagline;
1175 my $ns = $type->new;
1178 my $val = $tagline->textContent;
1180 $val =~ s/\pM+//sgo;
1181 $val =~ s/\pC+//sgo;
1182 $val =~ s/\W+$//sgo;
1188 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
1189 next unless $tagline;
1191 my $ns = $type->new;
1193 $ns->tag( $tagline->getAttribute( "tag" ) );
1194 my $val = $tagline->textContent;
1196 $val =~ s/\pM+//sgo;
1197 $val =~ s/\pC+//sgo;
1198 $val =~ s/\W+$//sgo;
1204 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
1205 next unless $tagline;
1207 my $tag = $tagline->getAttribute( "tag" );
1208 my $ind1 = $tagline->getAttribute( "ind1" );
1209 my $ind2 = $tagline->getAttribute( "ind2" );
1211 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
1214 my $ns = $type->new;
1219 $ns->subfield( $data->getAttribute( "code" ) );
1220 my $val = $data->textContent;
1222 $val =~ s/\pM+//sgo;
1223 $val =~ s/\pC+//sgo;
1224 $val =~ s/\W+$//sgo;
1225 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1226 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1227 $ns->value( lc($val) );
1232 if ($xmltype eq 'metabib' and $tag eq '245') {
1235 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
1236 next unless ($data and $data->getAttribute( "code" ) eq 'a');
1243 $ns->subfield( $data->getAttribute( "code" ) );
1244 my $val = substr( $data->textContent, $ind2 );
1246 $val =~ s/\pM+//sgo;
1247 $val =~ s/\pC+//sgo;
1248 $val =~ s/\W+$//sgo;
1249 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1250 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1251 $ns->value( lc($val) );
1258 $log->debug("Returning ".scalar(@ns_list)." Fieldmapper nodes from $xmltype xml");
1267 $log->debug("processing [$xml]");
1269 $xml = $parser->parse_string(OpenILS::Application::AppUtils->entityize($xml)) unless (ref $xml);
1271 my $type = 'metabib';
1272 $type = 'authority' if ($self->api_name =~ /authority/o);
1273 $type = 'serial' if ($self->api_name =~ /serial/o);
1275 OpenILS::Application::Ingest->post_init();
1277 $client->respond($_) for (_marcxml_to_full_rows($xml, $type));
1280 __PACKAGE__->register_method(
1281 api_name => "open-ils.ingest.flat_marc.authority.xml",
1282 method => "flat_marc_xml",
1287 __PACKAGE__->register_method(
1288 api_name => "open-ils.ingest.flat_marc.biblio.xml",
1289 method => "flat_marc_xml",
1294 __PACKAGE__->register_method(
1295 api_name => "open-ils.ingest.flat_marc.serial.xml",
1296 method => "flat_marc_xml",
1302 sub flat_marc_record {
1307 my $type = 'biblio';
1308 $type = 'authority' if ($self->api_name =~ /authority/o);
1309 $type = 'serial' if ($self->api_name =~ /serial/o);
1311 OpenILS::Application::Ingest->post_init();
1312 my $r = OpenSRF::AppSession
1313 ->create('open-ils.cstore')
1314 ->request( "open-ils.cstore.direct.${type}.record_entry.retrieve" => $rec )
1318 return undef unless ($r and $r->marc);
1320 my @rows = $self->method_lookup("open-ils.ingest.flat_marc.$type.xml")->run($r->marc);
1321 for my $row (@rows) {
1322 $client->respond($row);
1323 $log->debug(OpenSRF::Utils::JSON->perl2JSON($row), DEBUG);
1327 __PACKAGE__->register_method(
1328 api_name => "open-ils.ingest.flat_marc.biblio.record_entry",
1329 method => "flat_marc_record",
1334 __PACKAGE__->register_method(
1335 api_name => "open-ils.ingest.flat_marc.authority.record_entry",
1336 method => "flat_marc_record",
1341 __PACKAGE__->register_method(
1342 api_name => "open-ils.ingest.flat_marc.serial.record_entry",
1343 method => "flat_marc_record",
1349 # --------------------------------------------------------------------------------
1352 package OpenILS::Application::Ingest::Biblio::URI;
1353 use base qw/OpenILS::Application::Ingest/;
1354 use Unicode::Normalize;
1355 use OpenSRF::EX qw/:try/;
1358 sub _extract_856_uris {
1362 my $max_uri = shift;
1365 my $recid = $rec->id;
1366 my $marcxml = $rec->marc;
1368 my $document = $parser->parse_string($marcxml);
1369 my @nodes = $document->findnodes('//*[local-name()="datafield" and @tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]');
1371 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
1375 for my $node (@nodes) {
1376 # first, is there a URI?
1377 my $href = $node->findvalue('*[local-name()="subfield" and @code="u"]/text()');
1378 next unless ($href);
1380 # now, find the best possible label
1381 my $label = $node->findvalue('*[local-name()="subfield" and @code="y"]/text()');
1382 $label ||= $node->findvalue('*[local-name()="subfield" and @code="3"]/text()');
1386 my $use = $node->findvalue('*[local-name()="subfield" and @code="z"]/text()');
1387 $use ||= $node->findvalue('*[local-name()="subfield" and @code="2"]/text()');
1388 $use ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1390 # moving on to the URI owner
1391 my $owner = $node->findvalue('*[local-name()="subfield" and @code="9"]/text()'); # Evergreen special sauce
1392 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="w"]/text()');
1393 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1395 $owner =~ s/^.*?\((\w+)\).*$/$1/o; # unwrap first paren-enclosed string and then ...
1397 # no owner? skip it :(
1398 next unless ($owner);
1401 ->request( 'open-ils.cstore.direct.actor.org_unit.search' => { shortname => $owner} )
1406 # now we can construct the uri object
1408 ->request( 'open-ils.cstore.direct.asset.uri.search' => { label => $label, href => $href, use_restriction => $use, active => 't' } )
1412 $uri = Fieldmapper::asset::uri->new;
1414 $uri->id( $$max_uri++ );
1415 $uri->label($label);
1418 $uri->use_restriction($use);
1421 # see if we need to create a call number
1422 my $cn = $cn_cache{$org->id};
1423 $cn = $cn->clone if ($cn);
1424 $cn->clear_isnew if ($cn);
1427 ->request( 'open-ils.cstore.direct.asset.call_number.search' => { owning_lib => $org->id, record => $recid, label => '##URI##' } )
1431 $cn = Fieldmapper::asset::call_number->new;
1434 $cn->id( $$max_cn++ );
1435 $cn->owning_lib( $org->id );
1436 $cn->record( $recid );
1437 $cn->create_date( 'now' );
1438 $cn->creator( $rec->creator );
1439 $cn->editor( $rec->editor );
1440 $cn->edit_date( 'now' );
1441 $cn->label( '##URI##' );
1444 $cn_cache{$org->id} = $cn;
1446 push @objects, { uri => $uri, call_number => $cn };
1449 $log->debug("Returning ".scalar(@objects)." URI nodes for record $recid");
1450 $cstore->disconnect;
1454 sub get_uris_record {
1459 OpenILS::Application::Ingest->post_init();
1460 my $r = OpenSRF::AppSession
1461 ->create('open-ils.cstore')
1462 ->request( "open-ils.cstore.direct.biblio.record_entry.retrieve" => $rec )
1465 return undef unless ($r and $r->marc);
1467 $client->respond($_) for (_extract_856_uris($r));
1470 __PACKAGE__->register_method(
1471 api_name => "open-ils.ingest.856_uri.record",
1472 method => "get_uris_record",
1478 sub get_uris_object {
1483 my $max_uri = shift;
1485 return undef unless ($obj and $obj->marc);
1487 $client->respond($_) for (_extract_856_uris($obj, \$max_cn, \$max_uri));
1490 __PACKAGE__->register_method(
1491 api_name => "open-ils.ingest.856_uri.object",
1492 method => "get_uris_object",
1499 # --------------------------------------------------------------------------------
1502 package OpenILS::Application::Ingest::Biblio::Fingerprint;
1503 use base qw/OpenILS::Application::Ingest/;
1504 use Unicode::Normalize;
1505 use OpenSRF::EX qw/:try/;
1507 sub biblio_fingerprint_record {
1512 OpenILS::Application::Ingest->post_init();
1514 my $r = OpenSRF::AppSession
1515 ->create('open-ils.cstore')
1516 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1519 return undef unless ($r and $r->marc);
1521 my ($fp) = $self->method_lookup('open-ils.ingest.fingerprint.xml')->run($r->marc);
1522 $log->debug("Returning [$fp] as fingerprint for record $rec", INFO);
1523 $fp->{quality} = int($fp->{quality});
1526 __PACKAGE__->register_method(
1527 api_name => "open-ils.ingest.fingerprint.record",
1528 method => "biblio_fingerprint_record",
1534 sub biblio_fingerprint {
1537 my $xml = OpenILS::Application::AppUtils->entityize(shift);
1539 $log->internal("Got MARC [$xml]");
1542 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1543 my $conf = OpenSRF::Utils::SettingsClient->new;
1545 my $libs = $conf->config_value(@pfx, 'script_path');
1546 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_fingerprint');
1547 my $script_libs = (ref($libs)) ? $libs : [$libs];
1549 $log->debug("Loading script $script_file for biblio fingerprinting...");
1551 $fp_script = new OpenILS::Utils::ScriptRunner
1552 ( file => $script_file,
1553 paths => $script_libs,
1554 reset_count => 100 );
1557 $fp_script->insert('environment' => {marc => $xml} => 1);
1559 my $res = $fp_script->run || ($log->error( "Fingerprint script died! $@" ) && return undef);
1560 $log->debug("Script for biblio fingerprinting completed successfully...");
1564 __PACKAGE__->register_method(
1565 api_name => "open-ils.ingest.fingerprint.xml",
1566 method => "biblio_fingerprint",
1572 sub biblio_descriptor {
1575 my $xml = OpenILS::Application::AppUtils->entityize(shift);
1577 $log->internal("Got MARC [$xml]");
1580 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1581 my $conf = OpenSRF::Utils::SettingsClient->new;
1583 my $libs = $conf->config_value(@pfx, 'script_path');
1584 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_descriptor');
1585 my $script_libs = (ref($libs)) ? $libs : [$libs];
1587 $log->debug("Loading script $script_file for biblio descriptor extraction...");
1589 $rd_script = new OpenILS::Utils::ScriptRunner
1590 ( file => $script_file,
1591 paths => $script_libs,
1592 reset_count => 100 );
1595 $log->debug("Setting up environment for descriptor extraction script...");
1596 $rd_script->insert('environment.marc' => $xml => 1);
1597 $log->debug("Environment building complete...");
1599 my $res = $rd_script->run || ($log->error( "Descriptor script died! $@" ) && return undef);
1600 $log->debug("Script for biblio descriptor extraction completed successfully");
1602 my $d1 = $res->date1;
1603 if ($d1 && $d1 ne ' ') {
1608 my $d2 = $res->date2;
1609 if ($d2 && $d2 ne ' ') {
1616 __PACKAGE__->register_method(
1617 api_name => "open-ils.ingest.descriptor.xml",
1618 method => "biblio_descriptor",