]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchQuery.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / SearchQuery.pm
1 # Copyright (C) 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 and PublicInbox::WwwListing
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         my ($o) = (($qp->{o} || '0') =~ /(-?[0-9]+)/);
20         bless {
21                 q => $qp->{'q'},
22                 x => $qp->{x} || '',
23                 o => $o,
24                 l => $l,
25                 r => (defined $r && $r ne '0'),
26                 t => (defined $t && $t ne '0'),
27         }, $class;
28 }
29
30 sub qs_html {
31         my ($self, %override) = @_;
32
33         if (scalar(keys(%override))) {
34                 $self = bless { (%$self, %override) }, ref($self);
35         }
36         my $qs = '';
37         if (defined(my $q = $self->{'q'})) {
38                 $q = uri_escape($q, MID_ESC);
39                 $q =~ s/%20/+/g; # improve URL readability
40                 $qs .= "q=$q";
41         }
42         if (my $o = $self->{o}) { # ignore o == 0
43                 $qs .= "&amp;o=$o";
44         }
45         if (my $l = $self->{l}) {
46                 $qs .= "&amp;l=$l" unless $l == $LIM;
47         }
48         for my $bool (qw(r t)) {
49                 $qs .= "&amp;$bool" if $self->{$bool};
50         }
51         if (my $x = $self->{x}) {
52                 $qs .= "&amp;x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm');
53         }
54         $qs;
55 }
56
57 1;