X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FBackPAN%2FWeb.pm;h=87c332c6b4fed0f649edbb04f56e1737e9a7ba7e;hb=e819827c0c7ea47aef4897166f3f8a53d4bf98f0;hp=c1062a00f7e8983c501bcc21c675f99d285fa8c9;hpb=b67ffc2edcc6c4685a9e7c1e8f7be131d487626b;p=catagits%2FBackPAN-Web.git diff --git a/lib/BackPAN/Web.pm b/lib/BackPAN/Web.pm index c1062a0..87c332c 100644 --- a/lib/BackPAN/Web.pm +++ b/lib/BackPAN/Web.pm @@ -13,7 +13,7 @@ use File::stat; use DateTime; use Log::Log4perl 'get_logger'; -our $VERSION = '0.12'; +our $VERSION = '0.14'; default_config( template_dir => 'root/html', @@ -47,7 +47,6 @@ sub backpan_index { return $_[0]->{'backpan_index'} ||= BackPAN::Index->new({ update => 0, debug => 0, - cache_dir => 'index', releases_only_from_authors => 1, }); } @@ -89,8 +88,8 @@ sub error_404 { sub html_response { my ( $self, $args ) = @_; - my ( $header, $body ) = @$args{qw/header body/}; - return [ 200, [ + my ( $status, $header, $body ) = @$args{qw/status_code header body/}; + return [ $status || 200, [ $header ? ( %$header ) : (), 'Content-type' => 'text/html', ], ref $body ? $body->to_fh : [ $body ] ]; @@ -354,14 +353,21 @@ sub authors_page_content { ->apply($self->add_paging($authors->{'pager'})); } +sub _mangle_query_string { + my ( $self, $q ) = @_; + $q =~ s{\s+|::|\+}{-}g; + $q =~ s{-$}{}; + return $q =~ s{\*}{}g ? "$q%" : "%$q%"; +} + sub search { my ( $self, $q, $query_params ) = @_; - my $query = lc "%$q%"; + my $query_str = lc $self->_mangle_query_string($q); return $self->dists($query_params)->search({ -or => [ - { 'LOWER(me.name)' => { -like => $query } }, - { 'LOWER(me.first_author)' => { -like => $query } }, - { 'LOWER(me.latest_author)' => { -like => $query } }, + { 'LOWER(me.name)' => { -like => $query_str } }, + { 'LOWER(me.first_author)' => { -like => $query_str } }, + { 'LOWER(me.latest_author)' => { -like => $query_str } }, ], }); } @@ -369,7 +375,6 @@ sub search { dispatch { subdispatch sub () { $self->_build_request_obj_from($_[+PSGI_ENV]); - my $body; [ sub (/) { $self->html_response({ body => $self->index_page_content }); @@ -390,57 +395,76 @@ dispatch { sub ( /releases|/releases/ + ?* ) { my $release_rs = $self->releases($_[1]); if ( $release_rs->count ) { - $body = $self->releases_page_content($release_rs) + my $body = $self->releases_page_content($release_rs) ->select('#nav-releases')->add_to_attribute(class => 'active'); + return $self->html_response({ body => $body }); } else { - $body = $self->error_404; + return $self->html_response({ + status_code => 404, + body => $self->error_404, + }); } - $self->html_response({ body => $body }); }, sub ( /dists|/dists/ + ?* ) { my $dist_rs = $self->dists($_[1]); if ( $dist_rs->count ) { - $body = $self->dists_page_content($dist_rs) + my $body = $self->dists_page_content($dist_rs) ->select('#nav-dists')->add_to_attribute(class => 'active'); + return $self->html_response({ body => $body }); } else { - $body = $self->error_404; + return $self->html_response({ + status_code => 404, + body => $self->error_404, + }); } - $self->html_response({ body => $body }); }, sub ( /distribution/*|/distribution/*/ + ?* ) { if ( my $dist = $self->get_dist($_[1]) ) { - $body = $self->dist_info_page_content($dist, $_[2]); + my $body = $self->dist_info_page_content($dist, $_[2]); + return $self->html_response({ body => $body }); } else { - $body = $self->error_404; + return $self->html_response({ + status_code => 404, + body => $self->error_404, + }); } - $self->html_response({ body => $body }); }, sub ( /authors|/authors/ + ?* ) { if ( my $authors = $self->authors($_[1]) ) { - $body = $self->authors_page_content($authors) + my $body = $self->authors_page_content($authors) ->select('#nav-authors')->add_to_attribute(class => 'active'); + return $self->html_response({ body => $body }); } else { - $body = $self->error_404; + return $self->html_response({ + status_code => 404, + body => $self->error_404, + }); } - $self->html_response({ body => $body }); }, sub ( /search|/search/ + ?q=&* ) { - my $dist_rs = $self->search(@_[1,2]); + my ( $self, $query_str, $query_params ) = @_; + my $dist_rs = $self->search($query_str, $query_params); if ( $dist_rs->count ) { - $body = $self->dists_page_content($dist_rs); + my $body = $self->dists_page_content($dist_rs) + ->select('#q')->add_to_attribute( + value => $query_str + ); + return $self->html_response({ body => $body }); } else { - $body = $self->error_404; + return $self->html_response({ + status_code => 404, + body => $self->error_404, + }); } - $self->html_response({ body => $body }); }, ], },