diff options
Diffstat (limited to 'lib/PublicInbox/SearchQuery.pm')
-rw-r--r-- | lib/PublicInbox/SearchQuery.pm | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/PublicInbox/SearchQuery.pm b/lib/PublicInbox/SearchQuery.pm new file mode 100644 index 00000000..ce1eae12 --- /dev/null +++ b/lib/PublicInbox/SearchQuery.pm @@ -0,0 +1,53 @@ +# Copyright (C) 2015-2020 all contributors <meta@public-inbox.org> +# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> + +# used by PublicInbox::SearchView +package PublicInbox::SearchQuery; +use strict; +use v5.10.1; +use URI::Escape qw(uri_escape); +use PublicInbox::MID qw(MID_ESC); +our $LIM = 200; + +sub new { + my ($class, $qp) = @_; + + my $r = $qp->{r}; + my ($l) = (($qp->{l} || '') =~ /([0-9]+)/); + $l = $LIM if !$l || $l > $LIM; + bless { + q => $qp->{'q'}, + x => $qp->{x} || '', + o => (($qp->{o} || '0') =~ /(-?[0-9]+)/), + l => $l, + r => (defined $r && $r ne '0'), + }, $class; +} + +sub qs_html { + my ($self, %override) = @_; + + if (scalar(keys(%override))) { + $self = bless { (%$self, %override) }, ref($self); + } + + my $q = uri_escape($self->{'q'}, MID_ESC); + $q =~ s/%20/+/g; # improve URL readability + my $qs = "q=$q"; + + if (my $o = $self->{o}) { # ignore o == 0 + $qs .= "&o=$o"; + } + if (my $l = $self->{l}) { + $qs .= "&l=$l" unless $l == $LIM; + } + if (my $r = $self->{r}) { + $qs .= "&r"; + } + if (my $x = $self->{x}) { + $qs .= "&x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm'); + } + $qs; +} + +1; |