Baseline - Launch of backpan.org
[catagits/BackPAN-Web.git] / lib / Parse / BACKPAN / Packages.pm
1 package Parse::BACKPAN::Packages;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.39';
7
8 use parent qw(Class::Accessor::Fast);
9
10 use BackPAN::Index;
11
12 __PACKAGE__->mk_accessors(qw(
13     _delegate
14 ));
15
16 sub new {
17     my $class   = shift;
18     my $options = shift;
19
20     # Translate from PBP options to BackPAN::Index
21     $options->{update}                     = 1 if $options->{no_cache};
22     $options->{releases_only_from_authors} = $options->{only_authors};
23
24     my $backpan = BackPAN::Index->new($options);
25     return $class->SUPER::new({ _delegate => $backpan });
26 }
27
28 our $AUTOLOAD;
29 sub AUTOLOAD {
30     my $self = shift;
31     my($method) = $AUTOLOAD =~ /:: ([^:]+) $/x;
32
33     # Skip things like DESTROY
34     return if uc $method eq $method;
35
36     $self->_delegate->$method(@_);
37 }
38
39 sub files {
40     my $self = shift;
41
42     my %files;
43     my $rs = $self->_delegate->files;
44     while( my $file = $rs->next ) {
45         $files{$file->path} = $file;
46     }
47     
48     return \%files;
49 }
50
51 sub file {
52     my ( $self, $path ) = @_;
53
54     return $self->_delegate->files->single({ path => $path });
55 }
56
57 sub releases {
58     my($self, $dist) = @_;
59
60     return $self->_delegate->releases($dist)->all;
61 }
62
63
64 sub distributions {
65     my $self = shift;
66
67     # For backwards compatibilty when releases() was distributions()
68     return $self->releases(shift) if @_;
69
70     return [$self->_delegate->distributions->get_column("name")->all];
71 }
72
73 sub distributions_by {
74     my ( $self, $author ) = @_;
75     return unless $author;
76
77     my $dists = $self->_dbh->selectcol_arrayref(q[
78              SELECT DISTINCT dist
79              FROM   releases
80              WHERE  cpanid = ?
81              ORDER BY dist
82         ],
83         undef,
84         $author
85     );
86
87     return @$dists;
88 }
89
90 sub authors {
91     my $self     = shift;
92
93     my $authors = $self->_dbh->selectcol_arrayref(q[
94         SELECT DISTINCT cpanid
95         FROM     releases
96         ORDER BY cpanid
97     ]);
98
99     return @$authors;
100 }
101
102 sub size {
103     my $self = shift;
104
105     my $size = $self->_dbh->selectcol_arrayref(q[
106         SELECT SUM(size) FROM files
107     ]);
108
109     return $size->[0];
110 }
111
112 1;
113
114 __END__
115
116 =head1 NAME
117
118 Parse::BACKPAN::Packages - Provide an index of BACKPAN
119
120 =head1 SYNOPSIS
121
122   use Parse::BACKPAN::Packages;
123   my $p = Parse::BACKPAN::Packages->new();
124   print "BACKPAN is " . $p->size . " bytes\n";
125
126   my @filenames = keys %$p->files;
127
128   # see Parse::BACKPAN::Packages::File
129   my $file = $p->file("authors/id/L/LB/LBROCARD/Acme-Colour-0.16.tar.gz");
130   print "That's " . $file->size . " bytes\n";
131
132   # see Parse::BACKPAN::Packages::Release
133   my @acme_colours = $p->releases("Acme-Colour");
134   
135   my @authors = $p->authors;
136   my @acmes = $p->distributions_by('LBROCARD');
137
138 =head1 DESCRIPTION
139
140 Parse::BackPAN::Packages has been B<DEPRECATED>.  Please use the
141 faster and more flexible L<BackPAN::Index>.
142
143 The Comprehensive Perl Archive Network (CPAN) is a very useful
144 collection of Perl code. However, in order to keep CPAN relatively
145 small, authors of modules can delete older versions of modules to only
146 let CPAN have the latest version of a module. BACKPAN is where these
147 deleted modules are backed up. It's more like a full CPAN mirror, only
148 without the deletions. This module provides an index of BACKPAN and
149 some handy functions.
150
151 The data is fetched from the net and cached for an hour.
152
153 =head1 METHODS
154
155 =head2 new
156
157 The constructor downloads a ~1M index file from the web and parses it,
158 so it might take a while to run:
159
160   my $p = Parse::BACKPAN::Packages->new();
161
162 By default it caches the file locally for one hour. If you do not
163 want this caching then you can pass in:
164
165   my $p = Parse::BACKPAN::Packages->new( { no_cache => 1 } );
166
167 =head2 authors
168
169 The authors method returns a list of all the authors. This is meant so
170 that you can pass them into the distributions_by method:
171
172   my @authors = $p->authors;
173
174 =head2 distributions
175
176   my $distributions = $p->distributions;
177
178 The distributions method returns an array ref of the names of all the
179 distributions in BackPAN.
180
181 =head2 releases
182
183 The releases method returns a list of objects representing all
184 the different releases of a distribution:
185
186   # see Parse::BACKPAN::Packages::Release
187   my @acme_colours = $p->releases("Acme-Colour");
188
189 =head2 distributions_by
190
191 The distributions_by method returns a list of distribution names
192 representing all the distributions that an author has uploaded:
193
194   my @acmes = $p->distributions_by('LBROCARD');
195
196 =head2 file
197
198 The file method finds metadata relating to a file:
199
200   # see Parse::BACKPAN::Packages::File
201   my $file = $p->file("authors/id/L/LB/LBROCARD/Acme-Colour-0.16.tar.gz");
202   print "That's " . $file->size . " bytes\n";
203
204 =head2 files
205
206 The files method returns a hash reference where the keys are the
207 filenames of the files on CPAN and the values are
208 Parse::BACKPAN::Packages::File objects:
209
210   my @filenames = keys %$p->files;
211
212 =head2 size
213
214 The size method returns the sum of all the file sizes in BACKPAN:
215
216   print "BACKPAN is " . $p->size . " bytes\n";
217
218 =head1 AUTHOR
219
220 Leon Brocard <acme@astray.com>
221
222 =head1 COPYRIGHT
223
224 Copyright (C) 2005-9, Leon Brocard
225
226 =head1 LICENSE
227
228 This module is free software; you can redistribute it or modify it under
229 the same terms as Perl itself.
230
231 =head1 SEE ALSO
232
233 L<BackPAN::Index>, L<CPAN::DistInfoname>