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 # when fetching "all" search results for staff client
14 # start/end paging, fetch this many IDs at most
15 my $all_recs_limit = 10000;
18 sub _prepare_biblio_search_basics {
21 return $cgi->param('query') unless $cgi->param('qtype');
24 my @part_names = qw/qtype contains query bool/;
25 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
28 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
29 my ($qtype, $contains, $query, $bool) = map { $parts{$_}->[$i] } @part_names;
31 next unless $query =~ /\S/;
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 . '$';
47 $query = "$qtype:$query" unless $qtype eq 'keyword' and $i == 0;
49 $bool = ($bool and $bool eq 'or') ? '||' : '&&';
50 $full_query = $full_query ? "($full_query $bool $query)" : $query;
56 sub _prepare_biblio_search {
59 my $query = _prepare_biblio_search_basics($cgi) || '';
61 foreach ($cgi->param('modifier')) {
62 # The unless bit is to avoid stacking modifiers.
63 $query = ('#' . $_ . ' ' . $query) unless $query =~ qr/\#\Q$_/;
67 foreach (grep /^fi:/, $cgi->param) {
69 my $term = join(",", $cgi->param($_));
70 $query .= " $1($term)" if length $term;
73 # sort is treated specially, even though it's actually a filter
74 if ($cgi->param('sort')) {
75 $query =~ s/sort\([^\)]*\)//g; # override existing sort(). no stacking.
76 my ($axis, $desc) = split /\./, $cgi->param('sort');
77 $query .= " sort($axis)";
78 if ($desc and not $query =~ /\#descending/) {
79 $query .= '#descending';
81 $query =~ s/\#descending//;
85 if ($cgi->param('pubdate') && $cgi->param('date1')) {
86 if ($cgi->param('pubdate') eq 'between') {
87 $query .= ' between(' . $cgi->param('date1');
88 $query .= ',' . $cgi->param('date2') if $cgi->param('date2');
90 } elsif ($cgi->param('pubdate') eq 'is') {
91 $query .= ' between(' . $cgi->param('date1') .
92 ',' . $cgi->param('date1') . ')'; # sic, date1 twice
94 $query .= ' ' . $cgi->param('pubdate') .
95 '(' . $cgi->param('date1') . ')';
100 my $org = $cgi->param('loc');
101 if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
102 $site = $ctx->{get_aou}->($org)->shortname;
103 $query .= " site($site)";
107 ($site) = ($query =~ /site\(([^\)]+)\)/);
108 $site ||= $ctx->{aou_tree}->()->shortname;
113 if (defined($cgi->param('depth')) and not $query =~ /depth\(\d+\)/) {
114 $depth = defined $cgi->param('depth') ?
115 $cgi->param('depth') : $ctx->{get_aou}->($site)->ou_type->depth;
116 $query .= " depth($depth)";
119 return ($query, $site, $depth);
122 sub _get_search_limit {
125 # param takes precedence
126 my $limit = $self->cgi->param('limit');
127 return $limit if $limit;
129 if($self->editor->requestor) {
130 # See if the user has a hit count preference
131 my $lset = $self->editor->search_actor_user_setting({
132 usr => $self->editor->requestor->id,
133 name => 'opac.hits_per_page'
135 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
144 # records : list of bre's and copy-count objects
148 my $internal = $args{internal};
149 my $cgi = $self->cgi;
150 my $ctx = $self->ctx;
151 my $e = $self->editor;
153 $ctx->{page} = 'rresult' unless $internal;
155 $ctx->{records} = [];
156 $ctx->{search_facets} = {};
157 $ctx->{hit_count} = 0;
159 # Special alternative searches here. This could all stand to be cleaner.
160 if ($cgi->param("_special")) {
161 return $self->marc_expert_search(%args) if scalar($cgi->param("tag"));
162 return $self->item_barcode_shortcut if (
163 $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode")
165 return $self->call_number_browse_standalone if (
166 $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
170 my $page = $cgi->param('page') || 0;
171 my $facet = $cgi->param('facet');
172 my $limit = $self->_get_search_limit;
173 my $loc = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
174 my $offset = $page * $limit;
175 my $metarecord = $cgi->param('metarecord');
178 $ctx->{page_size} = $limit;
179 $ctx->{search_page} = $page;
181 # fetch the first hit from the next page
183 $limit = $all_recs_limit;
187 my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
189 $self->get_staff_search_settings;
191 if ($ctx->{staff_saved_search_size}) {
192 my ($key, $list) = $self->staff_save_search($query);
194 $self->apache->headers_out->add(
195 "Set-Cookie" => $self->cgi->cookie(
196 -name => (ref $self)->COOKIE_ANON_CACHE,
198 -value => ($key || ''),
199 -expires => ($key ? undef : "-1h")
202 $ctx->{saved_searches} = $list;
206 if ($metarecord and !$internal) {
208 # TODO: other limits, like SVF/format, etc.
209 $results = $U->simplereq(
211 'open-ils.search.biblio.metarecord_to_records',
212 $metarecord, {org => $loc, depth => $depth}
215 # force the metarecord result blob to match the format of regular search results
216 $results->{ids} = [map { [$_] } @{$results->{ids}}];
221 return Apache2::Const::OK if $internal;
222 return $self->generic_redirect;
225 # Limit and offset will stay here. Everything else should be part of
226 # the query string, not special args.
227 my $args = {'limit' => $limit, 'offset' => $offset};
229 # Stuff these into the TT context so that templates can use them in redrawing forms
230 $ctx->{processed_search_query} = $query;
232 $query = "$query $facet" if $facet; # TODO
234 $logger->activity("EGWeb: [search] $query");
238 my $method = 'open-ils.search.biblio.multiclass.query';
239 $method .= '.staff' if $ctx->{is_staff};
240 $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
244 $logger->error("multiclass search error: $err");
245 $results = {count => 0, ids => []};
249 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
251 $ctx->{ids} = $rec_ids;
252 $ctx->{hit_count} = $results->{count};
253 $ctx->{parsed_query} = $results->{parsed_query};
255 return Apache2::Const::OK if @$rec_ids == 0 or $internal;
257 my ($facets, @data) = $self->get_records_and_facets(
258 $rec_ids, $results->{facet_key},
260 flesh => '{holdings_xml,mra,acp}',
266 # shove recs into context in search results order
267 for my $rec_id (@$rec_ids) {
270 grep { $_->{id} == $rec_id } @data
274 $ctx->{search_facets} = $facets;
276 return Apache2::Const::OK;
279 # Searching by barcode is a special search that does /not/ respect any other
280 # of the usual search parameters, not even the ones for sorting and paging!
281 sub item_barcode_shortcut {
284 my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
285 if (my $search = create OpenSRF::AppSession("open-ils.search")) {
286 my $rec_ids = $search->request(
287 $method, $self->cgi->param("query")
291 if (ref $rec_ids ne 'ARRAY') {
293 if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
297 if (defined $U->event_code($rec_ids)) {
298 $self->apache->log->warn(
299 "$method returned event: " . $U->event_code($rec_ids)
302 $self->apache->log->warn(
303 "$method returned something unexpected: $rec_ids"
306 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
310 my ($facets, @data) = $self->get_records_and_facets(
311 $rec_ids, undef, {flesh => "{holdings_xml,mra}"}
314 $self->ctx->{records} = [@data];
315 $self->ctx->{search_facets} = {};
316 $self->ctx->{hit_count} = scalar @data;
317 $self->ctx->{page_size} = $self->ctx->{hit_count};
319 return Apache2::Const::OK;
321 $self->apache->log->warn("couldn't connect to open-ils.search");
322 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
326 # like item_barcode_search, this can't take all the usual search params, but
327 # this one will at least do site, limit and page
328 sub marc_expert_search {
329 my ($self, %args) = @_;
331 my @tags = $self->cgi->param("tag");
332 my @subfields = $self->cgi->param("subfield");
333 my @terms = $self->cgi->param("term");
336 for (my $i = 0; $i < scalar @tags; $i++) {
337 next if ($tags[$i] eq "" || $terms[$i] eq "");
338 $subfields[$i] = '_' unless $subfields[$i];
340 "term" => $terms[$i],
341 "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
345 $logger->info("query for expert search: " . Dumper($query));
347 # loc, limit and offset
348 my $page = $self->cgi->param("page") || 0;
349 my $limit = $self->_get_search_limit;
350 my $org_unit = $self->cgi->param("loc") || $self->ctx->{aou_tree}->()->id;
351 my $offset = $page * $limit;
353 $self->ctx->{records} = [];
354 $self->ctx->{search_facets} = {};
355 $self->ctx->{page_size} = $limit;
356 $self->ctx->{hit_count} = 0;
357 $self->ctx->{ids} = [];
358 $self->ctx->{search_page} = $page;
361 return Apache2::Const::OK if @$query == 0;
363 if ($args{internal}) {
364 $limit = $all_recs_limit;
369 my $ses = OpenSRF::AppSession->create('open-ils.search');
370 my $req = $ses->request(
371 'open-ils.search.biblio.marc',
372 {searches => $query, org_unit => $org_unit},
373 $limit, $offset, $timeout);
375 my $resp = $req->recv($timeout);
376 my $results = $resp ? $resp->content : undef;
379 if (defined $U->event_code($results)) {
380 $self->apache->log->warn(
381 "open-ils.search.biblio.marc returned event: " .
382 $U->event_code($results)
384 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
387 $self->ctx->{ids} = [ grep { $_ } @{$results->{ids}} ];
389 my ($facets, @data) = $self->get_records_and_facets(
390 $self->ctx->{ids}, undef, {flesh => "{holdings_xml,mra}"}
393 $self->ctx->{records} = [@data];
394 $self->ctx->{hit_count} = $results->{count};
396 return Apache2::Const::OK;
399 sub call_number_browse_standalone {
402 if (my $cnfrag = $self->cgi->param("query")) {
404 'http%s://%s%s/cnbrowse?cn=%s',
405 $self->cgi->https ? "s" : "",
406 $self->apache->hostname,
407 $self->ctx->{opac_root},
408 $cnfrag # XXX some kind of escaping needed here?
410 return $self->generic_redirect($url);
412 return $self->generic_redirect; # return to search page
419 $self->prepare_browse_call_numbers();
421 return Apache2::Const::OK;
424 sub get_staff_search_settings {
427 unless ($self->ctx->{is_staff}) {
428 $self->ctx->{staff_saved_search_size} = 0;
432 my $sss_size = $self->ctx->{get_org_setting}->(
433 $self->ctx->{orig_loc} || $self->ctx->{aou_tree}->()->id,
434 "opac.staff_saved_search.size",
437 # Sic: 0 is 0 (off), but undefined is 10.
438 $sss_size = 10 unless defined $sss_size;
440 $self->ctx->{staff_saved_search_size} = $sss_size;
443 sub staff_load_searches {
446 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
450 $list = $U->simplereq(
452 "open-ils.actor.anon_cache.get_value",
453 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH
462 return ($cache_key, $list);
465 sub staff_save_search {
466 my ($self, $query) = @_;
468 my $sss_size = $self->ctx->{staff_saved_search_size};
469 return unless $sss_size > 0;
471 my ($cache_key, $list) = $self->staff_load_searches;
472 my %already = ( map { $_ => 1 } @$list );
474 unshift @$list, $query unless $already{$query};
476 splice @$list, $sss_size;
478 $cache_key = $U->simplereq(
480 "open-ils.actor.anon_cache.set_value",
481 $cache_key, (ref $self)->ANON_CACHE_STAFF_SEARCH, $list
484 return ($cache_key, $list);