use warnings;
use strict;
+use feature 'state';
use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
use Exporter qw(import);
use Debbugs::Common qw(getparsedaddrs package_maintainer getmaintainers make_list hash_slice);
use Fcntl qw(O_RDONLY);
use MLDBM qw(DB_File Storable);
-use List::AllUtils qw(first);
+use List::AllUtils qw(first max);
use Carp;
=head2 get_bugs
=cut
-my $_non_search_key_regex = qr/^(bugs|archive|usertags|schema)$/;
+state $_non_search_key_regex = qr/^(bugs|archive|usertags|schema)$/;
my %_get_bugs_common_options =
(package => {type => SCALAR|ARRAYREF,
usertags => {type => HASHREF,
optional => 1,
},
+ newest => {type => SCALAR|ARRAYREF,
+ optional => 1,
+ },
schema => {type => OBJECT,
optional => 1,
},
);
-my $_get_bugs_options = {%_get_bugs_common_options};
+state $_get_bugs_options = {%_get_bugs_common_options};
sub get_bugs{
my %param = validate_with(params => \@_,
spec => $_get_bugs_options,
=cut
-my $_get_bugs_by_idx_options =
+state $_get_bugs_by_idx_options =
{hash_slice(%_get_bugs_common_options,
(qw(package submitter severity tag archive),
qw(owner src maint bugs correspondent),
- qw(affects usertags))
+ qw(affects usertags newest))
)
};
sub get_bugs_by_idx{
die "Can't handle empty maint (unmaintained packages) in get_bugs_by_idx";
}
}
-
+ if ($param{newest}) {
+ my $newest_bug = newest_bug();
+ my @bugs = ($newest_bug - max(make_list($param{newest})) + 1) .. $newest_bug;
+ $param{bugs} = [exists $param{bugs}?make_list($param{bugs}):(),
+ @bugs,
+ ];
+ }
# We handle src packages, maint and maintenc by mapping to the
# appropriate binary packages, then removing all packages which
# don't match all queries
=cut
-my $_get_bugs_by_db_options =
+state $_get_bugs_by_db_options =
{hash_slice(%_get_bugs_common_options,
(qw(package submitter severity tag archive),
qw(owner src maint bugs correspondent),
- qw(affects usertags))
+ qw(affects usertags newest))
),
schema => {type => OBJECT,
},
);
my %bugs = ();
+ my $s = $param{schema};
my $keys = grep {$_ !~ $_non_search_key_regex} keys(%param);
die "Need at least 1 key to search by" unless $keys;
- my $rs = $param{schema}->resultset('Bug');
- if (exists $param{package}) {
- $rs = $rs->search({-or => {map 'bin_package.'}})
- }
+ my $rs = $s->resultset('Bug');
if (exists $param{severity}) {
- $rs = $rs->search({-or => {map {('severity.severity' => $_)}
- make_list($param{severity})},
+ $rs = $rs->search({'severity.severity' =>
+ [make_list($param{severity})],
},
{join => 'severity'},
);
}
for my $key (qw(owner submitter done)) {
if (exists $param{$key}) {
- $rs = $rs->search({-or => {map {("${key}.addr" => $_)}
- make_list($param{$key})},
+ $rs = $rs->search({"${key}.addr" =>
+ [make_list($param{$key})],
},
{join => $key},
);
}
}
+ if (exists $param{newest}) {
+ $rs =
+ $rs->search({},
+ {order_by => {-desc => 'me.creation'},
+ rows => max(make_list($param{newest})),
+ },
+ );
+ }
if (exists $param{correspondent}) {
- $rs = $rs->search({-or => {map {('message_correspondents.addr' => $_)}
- make_list($param{correspondent})},
+ my $message_rs =
+ $s->resultset('Message')->
+ search({'correspondent.addr' =>
+ [make_list($param{correspondent})],
+ },
+ {join => {message_correspondents => 'correspondent'},
+ columns => ['id'],
+ group_by => ['me.id'],
+ },
+ );
+ $rs = $rs->search({'bug_messages.message' =>
+ {-in => $message_rs->get_column('id')->as_query()},
},
- {join => {correspondent =>
- {bug_messages =>
- {message => 'message_correspondents'}}}},
+ {join => 'bug_messages',
+ },
);
}
if (exists $param{affects}) {
- $rs = $rs->search({-or => {map {('bin_pkg.pkg' => $_,
- 'src_pkg.pkg' => $_,
- )}
- make_list($param{affects}),
+ my @aff_list = make_list($param{affects});
+ $rs = $rs->search({-or => {'bin_pkg.pkg' =>
+ [@aff_list],
+ 'src_pkg.pkg' =>
+ [@aff_list],
+ 'me.unknown_affects' =>
+ [@aff_list]
},
},
{join => [{bug_affects_binpackages => 'bin_pkg'},
);
}
if (exists $param{package}) {
- $rs = $rs->search({-or => {map {('bin_pkg.pkg' => $_)}
- make_list($param{package})},
+ $rs = $rs->search({-or => {'bin_pkg.pkg' =>
+ [make_list($param{package})],
+ 'me.unknown_packages' =>
+ [make_list($param{package})]},
},
{join => {bug_binpackages => 'bin_pkg'}});
}
- if (exists $param{maintainer}) {
- $rs = $rs->search({-or => {map {(correspondent => $_ eq '' ? undef : $_,
- correspondent2 => $_ eq '' ? undef : $_,
- )}
- make_list($param{maintainer})
- }
+ if (exists $param{maint}) {
+ my @maint_list =
+ map {$_ eq '' ? undef : $_}
+ make_list($param{maint});
+ my $bin_pkgs_rs =
+ $s->resultset('BinPkg')->
+ search({'correspondent.addr' => [@maint_list]},
+ {join => {bin_vers =>
+ {src_ver =>
+ {maintainer => 'correspondent'}}},
+ columns => ['id'],
+ group_by => ['me.id'],
+ },
+ );
+ my $src_pkgs_rs =
+ $s->resultset('SrcPkg')->
+ search({'correspondent.addr' => [@maint_list]},
+ {join => {src_vers =>
+ {maintainer => 'correspondent'}},
+ columns => ['id'],
+ group_by => ['me.id'],
+ },
+ );
+ $rs = $rs->search({-or => {'bug_binpackages.bin_pkg' =>
+ { -in => $bin_pkgs_rs->get_column('id')->as_query},
+ 'bug_srcpackages.src_pkg' =>
+ { -in => $bin_pkgs_rs->get_column('id')->as_query},
+ },
},
- {join => {bug_affects_binpackage =>
- {bin_pkg =>
- {bin_ver =>
- {src_ver =>
- {maintainer => 'correspondent'}
- }}},
- {bug_affects_srcpackage =>
- {src_pkg =>
- {src_ver =>
- {maintainer => 'correspondent'}
- }}}}
- }
+ {join => ['bug_binpackages',
+ 'bug_srcpackages',
+ ]}
);
}
if (exists $param{src}) {
- $rs = $rs->search({-or => {map {('src_pkg.pkg' => $_)}
- make_list($param{src})},
+ # identify all of the srcpackages and binpackages that match first
+ my $src_pkgs_rs =
+ $s->resultset('SrcPkg')->
+ search({-or => [map {('me.pkg' => $_,
+ )}
+ make_list($param{src})],
+ columns => ['id'],
+ group_by => ['me.id'],
+ },
+ );
+ my $bin_pkgs_rs =
+ $s->resultset('BinPkg')->
+ search({-or => [map {('src_pkg.pkg' => $_,
+ )}
+ make_list($param{src})],
+ },
+ {join => {bin_vers => {src_ver => 'src_pkg'}},
+ columns => ['id'],
+ group_by => ['me.id'],
+ });
+ $rs = $rs->search({-or => {'bug_binpackages.bin_pkg' =>
+ { -in => $bin_pkgs_rs->get_column('id')->as_query},
+ 'bug_srcpackages.src_pkg' =>
+ { -in => $bin_pkgs_rs->get_column('id')->as_query},
+ 'me.unknown_packages' =>
+ [make_list($param{src})],
+ },
},
- {join => {bug_srcpackages => 'src_pkg'}});
+ {join => ['bug_binpackages',
+ 'bug_srcpackages',
+ ]}
+ );
}
# tags are very odd, because we must handle usertags.
if (exists $param{tag}) {
=cut
-my $_get_bugs_flatfile_options =
+state $_get_bugs_flatfile_options =
{hash_slice(%_get_bugs_common_options,
map {$_ eq 'dist'?():($_)} keys %_get_bugs_common_options
)
spec => $_get_bugs_flatfile_options
);
my $flatfile;
+ if ($param{newest}) {
+ my $newest_bug = newest_bug();
+ my @bugs = ($newest_bug - max(make_list($param{newest})) + 1) .. $newest_bug;
+ $param{bugs} = [exists $param{bugs}?make_list($param{bugs}):(),
+ @bugs,
+ ];
+ }
if ($param{archive}) {
$flatfile = IO::File->new("$config{spool_dir}/index.archive", 'r')
or die "Unable to open $config{spool_dir}/index.archive for reading: $!";
return grep {$packages{$_} >= $package_keys} keys %packages;
}
-my %field_match = (
+state $field_match = {
'subject' => \&__contains_field_match,
'tags' => sub {
my ($field, $values, $status) = @_;
'originator' => \&__contains_field_match,
'forwarded' => \&__contains_field_match,
'owner' => \&__contains_field_match,
-);
+};
sub __bug_matches {
my ($hash, $status) = @_;
foreach my $key( keys( %$hash ) ) {
my $value = $hash->{$key};
- next unless exists $field_match{$key};
- my $sub = $field_match{$key};
+ next unless exists $field_match->{$key};
+ my $sub = $field_match->{$key};
if (not defined $sub) {
die "No defined subroutine for key: $key";
}