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';
14 sub _prepare_biblio_search_basics {
17 return $cgi->param('query') unless $cgi->param('qtype');
20 my @part_names = qw/qtype contains query bool/;
21 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
24 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
25 my ($qtype, $contains, $query, $bool) = map { $parts{$_}->[$i] } @part_names;
27 next unless $query =~ /\S/;
29 # This stuff probably will need refined or rethought to better handle
30 # the weird things Real Users will surely type in.
31 $contains = "" unless defined $contains; # silence warning
32 if ($contains eq 'nocontains') {
34 $query = ('"' . $query . '"') if index $query, ' ';
35 $query = '-' . $query;
36 } elsif ($contains eq 'phrase') {
38 $query = ('"' . $query . '"') if index $query, ' ';
39 } elsif ($contains eq 'exact') {
40 $query =~ s/[\^\$]//g;
41 $query = '^' . $query . '$';
43 $query = "$qtype:$query" unless $qtype eq 'keyword' and $i == 0;
45 $bool = ($bool and $bool eq 'or') ? '||' : '&&';
46 $full_query = $full_query ? "($full_query $bool $query)" : $query;
52 sub _prepare_biblio_search {
55 my $query = _prepare_biblio_search_basics($cgi) || '';
57 foreach ($cgi->param('modifier')) {
58 # The unless bit is to avoid stacking modifiers.
59 $query = ('#' . $_ . ' ' . $query) unless $query =~ qr/\#\Q$_/;
63 foreach (grep /^fi:/, $cgi->param) {
65 my $term = join(",", $cgi->param($_));
66 $query .= " $1($term)" if length $term;
69 # sort is treated specially, even though it's actually a filter
70 if ($cgi->param('sort')) {
71 $query =~ s/sort\([^\)]*\)//g; # override existing sort(). no stacking.
72 my ($axis, $desc) = split /\./, $cgi->param('sort');
73 $query .= " sort($axis)";
74 if ($desc and not $query =~ /\#descending/) {
75 $query .= '#descending';
77 $query =~ s/\#descending//;
81 if ($cgi->param('pubdate') && $cgi->param('date1')) {
82 if ($cgi->param('pubdate') eq 'between') {
83 $query .= ' between(' . $cgi->param('date1');
84 $query .= ',' . $cgi->param('date2') if $cgi->param('date2');
86 } elsif ($cgi->param('pubdate') eq 'is') {
87 $query .= ' between(' . $cgi->param('date1') .
88 ',' . $cgi->param('date1') . ')'; # sic, date1 twice
90 $query .= ' ' . $cgi->param('pubdate') .
91 '(' . $cgi->param('date1') . ')';
96 my $org = $cgi->param('loc');
97 if (defined($org) and $org ne '' and ($org ne $ctx->{aou_tree}->()->id) and not $query =~ /site\(\S+\)/) {
98 $site = $ctx->{get_aou}->($org)->shortname;
99 $query .= " site($site)";
103 ($site) = ($query =~ /site\(([^\)]+)\)/);
104 $site ||= $ctx->{aou_tree}->()->shortname;
109 if (defined($cgi->param('depth')) and not $query =~ /depth\(\d+\)/) {
110 $depth = defined $cgi->param('depth') ?
111 $cgi->param('depth') : $ctx->{get_aou}->($site)->ou_type->depth;
112 $query .= " depth($depth)";
115 return ($query, $site, $depth);
118 sub _get_search_limit {
121 # param takes precedence
122 my $limit = $self->cgi->param('limit');
123 return $limit if $limit;
125 if($self->editor->requestor) {
126 # See if the user has a hit count preference
127 my $lset = $self->editor->search_actor_user_setting({
128 usr => $self->editor->requestor->id,
129 name => 'opac.hits_per_page'
131 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
140 # records : list of bre's and copy-count objects
143 my $cgi = $self->cgi;
144 my $ctx = $self->ctx;
145 my $e = $self->editor;
147 $ctx->{page} = 'rresult';
149 # Special alternative searches here. This could all stand to be cleaner.
150 if ($cgi->param("_special")) {
151 return $self->marc_expert_search if scalar($cgi->param("tag"));
152 return $self->item_barcode_shortcut if (
153 $cgi->param("qtype") and ($cgi->param("qtype") eq "item_barcode")
155 return $self->call_number_browse_standalone if (
156 $cgi->param("qtype") and ($cgi->param("qtype") eq "cnbrowse")
160 my $page = $cgi->param('page') || 0;
161 my $facet = $cgi->param('facet');
162 my $limit = $self->_get_search_limit;
163 my $loc = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
164 my $offset = $page * $limit;
165 my $metarecord = $cgi->param('metarecord');
168 my ($query, $site, $depth) = _prepare_biblio_search($cgi, $ctx);
172 # TODO: other limits, like SVF/format, etc.
173 $results = $U->simplereq(
175 'open-ils.search.biblio.metarecord_to_records',
176 $metarecord, {org => $loc, depth => $depth}
179 # force the metarecord result blob to match the format of regular search results
180 $results->{ids} = [map { [$_] } @{$results->{ids}}];
184 return $self->generic_redirect unless $query;
186 # Limit and offset will stay here. Everything else should be part of
187 # the query string, not special args.
188 my $args = {'limit' => $limit, 'offset' => $offset};
190 # Stuff these into the TT context so that templates can use them in redrawing forms
191 $ctx->{processed_search_query} = $query;
193 $query = "$query $facet" if $facet; # TODO
195 $logger->activity("EGWeb: [search] $query");
199 my $method = 'open-ils.search.biblio.multiclass.query';
200 $method .= '.staff' if $ctx->{is_staff};
201 $results = $U->simplereq('open-ils.search', $method, $args, $query, 1);
205 $logger->error("multiclass search error: $err");
206 $results = {count => 0, ids => []};
210 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
212 $ctx->{records} = [];
213 $ctx->{search_facets} = {};
214 $ctx->{page_size} = $limit;
215 $ctx->{hit_count} = $results->{count};
216 $ctx->{parsed_query} = $results->{parsed_query};
218 return Apache2::Const::OK if @$rec_ids == 0;
220 my ($facets, @data) = $self->get_records_and_facets(
221 $rec_ids, $results->{facet_key},
223 flesh => '{holdings_xml,mra,acp}',
229 # shove recs into context in search results order
230 for my $rec_id (@$rec_ids) {
233 grep { $_->{id} == $rec_id } @data
237 $ctx->{search_facets} = $facets;
239 return Apache2::Const::OK;
242 # Searching by barcode is a special search that does /not/ respect any other
243 # of the usual search parameters, not even the ones for sorting and paging!
244 sub item_barcode_shortcut {
247 my $method = "open-ils.search.multi_home.bib_ids.by_barcode";
248 if (my $search = create OpenSRF::AppSession("open-ils.search")) {
249 my $rec_ids = $search->request(
250 $method, $self->cgi->param("query")
254 if (ref $rec_ids ne 'ARRAY') {
256 if($U->event_equals($rec_ids, 'ASSET_COPY_NOT_FOUND')) {
260 if (defined $U->event_code($rec_ids)) {
261 $self->apache->log->warn(
262 "$method returned event: " . $U->event_code($rec_ids)
265 $self->apache->log->warn(
266 "$method returned something unexpected: $rec_ids"
269 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
273 my ($facets, @data) = $self->get_records_and_facets(
274 $rec_ids, undef, {flesh => "{holdings_xml,mra}"}
277 $self->ctx->{records} = [@data];
278 $self->ctx->{search_facets} = {};
279 $self->ctx->{hit_count} = scalar @data;
280 $self->ctx->{page_size} = $self->ctx->{hit_count};
282 return Apache2::Const::OK;
284 $self->apache->log->warn("couldn't connect to open-ils.search");
285 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
289 # like item_barcode_search, this can't take all the usual search params, but
290 # this one will at least do site, limit and page
291 sub marc_expert_search {
294 my @tags = $self->cgi->param("tag");
295 my @subfields = $self->cgi->param("subfield");
296 my @terms = $self->cgi->param("term");
299 for (my $i = 0; $i < scalar @tags; $i++) {
300 next if ($tags[$i] eq "" || $terms[$i] eq "");
301 $subfields[$i] = '_' unless $subfields[$i];
303 "term" => $terms[$i],
304 "restrict" => [{"tag" => $tags[$i], "subfield" => $subfields[$i]}]
308 $logger->info("query for expert search: " . Dumper($query));
310 # loc, limit and offset
311 my $page = $self->cgi->param("page") || 0;
312 my $limit = $self->_get_search_limit;
313 my $org_unit = $self->cgi->param("loc") || $self->ctx->{aou_tree}->()->id;
314 my $offset = $page * $limit;
316 $self->ctx->{records} = [];
317 $self->ctx->{search_facets} = {};
318 $self->ctx->{page_size} = $limit;
319 $self->ctx->{hit_count} = 0;
322 return Apache2::Const::OK if @$query == 0;
324 my $results = $U->simplereq(
326 'open-ils.search.biblio.marc',
327 {searches => $query, org_unit => $org_unit}, $limit, $offset);
329 if (defined $U->event_code($results)) {
330 $self->apache->log->warn(
331 "open-ils.search.biblio.marc returned event: " .
332 $U->event_code($results)
334 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
337 my ($facets, @data) = $self->get_records_and_facets(
338 # filter out nulls that will turn up here
339 [ grep { $_ } @{$results->{ids}} ],
340 undef, {flesh => "{holdings_xml,mra}"}
343 $self->ctx->{records} = [@data];
344 $self->ctx->{page_size} = $limit;
345 $self->ctx->{hit_count} = $results->{count};
347 return Apache2::Const::OK;
350 sub call_number_browse_standalone {
353 if (my $cnfrag = $self->cgi->param("query")) {
355 'http%s://%s%s/cnbrowse?cn=%s',
356 $self->cgi->https ? "s" : "",
357 $self->apache->hostname,
358 $self->ctx->{opac_root},
359 $cnfrag # XXX some kind of escaping needed here?
361 return $self->generic_redirect($url);
363 return $self->generic_redirect; # return to search page
370 $self->prepare_browse_call_numbers();
372 return Apache2::Const::OK;