/
Record.pm
581 lines (473 loc) · 18.1 KB
/
Record.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package OpenILS::WWW::EGCatLoader;
use strict; use warnings;
use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_GONE HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST HTTP_NOT_FOUND);
use OpenSRF::Utils::Logger qw/$logger/;
use OpenILS::Utils::CStoreEditor qw/:funcs/;
use OpenILS::Utils::Fieldmapper;
use OpenILS::Application::AppUtils;
use Net::HTTP::NB;
use IO::Select;
my $U = 'OpenILS::Application::AppUtils';
our $ac_types = ['toc', 'anotes', 'excerpt', 'summary', 'reviews'];
# context additions:
# record : bre object
sub load_record {
my $self = shift;
my %kwargs = @_;
my $ctx = $self->ctx;
$ctx->{page} = 'record';
$self->timelog("load_record() began");
my $rec_id = $ctx->{page_args}->[0];
return Apache2::Const::HTTP_BAD_REQUEST
unless $rec_id and $rec_id =~ /^\d+$/;
$self->added_content_stage1($rec_id);
$self->timelog("past added content stage 1");
my $org = $self->_get_search_lib();
my $org_name = $ctx->{get_aou}->($org)->shortname;
my $pref_ou = $self->_get_pref_lib();
my $depth = $self->cgi->param('depth');
$depth = $ctx->{get_aou}->($org)->ou_type->depth
unless defined $depth; # can be 0
my $copy_depth = $self->cgi->param('copy_depth');
$copy_depth = $depth unless defined $copy_depth; # can be 0
$self->ctx->{copy_depth} = $copy_depth;
my $copy_limit = int($self->cgi->param('copy_limit') || 10);
my $copy_offset = int($self->cgi->param('copy_offset') || 0);
$self->get_staff_search_settings;
if ($ctx->{staff_saved_search_size}) {
$ctx->{saved_searches} = ($self->staff_load_searches)[1];
}
$self->timelog("past staff saved searches");
$self->fetch_related_search_info($rec_id) unless $kwargs{no_search};
$self->timelog("past related search info");
# Check for user and load lists and prefs
if ($self->ctx->{user}) {
$self->_load_lists_and_settings;
$self->timelog("load user lists and settings");
}
# run copy retrieval in parallel to bib retrieval
# XXX unapi
my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
my $copy_rec = $cstore->request(
'open-ils.cstore.json_query.atomic',
$self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset, $pref_ou)
);
# find foreign copy data
my $peer_rec = $U->simplereq(
'open-ils.search',
'open-ils.search.peer_bibs', $rec_id );
$ctx->{foreign_copies} = $peer_rec;
my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
site => $org_name,
depth => $depth,
pref_lib => $pref_ou
});
$self->timelog("past get_records_and_facets()");
$ctx->{bre_id} = $rec_data[0]->{id};
$ctx->{marc_xml} = $rec_data[0]->{marc_xml};
$ctx->{copies} = $copy_rec->gather(1);
# Add public copy notes to each copy - and while we're in there, grab peer bib records
my %cached_bibs = ();
foreach my $copy (@{$ctx->{copies}}) {
$copy->{notes} = $U->simplereq(
'open-ils.circ',
'open-ils.circ.copy_note.retrieve.all',
{itemid => $copy->{id}, pub => 1 }
);
$self->timelog("past copy note retrieval call");
$copy->{peer_bibs} = $U->simplereq(
'open-ils.search',
'open-ils.search.multi_home.bib_ids.by_barcode',
$copy->{barcode}
);
$self->timelog("past peer bib id retrieval");
my @peer_marc;
foreach my $bib (@{$copy->{peer_bibs}}) {
next if $bib eq $ctx->{bre_id};
next if $cached_bibs{$bib};
my (undef, @peer_data) = $self->get_records_and_facets(
[$bib], undef, {
flesh => '{}',
site => $org_name,
depth => $depth,
pref_lib => $pref_ou
});
$cached_bibs{$bib} = 1;
#$copy->{peer_bib_marc} = $peer_data[0]->{marc_xml};
push @peer_marc, $peer_data[0]->{marc_xml};
$self->timelog("fetched peer bib record $bib");
}
$copy->{peer_bib_marc} = \@peer_marc;
}
$self->timelog("past store copy retrieval call");
$ctx->{copy_limit} = $copy_limit;
$ctx->{copy_offset} = $copy_offset;
$ctx->{have_holdings_to_show} = 0;
$ctx->{have_mfhd_to_show} = 0;
$self->get_hold_copy_summary($rec_id, $org);
$self->timelog("past get_hold_copy_summary()");
$self->ctx->{bib_is_dead} = OpenILS::Application::AppUtils->is_true(
OpenILS::Utils::CStoreEditor->new->json_query({
select => { bre => [ 'deleted' ] },
from => 'bre',
where => { 'id' => $rec_id }
})->[0]->{deleted}
);
$cstore->kill_me;
# Shortcut and help the machines with a 410 Gone status code
if ($self->ctx->{bib_is_dead}) {
return Apache2::Const::HTTP_GONE;
}
# Shortcut and help the machines with a 404 Not Found status code
if (!$ctx->{bre_id}) {
return Apache2::Const::HTTP_NOT_FOUND;
}
if (
$ctx->{get_org_setting}->
($org, "opac.fully_compressed_serial_holdings")
) {
# We're loading this data here? Are we therefore assuming that we
# *are* going to display something in the "issues" expandy?
$self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
} else {
$ctx->{mfhd_summaries} =
$self->get_mfhd_summaries($rec_id, $org, $copy_depth);
if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
) {
$ctx->{have_mfhd_to_show} = 1;
};
}
$self->timelog("past serials holding stuff");
my %expandies = (
marchtml => sub {
$ctx->{marchtml} = $self->mk_marc_html($rec_id);
},
issues => sub {
return;
# XXX this needed?
},
cnbrowse => sub {
$self->prepare_browse_call_numbers();
}
);
my @expand = $self->cgi->param('expand');
if (grep {$_ eq 'all'} @expand) {
$ctx->{expand_all} = 1;
$expandies{$_}->() for keys %expandies;
} else {
for my $exp (@expand) {
$ctx->{"expand_$exp"} = 1;
$expandies{$exp}->() if exists $expandies{$exp};
}
}
$self->timelog("past expandies");
$self->added_content_stage2($rec_id);
$self->timelog("past added content stage 2");
return Apache2::Const::OK;
}
# collect IDs and info on the search that lead to this details page
# If no search query, etc is present, we leave ctx.search_result_index == -1
sub fetch_related_search_info {
my $self = shift;
my $rec_id = shift;
my $ctx = $self->ctx;
$ctx->{search_result_index} = -1;
$self->load_rresults(internal => 1);
my @search_ids = @{$ctx->{ids}};
return unless @search_ids;
for my $idx (0..$#search_ids) {
if ($search_ids[$idx] == $rec_id) {
$ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
$ctx->{next_search_record} = $search_ids[$idx + 1];
$ctx->{search_result_index} = $idx;
last;
}
}
$ctx->{first_search_record} = $search_ids[0];
$ctx->{last_search_record} = $search_ids[-1];
}
sub mk_copy_query {
my $self = shift;
my $rec_id = shift;
my $org = shift;
my $depth = shift;
my $copy_limit = shift;
my $copy_offset = shift;
my $pref_ou = shift;
my $query = $U->basic_opac_copy_query(
$rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
);
if($org != $self->ctx->{aou_tree}->()->id) {
# no need to add the org join filter if we're not actually filtering
$query->{from}->{acp}->{aou} = {
fkey => 'circ_lib',
field => 'id',
filter => {
id => {
in => {
select => {aou => [{
column => 'id',
transform => 'actor.org_unit_descendants',
result_field => 'id',
params => [$depth]
}]},
from => 'aou',
where => {id => $org}
}
}
}
};
};
# Unsure if we want these in the shared function, leaving here for now
unshift(@{$query->{order_by}},
{ class => "aou", field => 'id',
transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
}
);
push(@{$query->{order_by}},
{ class => "acp", field => 'status',
transform => 'evergreen.rank_cp_status'
}
);
return $query;
}
sub mk_marc_html {
my($self, $rec_id) = @_;
# could be optimized considerably by performing the xslt on the already fetched record
return $U->simplereq(
'open-ils.search',
'open-ils.search.biblio.record.html', $rec_id, 1);
}
sub load_serial_holding_summaries {
my ($self, $rec_id, $org, $depth) = @_;
my $limit = $self->cgi->param("slimit") || 10;
my $offset = $self->cgi->param("soffset") || 0;
my $serial = create OpenSRF::AppSession("open-ils.serial");
# First, get the tree of /summaries/ of holdings.
my $tree = $serial->request(
"open-ils.serial.holding_summary_tree.by_bib",
$rec_id, $org, $depth, $limit, $offset
)->gather(1);
return if $self->apache_log_if_event(
$tree, "getting holding summary tree for record $rec_id"
);
# Next, if requested, get a list of individual holdings under a
# particular summary.
my $holdings;
my $summary_id = int($self->cgi->param("sid") || 0);
my $summary_type = $self->cgi->param("stype");
if ($summary_id and $summary_type) {
my $expand_path = [ $self->cgi->param("sepath") ],
my $expand_limit = $self->cgi->param("selimit");
my $expand_offsets = [ $self->cgi->param("seoffset") ];
my $auto_expand_first = 0;
if (not @$expand_offsets) {
$expand_offsets = undef;
$auto_expand_first = 1;
}
$holdings = $serial->request(
"open-ils.serial.holdings.grouped_by_summary",
$summary_type, $summary_id,
$expand_path, $expand_limit, $expand_offsets,
$auto_expand_first,
1 + ($self->ctx->{is_staff} ? 1 : 0)
)->gather(1);
if ($holdings and ref $holdings eq "ARRAY") {
$self->place_holdings_with_summary(
$tree, $holdings, $summary_id, $summary_type
) or $self->apache->log->warn(
"could not place holdings within summary tree"
);
} else {
$self->apache_log_if_event(
$holdings, "getting holdings grouped by summary $summary_id"
);
}
}
$serial->kill_me;
# The presence of any keys in the tree hash other than 'more' means that we
# must have /something/ we could show.
$self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
$self->ctx->{holding_summary_tree} = $tree;
}
# This helper to load_serial_holding_summaries() recursively searches in
# $tree for a holding summary matching $sid and $stype, and places $holdings
# within the node for that summary. IOW, this is about showing expanded
# holdings under their "parent" summary.
sub place_holdings_with_summary {
my ($self, $tree, $holdings, $sid, $stype) = @_;
foreach my $sum (@{$tree->{holding_summaries}}) {
if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
$sum->{holdings} = $holdings;
return 1;
}
}
foreach my $child (@{$tree->{children}}) {
return 1 if $self->place_holdings_with_summary(
$child, $holdings, $sid, $stype
);
}
return;
}
sub get_mfhd_summaries {
my ($self, $rec_id, $org, $depth) = @_;
my $serial = create OpenSRF::AppSession("open-ils.search");
my $result = $serial->request(
"open-ils.search.serial.record.bib.retrieve",
$rec_id, $org, $depth
)->gather(1);
$serial->kill_me;
return $result;
}
sub any_call_number_label {
my ($self) = @_;
if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
return $self->ctx->{copies}->[0]->{call_number_label};
} else {
return;
}
}
sub prepare_browse_call_numbers {
my ($self) = @_;
my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
return [];
my $org_unit = $self->ctx->{get_aou}->($self->_get_search_lib()) ||
$self->ctx->{aou_tree}->();
my $supercat = create OpenSRF::AppSession("open-ils.supercat");
my $results = $supercat->request(
"open-ils.supercat.call_number.browse",
$cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
)->gather(1) || [];
$supercat->kill_me;
$self->ctx->{browsed_call_numbers} = [
map {
$_->record->marc(
(new XML::LibXML)->parse_string($_->record->marc)
);
$_;
} @$results
];
$self->ctx->{browsing_ou} = $org_unit;
}
sub get_hold_copy_summary {
my ($self, $rec_id, $org) = @_;
my $ctx = $self->ctx;
my $search = OpenSRF::AppSession->create('open-ils.search');
my $copy_count_meth = 'open-ils.search.biblio.record.copy_count';
# We want to include OPAC-invisible copies in a staff context
if ($ctx->{is_staff}) {
$copy_count_meth .= '.staff';
}
my $req1 = $search->request($copy_count_meth, $org, $rec_id);
# if org unit hiding applies, limit the hold count to holds
# whose pickup library is within our depth-scoped tree
my $count_args = {};
while ($org and $ctx->{org_within_hiding_scope}->($org)) {
$count_args->{pickup_lib_descendant} = $org;
$org = $ctx->{get_aou}->($org)->parent_ou;
}
$self->ctx->{record_hold_count} = $U->simplereq(
'open-ils.circ', 'open-ils.circ.bre.holds.count',
$rec_id, $count_args);
$self->ctx->{copy_summary} = $req1->recv->content;
$search->kill_me;
}
sub load_print_record {
my $self = shift;
my $rec_id = $self->ctx->{page_args}->[0]
or return Apache2::Const::HTTP_BAD_REQUEST;
$self->{ctx}->{bre_id} = $rec_id;
$self->{ctx}->{printable_record} = $U->simplereq(
'open-ils.search',
'open-ils.search.biblio.record.print', $rec_id);
return Apache2::Const::OK;
}
sub load_email_record {
my $self = shift;
my $rec_id = $self->ctx->{page_args}->[0]
or return Apache2::Const::HTTP_BAD_REQUEST;
$self->{ctx}->{bre_id} = $rec_id;
$U->simplereq(
'open-ils.search',
'open-ils.search.biblio.record.email',
$self->ctx->{authtoken}, $rec_id);
return Apache2::Const::OK;
}
# for each type, fire off the reqeust to see if content is available
# ctx.added_content.$type.status:
# 1 == available
# 2 == not available
# 3 == unknown
sub added_content_stage1 {
my $self = shift;
my $rec_id = shift;
my $ctx = $self->ctx;
my $sel_type = $self->cgi->param('ac') || '';
# Connect to this machine's IP address, using the same
# Host with which our caller used to connect to us.
# This avoids us having to route out of the cluster
# and back in to reach the top-level virtualhost.
my $ac_addr = $ENV{SERVER_ADDR};
my $ac_host = $self->apache->hostname;
my $ac_failed = 0;
$logger->info("tpac: added content connecting to $ac_addr / $ac_host");
$ctx->{added_content} = {};
for my $type (@$ac_types) {
last if $ac_failed;
$ctx->{added_content}->{$type} = {content => ''};
$ctx->{added_content}->{$type}->{status} = 3;
$logger->debug("tpac: starting added content request for $rec_id => $type");
# Net::HTTP::NB is non-blocking /after/ the initial connect()
# Passing Timeout=>1 ensures we wait no longer than 1 second to
# connect to the local Evergreen instance (i.e. ourself).
# Connecting to oneself should either be very fast (normal)
# or very slow (routing problems).
my $req = Net::HTTP::NB->new(Host => $ac_addr, Timeout => 1);
if (!$req) {
$logger->warn("Unable to connect to $ac_addr / $ac_host".
" for added content lookup for $rec_id: $@");
$ac_failed = 1;
next;
}
$req->host($self->apache->hostname);
my $http_type = ($type eq $sel_type) ? 'GET' : 'HEAD';
$req->write_request($http_type => "/opac/extras/ac/$type/html/r/" . $rec_id);
$ctx->{added_content}->{$type}->{request} = $req;
}
}
# check each outstanding request. If it's ready, read the HTTP
# status and use it to determine if content is available. Otherwise,
# leave the status as unknown.
sub added_content_stage2 {
my $self = shift;
my $ctx = $self->ctx;
my $sel_type = $self->cgi->param('ac') || '';
for my $type (keys %{$ctx->{added_content}}) {
my $content = $ctx->{added_content}->{$type};
if ($content->{status} == 3) {
$logger->debug("tpac: finishing added content request for $type");
my $req = $content->{request};
my $sel = IO::Select->new($req);
# if we are requesting a specific type of content, give the
# backend code a little extra time to retrieve the content.
my $wait = $type eq $sel_type ? 3 : 0; # TODO: config?
if ($sel->can_read($wait)) {
my ($code) = $req->read_response_headers;
$content->{status} = $code eq '200' ? 1 : 2;
$logger->debug("tpac: added content request for $type returned $code");
if ($code eq '200' and $type eq $sel_type) {
while (1) {
my $buf;
my $n = $req->read_entity_body($buf, 1024);
last unless $n;
$content->{content} .= $buf;
}
}
}
}
# To avoid a lot of hanging connections.
$content->{request}->shutdown(2) if ($content->{request});
}
}
1;