use DateTime;
use Log::Log4perl 'get_logger';
-our $VERSION = '0.12';
+our $VERSION = '0.14';
default_config(
template_dir => 'root/html',
return $_[0]->{'backpan_index'} ||= BackPAN::Index->new({
update => 0,
debug => 0,
- cache_dir => 'index',
releases_only_from_authors => 1,
});
}
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 ] ];
->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 } },
],
});
}
dispatch {
subdispatch sub () {
$self->_build_request_obj_from($_[+PSGI_ENV]);
- my $body;
[
sub (/) {
$self->html_response({ body => $self->index_page_content });
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 });
},
],
},