Skip to content

Commit

Permalink
Tidy everything.
Browse files Browse the repository at this point in the history
  • Loading branch information
oalders committed Feb 1, 2014
1 parent f4a9948 commit 10cb8c4
Show file tree
Hide file tree
Showing 95 changed files with 1,778 additions and 1,450 deletions.
69 changes: 39 additions & 30 deletions lib/Catalyst/Action/Deserialize/MetaCPANSanitizedJSON.pm
Expand Up @@ -3,60 +3,69 @@ package Catalyst::Action::Deserialize::MetaCPANSanitizedJSON;
use Moose;
use namespace::autoclean;
use Try::Tiny;
use JSON ();
use JSON ();
use MetaCPAN::Server::QuerySanitizer ();

extends 'Catalyst::Action::Deserialize::JSON';

around execute => sub {
my ($orig, $self, $controller, $c) = @_;
my ( $orig, $self, $controller, $c ) = @_;
my $result;

try {
$result = $self->$orig($controller, $c);
$result = $self->$orig( $controller, $c );

# if sucessfully serialized
if( $result eq '1' ){
if ( $result eq '1' ) {

# if we got something
if( my $data = $c->req->data ){
if ( my $data = $c->req->data ) {

# clean it
$c->req->data(MetaCPAN::Server::QuerySanitizer->new(
query => $data,
)->query);
$c->req->data(
MetaCPAN::Server::QuerySanitizer->new( query => $data, )
->query );
}
}

foreach my $attr ( qw( query_parameters parameters ) ){
# there's probably a more appropriate place for this
# but it's the same concept and we can reuse the error handling
if( my $params = $c->req->$attr ){
# ES also accepts the content in the querystring
if( exists $params->{source} ){
if( my $source = delete $params->{source} ){
# NOTE: merge $controller->{json_options} if we ever use it
my $json = JSON->new->utf8;
# if it decodes
if( try { $source = $json->decode($source); } ){
# clean it
$source = MetaCPAN::Server::QuerySanitizer->new(
query => $source,
)->query;
# update the $req
$params->{source} = $json->encode($source);
$c->req->$attr($params);
foreach my $attr (qw( query_parameters parameters )) {

# there's probably a more appropriate place for this
# but it's the same concept and we can reuse the error handling
if ( my $params = $c->req->$attr ) {

# ES also accepts the content in the querystring
if ( exists $params->{source} ) {
if ( my $source = delete $params->{source} ) {

# NOTE: merge $controller->{json_options} if we ever use it
my $json = JSON->new->utf8;

# if it decodes
if ( try { $source = $json->decode($source); } ) {

# clean it
$source = MetaCPAN::Server::QuerySanitizer->new(
query => $source, )->query;

# update the $req
$params->{source} = $json->encode($source);
$c->req->$attr($params);
}
}
}
}
}
}
}
catch {
my $e = $_[0];
if( try { $e->isa('MetaCPAN::Server::QuerySanitizer::Error') } ){
# this will return a 400 (text) through Catalyst::Action::Deserialize
if ( try { $e->isa('MetaCPAN::Server::QuerySanitizer::Error') } ) {

# this will return a 400 (text) through Catalyst::Action::Deserialize
$result = $e->message;

# this is our custom version (403) that returns json
$c->detach("/not_allowed", [ $e->message ]);
$c->detach( "/not_allowed", [ $e->message ] );
}
else {
$result = $e;
Expand Down
3 changes: 2 additions & 1 deletion lib/Catalyst/Plugin/OAuth2/Provider.pm
Expand Up @@ -139,9 +139,10 @@ sub redirect {
$c->res->cookies->{oauth_tmp} = $cid;
}
my ( $client, $redirect_uri ) = @$params{qw(client_id redirect_uri)};

# we don't trust the user's redirect uri
$redirect_uri = $self->clients->{$client}->{redirect_uri}->[0]
if($client);
if ($client);

if ($redirect_uri) {
$c->res->redirect( $redirect_uri . "?$type=$message" );
Expand Down
4 changes: 2 additions & 2 deletions lib/MetaCPAN/Document/Author/Profile.pm
Expand Up @@ -3,7 +3,7 @@ use Moose;
use ElasticSearchX::Model::Document;
use MetaCPAN::Util;

has name => ( is => 'ro', required => 1, isa => 'Str' );
has id => ( is => 'ro', isa => 'Str', analyzer => ['simple'] );
has name => ( is => 'ro', required => 1, isa => 'Str' );
has id => ( is => 'ro', isa => 'Str', analyzer => ['simple'] );

__PACKAGE__->meta->make_immutable;
100 changes: 53 additions & 47 deletions lib/MetaCPAN/Document/File.pm
Expand Up @@ -45,6 +45,7 @@ sub _build_abstract {
return undef unless ($section);
$section =~ s/^=\w+.*$//mg;
$section =~ s/X<.*?>//mg;

if ( $section =~ /^\s*(\S+)((\h+-+\h+(.+))|(\r?\n\h*\r?\n\h*(.+)))?/ms ) {
chomp( $abstract = $4 || $6 ) if ( $4 || $6 );
my $name = MetaCPAN::Util::strip_pod($1);
Expand Down Expand Up @@ -592,7 +593,7 @@ sub is_perl_file {
return 1 if ( $self->mime eq "text/x-script.perl" );
return 1
if ( $self->name !~ /\./
&& !(grep { $self->name eq $_ } @NOT_PERL_FILES)
&& !( grep { $self->name eq $_ } @NOT_PERL_FILES )
&& !$self->binary
&& $self->stat->{size} < 2**17 );
return 0;
Expand Down Expand Up @@ -756,7 +757,7 @@ sub find {
@candidates;

$file ||= shift @candidates;
return $file ? $self->get($file->id) : undef;
return $file ? $self->get( $file->id ) : undef;
}

sub find_pod {
Expand Down Expand Up @@ -816,13 +817,14 @@ sub prefix {
map {
{ field => { 'documentation.analyzed' => "$_*" } },
{ field => { 'documentation.camelcase' => "$_*" } }
} grep {$_} @query
} grep {$_} @query
];
return $self->query(
{ filtered => {
query => {
custom_score => {
query => { bool => { should => $should } },

#metacpan_script => 'prefer_shorter_module_names_100',
script =>
"_score - doc['documentation'].value.length()/100"
Expand Down Expand Up @@ -866,76 +868,80 @@ Find the history of a given module/documentation.
=cut

sub history {
my ($self, $type, $module, @path) = @_;
my $search = $type eq "module"
? $self->filter({
nested => {
path => "module",
my ( $self, $type, $module, @path ) = @_;
my $search
= $type eq "module" ? $self->filter(
{ nested => {
path => "module",
query => {
constant_score => {
filter => { and => [
{ term =>
{ "module.authorized" => \1 }
},
{ term => { "module.indexed" => \1 }
},
{ term => { "module.name" => $module }
},
] }
filter => {
and => [
{ term => { "module.authorized" => \1 } },
{ term => { "module.indexed" => \1 } },
{ term => { "module.name" => $module } },
]
}
}
}
}
})
: $type eq "file"
? $self->filter({
and => [
{ term => { "file.path" => join("/", @path) } },
}
)
: $type eq "file" ? $self->filter(
{ and => [
{ term => { "file.path" => join( "/", @path ) } },
{ term => { "file.distribution" => $module } },
]
})
: $self->filter({
and => [
}
)
: $self->filter(
{ and => [
{ term => { "file.documentation" => $module } },
{ term => { "file.indexed" => \1 } },
{ term => { "file.authorized" => \1 } },
{ term => { "file.indexed" => \1 } },
{ term => { "file.authorized" => \1 } },
]
});
return $search->sort([{ "file.date" => "desc"}]);
}
);
return $search->sort( [ { "file.date" => "desc" } ] );
}

sub _not_rogue {
my @rogue_dists = map { { term => { 'file.distribution' => $_ } } } @ROGUE_DISTRIBUTIONS;
my @rogue_dists = map { { term => { 'file.distribution' => $_ } } }
@ROGUE_DISTRIBUTIONS;
return { not => { filter => { or => \@rogue_dists } } };
}

sub autocomplete {
my ($self, @terms) = @_;
my $query = join(" ", @terms);
my ( $self, @terms ) = @_;
my $query = join( " ", @terms );
return $self unless $query;
$query =~ s/::/ /g;
my @query = split( /\s+/, $query );
my @query = split( /\s+/, $query );
my $should = [
map {
{ field => { 'documentation.analyzed' => "$_*" } },
{ field => { 'documentation.camelcase' => "$_*" } }
} grep {$_} @query
} grep {$_} @query
];

# TODO: custom_score is deprecated in 0.90.4 in favor of function_score.
# As of 2013-10-27 we are still using 0.20.2 in production.
return $self->query({
custom_score => {
query => { bool => { should => $should } },
script => "_score - doc['documentation'].value.length()/100",
return $self->query(
{ custom_score => {
query => { bool => { should => $should } },
script => "_score - doc['documentation'].value.length()/100",
}
}
})->filter({
and => [
$self->_not_rogue,
{ exists => { field => 'documentation' } },
{ term => { 'file.indexed' => \1 } },
{ term => { 'file.authorized' => \1 } },
{ term => { 'file.status' => 'latest' } },
]
})->sort(['_score', 'documentation']);
)->filter(
{ and => [
$self->_not_rogue,
{ exists => { field => 'documentation' } },
{ term => { 'file.indexed' => \1 } },
{ term => { 'file.authorized' => \1 } },
{ term => { 'file.status' => 'latest' } },
]
}
)->sort( [ '_score', 'documentation' ] );
}

__PACKAGE__->meta->make_immutable;
13 changes: 8 additions & 5 deletions lib/MetaCPAN/Document/Module.pm
Expand Up @@ -83,6 +83,7 @@ sub hide_from_pause {
my ( $self, $content, $file_name ) = @_;
return 0 if defined($file_name) && $file_name =~ m{\.pm\.PL\z};
my $pkg = $self->name;

# This regexp is *almost* the same as $PKG_REGEXP in Module::Metadata.
return $content =~ / # match a package declaration
^[\h\{;]* # intro chars on a line
Expand All @@ -108,11 +109,13 @@ L</associated_pod> is set to the path of the file, which contains the documentat
sub set_associated_pod {
my ( $self, $file, $associated_pod ) = @_;
return unless ( my $files = $associated_pod->{ $self->name } );
my ($pod) =
((grep { $_->name =~ /\.pod$/i } @$files),
(grep { $_->name =~ /\.pm$/i } @$files),
(grep { $_->name =~ /\.pl$/i } @$files), @$files);
$self->associated_pod( $pod );
my ($pod) = (
( grep { $_->name =~ /\.pod$/i } @$files ),
( grep { $_->name =~ /\.pm$/i } @$files ),
( grep { $_->name =~ /\.pl$/i } @$files ),
@$files
);
$self->associated_pod($pod);
return $pod;
}

Expand Down
43 changes: 27 additions & 16 deletions lib/MetaCPAN/Document/Release.pm
Expand Up @@ -120,7 +120,8 @@ has resources => (
type => 'nested',
include_in_root => 1,
);
has abstract => ( is => 'rw', index => 'analyzed', predicate => 'has_abstract' );
has abstract =>
( is => 'rw', index => 'analyzed', predicate => 'has_abstract' );
has dependency => (
required => 0,
is => 'rw',
Expand All @@ -141,7 +142,13 @@ has first => (
lazy => 1,
builder => '_build_first'
);
has metadata => ( coerce => 1, is => 'ro', isa => 'HashRef', dynamic => 1, source_only => 1 );
has metadata => (
coerce => 1,
is => 'ro',
isa => 'HashRef',
dynamic => 1,
source_only => 1
);

sub _build_version_numified {
return MetaCPAN::Util::numify_version( shift->version );
Expand All @@ -157,18 +164,22 @@ sub _build_download_url {

sub _build_first {
my $self = shift;
$self->index->type('release')
->filter(
{
and => [
$self->index->type('release')->filter(
{ and => [
{ term => { 'release.distribution' => $self->distribution } },
{ range => { 'release.version_numified' => { 'lt' => $self->version_numified } } },
# REINDEX: after a full reindex, the above line is to replaced with:
# { term => { 'release.first' => \1 } },
# currently, the "first" property is not computed on all releases
# since this feature has not been around when last reindexed
{ range => {
'release.version_numified' =>
{ 'lt' => $self->version_numified }
}
},

# REINDEX: after a full reindex, the above line is to replaced with:
# { term => { 'release.first' => \1 } },
# currently, the "first" property is not computed on all releases
# since this feature has not been around when last reindexed
]
} )->count
}
)->count
? 0
: 1;
}
Expand Down Expand Up @@ -214,6 +225,7 @@ sub predecessor {

sub find_github_based {
my $or = [

# { prefix => { "resources.homepage" => 'http://github.com/' } },
# { prefix => { "resources.homepage" => 'https://github.com/' } },
# { prefix => { "resources.repository.web" => 'http://github.com/' } },
Expand All @@ -224,10 +236,9 @@ sub find_github_based {
{ prefix => { "resources.bugtracker.web" => 'http://github.com/' } },
{ prefix => { "resources.bugtracker.web" => 'https://github.com/' } },
];
shift#->fields([qw(resources)])
->filter(
{ and => [ { term => { status => 'latest' } }, { or => $or } ] }
);
shift #->fields([qw(resources)])
->filter(
{ and => [ { term => { status => 'latest' } }, { or => $or } ] } );
}

__PACKAGE__->meta->make_immutable;

0 comments on commit 10cb8c4

Please sign in to comment.