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 $cstore->session_locale($OpenILS::Utils::CStoreEditor::default_locale);
65 my $copy_rec = $cstore->request(
66 'open-ils.cstore.json_query.atomic',
67 $self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset, $pref_ou)
70 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
71 flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
77 $self->timelog("past get_records_and_facets()");
78 $ctx->{bre_id} = $rec_data[0]->{id};
79 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
81 $ctx->{copies} = $copy_rec->gather(1);
83 # Add public copy notes to each copy
84 foreach my $copy (@{$ctx->{copies}}) {
85 $copy->{notes} = $U->simplereq(
87 'open-ils.circ.copy_note.retrieve.all',
88 {itemid => $copy->{id}, pub => 1 }
92 $self->timelog("past store copy retrieval call");
93 $ctx->{copy_limit} = $copy_limit;
94 $ctx->{copy_offset} = $copy_offset;
96 $ctx->{have_holdings_to_show} = 0;
97 $ctx->{have_mfhd_to_show} = 0;
99 $self->get_hold_copy_summary($rec_id, $org);
101 $self->timelog("past get_hold_copy_summary()");
102 $self->ctx->{bib_is_dead} = OpenILS::Application::AppUtils->is_true(
103 OpenILS::Utils::CStoreEditor->new->json_query({
104 select => { bre => [ 'deleted' ] },
106 where => { 'id' => $rec_id }
113 $ctx->{get_org_setting}->
114 ($org, "opac.fully_compressed_serial_holdings")
116 # We're loading this data here? Are we therefore assuming that we
117 # *are* going to display something in the "issues" expandy?
118 $self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
120 $ctx->{mfhd_summaries} =
121 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
123 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
125 $ctx->{have_mfhd_to_show} = 1;
129 $self->timelog("past serials holding stuff");
133 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
140 $self->prepare_browse_call_numbers();
144 my @expand = $self->cgi->param('expand');
145 if (grep {$_ eq 'all'} @expand) {
146 $ctx->{expand_all} = 1;
147 $expandies{$_}->() for keys %expandies;
150 for my $exp (@expand) {
151 $ctx->{"expand_$exp"} = 1;
152 $expandies{$exp}->() if exists $expandies{$exp};
156 $self->timelog("past expandies");
158 $self->added_content_stage2($rec_id);
160 $self->timelog("past added content stage 2");
162 return Apache2::Const::OK;
165 # collect IDs and info on the search that lead to this details page
166 # If no search query, etc is present, we leave ctx.search_result_index == -1
167 sub fetch_related_search_info {
170 my $ctx = $self->ctx;
171 $ctx->{search_result_index} = -1;
173 $self->load_rresults(internal => 1);
175 my @search_ids = @{$ctx->{ids}};
176 return unless @search_ids;
178 for my $idx (0..$#search_ids) {
179 if ($search_ids[$idx] == $rec_id) {
180 $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
181 $ctx->{next_search_record} = $search_ids[$idx + 1];
182 $ctx->{search_result_index} = $idx;
187 $ctx->{first_search_record} = $search_ids[0];
188 $ctx->{last_search_record} = $search_ids[-1];
197 my $copy_limit = shift;
198 my $copy_offset = shift;
201 my $query = $U->basic_opac_copy_query(
202 $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
205 if($org != $self->ctx->{aou_tree}->()->id) {
206 # no need to add the org join filter if we're not actually filtering
207 $query->{from}->{acp}->{aou} = {
215 transform => 'actor.org_unit_descendants',
216 result_field => 'id',
220 where => {id => $org}
227 # Unsure if we want these in the shared function, leaving here for now
228 unshift(@{$query->{order_by}},
229 { class => "aou", field => 'id',
230 transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
233 push(@{$query->{order_by}},
234 { class => "acp", field => 'status',
235 transform => 'evergreen.rank_cp_status'
243 my($self, $rec_id) = @_;
245 # could be optimized considerably by performing the xslt on the already fetched record
246 return $U->simplereq(
248 'open-ils.search.biblio.record.html', $rec_id, 1);
251 sub load_serial_holding_summaries {
252 my ($self, $rec_id, $org, $depth) = @_;
254 my $limit = $self->cgi->param("slimit") || 10;
255 my $offset = $self->cgi->param("soffset") || 0;
257 my $serial = create OpenSRF::AppSession("open-ils.serial");
259 # First, get the tree of /summaries/ of holdings.
260 my $tree = $serial->request(
261 "open-ils.serial.holding_summary_tree.by_bib",
262 $rec_id, $org, $depth, $limit, $offset
265 return if $self->apache_log_if_event(
266 $tree, "getting holding summary tree for record $rec_id"
269 # Next, if requested, get a list of individual holdings under a
270 # particular summary.
272 my $summary_id = int($self->cgi->param("sid") || 0);
273 my $summary_type = $self->cgi->param("stype");
275 if ($summary_id and $summary_type) {
276 my $expand_path = [ $self->cgi->param("sepath") ],
277 my $expand_limit = $self->cgi->param("selimit");
278 my $expand_offsets = [ $self->cgi->param("seoffset") ];
279 my $auto_expand_first = 0;
281 if (not @$expand_offsets) {
282 $expand_offsets = undef;
283 $auto_expand_first = 1;
286 $holdings = $serial->request(
287 "open-ils.serial.holdings.grouped_by_summary",
288 $summary_type, $summary_id,
289 $expand_path, $expand_limit, $expand_offsets,
291 1 + ($self->ctx->{is_staff} ? 1 : 0)
294 if ($holdings and ref $holdings eq "ARRAY") {
295 $self->place_holdings_with_summary(
296 $tree, $holdings, $summary_id, $summary_type
297 ) or $self->apache->log->warn(
298 "could not place holdings within summary tree"
301 $self->apache_log_if_event(
302 $holdings, "getting holdings grouped by summary $summary_id"
309 # The presence of any keys in the tree hash other than 'more' means that we
310 # must have /something/ we could show.
311 $self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
313 $self->ctx->{holding_summary_tree} = $tree;
316 # This helper to load_serial_holding_summaries() recursively searches in
317 # $tree for a holding summary matching $sid and $stype, and places $holdings
318 # within the node for that summary. IOW, this is about showing expanded
319 # holdings under their "parent" summary.
320 sub place_holdings_with_summary {
321 my ($self, $tree, $holdings, $sid, $stype) = @_;
323 foreach my $sum (@{$tree->{holding_summaries}}) {
324 if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
325 $sum->{holdings} = $holdings;
330 foreach my $child (@{$tree->{children}}) {
331 return 1 if $self->place_holdings_with_summary(
332 $child, $holdings, $sid, $stype
339 sub get_mfhd_summaries {
340 my ($self, $rec_id, $org, $depth) = @_;
342 my $serial = create OpenSRF::AppSession("open-ils.search");
343 my $result = $serial->request(
344 "open-ils.search.serial.record.bib.retrieve",
345 $rec_id, $org, $depth
352 sub any_call_number_label {
355 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
356 return $self->ctx->{copies}->[0]->{call_number_label};
362 sub prepare_browse_call_numbers {
365 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
368 my $org_unit = $self->ctx->{get_aou}->($self->_get_search_lib()) ||
369 $self->ctx->{aou_tree}->();
371 my $supercat = create OpenSRF::AppSession("open-ils.supercat");
372 my $results = $supercat->request(
373 "open-ils.supercat.call_number.browse",
374 $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
379 $self->ctx->{browsed_call_numbers} = [
382 (new XML::LibXML)->parse_string($_->record->marc)
387 $self->ctx->{browsing_ou} = $org_unit;
390 sub get_hold_copy_summary {
391 my ($self, $rec_id, $org) = @_;
392 my $ctx = $self->ctx;
394 my $search = OpenSRF::AppSession->create('open-ils.search');
395 my $copy_count_meth = 'open-ils.search.biblio.record.copy_count';
396 # We want to include OPAC-invisible copies in a staff context
397 if ($ctx->{is_staff}) {
398 $copy_count_meth .= '.staff';
400 my $req1 = $search->request($copy_count_meth, $org, $rec_id);
402 # if org unit hiding applies, limit the hold count to holds
403 # whose pickup library is within our depth-scoped tree
405 while ($org and $ctx->{org_within_hiding_scope}->($org)) {
406 $count_args->{pickup_lib_descendant} = $org;
407 $org = $ctx->{get_aou}->($org)->parent_ou;
410 $self->ctx->{record_hold_count} = $U->simplereq(
411 'open-ils.circ', 'open-ils.circ.bre.holds.count',
412 $rec_id, $count_args);
414 $self->ctx->{copy_summary} = $req1->recv->content;
419 sub load_print_record {
422 my $rec_id = $self->ctx->{page_args}->[0]
423 or return Apache2::Const::HTTP_BAD_REQUEST;
425 $self->{ctx}->{bre_id} = $rec_id;
426 $self->{ctx}->{printable_record} = $U->simplereq(
428 'open-ils.search.biblio.record.print', $rec_id);
430 return Apache2::Const::OK;
433 sub load_email_record {
436 my $rec_id = $self->ctx->{page_args}->[0]
437 or return Apache2::Const::HTTP_BAD_REQUEST;
439 $self->{ctx}->{bre_id} = $rec_id;
442 'open-ils.search.biblio.record.email',
443 $self->ctx->{authtoken}, $rec_id);
445 return Apache2::Const::OK;
448 # for each type, fire off the reqeust to see if content is available
449 # ctx.added_content.$type.status:
453 sub added_content_stage1 {
456 my $ctx = $self->ctx;
457 my $sel_type = $self->cgi->param('ac') || '';
458 my $key = $self->get_ac_key($rec_id);
459 ($key = $key->{value}) =~ s/^\s+//g if $key;
461 # Connect to this machine's IP address, using the same
462 # Host with which our caller used to connect to us.
463 # This avoids us having to route out of the cluster
464 # and back in to reach the top-level virtualhost.
465 my $ac_addr = $ENV{SERVER_ADDR};
466 my $ac_host = $self->apache->hostname;
469 $logger->info("tpac: added content connecting to $ac_addr / $ac_host");
471 $ctx->{added_content} = {};
472 for my $type (@$ac_types) {
474 $ctx->{added_content}->{$type} = {content => ''};
475 $ctx->{added_content}->{$type}->{status} = $key ? 3 : 2;
478 $logger->debug("tpac: starting added content request for $key => $type");
480 # Net::HTTP::NB is non-blocking /after/ the initial connect()
481 # Passing Timeout=>1 ensures we wait no longer than 1 second to
482 # connect to the local Evergreen instance (i.e. ourself).
483 # Connecting to oneself should either be very fast (normal)
484 # or very slow (routing problems).
486 my $req = Net::HTTP::NB->new(Host => $ac_addr, Timeout => 1);
488 $logger->warn("Unable to connect to $ac_addr / $ac_host".
489 " for added content lookup for $key: $@");
494 $req->host($self->apache->hostname);
496 my $http_type = ($type eq $sel_type) ? 'GET' : 'HEAD';
497 $req->write_request($http_type => "/opac/extras/ac/$type/html/" . uri_escape_utf8($key));
498 $ctx->{added_content}->{$type}->{request} = $req;
503 # check each outstanding request. If it's ready, read the HTTP
504 # status and use it to determine if content is available. Otherwise,
505 # leave the status as unknown.
506 sub added_content_stage2 {
508 my $ctx = $self->ctx;
509 my $sel_type = $self->cgi->param('ac') || '';
511 for my $type (keys %{$ctx->{added_content}}) {
512 my $content = $ctx->{added_content}->{$type};
514 if ($content->{status} == 3) {
515 $logger->debug("tpac: finishing added content request for $type");
517 my $req = $content->{request};
518 my $sel = IO::Select->new($req);
520 # if we are requesting a specific type of content, give the
521 # backend code a little extra time to retrieve the content.
522 my $wait = $type eq $sel_type ? 3 : 0; # TODO: config?
524 if ($sel->can_read($wait)) {
525 my ($code) = $req->read_response_headers;
526 $content->{status} = $code eq '200' ? 1 : 2;
527 $logger->debug("tpac: added content request for $type returned $code");
529 if ($code eq '200' and $type eq $sel_type) {
532 my $n = $req->read_entity_body($buf, 1024);
534 $content->{content} .= $buf;
539 # To avoid a lot of hanging connections.
540 $content->{request}->shutdown(2) if ($content->{request});
544 # XXX this is copied directly from AddedContent.pm in
545 # working/user/jeff/ac_by_record_id_rebase. When Jeff's
546 # branch is merged and Evergreen gets added content
547 # lookup by ID, this can be removed.
548 # returns [{tag => $tag, value => $value}, {tag => $tag2, value => $value2}]
552 my $key_data = $self->editor->json_query({
553 select => {mfr => ['tag', 'value']},
575 grep {$_->{tag} eq '020'} @$key_data,
576 grep {$_->{tag} eq '024'} @$key_data