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;
8 use OpenSRF::Utils::JSON;
10 $Data::Dumper::Indent = 0;
11 my $U = 'OpenILS::Application::AppUtils';
13 sub _prepare_biblio_search_basics {
16 return $cgi->param('query') unless $cgi->param('qtype');
19 my @part_names = qw/qtype contains query bool/;
20 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
23 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
24 my ($qtype, $contains, $query, $bool) = map { $parts{$_}->[$i] } @part_names;
26 next unless $query =~ /\S/;
28 # Hack for journal title
29 if ($qtype eq 'jtitle') {
33 # This stuff probably will need refined or rethought to better handle
34 # the weird things Real Users will surely type in.
35 $contains = "" unless defined $contains; # silence warning
36 if ($contains eq 'nocontains') {
38 $query = ('"' . $query . '"') if index $query, ' ';
39 $query = '-' . $query;
40 } elsif ($contains eq 'phrase') {
42 $query = ('"' . $query . '"') if index $query, ' ';
43 } elsif ($contains eq 'exact') {
44 $query =~ s/[\^\$]//g;
45 $query = '^' . $query . '$';
46 } elsif ($contains eq 'starts') {
48 $query =~ s/[\^\$]//g;
49 $query = '^' . $query;
50 $query = ('"' . $query . '"') if index $query, ' ';
52 $query = "$qtype:$query" unless $qtype eq 'keyword' and $i == 0;
54 $bool = ($bool and $bool eq 'or') ? '||' : '&&';
55 $full_query = $full_query ? "($full_query $bool $query)" : $query;
61 sub _prepare_biblio_search {
64 my $query = _prepare_biblio_search_basics($cgi) || '';
66 $query .= ' ' . $ctx->{global_search_filter} if $ctx->{global_search_filter};
68 foreach ($cgi->param('modifier')) {
69 # The unless bit is to avoid stacking modifiers.
70 $query = ('#' . $_ . ' ' . $query) unless $query =~ qr/\#\Q$_/;
74 foreach (grep /^fi:/, $cgi->param) {
76 my $term = join(",", $cgi->param($_));
77 $query .= " $1($term)" if length $term;
80 # filter group entries. Entries from like filters are grouped into a single
81 # filter_group_entry() filter (ORed). Each collection is ANDed together.
82 # fg:foo_group=foo_entry_id
83 foreach (grep /^fg:/, $cgi->param) {
85 my $term = join(",", $cgi->param($_));
86 $query = "filter_group_entry($term) $query" if length $term;
89 if ($cgi->param("bookbag")) {
90 $query = "container(bre,bookbag," . int($cgi->param("bookbag")) . ") $query";
93 # Journal title hackery complete
94 if ($cgi->param("qtype") && $cgi->param("qtype") eq "jtitle") {
95 $query = "bib_level(s) $query";
98 if ($cgi->param('pubdate') && $cgi->param('date1')) {
99 if ($cgi->param('pubdate') eq 'between') {
100 my $btw = 'between(' . $cgi->param('date1');
101 $btw .= ',' . $cgi->param('date2') if $cgi->param('date2');
103 $query = "$btw $query";
104 } elsif ($cgi->param('pubdate') eq 'is') {
105 $query = 'between(' . $cgi->param('date1') .
106 ',' . $cgi->param('date1') . ") $query"; # sic, date1 twice
108 $query = $cgi->param('pubdate') .
109 '(' . $cgi->param('date1') . ") $query";
113 # ---------------------------------------------------------------------
114 # Nothing below here constitutes a query by itself. If the query value
115 # is still empty up to this point, there is no query. abandon ship.
116 return () unless $query;
118 # sort is treated specially, even though it's actually a filter
119 if ($cgi->param('sort')) {
120 $query =~ s/sort\([^\)]*\)//g; # override existing sort(). no stacking.
121 my ($axis, $desc) = split /\./, $cgi->param('sort');
122 $query = "sort($axis) $query";
123 if ($desc and not $query =~ /\#descending/) {
124 $query = "#descending $query";
125 } elsif (not $desc) {
126 $query =~ s/\#descending//;
130 my (@naive_query_re, $site);
132 my $org = $ctx->{search_ou};
133 if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
134 my $thing = " site(" . $ctx->{get_aou}->($org)->shortname . ")";
137 push @naive_query_re, $thing;
140 my $pref_ou = $ctx->{pref_ou};
141 if (defined($pref_ou) and $pref_ou ne '' and $pref_ou != $org and ($pref_ou ne $ctx->{aou_tree}->()->id) and not $query =~ / pref_ou\(\S+\)/) {
142 my $plib = $ctx->{get_aou}->($pref_ou)->shortname;
143 $query = "pref_ou($plib) $query";
146 if (my $grp = $ctx->{copy_location_group}) {
147 $query = "location_groups($grp) $query";
151 ($site) = ($query =~ /site\(([^\)]+)\)/);
152 $site ||= $ctx->{aou_tree}->()->shortname;
156 if ($query =~ /depth\(\d+\)/) {
158 # depth is encoded in the search query
159 ($depth) = ($query =~ /depth\((\d+)\)/);
163 if (defined $cgi->param('depth')) {
164 $depth = $cgi->param('depth');
166 # no depth specified. match the depth to the search org
167 my ($org) = grep { $_->shortname eq $site } @{$ctx->{aou_list}->()};
168 $depth = $org->ou_type->depth;
170 my $thing = " depth($depth)";
173 push @naive_query_re, $thing;
176 # This gives templates a way to take site() and depth() back out of
177 # query strings when they shouldn't be there (because they're controllable
178 # with other widgets).
179 $ctx->{naive_query_scrub} = sub {
181 $query =~ s/\Q$_\E// foreach (@naive_query_re);
185 $logger->info("tpac: site=$site, depth=$depth, query=$query");
187 return ($query, $site, $depth);
190 sub _get_search_limit {
193 # param takes precedence
194 my $limit = $self->cgi->param('limit');
195 return $limit if $limit;
197 if($self->editor->requestor) {
198 $self->timelog("Checking for opac.hits_per_page preference");
199 # See if the user has a hit count preference
200 my $lset = $self->editor->search_actor_user_setting({
201 usr => $self->editor->requestor->id,
202 name => 'opac.hits_per_page'
204 $self->timelog("Got opac.hits_per_page preference");
205 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
211 sub tag_circed_items {
213 my $e = $self->editor;
215 $self->timelog("Tag circed items?");
216 return 0 unless $e->requestor;
217 $self->timelog("Checking for opac.search.tag_circulated_items");
218 return 0 unless $self->ctx->{get_org_setting}->(
219 $e->requestor->home_ou,
220 'opac.search.tag_circulated_items');
222 # user has to be opted-in to circ history in some capacity
223 $self->timelog("Checking for history.circ.retention_*");
224 my $sets = $e->search_actor_user_setting({
225 usr => $e->requestor->id,
227 'history.circ.retention_age',
228 'history.circ.retention_start'
232 $self->timelog("Return from checking for history.circ.retention_*");
234 return 0 unless @$sets;
239 # This only loads the bookbag itself (in support of a record results page)
240 # if a "bookbag" CGI parameter is specified and if the bookbag is public
241 # or owned by the logged-in user (if any). Bookbag notes are fetched
242 # later if applicable.
243 sub load_rresults_bookbag {
246 my $bookbag_id = int($self->cgi->param("bookbag") || 0);
247 return if $bookbag_id < 1;
249 my %authz = $self->ctx->{"user"} ?
250 ("-or" => {"pub" => "t", "owner" => $self->ctx->{"user"}->id}) :
253 $self->timelog("Load results bookbag");
254 my $bbag = $self->editor->search_container_biblio_record_entry_bucket(
255 {"id" => $bookbag_id, "btype" => "bookbag", %authz}
257 $self->timelog("Got results bookbag");
260 $self->apache->log->warn(
261 "error from cstore retrieving bookbag $bookbag_id!"
263 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
265 $self->ctx->{"bookbag"} = shift @$bbag;
271 # assumes context has a bookbag we're already authorized to look at, and
272 # a list of rec_ids, reasonably sized (from paged search).
273 sub load_rresults_bookbag_item_notes {
274 my ($self, $rec_ids) = @_;
276 $self->timelog("Load results bookbag item notes");
277 my $items_with_notes =
278 $self->editor->search_container_biblio_record_entry_bucket_item([
279 {"target_biblio_record_entry" => $rec_ids,
280 "bucket" => $self->ctx->{"bookbag"}->id},
281 {"flesh" => 1, "flesh_fields" => {"cbrebi" => ["notes"]},
282 "order_by" => {"cbrebi" => ["id"]}}
284 $self->timelog("Got results bookbag item notes");
286 if (!$items_with_notes) {
287 $self->apache->log->warn("error from cstore retrieving cbrebi objects");
288 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
291 $self->ctx->{"bookbag_items_by_bre_id"} = +{
292 map { $_->target_biblio_record_entry => $_ } @$items_with_notes
301 # records : list of bre's and copy-count objects
305 my $internal = $args{internal};
306 my $cgi = $self->cgi;
307 my $ctx = $self->ctx;
308 my $e = $self->editor;
310 # find the last record in the set, then redirect
311 my $find_last = $cgi->param('find_last');
313 $self->timelog("Loading results");
314 # load bookbag metadata, if requested.
315 if (my $bbag_err = $self->load_rresults_bookbag) {
319 $ctx->{page} = 'rresult' unless $internal;
321 $ctx->{records} = [];
322 $ctx->{search_facets} = {};
323 $ctx->{hit_count} = 0;
325 # Special alternative searches here. This could all stand to be cleaner.
326 if ($cgi->param("_special")) {
327 $self->timelog("Calling MARC expert search");
328 return $self->marc_expert_search(%args) if scalar($cgi->param("tag"));
329 $self->timelog("Calling item barcode search");
330 return $self->item_barcode_shortcut if (
331 $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode") and not $internal
333 $self->timelog("Calling call number browse");
334 return $self->call_number_browse_standalone if (
335 $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
339 $self->timelog("Getting search parameters");
340 my $page = $cgi->param('page') || 0;
341 my @facets = $cgi->param('facet');
342 my $limit = $self->_get_search_limit;
343 $ctx->{search_ou} = $self->_get_search_lib();
344 $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
345 my $offset = $page * $limit;
346 my $metarecord = $cgi->param('metarecord');
348 my $tag_circs = $self->tag_circed_items;
349 $self->timelog("Got search parameters");
351 $ctx->{page_size} = $limit;
352 $ctx->{search_page} = $page;
354 # fetch this page plus the first hit from the next page
356 $limit = $offset + $limit + 1;
360 my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
362 $self->get_staff_search_settings;
364 if (!$find_last and $ctx->{staff_saved_search_size}) {
365 my ($key, $list) = $self->staff_save_search($query);
367 $self->apache->headers_out->add(
368 "Set-Cookie" => $self->cgi->cookie(
369 -name => (ref $self)->COOKIE_ANON_CACHE,
371 -value => ($key || ''),
372 -expires => ($key ? undef : "-1h")
375 $ctx->{saved_searches} = $list;
379 if ($metarecord and !$internal) {
381 # TODO: other limits, like SVF/format, etc.
382 $self->timelog("Getting metarecords to records");
383 $results = $U->simplereq(
385 'open-ils.search.biblio.metarecord_to_records',
386 $metarecord, {org => $ctx->{search_ou}, depth => $depth}
388 $self->timelog("Got metarecords to records");
390 # force the metarecord result blob to match the format of regular search results
391 $results->{ids} = [map { [$_] } @{$results->{ids}}];
396 return Apache2::Const::OK if $internal;
397 return $self->generic_redirect;
400 # Limit and offset will stay here. Everything else should be part of
401 # the query string, not special args.
402 my $args = {'limit' => $limit, 'offset' => $offset};
405 $args->{tag_circulated_records} = 1;
406 $args->{authtoken} = $self->editor->authtoken;
409 # Stuff these into the TT context so that templates can use them in redrawing forms
410 $ctx->{processed_search_query} = $query;
412 $query = "$_ $query" for @facets;
414 $logger->activity("EGWeb: [search] $query");
418 my $method = 'open-ils.search.biblio.multiclass.query';
419 $method .= '.staff' if $ctx->{is_staff};
421 my $ses = OpenSRF::AppSession->create('open-ils.search');
422 $ses->session_locale($OpenILS::Utils::CStoreEditor::default_locale); # set in EGWeb
424 $self->timelog("Firing off the multiclass query");
425 my $req = $ses->request($method, $args, $query, 1);
426 $results = $req->gather(1);
427 $self->timelog("Returned from the multiclass query");
431 $logger->error("multiclass search error: $err");
432 $results = {count => 0, ids => []};
436 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
438 $ctx->{ids} = $rec_ids;
439 $ctx->{hit_count} = $results->{count};
440 $ctx->{parsed_query} = $results->{parsed_query};
443 # redirect to the record detail page for the last record in the results
444 my $rec_id = pop @$rec_ids;
445 $cgi->delete('find_last');
446 my $url = $cgi->url(-full => 1, -path => 1, -query => 1);
447 $url =~ s|/results|/record/$rec_id|;
448 return $self->generic_redirect($url);
451 return Apache2::Const::OK if @$rec_ids == 0 or $internal;
453 $self->load_rresults_bookbag_item_notes($rec_ids) if $ctx->{bookbag};
455 $self->timelog("Calling get_records_and_facets()");
456 my ($facets, @data) = $self->get_records_and_facets(
457 $rec_ids, $results->{facet_key},
459 flesh => '{holdings_xml,mra,acp,acnp,acns,bmp}',
462 pref_lib => $ctx->{pref_ou},
465 $self->timelog("Returned from get_records_and_facets()");
468 my $stat = $self->check_1hit_redirect($rec_ids);
469 return $stat if $stat;
472 # load temporary_list settings for user and ou:
473 $self->_load_lists_and_settings if ($ctx->{user});
475 # shove recs into context in search results order
476 for my $rec_id (@$rec_ids) {
479 grep { $_->{id} == $rec_id } @data
484 for my $rec (@{$ctx->{records}}) {
485 my ($res_rec) = grep { $_->[0] == $rec->{id} } @{$results->{ids}};
486 # index 1 in the per-record result array is a boolean which
487 # indicates whether the record in question is in the users
488 # accessible circ history list
489 $rec->{user_circulated} = 1 if $res_rec->[1];
493 $ctx->{search_facets} = $facets;
495 return Apache2::Const::OK;
498 # If the calling search results in 1 record and the client
499 # is configured to do so, redirect the search results to
500 # the record details page.
501 sub check_1hit_redirect {
502 my ($self, $rec_ids) = @_;
503 my $ctx = $self->ctx;
505 return undef unless $rec_ids and @$rec_ids == 1;
509 $self->timelog("Checking whether to jump to details on a single hit");
510 if ($ctx->{is_staff}) {
511 $sname = 'opac.staff.jump_to_details_on_single_hit';
512 $org = $ctx->{user}->ws_ou;
515 $sname = 'opac.patron.jump_to_details_on_single_hit';
516 $org = $self->_get_search_lib();
519 $self->timelog("Return from checking whether to jump to details on a single hit");
522 $self->ctx->{get_org_setting}->($org, $sname);
524 my $base_url = sprintf(
525 '%s://%s%s/record/%s',
527 $self->ctx->{hostname},
528 $self->ctx->{opac_root},
532 # If we get here from the same record detail page to which we
533 # now wish to redirect, do not perform the redirect. This
534 # approach seems to work well, with the rare exception of
535 # performing a new search directly from the detail page that
536 # happens to result in the same single hit. In this case, the
537 # user will be left on the search results page. This could be
538 # overcome w/ additional CGI, etc., but I'm not sure it's necessary.
539 if (my $referer = $ctx->{referer}) {
540 $referer =~ s/([^?]*).*/$1/g;
541 return undef if $base_url eq $referer;
544 return $self->generic_redirect($base_url . '?' . $self->cgi->query_string);
547 # Searching by barcode is a special search that does /not/ respect any other
548 # of the usual search parameters, not even the ones for sorting and paging!
549 sub item_barcode_shortcut {
552 $self->timelog("Searching for item_barcode");
553 my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
554 if (my $search = create OpenSRF::AppSession("open-ils.search")) {
555 my $rec_ids = $search->request(
556 $method, $self->cgi->param("query")
559 $self->timelog("Finished searching for item_barcode");
561 if (ref $rec_ids ne 'ARRAY') {
563 if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
567 if (defined $U->event_code($rec_ids)) {
568 $self->apache->log->warn(
569 "$method returned event: " . $U->event_code($rec_ids)
572 $self->apache->log->warn(
573 "$method returned something unexpected: $rec_ids"
576 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
580 $self->timelog("Calling get_records_and_facets() for item_barcode");
581 my ($facets, @data) = $self->get_records_and_facets(
582 $rec_ids, undef, {flesh => "{holdings_xml,mra,acnp,acns,bmp}"}
584 $self->timelog("Returned from calling get_records_and_facets() for item_barcode");
586 my $stat = $self->check_1hit_redirect($rec_ids);
587 return $stat if $stat;
589 $self->ctx->{records} = [@data];
590 $self->ctx->{search_facets} = {};
591 $self->ctx->{hit_count} = scalar @data;
592 $self->ctx->{page_size} = $self->ctx->{hit_count};
593 # load temporary_list settings for user and ou:
594 $self->_load_lists_and_settings if ($self->ctx->{user});
596 return Apache2::Const::OK;
598 $self->apache->log->warn("couldn't connect to open-ils.search");
599 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
603 # like item_barcode_search, this can't take all the usual search params, but
604 # this one will at least do site, limit and page
605 sub marc_expert_search {
606 my ($self, %args) = @_;
608 my @tags = $self->cgi->param("tag");
609 my @subfields = $self->cgi->param("subfield");
610 my @terms = $self->cgi->param("term");
613 for (my $i = 0; $i < scalar @tags; $i++) {
614 next if ($tags[$i] eq "" || $terms[$i] eq "");
615 $subfields[$i] = '_' unless $subfields[$i];
617 "term" => $terms[$i],
618 "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
622 $logger->info("query for expert search: " . Dumper($query));
624 $self->timelog("Getting search parameters");
625 # loc, limit and offset
626 my $page = $self->cgi->param("page") || 0;
627 my $limit = $self->_get_search_limit;
628 $self->ctx->{search_ou} = $self->_get_search_lib();
629 $self->ctx->{pref_ou} = $self->_get_pref_lib();
630 my $offset = $page * $limit;
631 $self->timelog("Got search parameters");
633 $self->ctx->{records} = [];
634 $self->ctx->{search_facets} = {};
635 $self->ctx->{page_size} = $limit;
636 $self->ctx->{hit_count} = 0;
637 $self->ctx->{ids} = [];
638 $self->ctx->{search_page} = $page;
641 return Apache2::Const::OK if @$query == 0;
643 if ($args{internal}) {
644 $limit = $offset + $limit + 1;
648 $self->timelog("Searching for MARC expert");
649 my $method = 'open-ils.search.biblio.marc';
650 $method .= '.staff' if $self->ctx->{is_staff};
652 my $ses = OpenSRF::AppSession->create('open-ils.search');
653 my $req = $ses->request(
655 {searches => $query, org_unit => $self->ctx->{search_ou}},
656 $limit, $offset, $timeout);
658 my $resp = $req->recv($timeout);
659 my $results = $resp ? $resp->content : undef;
661 $self->timelog("Got our MARC expert results");
663 if (defined $U->event_code($results)) {
664 $self->apache->log->warn(
665 "open-ils.search.biblio.marc returned event: " .
666 $U->event_code($results)
668 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
671 $self->ctx->{ids} = [ grep { $_ } @{$results->{ids}} ];
672 $self->ctx->{hit_count} = $results->{count};
674 return Apache2::Const::OK if @{$self->ctx->{ids}} == 0 or $args{internal};
677 my $stat = $self->check_1hit_redirect($self->ctx->{ids});
678 return $stat if $stat;
681 $self->timelog("Calling get_records_and_facets() for MARC expert");
682 my ($facets, @data) = $self->get_records_and_facets(
683 $self->ctx->{ids}, undef, {
684 flesh => "{holdings_xml,mra,acnp,acns}",
685 pref_lib => $self->ctx->{pref_ou},
688 $self->timelog("Returned from calling get_records_and_facets() for MARC expert");
690 # load temporary_list settings for user and ou:
691 $self->_load_lists_and_settings if ($self->ctx->{user});
693 $self->ctx->{records} = [@data];
695 return Apache2::Const::OK;
698 sub call_number_browse_standalone {
701 if (my $cnfrag = $self->cgi->param("query")) {
703 '%s://%s%s/cnbrowse?cn=%s',
705 $self->ctx->{hostname},
706 $self->ctx->{opac_root},
707 $cnfrag # XXX some kind of escaping needed here?
709 $url .= '&locg=' . $self->_get_search_lib() if ($self->_get_search_lib());
710 return $self->generic_redirect($url);
712 return $self->generic_redirect; # return to search page
719 $self->prepare_browse_call_numbers();
721 return Apache2::Const::OK;
724 sub get_staff_search_settings {
727 unless ($self->ctx->{is_staff}) {
728 $self->ctx->{staff_saved_search_size} = 0;
732 $self->timelog("Getting staff search size");
733 my $sss_size = $self->ctx->{get_org_setting}->(
734 $self->ctx->{physical_loc} || $self->ctx->{aou_tree}->()->id,
735 "opac.staff_saved_search.size",
737 $self->timelog("Got staff search size");
739 # Sic: 0 is 0 (off), but undefined is 10.
740 $sss_size = 10 unless defined $sss_size;
742 $self->ctx->{staff_saved_search_size} = $sss_size;
745 sub staff_load_searches {
748 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
752 $self->timelog("Getting anon_cache value");
753 $list = $U->simplereq(
755 "open-ils.actor.anon_cache.get_value",
756 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH
758 $self->timelog("Got anon_cache value");
766 return ($cache_key, $list);
769 sub staff_save_search {
770 my ($self, $query) = @_;
772 my $sss_size = $self->ctx->{staff_saved_search_size};
773 return unless $sss_size > 0;
775 my ($cache_key, $list) = $self->staff_load_searches;
776 my %already = ( map { $_ => 1 } @$list );
778 unshift @$list, $query unless $already{$query};
780 splice @$list, $sss_size if scalar @$list > $sss_size;
782 $self->timelog("Setting anon_cache value");
783 $cache_key = $U->simplereq(
785 "open-ils.actor.anon_cache.set_value",
786 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH, $list
788 $self->timelog("Set anon_cache value");
790 return ($cache_key, $list);