3 use Web::Simple __PACKAGE__;
8 use HTML::Zoom::FilterBuilder::Template;
11 use Data::Page::FlickrLike;
14 use Log::Log4perl 'get_logger';
16 our $VERSION = '0.12';
19 template_dir => 'root/html',
20 backpan_url => 'http://backpan.perl.org/',
21 cpan_search_url => 'http://search.cpan.org/',
24 sub _build_request_obj_from {
25 my ( $self, $env ) = @_;
26 return $self->request(Plack::Request->new($env));
31 $self->{'request'} = shift if @_;
32 return $self->{'request'};
35 sub req { return shift->request(@_) }
38 my ( $self, $level, $msg ) = @_;
40 $self->request->{'env'}->{'psgix.logger'}->({
47 return $_[0]->{'backpan_index'} ||= BackPAN::Index->new({
51 releases_only_from_authors => 1,
56 my ( $self, $filename ) = @_;
57 return do { local (@ARGV, $/) = $filename; <> };
60 sub template_filename_for {
61 my ( $self, $name ) = @_;
62 return $self->config->{'template_dir'} . "/${name}.html";
67 return $self->{'template_zoom_for_template'}{'layout'} ||= HTML::Zoom->from_file(
68 $self->template_filename_for('layout')
72 sub template_zoom_for {
73 my ( $self, $template_name ) = @_;
74 $self->{'template_zoom_for_template'}{$template_name} ||= do {
76 HTML::Zoom->from_file(
77 $self->template_filename_for($template_name)
78 )->select('#content')->collect_content({ into => \@body })->run;
80 ->select('#content')->replace_content(\@body)
87 return $self->slurp( $self->template_filename_for('error_404') );
91 my ( $self, $args ) = @_;
92 my ( $header, $body ) = @$args{qw/header body/};
94 $header ? ( %$header ) : (),
95 'Content-type' => 'text/html',
96 ], ref $body ? $body->to_fh : [ $body ] ];
100 my ( $self, $resultset, $row_data_cb ) = @_;
101 my $req_base = $self->req->base;
104 $_->select('.main-list')
107 my ( $name, $label, $href ) = $self->$row_data_cb($row);
110 $zoom = $zoom->select('li')->add_to_attribute(class => 'even')
112 if ( $href =~ m/http/i ) {
113 $zoom = $zoom->select('a')->add_to_attribute(href => $href)
115 ->add_to_attribute(target => '_blank')
119 $zoom = $zoom->select('a')->add_to_attribute(
120 href => $req_base . "${href}/${name}/"
123 $zoom->replace_content($label);
125 } ( ref $resultset eq 'ARRAY' ? @$resultset : $resultset->all )
130 sub add_paging_ordering {
131 my ( $self, $pager, $ordering_options ) = @_;
133 $_->apply($self->add_paging($pager))
134 ->apply($self->add_ordering($ordering_options));
139 my ( $self, $pager ) = @_;
140 my ( $curr_page, $curr_page_size )
141 = ( $pager->current_page, $pager->entries_per_page );
142 my $paging_uri = $self->req->uri;
146 map { my $page_number = $_;
149 $_->select('span')->replace_content('...');
152 $paging_uri->query_form({
153 $paging_uri->query_form,
154 page => $page_number,
155 rows => $curr_page_size,
157 $_->select('a')->add_to_attribute(href => $paging_uri)
159 ->replace_content($page_number);
161 } $pager->navigations
163 ->select('.paging-desc')
165 join(q{ }, 'Page', $curr_page, 'of', $pager->last_page) . q{.}
168 ->replace_content($pager->total_entries . ' entries.')
169 ->select('.page-size-options')
172 my $page_size = $_; sub {
173 $paging_uri->query_form({
174 $paging_uri->query_form,
178 $_->select('a')->add_to_attribute(href => $paging_uri)
180 ->replace_content($page_size);
182 } qw/10 20 30 50 100 200/
188 my ( $self, $options ) = @_;
189 my $ordering_uri = $self->req->uri;
191 $_->select('.ordering-options')
193 map { my $order_by = $_; sub {
194 $ordering_uri->query_form({
195 $ordering_uri->query_form,
196 order_by => $order_by,
198 my $order_by_label = join(q{ }, map ucfirst, split(/\_/, $order_by));
199 $_->select('a')->add_to_attribute(href => $ordering_uri)
201 ->replace_content($order_by_label);
208 sub index_page_content {
210 return $self->template_zoom_for('index')
211 ->apply($self->add_listing(scalar $self->releases, sub {
212 return ((map { $_[1]->$_ } qw/dist distvname/), 'distribution');
216 sub validate_paging_params {
217 my ( $self, $args ) = @_;
218 my ( $page, $rows ) = @$args{qw/page rows/};
219 $page = 1 unless $page && $page =~ /^\d+$/;
220 $rows = 25 unless $rows && $rows =~ /^\d+$/;
221 return ( $page, $rows );
225 my ( $self, $args ) = @_;
226 my ( $order_by, $page, $rows )
227 = ( $args->{'order_by'}, $self->validate_paging_params($args) );
228 return $self->backpan_index->releases->search({}, {
229 order_by => { -desc => 'date' },
235 sub releases_page_content {
236 my ( $self, $release_rs ) = @_;
237 return $self->template_zoom_for('listing')
238 ->apply($self->add_listing($release_rs, sub {
239 return ((map { $_[1]->$_ } qw/dist distvname/), 'distribution');
241 ->apply($self->add_paging($release_rs->pager));
245 my ( $self, $args ) = @_;
246 my ( $order_by, $page, $rows )
247 = ( $args->{'order_by'}, $self->validate_paging_params($args) );
248 return $self->backpan_index->dists->search({}, {
255 sub get_dist { return shift->backpan_index->dist(@_) }
257 sub format_dist_name { return join(q{::}, split /-/, $_[1] ) }
259 sub dists_page_content {
260 my ( $self, $dist_rs ) = @_;
261 return $self->template_zoom_for('listing')
262 ->apply($self->add_listing($dist_rs, sub {
263 my $dist_name = $_[1]->name;
265 $dist_name, $self->format_dist_name($dist_name), 'distribution'
268 ->apply($self->add_paging($dist_rs->pager));
271 sub dist_info_page_content {
272 my ( $self, $dist, $query_params ) = @_;
273 my ( $page, $rows ) = $self->validate_paging_params($query_params);
274 my $release_rs = $dist->releases->search({}, {
275 order_by => { -desc => 'date' },
279 my ( $f_release, $l_release )
280 = ( $dist->first_release, $dist->latest_release );
281 my @maints = $dist->authors;
282 my $config = $self->config;
283 my ( $backpan_url, $cpan_search_url )
284 = ( $config->{'backpan_url'}, $config->{'cpan_search_url'} );
285 return $self->template_zoom_for('dist')
286 ->select('#dist')->template_text_raw({
287 name => $self->format_dist_name($dist->name),
288 num_releases => $dist->num_releases,
289 f_release_label => $f_release->distvname,
290 l_release_label => $l_release->distvname,
292 ->select('.f_rel_link')->add_to_attribute(
293 href => $backpan_url . $f_release->path->path,
295 ->select('.l_rel_link')->add_to_attribute(
296 href => $backpan_url . $l_release->path->path,
298 ->select('.maintainer-list')->repeat_content([
299 map { my $cpanid = $_; sub {
300 $_->select('a')->add_to_attribute(
301 href => $cpan_search_url . lc "~${cpanid}"
303 ->add_to_attribute(target => '_blank')
305 ->replace_content($cpanid);
309 ->apply($self->add_listing($release_rs, sub {
313 join(q{ | }, $release->distvname,
314 DateTime->from_epoch({ epoch => $release->date })
315 ->strftime('%b %d, %Y - %T')),
316 $backpan_url . $release->path->path,
319 ->apply($self->add_paging($release_rs->pager));
323 my ( $self, $args ) = @_;
324 my ( $page, $rows ) = $self->validate_paging_params($args);
325 my @authors = $self->backpan_index->releases->search({}, {
326 group_by => 'cpanid',
327 order_by => 'cpanid',
328 })->get_column('cpanid')->all;
329 my $pager = Data::Page->new;
330 $pager->total_entries(scalar @authors);
331 $pager->entries_per_page($rows);
332 if ( $page > $pager->last_page ) {
336 $pager->current_page($page);
338 list => [ splice @authors, ($page-1) * $rows, $rows ],
344 sub authors_page_content {
345 my ( $self, $authors ) = @_;
346 return $self->template_zoom_for('listing')
347 ->apply($self->add_listing($authors->{'list'}, sub {
351 $self->config->{'cpan_search_url'} . lc "~${cpanid}"
354 ->apply($self->add_paging($authors->{'pager'}));
358 my ( $self, $q, $query_params ) = @_;
359 my $query = lc "%$q%";
360 return $self->dists($query_params)->search({
362 { 'LOWER(me.name)' => { -like => $query } },
363 { 'LOWER(me.first_author)' => { -like => $query } },
364 { 'LOWER(me.latest_author)' => { -like => $query } },
371 $self->_build_request_obj_from($_[+PSGI_ENV]);
375 $self->html_response({ body => $self->index_page_content });
378 sub ( /about|/about/ ) {
379 my $about_filename = $self->template_filename_for('about');
380 my $about_st = stat($about_filename)
381 or $self->log(error_die => "No $about_filename: $!");
382 $self->html_response({
384 'Last-Modified' => $about_st->mtime,
386 body => $self->slurp($about_filename),
390 sub ( /releases|/releases/ + ?* ) {
391 my $release_rs = $self->releases($_[1]);
392 if ( $release_rs->count ) {
393 $body = $self->releases_page_content($release_rs)
394 ->select('#nav-releases')->add_to_attribute(class => 'active');
397 $body = $self->error_404;
399 $self->html_response({ body => $body });
402 sub ( /dists|/dists/ + ?* ) {
403 my $dist_rs = $self->dists($_[1]);
404 if ( $dist_rs->count ) {
405 $body = $self->dists_page_content($dist_rs)
406 ->select('#nav-dists')->add_to_attribute(class => 'active');
409 $body = $self->error_404;
411 $self->html_response({ body => $body });
414 sub ( /distribution/*|/distribution/*/ + ?* ) {
415 if ( my $dist = $self->get_dist($_[1]) ) {
416 $body = $self->dist_info_page_content($dist, $_[2]);
419 $body = $self->error_404;
421 $self->html_response({ body => $body });
424 sub ( /authors|/authors/ + ?* ) {
425 if ( my $authors = $self->authors($_[1]) ) {
426 $body = $self->authors_page_content($authors)
427 ->select('#nav-authors')->add_to_attribute(class => 'active');
430 $body = $self->error_404;
432 $self->html_response({ body => $body });
435 sub ( /search|/search/ + ?q=&* ) {
436 my $dist_rs = $self->search(@_[1,2]);
437 if ( $dist_rs->count ) {
438 $body = $self->dists_page_content($dist_rs);
441 $body = $self->error_404;
443 $self->html_response({ body => $body });
451 my $app = $class->SUPER::as_psgi_app;
453 enable_if { $ENV{PLACK_ENV} ne 'deployment' }
455 my $mw_prefix = 'Plack::Middleware';
456 my $mw_class = Plack::Util::load_class('Static', $mw_prefix);
457 $app = $mw_class->wrap($app,
458 path => qr{^/static},
461 $mw_class = Plack::Util::load_class('Debug', $mw_prefix);
462 $app = $mw_class->wrap($app,
463 panels => [qw(DBITrace Memory Timer Environment Response)],
467 enable 'ContentLength';
468 enable 'ConditionalGET';
469 enable 'ErrorDocument',
470 500 => 'root/html/error_500.html',
471 404 => 'root/html/error_404.html';
472 enable 'HTTPExceptions';
475 format => 'combined',
476 logger => sub { get_logger('accesslog')->info(@_) };
477 enable 'Log4perl', conf => 'log/log.conf';
484 Wallace Reis, C<< <wreis at cpan.org> >>
486 =head1 LICENSE AND COPYRIGHT
488 Copyright 2010 Wallace Reis.
492 __PACKAGE__->run_if_script;