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);
382 $count += ($self->method_lookup("open-ils.ingest.full.biblio.object")->run($_))[0] for (@$r);
386 __PACKAGE__->register_method(
387 api_name => "open-ils.ingest.full.biblio.record_list",
388 method => "rw_biblio_ingest_record_list",
393 sub ro_biblio_ingest_single_object {
397 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
401 my $cstore = OpenSRF::AppSession->connect( 'open-ils.cstore' );
404 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
405 $max_cn = int($cn->id) + 1000;
409 my $cn = $cstore->request( 'open-ils.cstore.direct.asset.call_number.search' => { id => { '!=' => undef } }, { limit => 1, order_by => { acn => 'id desc' } } )->gather(1);
410 $max_uri = int($cn->id) + 1000;
415 my $document = $parser->parse_string($xml);
417 my @uris = $self->method_lookup("open-ils.ingest.856_uri.object")->run($bib, $max_cn, $max_uri);
418 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
419 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
420 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
421 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
423 $_->source($bib->id) for (@mXfe);
424 $_->record($bib->id) for (@mfr);
425 $rd->record($bib->id) if ($rd);
427 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd, uri => \@uris };
429 __PACKAGE__->register_method(
430 api_name => "open-ils.ingest.full.biblio.object.readonly",
431 method => "ro_biblio_ingest_single_object",
436 sub ro_biblio_ingest_single_xml {
439 my $xml = OpenILS::Application::AppUtils->entityize(shift);
441 my $document = $parser->parse_string($xml);
443 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.biblio.xml")->run($document);
444 my @mXfe = $self->method_lookup("open-ils.ingest.extract.field_entry.all.xml")->run($document);
445 my ($fp) = $self->method_lookup("open-ils.ingest.fingerprint.xml")->run($xml);
446 my ($rd) = $self->method_lookup("open-ils.ingest.descriptor.xml")->run($xml);
448 return { full_rec => \@mfr, field_entries => \@mXfe, fingerprint => $fp, descriptor => $rd };
450 __PACKAGE__->register_method(
451 api_name => "open-ils.ingest.full.biblio.xml.readonly",
452 method => "ro_biblio_ingest_single_xml",
457 sub ro_biblio_ingest_single_record {
462 OpenILS::Application::Ingest->post_init();
463 my $r = OpenSRF::AppSession
464 ->create('open-ils.cstore')
465 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
468 return undef unless ($r and @$r);
470 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($r->marc);
472 $_->source($rec) for (@{$res->{field_entries}});
473 $_->record($rec) for (@{$res->{full_rec}});
474 $res->{descriptor}->record($rec);
478 __PACKAGE__->register_method(
479 api_name => "open-ils.ingest.full.biblio.record.readonly",
480 method => "ro_biblio_ingest_single_record",
485 sub ro_biblio_ingest_stream_record {
489 OpenILS::Application::Ingest->post_init();
491 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
493 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
495 my $rec = $resp->content;
496 last unless (defined $rec);
498 $log->debug("Running open-ils.ingest.full.biblio.record.readonly ...");
499 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.record.readonly")->run($rec);
501 $_->source($rec) for (@{$res->{field_entries}});
502 $_->record($rec) for (@{$res->{full_rec}});
504 $client->respond( $res );
509 __PACKAGE__->register_method(
510 api_name => "open-ils.ingest.full.biblio.record_stream.readonly",
511 method => "ro_biblio_ingest_stream_record",
516 sub ro_biblio_ingest_stream_xml {
520 OpenILS::Application::Ingest->post_init();
522 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
524 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
526 my $xml = $resp->content;
527 last unless (defined $xml);
529 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
530 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($xml);
532 $client->respond( $res );
537 __PACKAGE__->register_method(
538 api_name => "open-ils.ingest.full.biblio.xml_stream.readonly",
539 method => "ro_biblio_ingest_stream_xml",
544 sub rw_biblio_ingest_stream_import {
548 OpenILS::Application::Ingest->post_init();
550 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
552 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
554 my $bib = $resp->content;
555 last unless (defined $bib);
557 $log->debug("Running open-ils.ingest.full.biblio.xml.readonly ...");
558 my ($res) = $self->method_lookup("open-ils.ingest.full.biblio.xml.readonly")->run($bib->marc);
560 $_->source($bib->id) for (@{$res->{field_entries}});
561 $_->record($bib->id) for (@{$res->{full_rec}});
563 $client->respond( $res );
568 __PACKAGE__->register_method(
569 api_name => "open-ils.ingest.full.biblio.bib_stream.import",
570 method => "rw_biblio_ingest_stream_import",
576 # --------------------------------------------------------------------------------
579 package OpenILS::Application::Ingest::Authority;
580 use base qw/OpenILS::Application::Ingest/;
581 use Unicode::Normalize;
583 sub ro_authority_ingest_single_object {
587 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
589 my $document = $parser->parse_string($xml);
591 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
593 $_->record($bib->id) for (@mfr);
595 return { full_rec => \@mfr };
597 __PACKAGE__->register_method(
598 api_name => "open-ils.ingest.full.authority.object.readonly",
599 method => "ro_authority_ingest_single_object",
603 __PACKAGE__->register_method(
604 api_name => "open-ils.ingest.full.serial.object.readonly",
605 method => "ro_authority_ingest_single_object",
611 sub ro_authority_ingest_single_xml {
614 my $xml = OpenILS::Application::AppUtils->entityize(shift);
616 my $document = $parser->parse_string($xml);
618 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.authority.xml")->run($document);
620 return { full_rec => \@mfr };
622 __PACKAGE__->register_method(
623 api_name => "open-ils.ingest.full.authority.xml.readonly",
624 method => "ro_authority_ingest_single_xml",
629 sub ro_authority_ingest_single_record {
634 OpenILS::Application::Ingest->post_init();
635 my $r = OpenSRF::AppSession
636 ->create('open-ils.cstore')
637 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
640 return undef unless ($r and @$r);
642 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($r->marc);
644 $_->record($rec) for (@{$res->{full_rec}});
645 $res->{descriptor}->record($rec);
649 __PACKAGE__->register_method(
650 api_name => "open-ils.ingest.full.authority.record.readonly",
651 method => "ro_authority_ingest_single_record",
656 sub ro_authority_ingest_stream_record {
660 OpenILS::Application::Ingest->post_init();
662 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
664 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
666 my $rec = $resp->content;
667 last unless (defined $rec);
669 $log->debug("Running open-ils.ingest.full.authority.record.readonly ...");
670 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.record.readonly")->run($rec);
672 $_->record($rec) for (@{$res->{full_rec}});
674 $client->respond( $res );
679 __PACKAGE__->register_method(
680 api_name => "open-ils.ingest.full.authority.record_stream.readonly",
681 method => "ro_authority_ingest_stream_record",
686 sub ro_authority_ingest_stream_xml {
690 OpenILS::Application::Ingest->post_init();
692 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
694 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
696 my $xml = $resp->content;
697 last unless (defined $xml);
699 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
700 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($xml);
702 $client->respond( $res );
707 __PACKAGE__->register_method(
708 api_name => "open-ils.ingest.full.authority.xml_stream.readonly",
709 method => "ro_authority_ingest_stream_xml",
714 sub rw_authority_ingest_stream_import {
718 OpenILS::Application::Ingest->post_init();
720 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
722 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
724 my $bib = $resp->content;
725 last unless (defined $bib);
727 $log->debug("Running open-ils.ingest.full.authority.xml.readonly ...");
728 my ($res) = $self->method_lookup("open-ils.ingest.full.authority.xml.readonly")->run($bib->marc);
730 $_->record($bib->id) for (@{$res->{full_rec}});
732 $client->respond( $res );
737 __PACKAGE__->register_method(
738 api_name => "open-ils.ingest.full.authority.bib_stream.import",
739 method => "rw_authority_ingest_stream_import",
744 # --------------------------------------------------------------------------------
747 package OpenILS::Application::Ingest::Serial;
748 use base qw/OpenILS::Application::Ingest/;
749 use Unicode::Normalize;
751 sub ro_serial_ingest_single_object {
755 my $xml = OpenILS::Application::AppUtils->entityize($bib->marc);
757 my $document = $parser->parse_string($xml);
759 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.serial.xml")->run($document);
761 $_->record($bib->id) for (@mfr);
763 return { full_rec => \@mfr };
765 __PACKAGE__->register_method(
766 api_name => "open-ils.ingest.full.serial.object.readonly",
767 method => "ro_serial_ingest_single_object",
772 sub ro_serial_ingest_single_xml {
775 my $xml = OpenILS::Application::AppUtils->entityize(shift);
777 my $document = $parser->parse_string($xml);
779 my @mfr = $self->method_lookup("open-ils.ingest.flat_marc.serial.xml")->run($document);
781 return { full_rec => \@mfr };
783 __PACKAGE__->register_method(
784 api_name => "open-ils.ingest.full.serial.xml.readonly",
785 method => "ro_serial_ingest_single_xml",
790 sub ro_serial_ingest_single_record {
795 OpenILS::Application::Ingest->post_init();
796 my $r = OpenSRF::AppSession
797 ->create('open-ils.cstore')
798 ->request( 'open-ils.cstore.direct.serial.record_entry.retrieve' => $rec )
801 return undef unless ($r and @$r);
803 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($r->marc);
805 $_->record($rec) for (@{$res->{full_rec}});
806 $res->{descriptor}->record($rec);
810 __PACKAGE__->register_method(
811 api_name => "open-ils.ingest.full.serial.record.readonly",
812 method => "ro_serial_ingest_single_record",
817 sub ro_serial_ingest_stream_record {
821 OpenILS::Application::Ingest->post_init();
823 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
825 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
827 my $rec = $resp->content;
828 last unless (defined $rec);
830 $log->debug("Running open-ils.ingest.full.serial.record.readonly ...");
831 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.record.readonly")->run($rec);
833 $_->record($rec) for (@{$res->{full_rec}});
835 $client->respond( $res );
840 __PACKAGE__->register_method(
841 api_name => "open-ils.ingest.full.serial.record_stream.readonly",
842 method => "ro_serial_ingest_stream_record",
847 sub ro_serial_ingest_stream_xml {
851 OpenILS::Application::Ingest->post_init();
853 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
855 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
857 my $xml = $resp->content;
858 last unless (defined $xml);
860 $log->debug("Running open-ils.ingest.full.serial.xml.readonly ...");
861 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($xml);
863 $client->respond( $res );
868 __PACKAGE__->register_method(
869 api_name => "open-ils.ingest.full.serial.xml_stream.readonly",
870 method => "ro_serial_ingest_stream_xml",
875 sub rw_serial_ingest_stream_import {
879 OpenILS::Application::Ingest->post_init();
881 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
883 while (my ($resp) = $client->recv( count => 1, timeout => 5 )) {
885 my $bib = $resp->content;
886 last unless (defined $bib);
888 $log->debug("Running open-ils.ingest.full.serial.xml.readonly ...");
889 my ($res) = $self->method_lookup("open-ils.ingest.full.serial.xml.readonly")->run($bib->marc);
891 $_->record($bib->id) for (@{$res->{full_rec}});
893 $client->respond( $res );
898 __PACKAGE__->register_method(
899 api_name => "open-ils.ingest.full.serial.bib_stream.import",
900 method => "rw_serial_ingest_stream_import",
906 # --------------------------------------------------------------------------------
907 # MARC index extraction
909 package OpenILS::Application::Ingest::XPATH;
910 use base qw/OpenILS::Application::Ingest/;
911 use Unicode::Normalize;
913 # give this an XML documentElement and an XPATH expression
914 sub xpath_to_string {
918 my $ns_prefix = shift;
921 $xml->setNamespace( $ns_uri, $ns_prefix, 1 ) if ($ns_uri && $ns_prefix);
925 # grab the set of matching nodes
926 my @nodes = $xml->findnodes( $xpath );
927 for my $value (@nodes) {
929 # grab all children of the node
930 my @children = $value->childNodes();
931 for my $child (@children) {
933 # add the childs content to the growing buffer
934 my $content = quotemeta($child->textContent);
935 next if ($unique && $string =~ /$content/); # uniquify the values
936 $string .= $child->textContent . " ";
939 $string .= $value->textContent . " ";
943 $string =~ s/(\w+)\/(\w+)/$1 $2/sgo;
944 $string =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
949 sub class_index_string_xml {
955 OpenILS::Application::Ingest->post_init();
956 $xml = $parser->parse_string(OpenILS::Application::AppUtils->entityize($xml)) unless (ref $xml);
960 for my $class (@classes) {
961 my $class_constructor = "Fieldmapper::metabib::${class}_field_entry";
962 for my $type ( keys %{ $xpathset->{$class} } ) {
964 my $def = $xpathset->{$class}->{$type};
965 my $sf = $OpenILS::Application::Ingest::supported_formats{$def->{format}};
970 $document = $transform_cache{$def->{format}} || $sf->{xslt}->transform($xml);
971 $transform_cache{$def->{format}} = $document;
974 my $value = xpath_to_string(
975 $document->documentElement => $def->{xpath},
976 $sf->{ns} => $def->{format},
982 $value = NFD($value);
983 $value =~ s/\pM+//sgo;
984 $value =~ s/\pC+//sgo;
985 $value =~ s/\W+$//sgo;
987 $value =~ s/\b\.+\b//sgo;
990 my $fm = $class_constructor->new;
991 $fm->value( $value );
992 $fm->field( $xpathset->{$class}->{$type}->{id} );
993 $client->respond($fm);
998 __PACKAGE__->register_method(
999 api_name => "open-ils.ingest.field_entry.class.xml",
1000 method => "class_index_string_xml",
1006 sub class_index_string_record {
1010 my @classes = shift;
1012 OpenILS::Application::Ingest->post_init();
1013 my $r = OpenSRF::AppSession
1014 ->create('open-ils.cstore')
1015 ->request( 'open-ils.cstore.direct.authority.record_entry.retrieve' => $rec )
1018 return undef unless ($r and @$r);
1020 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, @classes)) {
1022 $client->respond($fm);
1026 __PACKAGE__->register_method(
1027 api_name => "open-ils.ingest.field_entry.class.record",
1028 method => "class_index_string_record",
1034 sub all_index_string_xml {
1039 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($xml, keys(%$xpathset))) {
1040 $client->respond($fm);
1044 __PACKAGE__->register_method(
1045 api_name => "open-ils.ingest.extract.field_entry.all.xml",
1046 method => "all_index_string_xml",
1052 sub all_index_string_record {
1057 OpenILS::Application::Ingest->post_init();
1058 my $r = OpenSRF::AppSession
1059 ->create('open-ils.cstore')
1060 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1063 return undef unless ($r and @$r);
1065 for my $fm ($self->method_lookup("open-ils.ingest.field_entry.class.xml")->run($r->marc, keys(%$xpathset))) {
1067 $client->respond($fm);
1071 __PACKAGE__->register_method(
1072 api_name => "open-ils.ingest.extract.field_entry.all.record",
1073 method => "all_index_string_record",
1079 # --------------------------------------------------------------------------------
1082 package OpenILS::Application::Ingest::FlatMARC;
1083 use base qw/OpenILS::Application::Ingest/;
1084 use Unicode::Normalize;
1087 sub _marcxml_to_full_rows {
1089 my $marcxml = shift;
1090 my $xmltype = shift || 'metabib';
1092 my $type = "Fieldmapper::${xmltype}::full_rec";
1096 my ($root) = $marcxml->findnodes('//*[local-name()="record"]');
1098 for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
1099 next unless $tagline;
1101 my $ns = $type->new;
1104 my $val = $tagline->textContent;
1106 $val =~ s/\pM+//sgo;
1107 $val =~ s/\pC+//sgo;
1108 $val =~ s/\W+$//sgo;
1114 for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
1115 next unless $tagline;
1117 my $ns = $type->new;
1119 $ns->tag( $tagline->getAttribute( "tag" ) );
1120 my $val = $tagline->textContent;
1122 $val =~ s/\pM+//sgo;
1123 $val =~ s/\pC+//sgo;
1124 $val =~ s/\W+$//sgo;
1130 for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
1131 next unless $tagline;
1133 my $tag = $tagline->getAttribute( "tag" );
1134 my $ind1 = $tagline->getAttribute( "ind1" );
1135 my $ind2 = $tagline->getAttribute( "ind2" );
1137 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
1140 my $ns = $type->new;
1145 $ns->subfield( $data->getAttribute( "code" ) );
1146 my $val = $data->textContent;
1148 $val =~ s/\pM+//sgo;
1149 $val =~ s/\pC+//sgo;
1150 $val =~ s/\W+$//sgo;
1151 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1152 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1153 $ns->value( lc($val) );
1158 if ($xmltype eq 'metabib' and $tag eq '245') {
1161 for my $data ( @{$tagline->getChildrenByTagName('subfield')} ) {
1162 next unless ($data and $data->getAttribute( "code" ) eq 'a');
1169 $ns->subfield( $data->getAttribute( "code" ) );
1170 my $val = substr( $data->textContent, $ind2 );
1172 $val =~ s/\pM+//sgo;
1173 $val =~ s/\pC+//sgo;
1174 $val =~ s/\W+$//sgo;
1175 $val =~ s/(\w+)\/(\w+)/$1 $2/sgo;
1176 $val =~ s/(\d{4})-(\d{4})/$1 $2/sgo;
1177 $ns->value( lc($val) );
1184 $log->debug("Returning ".scalar(@ns_list)." Fieldmapper nodes from $xmltype xml");
1193 $log->debug("processing [$xml]");
1195 $xml = $parser->parse_string(OpenILS::Application::AppUtils->entityize($xml)) unless (ref $xml);
1197 my $type = 'metabib';
1198 $type = 'authority' if ($self->api_name =~ /authority/o);
1199 $type = 'serial' if ($self->api_name =~ /serial/o);
1201 OpenILS::Application::Ingest->post_init();
1203 $client->respond($_) for (_marcxml_to_full_rows($xml, $type));
1206 __PACKAGE__->register_method(
1207 api_name => "open-ils.ingest.flat_marc.authority.xml",
1208 method => "flat_marc_xml",
1213 __PACKAGE__->register_method(
1214 api_name => "open-ils.ingest.flat_marc.biblio.xml",
1215 method => "flat_marc_xml",
1220 __PACKAGE__->register_method(
1221 api_name => "open-ils.ingest.flat_marc.serial.xml",
1222 method => "flat_marc_xml",
1228 sub flat_marc_record {
1233 my $type = 'biblio';
1234 $type = 'authority' if ($self->api_name =~ /authority/o);
1235 $type = 'serial' if ($self->api_name =~ /serial/o);
1237 OpenILS::Application::Ingest->post_init();
1238 my $r = OpenSRF::AppSession
1239 ->create('open-ils.cstore')
1240 ->request( "open-ils.cstore.direct.${type}.record_entry.retrieve" => $rec )
1244 return undef unless ($r and $r->marc);
1246 my @rows = $self->method_lookup("open-ils.ingest.flat_marc.$type.xml")->run($r->marc);
1247 for my $row (@rows) {
1248 $client->respond($row);
1249 $log->debug(OpenSRF::Utils::JSON->perl2JSON($row), DEBUG);
1253 __PACKAGE__->register_method(
1254 api_name => "open-ils.ingest.flat_marc.biblio.record_entry",
1255 method => "flat_marc_record",
1260 __PACKAGE__->register_method(
1261 api_name => "open-ils.ingest.flat_marc.authority.record_entry",
1262 method => "flat_marc_record",
1267 __PACKAGE__->register_method(
1268 api_name => "open-ils.ingest.flat_marc.serial.record_entry",
1269 method => "flat_marc_record",
1275 # --------------------------------------------------------------------------------
1278 package OpenILS::Application::Ingest::Biblio::URI;
1279 use base qw/OpenILS::Application::Ingest/;
1280 use Unicode::Normalize;
1281 use OpenSRF::EX qw/:try/;
1284 sub _extract_856_uris {
1288 my $max_uri = shift;
1291 my $recid = $rec->id;
1292 my $marcxml = $rec->marc;
1294 my $document = $parser->parse_string($marcxml);
1295 my @nodes = $document->findnodes('//*[local-name()="datafield" and @tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]');
1297 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
1301 for my $node (@nodes) {
1302 # first, is there a URI?
1303 my $href = $node->findvalue('*[local-name()="subfield" and @code="u"]/text()');
1304 next unless ($href);
1306 # now, find the best possible label
1307 my $label = $node->findvalue('*[local-name()="subfield" and @code="y"]/text()');
1308 $label ||= $node->findvalue('*[local-name()="subfield" and @code="3"]/text()');
1312 my $use = $node->findvalue('*[local-name()="subfield" and @code="z"]/text()');
1313 $use ||= $node->findvalue('*[local-name()="subfield" and @code="2"]/text()');
1314 $use ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1316 # moving on to the URI owner
1317 my $owner = $node->findvalue('*[local-name()="subfield" and @code="w"]/text()');
1318 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="n"]/text()');
1319 $owner ||= $node->findvalue('*[local-name()="subfield" and @code="9"]/text()'); # Evergreen special sauce
1321 $owner =~ s/^.*?\((\w+)\).*$/$1/o; # unwrap first paren-enclosed string and then ...
1323 # no owner? skip it :(
1324 next unless ($owner);
1327 ->request( 'open-ils.cstore.direct.actor.org_unit.search' => { shortname => $owner} )
1332 # now we can construct the uri object
1334 ->request( 'open-ils.cstore.direct.asset.uri.search' => { label => $label, href => $href, use_restriction => $use, active => 't' } )
1338 $uri = Fieldmapper::asset::uri->new;
1340 $uri->id( $$max_uri++ );
1341 $uri->label($label);
1344 $uri->use_restriction($use);
1347 # see if we need to create a call number
1348 my $cn = $cn_cache{$org->id};
1349 $cn = $cn->clone if ($cn);
1350 $cn->clear_isnew if ($cn);
1353 ->request( 'open-ils.cstore.direct.asset.call_number.search' => { owning_lib => $org->id, record => $recid, label => '##URI##' } )
1357 $cn = Fieldmapper::asset::call_number->new;
1360 $cn->id( $$max_cn++ );
1361 $cn->owning_lib( $org->id );
1362 $cn->record( $recid );
1363 $cn->create_date( 'now' );
1364 $cn->creator( $rec->creator );
1365 $cn->editor( $rec->editor );
1366 $cn->edit_date( 'now' );
1367 $cn->label( '##URI##' );
1370 $cn_cache{$org->id} = $cn;
1372 push @objects, { uri => $uri, call_number => $cn };
1375 $log->debug("Returning ".scalar(@objects)." URI nodes for record $recid");
1379 sub get_uris_record {
1384 OpenILS::Application::Ingest->post_init();
1385 my $r = OpenSRF::AppSession
1386 ->create('open-ils.cstore')
1387 ->request( "open-ils.cstore.direct.biblio.record_entry.retrieve" => $rec )
1390 return undef unless ($r and $r->marc);
1392 $client->respond($_) for (_extract_856_uris($r));
1395 __PACKAGE__->register_method(
1396 api_name => "open-ils.ingest.856_uri.record",
1397 method => "get_uris_record",
1403 sub get_uris_object {
1408 my $max_uri = shift;
1410 return undef unless ($obj and $obj->marc);
1412 $client->respond($_) for (_extract_856_uris($obj, \$max_cn, \$max_uri));
1415 __PACKAGE__->register_method(
1416 api_name => "open-ils.ingest.856_uri.object",
1417 method => "get_uris_object",
1424 # --------------------------------------------------------------------------------
1427 package OpenILS::Application::Ingest::Biblio::Fingerprint;
1428 use base qw/OpenILS::Application::Ingest/;
1429 use Unicode::Normalize;
1430 use OpenSRF::EX qw/:try/;
1432 sub biblio_fingerprint_record {
1437 OpenILS::Application::Ingest->post_init();
1439 my $r = OpenSRF::AppSession
1440 ->create('open-ils.cstore')
1441 ->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve' => $rec )
1444 return undef unless ($r and $r->marc);
1446 my ($fp) = $self->method_lookup('open-ils.ingest.fingerprint.xml')->run($r->marc);
1447 $log->debug("Returning [$fp] as fingerprint for record $rec", INFO);
1448 $fp->{quality} = int($fp->{quality});
1451 __PACKAGE__->register_method(
1452 api_name => "open-ils.ingest.fingerprint.record",
1453 method => "biblio_fingerprint_record",
1459 sub biblio_fingerprint {
1462 my $xml = OpenILS::Application::AppUtils->entityize(shift);
1464 $log->internal("Got MARC [$xml]");
1467 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1468 my $conf = OpenSRF::Utils::SettingsClient->new;
1470 my $libs = $conf->config_value(@pfx, 'script_path');
1471 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_fingerprint');
1472 my $script_libs = (ref($libs)) ? $libs : [$libs];
1474 $log->debug("Loading script $script_file for biblio fingerprinting...");
1476 $fp_script = new OpenILS::Utils::ScriptRunner
1477 ( file => $script_file,
1478 paths => $script_libs,
1479 reset_count => 100 );
1482 $fp_script->insert('environment' => {marc => $xml} => 1);
1484 my $res = $fp_script->run || ($log->error( "Fingerprint script died! $@" ) && return undef);
1485 $log->debug("Script for biblio fingerprinting completed successfully...");
1489 __PACKAGE__->register_method(
1490 api_name => "open-ils.ingest.fingerprint.xml",
1491 method => "biblio_fingerprint",
1497 sub biblio_descriptor {
1500 my $xml = OpenILS::Application::AppUtils->entityize(shift);
1502 $log->internal("Got MARC [$xml]");
1505 my @pfx = ( "apps", "open-ils.ingest","app_settings" );
1506 my $conf = OpenSRF::Utils::SettingsClient->new;
1508 my $libs = $conf->config_value(@pfx, 'script_path');
1509 my $script_file = $conf->config_value(@pfx, 'scripts', 'biblio_descriptor');
1510 my $script_libs = (ref($libs)) ? $libs : [$libs];
1512 $log->debug("Loading script $script_file for biblio descriptor extraction...");
1514 $rd_script = new OpenILS::Utils::ScriptRunner
1515 ( file => $script_file,
1516 paths => $script_libs,
1517 reset_count => 100 );
1520 $log->debug("Setting up environment for descriptor extraction script...");
1521 $rd_script->insert('environment.marc' => $xml => 1);
1522 $log->debug("Environment building complete...");
1524 my $res = $rd_script->run || ($log->error( "Descriptor script died! $@" ) && return undef);
1525 $log->debug("Script for biblio descriptor extraction completed successfully");
1527 my $d1 = $res->date1;
1528 if ($d1 && $d1 ne ' ') {
1533 my $d2 = $res->date2;
1534 if ($d2 && $d2 ne ' ') {
1541 __PACKAGE__->register_method(
1542 api_name => "open-ils.ingest.descriptor.xml",
1543 method => "biblio_descriptor",