Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 1 | package Kalamar::Controller::Search2; |
| 2 | use Mojo::Base 'Mojolicious::Controller'; |
| 3 | |
| 4 | has api => 'http://10.0.10.52:9000/api/'; |
| 5 | |
| 6 | has no_cache => 0; |
| 7 | |
| 8 | has items_per_page => 25; |
| 9 | |
| 10 | |
| 11 | sub query { |
| 12 | my $c = shift; |
| 13 | |
| 14 | # Validate user input |
| 15 | my $v = $c->validation; |
| 16 | |
| 17 | # In case the user is not known, it is assumed, the user is not logged in |
| 18 | my $user = $c->stash('user') // 'not_logged_in'; |
| 19 | |
| 20 | $v->optional('q', 'trim'); |
| 21 | $v->optional('ql')->in(qw/poliqarp cosmas2 annis cql fcsql/); |
| 22 | $v->optional('collection'); # Legacy |
| 23 | $v->optional('cq', 'trim'); |
| 24 | # $v->optional('action'); # action 'inspect' is no longer valid |
| 25 | $v->optional('snippet'); |
| 26 | $v->optional('cutoff')->in(qw/true false/); |
| 27 | $v->optional('count')->num(1, undef); |
| 28 | $v->optional('p'); |
| 29 | $v->optional('context'); |
| 30 | |
| 31 | # Get query |
| 32 | my $query = $v->param('q'); |
| 33 | |
| 34 | # No query |
| 35 | unless ($query) { |
| 36 | return $c->render($c->loc('Template_intro', 'intro')); |
| 37 | }; |
| 38 | |
| 39 | my %query = (); |
| 40 | $query{q} = $v->param('q'); |
| 41 | $query{ql} = $v->param('ql'); |
| 42 | $query{page} = $v->param('p') if $v->param('p'); |
| 43 | $query{count} = $v->param('count') // $c->items_per_page; |
| 44 | $query{cq} = $v->param('cq') // $v->param('collection'); |
| 45 | $query{cutoff} = $v->param('cutoff'); |
| 46 | $query{context} = $v->param('context') // '40-t,40-t'; # 'base/s:p'/'paragraph' |
| 47 | |
| 48 | |
| 49 | # Create remote request URL |
| 50 | my $url = Mojo::URL->new($c->api); |
| 51 | $url->query(\%query); |
| 52 | |
| 53 | # Check if total results is cached |
| 54 | my $total_results; |
| 55 | if (!$c->no_cache) { |
| 56 | $total_results = $c->chi->get('total-' . $user . '-' . $url->to_string); |
| 57 | $c->stash(total_results => $total_results); |
| 58 | $c->app->log->debug('Get total result from cache'); |
| 59 | |
| 60 | # Set cutoff unless already set |
| 61 | $url->query({cutoff => 'true'}); |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | |
| 66 | # Establish 'search_results' taghelper |
| 67 | # This is based on Mojolicious::Plugin::Search |
| 68 | $c->app->helper( |
| 69 | search_results => sub { |
| 70 | my $c = shift; |
| 71 | |
| 72 | # This is a tag helper for templates |
| 73 | my $cb = shift; |
| 74 | if (!ref $cb || !(ref $cb eq 'CODE')) { |
| 75 | $c->app->log->error('search_results expects a code block'); |
| 76 | return ''; |
| 77 | }; |
| 78 | |
| 79 | # Iterate over results |
| 80 | my $string = $c->stash('search.results')->map( |
| 81 | sub { |
| 82 | # Call hit callback |
| 83 | $c->stash('search.hit' => $_); |
| 84 | local $_ = $_[0]; |
| 85 | return $cb->($_); |
| 86 | })->join; |
| 87 | |
| 88 | # Remove hit from stash |
| 89 | delete $c->stash->{'search.hit'}; |
| 90 | return b($string); |
| 91 | } |
| 92 | ); |
| 93 | |
| 94 | |
| 95 | return $c->render( |
| 96 | template => 'search2', |
| 97 | q => $query, |
| 98 | ql => scalar $v->param('ql') // 'poliqarp', |
| 99 | result_size => 0, |
| 100 | start_page => 1, |
| 101 | total_pages => 20, |
| 102 | total_results => 40, |
| 103 | time_exceeded => 0, |
| 104 | benchmark => 'Long ...', |
| 105 | api_response => '' |
| 106 | ); |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | 1; |