summaryrefslogtreecommitdiffstats
path: root/extensions/ProdCompSearch/lib
diff options
context:
space:
mode:
authorDave Lawrence <dlawrence@mozilla.com>2012-12-21 23:11:41 +0100
committerDave Lawrence <dlawrence@mozilla.com>2012-12-21 23:11:41 +0100
commit63ed510dfa9ac705d16df4344da5e41fcab6137e (patch)
treef0ff5b4550253284b9d51549d9d98e580ab82c25 /extensions/ProdCompSearch/lib
parent7a7d55ccbb4d5b4ac3f98270c8b8e27087fa47a1 (diff)
downloadbugzilla-63ed510dfa9ac705d16df4344da5e41fcab6137e.tar.gz
bugzilla-63ed510dfa9ac705d16df4344da5e41fcab6137e.tar.xz
Introduction of new ProdCompSearch extension
Diffstat (limited to 'extensions/ProdCompSearch/lib')
-rw-r--r--extensions/ProdCompSearch/lib/WebService.pm94
1 files changed, 94 insertions, 0 deletions
diff --git a/extensions/ProdCompSearch/lib/WebService.pm b/extensions/ProdCompSearch/lib/WebService.pm
new file mode 100644
index 000000000..f972b80f7
--- /dev/null
+++ b/extensions/ProdCompSearch/lib/WebService.pm
@@ -0,0 +1,94 @@
+package Bugzilla::Extension::ProdCompSearch::WebService;
+
+use strict;
+use warnings;
+
+use base qw(Bugzilla::WebService);
+
+use Bugzilla::Error;
+use Bugzilla::Util qw(detaint_natural trick_taint);
+
+sub prod_comp_search {
+ my ($self, $params) = @_;
+ my $user = Bugzilla->user;
+ my $dbh = Bugzilla->switch_to_shadow_db();
+
+ my $search = $params->{'search'};
+ $search || ThrowCodeError('param_required',
+ { function => 'Bug.prod_comp_search', param => 'search' });
+
+ my $limit = detaint_natural($params->{'limit'})
+ ? $dbh->sql_limit($params->{'limit'})
+ : '';
+
+ # We do this in the DB directly as we want it to be fast and
+ # not have the overhead of loading full product objects
+
+ # All products which the user has "Entry" access to.
+ my $enterable_ids = $dbh->selectcol_arrayref(
+ 'SELECT products.id FROM products
+ LEFT JOIN group_control_map
+ ON group_control_map.product_id = products.id
+ AND group_control_map.entry != 0
+ AND group_id NOT IN (' . $user->groups_as_string . ')
+ WHERE group_id IS NULL
+ AND products.isactive = 1');
+
+ if (scalar @$enterable_ids) {
+ # And all of these products must have at least one component
+ # and one version.
+ $enterable_ids = $dbh->selectcol_arrayref(
+ 'SELECT DISTINCT products.id FROM products
+ WHERE ' . $dbh->sql_in('products.id', $enterable_ids) .
+ ' AND products.id IN (SELECT DISTINCT components.product_id
+ FROM components
+ WHERE components.isactive = 1)
+ AND products.id IN (SELECT DISTINCT versions.product_id
+ FROM versions
+ WHERE versions.isactive = 1)');
+ }
+
+ return { products => [] } if !scalar @$enterable_ids;
+
+ my @list;
+ foreach my $word (split(/[\s,]+/, $search)) {
+ if ($word ne "") {
+ my $sql_word = $dbh->quote($word);
+ trick_taint($sql_word);
+ # XXX CONCAT_WS is MySQL specific
+ my $field = "CONCAT_WS(' ', products.name, components.name, components.description)";
+ push(@list, $dbh->sql_iposition($sql_word, $field) . " > 0");
+ }
+ }
+
+ my $products = $dbh->selectall_arrayref("
+ SELECT products.name AS product,
+ components.name AS component
+ FROM products
+ INNER JOIN components ON products.id = components.product_id
+ WHERE (" . join(" AND ", @list) . ")
+ AND products.id IN (" . join(",", @$enterable_ids) . ")
+ ORDER BY products.name $limit",
+ { Slice => {} });
+
+ # To help mozilla staff file bmo administration bugs into the right
+ # component, sort bmo in front of bugzilla.
+ if ($user->in_group('mozilla-corporation') || $user->in_group('mozilla-foundation')) {
+ $products = [
+ sort {
+ return 1 if $a->{product} eq 'Bugzilla'
+ && $b->{product} eq 'bugzilla.mozilla.org';
+ return -1 if $b->{product} eq 'Bugzilla'
+ && $a->{product} eq 'bugzilla.mozilla.org';
+ return lc($a->{product}) cmp lc($b->{product})
+ || lc($a->{component}) cmp lc($b->{component});
+ } @$products
+ ];
+ }
+
+ return { products => $products };
+}
+
+1;
+
+