1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
| | # Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# used by PublicInbox::SearchView and PublicInbox::WwwListing
package PublicInbox::SearchQuery;
use strict;
use v5.10.1;
use URI::Escape qw(uri_escape);
use PublicInbox::Hval qw(ascii_html);
our $LIM = 200;
sub new {
my ($class, $qp) = @_;
my $r = $qp->{r}; # relevance
my $t = $qp->{t}; # collapse threads
my ($l) = (($qp->{l} || '') =~ /([0-9]+)/);
$l = $LIM if !$l || $l > $LIM;
my ($o) = (($qp->{o} || '0') =~ /(-?[0-9]+)/);
bless {
q => $qp->{'q'},
x => $qp->{x} || '',
o => $o,
l => $l,
r => (defined $r && $r ne '0'),
t => (defined $t && $t ne '0'),
}, $class;
}
sub qs_html {
my ($self, %override) = @_;
if (scalar(keys(%override))) {
$self = bless { (%$self, %override) }, ref($self);
}
my $qs = '';
if (defined(my $q = $self->{'q'})) {
# not using MID_ESC since that's for the path component and
# this is for the query component. Unlike MID_ESC,
# this disallows [\&\'\+=] and allows slash [/] for
# nicer looking dfn: queries
$q = uri_escape($q, '^A-Za-z0-9\-\._~!\$\(\)\*,;:@/');
$q =~ s/%20/+/g; # improve URL readability
$qs .= 'q='.ascii_html($q);
}
if (my $o = $self->{o}) { # ignore o == 0
$qs .= "&o=$o";
}
if (my $l = $self->{l}) {
$qs .= "&l=$l" unless $l == $LIM;
}
for my $bool (qw(r t)) {
$qs .= "&$bool" if $self->{$bool};
}
if (my $x = $self->{x}) {
$qs .= "&x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm');
}
$qs;
}
1;
|