]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchQuery.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / SearchQuery.pm
1 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # used by PublicInbox::SearchView
5 package PublicInbox::SearchQuery;
6 use strict;
7 use v5.10.1;
8 use URI::Escape qw(uri_escape);
9 use PublicInbox::MID qw(MID_ESC);
10 our $LIM = 200;
11
12 sub new {
13         my ($class, $qp) = @_;
14
15         my $r = $qp->{r}; # relevance
16         my $t = $qp->{t}; # collapse threads
17         my ($l) = (($qp->{l} || '') =~ /([0-9]+)/);
18         $l = $LIM if !$l || $l > $LIM;
19         bless {
20                 q => $qp->{'q'},
21                 x => $qp->{x} || '',
22                 o => (($qp->{o} || '0') =~ /(-?[0-9]+)/),
23                 l => $l,
24                 r => (defined $r && $r ne '0'),
25                 t => (defined $t && $t ne '0'),
26         }, $class;
27 }
28
29 sub qs_html {
30         my ($self, %override) = @_;
31
32         if (scalar(keys(%override))) {
33                 $self = bless { (%$self, %override) }, ref($self);
34         }
35
36         my $q = uri_escape($self->{'q'}, MID_ESC);
37         $q =~ s/%20/+/g; # improve URL readability
38         my $qs = "q=$q";
39
40         if (my $o = $self->{o}) { # ignore o == 0
41                 $qs .= "&amp;o=$o";
42         }
43         if (my $l = $self->{l}) {
44                 $qs .= "&amp;l=$l" unless $l == $LIM;
45         }
46         for my $bool (qw(r t)) {
47                 $qs .= "&amp;$bool" if $self->{$bool};
48         }
49         if (my $x = $self->{x}) {
50                 $qs .= "&amp;x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm');
51         }
52         $qs;
53 }
54
55 1;