1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
10 my $U = 'OpenILS::Application::AppUtils';
12 our $ac_types = ['toc', 'anotes', 'excerpt', 'summary', 'reviews'];
20 $ctx->{page} = 'record';
22 $self->timelog("load_record() began");
24 my $rec_id = $ctx->{page_args}->[0];
26 return Apache2::Const::HTTP_BAD_REQUEST
27 unless $rec_id and $rec_id =~ /^\d+$/;
29 $self->added_content_stage1($rec_id);
30 $self->timelog("past added content stage 1");
32 my $org = $self->_get_search_lib();
33 my $org_name = $ctx->{get_aou}->($org)->shortname;
34 my $pref_ou = $self->_get_pref_lib();
35 my $depth = $self->cgi->param('depth');
36 $depth = $ctx->{get_aou}->($org)->ou_type->depth
37 unless defined $depth; # can be 0
39 my $copy_depth = $self->cgi->param('copy_depth');
40 $copy_depth = $depth unless defined $copy_depth; # can be 0
41 $self->ctx->{copy_depth} = $copy_depth;
43 my $copy_limit = int($self->cgi->param('copy_limit') || 10);
44 my $copy_offset = int($self->cgi->param('copy_offset') || 0);
46 $self->get_staff_search_settings;
47 if ($ctx->{staff_saved_search_size}) {
48 $ctx->{saved_searches} = ($self->staff_load_searches)[1];
50 $self->timelog("past staff saved searches");
52 $self->fetch_related_search_info($rec_id) unless $kwargs{no_search};
53 $self->timelog("past related search info");
55 # Check for user and load lists and prefs
56 if ($self->ctx->{user}) {
57 $self->_load_lists_and_settings;
58 $self->timelog("load user lists and settings");
61 # run copy retrieval in parallel to bib retrieval
63 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
64 my $copy_rec = $cstore->request(
65 'open-ils.cstore.json_query.atomic',
66 $self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset, $pref_ou)
69 # find foreign copy data
70 my $peer_rec = $U->simplereq(
72 'open-ils.search.peer_bibs', $rec_id );
74 $ctx->{foreign_copies} = $peer_rec;
76 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
77 flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
83 $self->timelog("past get_records_and_facets()");
84 $ctx->{bre_id} = $rec_data[0]->{id};
85 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
87 $ctx->{copies} = $copy_rec->gather(1);
89 # Add public copy notes to each copy - and while we're in there, grab peer bib records
91 foreach my $copy (@{$ctx->{copies}}) {
92 $copy->{notes} = $U->simplereq(
94 'open-ils.circ.copy_note.retrieve.all',
95 {itemid => $copy->{id}, pub => 1 }
97 $self->timelog("past copy note retrieval call");
98 $copy->{peer_bibs} = $U->simplereq(
100 'open-ils.search.multi_home.bib_ids.by_barcode',
103 $self->timelog("past peer bib id retrieval");
105 foreach my $bib (@{$copy->{peer_bibs}}) {
106 next if $bib eq $ctx->{bre_id};
107 next if $cached_bibs{$bib};
108 my (undef, @peer_data) = $self->get_records_and_facets(
115 $cached_bibs{$bib} = 1;
116 #$copy->{peer_bib_marc} = $peer_data[0]->{marc_xml};
117 push @peer_marc, $peer_data[0]->{marc_xml};
118 $self->timelog("fetched peer bib record $bib");
120 $copy->{peer_bib_marc} = \@peer_marc;
123 $self->timelog("past store copy retrieval call");
124 $ctx->{copy_limit} = $copy_limit;
125 $ctx->{copy_offset} = $copy_offset;
127 $ctx->{have_holdings_to_show} = 0;
128 $ctx->{have_mfhd_to_show} = 0;
130 $self->get_hold_copy_summary($rec_id, $org);
132 $self->timelog("past get_hold_copy_summary()");
133 $self->ctx->{bib_is_dead} = OpenILS::Application::AppUtils->is_true(
134 OpenILS::Utils::CStoreEditor->new->json_query({
135 select => { bre => [ 'deleted' ] },
137 where => { 'id' => $rec_id }
144 $ctx->{get_org_setting}->
145 ($org, "opac.fully_compressed_serial_holdings")
147 # We're loading this data here? Are we therefore assuming that we
148 # *are* going to display something in the "issues" expandy?
149 $self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
151 $ctx->{mfhd_summaries} =
152 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
154 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
156 $ctx->{have_mfhd_to_show} = 1;
160 $self->timelog("past serials holding stuff");
164 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
171 $self->prepare_browse_call_numbers();
175 my @expand = $self->cgi->param('expand');
176 if (grep {$_ eq 'all'} @expand) {
177 $ctx->{expand_all} = 1;
178 $expandies{$_}->() for keys %expandies;
181 for my $exp (@expand) {
182 $ctx->{"expand_$exp"} = 1;
183 $expandies{$exp}->() if exists $expandies{$exp};
187 $self->timelog("past expandies");
189 $self->added_content_stage2($rec_id);
191 $self->timelog("past added content stage 2");
193 return Apache2::Const::OK;
196 # collect IDs and info on the search that lead to this details page
197 # If no search query, etc is present, we leave ctx.search_result_index == -1
198 sub fetch_related_search_info {
201 my $ctx = $self->ctx;
202 $ctx->{search_result_index} = -1;
204 $self->load_rresults(internal => 1);
206 my @search_ids = @{$ctx->{ids}};
207 return unless @search_ids;
209 for my $idx (0..$#search_ids) {
210 if ($search_ids[$idx] == $rec_id) {
211 $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
212 $ctx->{next_search_record} = $search_ids[$idx + 1];
213 $ctx->{search_result_index} = $idx;
218 $ctx->{first_search_record} = $search_ids[0];
219 $ctx->{last_search_record} = $search_ids[-1];
228 my $copy_limit = shift;
229 my $copy_offset = shift;
232 my $query = $U->basic_opac_copy_query(
233 $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
236 if($org != $self->ctx->{aou_tree}->()->id) {
237 # no need to add the org join filter if we're not actually filtering
238 $query->{from}->{acp}->{aou} = {
246 transform => 'actor.org_unit_descendants',
247 result_field => 'id',
251 where => {id => $org}
258 # Unsure if we want these in the shared function, leaving here for now
259 unshift(@{$query->{order_by}},
260 { class => "aou", field => 'id',
261 transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
264 push(@{$query->{order_by}},
265 { class => "acp", field => 'status',
266 transform => 'evergreen.rank_cp_status'
274 my($self, $rec_id) = @_;
276 # could be optimized considerably by performing the xslt on the already fetched record
277 return $U->simplereq(
279 'open-ils.search.biblio.record.html', $rec_id, 1);
282 sub load_serial_holding_summaries {
283 my ($self, $rec_id, $org, $depth) = @_;
285 my $limit = $self->cgi->param("slimit") || 10;
286 my $offset = $self->cgi->param("soffset") || 0;
288 my $serial = create OpenSRF::AppSession("open-ils.serial");
290 # First, get the tree of /summaries/ of holdings.
291 my $tree = $serial->request(
292 "open-ils.serial.holding_summary_tree.by_bib",
293 $rec_id, $org, $depth, $limit, $offset
296 return if $self->apache_log_if_event(
297 $tree, "getting holding summary tree for record $rec_id"
300 # Next, if requested, get a list of individual holdings under a
301 # particular summary.
303 my $summary_id = int($self->cgi->param("sid") || 0);
304 my $summary_type = $self->cgi->param("stype");
306 if ($summary_id and $summary_type) {
307 my $expand_path = [ $self->cgi->param("sepath") ],
308 my $expand_limit = $self->cgi->param("selimit");
309 my $expand_offsets = [ $self->cgi->param("seoffset") ];
310 my $auto_expand_first = 0;
312 if (not @$expand_offsets) {
313 $expand_offsets = undef;
314 $auto_expand_first = 1;
317 $holdings = $serial->request(
318 "open-ils.serial.holdings.grouped_by_summary",
319 $summary_type, $summary_id,
320 $expand_path, $expand_limit, $expand_offsets,
322 1 + ($self->ctx->{is_staff} ? 1 : 0)
325 if ($holdings and ref $holdings eq "ARRAY") {
326 $self->place_holdings_with_summary(
327 $tree, $holdings, $summary_id, $summary_type
328 ) or $self->apache->log->warn(
329 "could not place holdings within summary tree"
332 $self->apache_log_if_event(
333 $holdings, "getting holdings grouped by summary $summary_id"
340 # The presence of any keys in the tree hash other than 'more' means that we
341 # must have /something/ we could show.
342 $self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
344 $self->ctx->{holding_summary_tree} = $tree;
347 # This helper to load_serial_holding_summaries() recursively searches in
348 # $tree for a holding summary matching $sid and $stype, and places $holdings
349 # within the node for that summary. IOW, this is about showing expanded
350 # holdings under their "parent" summary.
351 sub place_holdings_with_summary {
352 my ($self, $tree, $holdings, $sid, $stype) = @_;
354 foreach my $sum (@{$tree->{holding_summaries}}) {
355 if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
356 $sum->{holdings} = $holdings;
361 foreach my $child (@{$tree->{children}}) {
362 return 1 if $self->place_holdings_with_summary(
363 $child, $holdings, $sid, $stype
370 sub get_mfhd_summaries {
371 my ($self, $rec_id, $org, $depth) = @_;
373 my $serial = create OpenSRF::AppSession("open-ils.search");
374 my $result = $serial->request(
375 "open-ils.search.serial.record.bib.retrieve",
376 $rec_id, $org, $depth
383 sub any_call_number_label {
386 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
387 return $self->ctx->{copies}->[0]->{call_number_label};
393 sub prepare_browse_call_numbers {
396 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
399 my $org_unit = $self->ctx->{get_aou}->($self->_get_search_lib()) ||
400 $self->ctx->{aou_tree}->();
402 my $supercat = create OpenSRF::AppSession("open-ils.supercat");
403 my $results = $supercat->request(
404 "open-ils.supercat.call_number.browse",
405 $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
410 $self->ctx->{browsed_call_numbers} = [
413 (new XML::LibXML)->parse_string($_->record->marc)
418 $self->ctx->{browsing_ou} = $org_unit;
421 sub get_hold_copy_summary {
422 my ($self, $rec_id, $org) = @_;
423 my $ctx = $self->ctx;
425 my $search = OpenSRF::AppSession->create('open-ils.search');
426 my $copy_count_meth = 'open-ils.search.biblio.record.copy_count';
427 # We want to include OPAC-invisible copies in a staff context
428 if ($ctx->{is_staff}) {
429 $copy_count_meth .= '.staff';
431 my $req1 = $search->request($copy_count_meth, $org, $rec_id);
433 # if org unit hiding applies, limit the hold count to holds
434 # whose pickup library is within our depth-scoped tree
436 while ($org and $ctx->{org_within_hiding_scope}->($org)) {
437 $count_args->{pickup_lib_descendant} = $org;
438 $org = $ctx->{get_aou}->($org)->parent_ou;
441 $self->ctx->{record_hold_count} = $U->simplereq(
442 'open-ils.circ', 'open-ils.circ.bre.holds.count',
443 $rec_id, $count_args);
445 $self->ctx->{copy_summary} = $req1->recv->content;
450 sub load_print_record {
453 my $rec_id = $self->ctx->{page_args}->[0]
454 or return Apache2::Const::HTTP_BAD_REQUEST;
456 $self->{ctx}->{bre_id} = $rec_id;
457 $self->{ctx}->{printable_record} = $U->simplereq(
459 'open-ils.search.biblio.record.print', $rec_id);
461 return Apache2::Const::OK;
464 sub load_email_record {
467 my $rec_id = $self->ctx->{page_args}->[0]
468 or return Apache2::Const::HTTP_BAD_REQUEST;
470 $self->{ctx}->{bre_id} = $rec_id;
473 'open-ils.search.biblio.record.email',
474 $self->ctx->{authtoken}, $rec_id);
476 return Apache2::Const::OK;
479 # for each type, fire off the reqeust to see if content is available
480 # ctx.added_content.$type.status:
484 sub added_content_stage1 {
487 my $ctx = $self->ctx;
488 my $sel_type = $self->cgi->param('ac') || '';
489 my $key = $self->get_ac_key($rec_id);
490 ($key = $key->{value}) =~ s/^\s+//g if $key;
492 # Connect to this machine's IP address, using the same
493 # Host with which our caller used to connect to us.
494 # This avoids us having to route out of the cluster
495 # and back in to reach the top-level virtualhost.
496 my $ac_addr = $ENV{SERVER_ADDR};
497 my $ac_host = $self->apache->hostname;
500 $logger->info("tpac: added content connecting to $ac_addr / $ac_host");
502 $ctx->{added_content} = {};
503 for my $type (@$ac_types) {
505 $ctx->{added_content}->{$type} = {content => ''};
506 $ctx->{added_content}->{$type}->{status} = $key ? 3 : 2;
509 $logger->debug("tpac: starting added content request for $key => $type");
511 # Net::HTTP::NB is non-blocking /after/ the initial connect()
512 # Passing Timeout=>1 ensures we wait no longer than 1 second to
513 # connect to the local Evergreen instance (i.e. ourself).
514 # Connecting to oneself should either be very fast (normal)
515 # or very slow (routing problems).
517 my $req = Net::HTTP::NB->new(Host => $ac_addr, Timeout => 1);
519 $logger->warn("Unable to connect to $ac_addr / $ac_host".
520 " for added content lookup for $key: $@");
525 $req->host($self->apache->hostname);
527 my $http_type = ($type eq $sel_type) ? 'GET' : 'HEAD';
528 $req->write_request($http_type => "/opac/extras/ac/$type/html/" . uri_escape_utf8($key));
529 $ctx->{added_content}->{$type}->{request} = $req;
534 # check each outstanding request. If it's ready, read the HTTP
535 # status and use it to determine if content is available. Otherwise,
536 # leave the status as unknown.
537 sub added_content_stage2 {
539 my $ctx = $self->ctx;
540 my $sel_type = $self->cgi->param('ac') || '';
542 for my $type (keys %{$ctx->{added_content}}) {
543 my $content = $ctx->{added_content}->{$type};
545 if ($content->{status} == 3) {
546 $logger->debug("tpac: finishing added content request for $type");
548 my $req = $content->{request};
549 my $sel = IO::Select->new($req);
551 # if we are requesting a specific type of content, give the
552 # backend code a little extra time to retrieve the content.
553 my $wait = $type eq $sel_type ? 3 : 0; # TODO: config?
555 if ($sel->can_read($wait)) {
556 my ($code) = $req->read_response_headers;
557 $content->{status} = $code eq '200' ? 1 : 2;
558 $logger->debug("tpac: added content request for $type returned $code");
560 if ($code eq '200' and $type eq $sel_type) {
563 my $n = $req->read_entity_body($buf, 1024);
565 $content->{content} .= $buf;
570 # To avoid a lot of hanging connections.
571 $content->{request}->shutdown(2) if ($content->{request});
575 # XXX this is copied directly from AddedContent.pm in
576 # working/user/jeff/ac_by_record_id_rebase. When Jeff's
577 # branch is merged and Evergreen gets added content
578 # lookup by ID, this can be removed.
579 # returns [{tag => $tag, value => $value}, {tag => $tag2, value => $value2}]
583 my $key_data = $self->editor->json_query({
584 select => {mfr => ['tag', 'value']},
606 grep {$_->{tag} eq '020'} @$key_data,
607 grep {$_->{tag} eq '024'} @$key_data