From d9563ea5516e8e786debf223e10ec11695aee9d7 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 9 Feb 2017 01:37:03 +0000 Subject: repobrowse: shorten internal names 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. --- lib/PublicInbox/RepoGitQuery.pm | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/PublicInbox/RepoGitQuery.pm (limited to 'lib/PublicInbox/RepoGitQuery.pm') 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 +# License: AGPL-3.0+ + +# 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; -- cgit v1.2.3-24-ge0c7