diff options
author | Eric Wong <e@80x24.org> | 2017-02-09 01:37:03 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2017-02-09 01:37:03 +0000 |
commit | d9563ea5516e8e786debf223e10ec11695aee9d7 (patch) | |
tree | a0842b1ee0953bc8e65d21d5d96432f41973adb7 /lib/PublicInbox/RepoGitQuery.pm | |
parent | fb9ed5324ec7de9420956840ba9a6585b81e8231 (diff) | |
download | public-inbox-d9563ea5516e8e786debf223e10ec11695aee9d7.tar.gz |
We'll still be keeping "repobrowse" for the public API for use with .psgi files, but shortening the name means less typing and we may have command-line tools, too.
Diffstat (limited to 'lib/PublicInbox/RepoGitQuery.pm')
-rw-r--r-- | lib/PublicInbox/RepoGitQuery.pm | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/PublicInbox/RepoGitQuery.pm b/lib/PublicInbox/RepoGitQuery.pm new file mode 100644 index 00000000..638a1316 --- /dev/null +++ b/lib/PublicInbox/RepoGitQuery.pm @@ -0,0 +1,50 @@ +# Copyright (C) 2015 all contributors <meta@public-inbox.org> +# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> + +# query parameter management for repobrowse +package PublicInbox::RepoGitQuery; +use strict; +use warnings; +use PublicInbox::Hval; +use URI::Escape qw(uri_unescape); +my @KNOWN_PARAMS = qw(id id2 h ofs); + +sub new { + my ($class, $env) = @_; + # we don't care about multi-value + my %tmp = map { + my ($k, $v) = split('=', uri_unescape($_), 2); + $v = '' unless defined $v; + $v =~ tr/+/ /; + ($k, $v) + } split(/[&;]/, $env->{QUERY_STRING}); + + my $self = {}; + foreach (@KNOWN_PARAMS) { + my $v = $tmp{$_}; + $self->{$_} = defined $v ? $v : ''; + } + bless $self, $class; +} + +sub qs { + my ($self, %over) = @_; + + if (keys %over) { + my $tmp = bless { %$self }, ref($self); + foreach my $k (keys %over) { $tmp->{$k} = $over{$k}; } + $self = $tmp; + } + + my @qs; + foreach my $k (@KNOWN_PARAMS) { + my $v = $self->{$k}; + + next if ($v eq ''); + $v = PublicInbox::Hval->new($v)->as_href; + push @qs, "$k=$v"; + } + scalar(@qs) ? ('?' . join('&', @qs)) : ''; +} + +1; |