File: List.pm

package info (click to toggle)
libsvn-web-perl 0.63-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: perl: 1,987; sh: 73; makefile: 11
file content (177 lines) | stat: -rw-r--r-- 3,936 bytes parent folder | download | duplicates (4)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package SVN::Web::List;

use strict;
use warnings;

use base 'SVN::Web::action';

use File::Basename ();

our $VERSION = 0.54;

=head1 NAME

SVN::Web::List - SVN::Web action to list available repositories

=head1 SYNOPSIS

In F<config.yaml>

  actions:
    ...
    list:
      class: SVN::Web::List
      opts:
        redirect_to_browse_when_one_repo: 0 # or 1
        public_repos_uri: hide_local
    ...

=head1 DESCRIPTION

Displays a list of available Subversion repositories for browsing.  If only
one repo is available then may redirect straight to it.

=head1 CONFIGURATION

The following options may be specified in F<config.yaml>

=over

=item redirect_to_browse_when_one_repo

Boolean indicating whether, if only one repository is available, SVN::Web::List
should immediately issue a redirect to browse that repository, thereby saving
the user a mouse click.

Defaults to 0.

=item public_repos_uri

Can be 'hide_local', which will hide the URL for any file:// repositories,
'hide_all', which will hide all repositories, or a hash.  The hash should be
keyed by the repository name, and the values should be either a URL display, or
a false value to hide the URL.

The default is to show all repository URLs.

=back

=head1 TEMPLATE VARIABLES

=over 8

=item reposcount

The number of repositories that were configured.

=item repos

A hash.  Keys are repository names, paths are repository URLs.

=back

=head1 EXCEPTIONS

None.

=cut

my %default_opts = ( redirect_to_browse_when_one_repo => 0 );

sub run {
    my $self = shift;

    $self->{opts} = { %default_opts, %{ $self->{opts} } };

    my %repos = repos_list( $self->{config} );

    # If there's only one repo listed then jump straight to it
    if ( keys %repos == 1 and $self->{opts}{redirect_to_browse_when_one_repo} )
    {
        my $url = $self->{cgi}->self_url();
        $url =~ s{/$}{};
        $url .= '/' . ( keys %repos )[0];
        print $self->{cgi}->redirect( -uri => $url );
        return;
    }

    my $public_repos = $self->{opts}{public_repos_uri} || '';
    if (lc $public_repos eq 'hide_all') {
        for my $rep (values %repos) {
            undef $rep;
        }
    }
    elsif (lc $public_repos eq 'hide_local') {
        for my $rep (values %repos) {
            if ($rep =~ m{file://}) {
                undef $rep;
            }
        }
    }
    elsif (ref $public_repos eq 'HASH') {
        for my $repname (keys %repos) {
            if (exists $public_repos->{$repname}) {
                $repos{$repname} = $public_repos->{$repname};
            }
        }
    }

    return {
        template => 'list',
        data     => {
            action     => 'list',
            nonav      => 1,
            repos      => \%repos,
            reposcount => scalar keys %repos
        }
    };
}

sub repos_list {
    my $config = shift;

    my %repos;
    if ( $config->{reposparent} ) {
        opendir my $dh, "$config->{reposparent}"
          or SVN::Web::X->throw(
            error => '(opendir reposparent %1 %2)',
            vars  => [ $config->{reposparent}, $! ]
          );

        for my $dir (
            grep {
                -d File::Spec->catdir( $config->{reposparent}, $_ )
                  && !/^\./
            } readdir $dh
          )
        {
            $repos{$dir} =
              'file://' . File::Spec->catdir( $config->{reposparent}, $dir );
        }
    }
    else {

        %repos = %{ $config->{repos} };
    }

    delete @repos{ @{ $config->{block} } } if exists $config->{block};

    return %repos;
}

1;

=head1 COPYRIGHT

Copyright 2003-2004 by Chia-liang Kao C<< <clkao@clkao.org> >>.

Copyright 2005-2007 by Nik Clayton C<< <nik@FreeBSD.org> >>.

Copyright 2012 by Dean Hamstead C<< <dean@fragfest.com.au> >>.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut