]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 328638: Remove @::legal_keywords and %::keywordsbyname
authormkanat%kerio.com <>
Thu, 9 Mar 2006 08:08:57 +0000 (08:08 +0000)
committermkanat%kerio.com <>
Thu, 9 Mar 2006 08:08:57 +0000 (08:08 +0000)
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=LpSolit, a=justdave

15 files changed:
Bugzilla/Bug.pm
Bugzilla/Keyword.pm [new file with mode: 0644]
Bugzilla/Search.pm
Bugzilla/Search/Quicksearch.pm
buglist.cgi
colchange.cgi
config.cgi
enter_bug.cgi
globals.pl
importxml.pl
post_bug.cgi
process_bug.cgi
query.cgi
show_bug.cgi
template/en/default/bug/edit.html.tmpl

index 43f5a128537d05fe9f1a1e864d77ef8d436e8610..c314b8ee1ca80131f6c9581d0a2c8c7dd0043bdd 100755 (executable)
@@ -30,7 +30,7 @@ package Bugzilla::Bug;
 
 use strict;
 
-use vars qw($legal_keywords @legal_platform
+use vars qw(@legal_platform
             @legal_priority @legal_severity @legal_opsys @legal_bug_status
             @settable_resolution %components %target_milestone
             @enterable_products %milestoneurl %prodmaxvotes);
@@ -523,11 +523,6 @@ sub show_attachment_flags {
     return $self->{'show_attachment_flags'};
 }
 
-
-sub use_keywords {
-    return @::legal_keywords;
-}
-
 sub use_votes {
     my ($self) = @_;
     return 0 if $self->{'error'};
@@ -1337,7 +1332,7 @@ sub _validate_attribute {
            longdescs milestoneurl attachments
            isopened isunconfirmed
            flag_types num_attachment_flag_types
-           show_attachment_flags use_keywords any_flags_requesteeble),
+           show_attachment_flags any_flags_requesteeble),
 
         # Bug fields.
         Bugzilla::Bug->fields
diff --git a/Bugzilla/Keyword.pm b/Bugzilla/Keyword.pm
new file mode 100644 (file)
index 0000000..b358272
--- /dev/null
@@ -0,0 +1,213 @@
+# -*- Mode: perl; indent-tabs-mode: nil -*-
+#
+# The contents of this file are subject to the Mozilla Public
+# License Version 1.1 (the "License"); you may not use this file
+# except in compliance with the License. You may obtain a copy of
+# the License at http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS
+# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+# implied. See the License for the specific language governing
+# rights and limitations under the License.
+#
+# The Original Code is the Bugzilla Bug Tracking System.
+#
+# Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
+
+use strict;
+
+package Bugzilla::Keyword;
+
+use Bugzilla::Util;
+use Bugzilla::Error;
+
+###############################
+####    Initialization     ####
+###############################
+
+use constant DB_COLUMNS => qw(
+   keyworddefs.id
+   keyworddefs.name
+   keyworddefs.description
+);
+
+my $columns = join(", ", DB_COLUMNS);
+
+sub new {
+    my $invocant = shift;
+    my $class = ref($invocant) || $invocant;
+    my $self = {};
+    bless($self, $class);
+    return $self->_init(@_);
+}
+
+sub _init {
+    my $self = shift;
+    my ($param) = @_;
+    my $dbh = Bugzilla->dbh;
+
+    my $id = $param unless (ref $param eq 'HASH');
+    my $keyword;
+
+    if (defined $id) {
+        detaint_natural($id)
+          || ThrowCodeError('param_must_be_numeric',
+                            {function => 'Bugzilla::Keyword::_init'});
+
+        $keyword = $dbh->selectrow_hashref(qq{
+            SELECT $columns FROM keyworddefs
+             WHERE id = ?}, undef, $id);
+    } elsif (defined $param->{'name'}) {
+        trick_taint($param->{'name'});
+        $keyword = $dbh->selectrow_hashref(qq{
+            SELECT $columns FROM keyworddefs
+             WHERE name = ?}, undef, $param->{'name'});
+    } else {
+        ThrowCodeError('bad_arg',
+            {argument => 'param',
+             function => 'Bugzilla::Keyword::_init'});
+    }
+
+    return undef unless (defined $keyword);
+
+    foreach my $field (keys %$keyword) {
+        $self->{$field} = $keyword->{$field};
+    }
+    return $self;
+}
+
+sub new_from_list {
+    my $class = shift;
+    my ($id_list) = @_;
+    my $dbh = Bugzilla->dbh;
+
+    my $keywords;
+    if ($id_list) {
+        my @detainted_ids;
+        foreach my $id (@$id_list) {
+            detaint_natural($id) ||
+                ThrowCodeError('param_must_be_numeric',
+                              {function => 'Bugzilla::Keyword::new_from_list'});
+            push(@detainted_ids, $id);
+        }
+        $keywords = $dbh->selectall_arrayref(
+            "SELECT $columns FROM keyworddefs WHERE id IN (" 
+            . join(',', @detainted_ids) . ")", {Slice=>{}}) || [];
+    } else {
+        ThrowCodeError('bad_arg',
+            {argument => 'id_list',
+             function => 'Bugzilla::Keyword::new_from_list'});
+    }
+
+    foreach my $keyword (@$keywords) {
+        bless($keyword, $class);
+    }
+    return $keywords;
+}
+
+###############################
+####      Accessors      ######
+###############################
+
+sub id                { return $_[0]->{'id'};          }
+sub name              { return $_[0]->{'name'};        }
+sub description       { return $_[0]->{'description'}; }
+
+###############################
+####      Subroutines    ######
+###############################
+
+sub get_all_keywords {
+    my $dbh = Bugzilla->dbh;
+
+    my $ids = $dbh->selectcol_arrayref(q{
+        SELECT id FROM keyworddefs ORDER BY name});
+
+    my $keywords = Bugzilla::Keyword->new_from_list($ids);
+    return @$keywords;
+}
+
+sub keyword_count {
+    my ($count) = 
+        Bugzilla->dbh->selectrow_array('SELECT COUNT(*) FROM keyworddefs');
+    return $count;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Bugzilla::Keyword - A Keyword that can be added to a bug.
+
+=head1 SYNOPSIS
+
+ use Bugzilla::Keyword;
+
+ my $keyword = new Bugzilla::Keyword(1);
+ my $keyword = new Bugzilla::Keyword({name => 'perf'});
+
+ my $id          = $keyword->id;
+ my $name        = $keyword->name;
+ my $description = $keyword->description;
+
+=head1 DESCRIPTION
+
+Bugzilla::Keyword represents a keyword that can be added to a bug.
+
+=head1 METHODS
+
+=over
+
+=item C<new($param)>
+
+ Description: The constructor is used to load an existing keyword
+              by passing a keyword id or a hash.
+
+ Params:      $param - If you pass an integer, the integer is the
+                       keyword id from the database that we want to
+                       read in. If you pass in a hash with 'name' key,
+                       then the value of the name key is the name of a
+                       keyword from the DB.
+
+ Returns:     A Bugzilla::Keyword object.
+
+=item C<new_from_list(\@id_list)>
+
+ Description: Creates an array of Keyword objects, given an
+              array of ids.
+
+ Params:      \@id_list - A reference to an array of numbers, keyword ids.
+                          If any of these are not numeric, the function
+                          will throw an error. If any of these are not
+                          valid keyword ids, they will simply be skipped.
+
+ Returns:     A reference to an array of C<Bugzilla::Keyword> objects.
+
+=back
+
+=head1 SUBROUTINES
+
+=over
+
+=item C<get_all_keywords()>
+
+ Description: Returns all keywords from the database.
+
+ Params:      none.
+
+ Returns:     A list of C<Bugzilla::Keyword> objects,
+              or an empty list if there are none.
+
+=item C<keyword_count()> 
+
+ Description: A utility function to get the total number
+              of keywords defined. Mostly used to see
+              if there are any keywords defined at all.
+ Params:      none
+ Returns:     An integer, the count of keywords.
+
+=back
+
+=cut
index 0e1f7057d1b7c0e56782ce13f1a7357652332e38..181f49b193c5eccfa795c7a47463cecd67501cdb 100644 (file)
@@ -42,6 +42,7 @@ use Bugzilla::Group;
 use Bugzilla::User;
 use Bugzilla::Field;
 use Bugzilla::Bug;
+use Bugzilla::Keyword;
 
 use Date::Format;
 use Date::Parse;
@@ -933,9 +934,9 @@ sub init {
                  if ($value eq '') {
                      next;
                  }
-                 my $id = &::GetKeywordIdFromName($value);
-                 if ($id) {
-                     push(@list, "$table.keywordid = $id");
+                 my $keyword = new Bugzilla::Keyword({name => $value});
+                 if ($keyword) {
+                     push(@list, "$table.keywordid = " . $keyword->id);
                  }
                  else {
                      ThrowUserError("unknown_keyword",
index 4d9958c6319891b740568cc3aad0e231fe3e5f71..e4224b95987f46e3464ab1e4ddb2bf6b98b25631 100644 (file)
@@ -27,6 +27,7 @@ use Bugzilla;
 use Bugzilla::Config;
 use Bugzilla::Error;
 use Bugzilla::Constants;
+use Bugzilla::Keyword;
 use Bugzilla::Bug;
 
 use base qw(Exporter);
@@ -325,7 +326,7 @@ sub quicksearch {
                                              $word, $negate);
                                 }
                                 if (grep({lc($word) eq $_}
-                                         @::legal_keywords)) {
+                                         map($_->name, Bugzilla::Keyword::get_all_keywords()))) {
                                     addChart('keywords', 'substring',
                                              $word, $negate);
                                     if (length($word)>2) {
index c778c5f10b3375283cb6ba9d71c6a5974e08eba3..0e6fb2ba5a6d58fab9e66d957634ac548895ab3c 100755 (executable)
@@ -41,12 +41,12 @@ use Bugzilla::Constants;
 use Bugzilla::User;
 use Bugzilla::Bug;
 use Bugzilla::Product;
+use Bugzilla::Keyword;
 
 # Include the Bugzilla CGI and general utility library.
 require "globals.pl";
 
 use vars qw(@components
-            @legal_keywords
             @legal_platform
             @legal_priority
             @legal_product
@@ -1042,7 +1042,7 @@ $vars->{'currenttime'} = time();
 # The following variables are used when the user is making changes to multiple bugs.
 if ($dotweak) {
     $vars->{'dotweak'} = 1;
-    $vars->{'use_keywords'} = 1 if @::legal_keywords;
+    $vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
 
     $vars->{'products'} = Bugzilla->user->get_enterable_products;
     $vars->{'platforms'} = \@::legal_platform;
index 43bfd86eeccedb741cf7d33552267e90132acd3a..72ce01f44d5e7bfa0bfc99291984f22c45d758ca 100755 (executable)
@@ -25,13 +25,10 @@ use strict;
 
 use lib qw(.);
 
-use vars qw(
-  @legal_keywords
-);
-
 use Bugzilla;
 use Bugzilla::Constants;
 use Bugzilla::User;
+use Bugzilla::Keyword;
 require "globals.pl";
 
 Bugzilla->login();
@@ -71,7 +68,7 @@ if (Param("useqacontact")) {
 if (Param("usestatuswhiteboard")) {
     push(@masterlist, "status_whiteboard");
 }
-if (@::legal_keywords) {
+if (Bugzilla::Keyword::keyword_count()) {
     push(@masterlist, "keywords");
 }
 
index 371383e1c0659abeafa9b771b5dfbd9fb52207ff..f3471e14f514492f55c137db15ed8822d6dbc951 100755 (executable)
@@ -33,6 +33,7 @@ use lib qw(.);
 require "globals.pl";
 use Bugzilla;
 use Bugzilla::Constants;
+use Bugzilla::Keyword;
 use Bugzilla::Bug;
 
 # Suppress "used only once" warnings.
@@ -46,7 +47,6 @@ use vars
 
     @legal_components 
     @legal_target_milestone 
-    @legal_keywords 
   );
 
 # Use the global template variables defined in globals.pl 
@@ -69,7 +69,7 @@ $vars->{'priority'}  = \@::legal_priority;
 $vars->{'severity'}  = \@::legal_severity;
 $vars->{'platform'}   = \@::legal_platform;
 $vars->{'op_sys'}    = \@::legal_opsys;
-$vars->{'keyword'}    = \@::legal_keywords;
+$vars->{'keyword'}    = [map($_->name, Bugzilla::Keyword::get_all_keywords())];
 $vars->{'resolution'} = \@::legal_resolution;
 $vars->{'status'}    = \@::legal_bug_status;
 
index 3e87bf4052370b0b1458ce2e6a40e576cc01c6a3..d1d07014829bf575c3523cfd9891312d2bcd70b2 100755 (executable)
@@ -43,6 +43,7 @@ use Bugzilla::Bug;
 use Bugzilla::User;
 use Bugzilla::Hook;
 use Bugzilla::Product;
+use Bugzilla::Keyword;
 require "globals.pl";
 
 use vars qw(
@@ -51,7 +52,6 @@ use vars qw(
   @legal_platform
   @legal_priority
   @legal_severity
-  @legal_keywords
   %target_milestone
 );
 
@@ -375,7 +375,7 @@ $vars->{'bug_severity'}          = \@legal_severity;
 $vars->{'rep_platform'}          = \@legal_platform;
 $vars->{'op_sys'}                = \@legal_opsys; 
 
-$vars->{'use_keywords'}          = 1 if (@::legal_keywords);
+$vars->{'use_keywords'}          = 1 if Bugzilla::Keyword::keyword_count();
 
 $vars->{'assigned_to'}           = formvalue('assigned_to');
 $vars->{'assigned_to_disabled'}  = !UserInGroup('editbugs');
index be3df7d75a0d0421300364c4274ddd63d2d62db5..556ea3f3e77b5430e6f6a556c4c25f0a808f8a38 100644 (file)
@@ -45,10 +45,8 @@ use Bugzilla::Error;
 sub globals_pl_sillyness {
     my $zz;
     $zz = @main::enterable_products;
-    $zz = %main::keywordsbyname;
     $zz = @main::legal_bug_status;
     $zz = @main::legal_components;
-    $zz = @main::legal_keywords;
     $zz = @main::legal_opsys;
     $zz = @main::legal_platform;
     $zz = @main::legal_priority;
@@ -258,17 +256,6 @@ sub GenerateVersionTable {
                                        '*::milestoneurl']));
     }
 
-    SendSQL("SELECT id, name FROM keyworddefs ORDER BY name");
-    while (MoreSQLData()) {
-        my ($id, $name) = FetchSQLData();
-        push(@::legal_keywords, $name);
-        $name = lc($name);
-        $::keywordsbyname{$name} = $id;
-    }
-
-    print $fh (Data::Dumper->Dump([\@::legal_keywords, \%::keywordsbyname],
-                                  ['*::legal_keywords', '*::keywordsbyname']));
-
     print $fh "1;\n";
     close $fh;
 
@@ -278,13 +265,6 @@ sub GenerateVersionTable {
 }
 
 
-sub GetKeywordIdFromName {
-    my ($name) = (@_);
-    $name = lc($name);
-    return $::keywordsbyname{$name};
-}
-
-
 $::VersionTableLoaded = 0;
 sub GetVersionTable {
     return if $::VersionTableLoaded;
index 7b96cb59ee25d3c4ee583e44196848c1af0e4a19..494a00eca9b6a181cf52f2e9864046a57256a6d0 100755 (executable)
@@ -84,6 +84,7 @@ use Bugzilla::BugMail;
 use Bugzilla::User;
 use Bugzilla::Util;
 use Bugzilla::Constants;
+use Bugzilla::Keyword;
 
 use MIME::Base64;
 use MIME::Parser;
@@ -1070,14 +1071,14 @@ sub process_bug {
         );
         foreach my $keyword ( split( /[\s,]+/, $bug_fields{'keywords'} )) {
             next unless $keyword;
-            my $i = GetKeywordIdFromName($keyword);
-            if ( !$i ) {
+            my $keyword_obj = new Bugzilla::Keyword({name => $keyword});
+            if (!$keyword_obj) {
                 $err .= "Skipping unknown keyword: $keyword.\n";
                 next;
             }
-            if ( !$keywordseen{$i} ) {
-                $key_sth->execute( $id, $i );
-                $keywordseen{$i} = 1;
+            if (!$keywordseen{$keyword_obj->id}) {
+                $key_sth->execute($id, $keyword_obj->id);
+                $keywordseen{$keyword_obj->id} = 1;
             }
         }
         my ($keywordarray) = $dbh->selectcol_arrayref(
index 3e589ebe81829299839a04112acfd646cdddca6e..de806354326b1cffe072f2ab5389288c6636e6b4 100755 (executable)
@@ -34,6 +34,7 @@ use Bugzilla::Bug;
 use Bugzilla::User;
 use Bugzilla::Field;
 use Bugzilla::Product;
+use Bugzilla::Keyword;
 
 # Shut up misguided -w warnings about "used only once". For some reason,
 # "use vars" chokes on me when I try it here.
@@ -273,14 +274,14 @@ if ($cgi->param('keywords') && UserInGroup("editbugs")) {
         if ($keyword eq '') {
            next;
         }
-        my $i = GetKeywordIdFromName($keyword);
-        if (!$i) {
+        my $keyword_obj = new Bugzilla::Keyword({name => $keyword});
+        if (!$keyword_obj) {
             ThrowUserError("unknown_keyword",
                            { keyword => $keyword });
         }
-        if (!$keywordseen{$i}) {
-            push(@keywordlist, $i);
-            $keywordseen{$i} = 1;
+        if (!$keywordseen{$keyword_obj->id}) {
+            push(@keywordlist, $keyword_obj->id);
+            $keywordseen{$keyword_obj->id} = 1;
         }
     }
 }
@@ -518,6 +519,7 @@ if ($cgi->cookie("BUGLIST")) {
     @bug_list = split(/:/, $cgi->cookie("BUGLIST"));
 }
 $vars->{'bug_list'} = \@bug_list;
+$vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
 
 print $cgi->header();
 $template->process("bug/create/created.html.tmpl", $vars)
index c2605afc2465b7d4c4b67f818482b135bb61bbbc..d1efd38251a2750e0e7f0df3c4ed7c5a91181376 100755 (executable)
@@ -57,6 +57,7 @@ use Bugzilla::User;
 use Bugzilla::Util;
 use Bugzilla::Field;
 use Bugzilla::Product;
+use Bugzilla::Keyword;
 
 # Use the Flag module to modify flag data if the user set flags.
 use Bugzilla::Flag;
@@ -82,6 +83,7 @@ my $cgi = Bugzilla->cgi;
 my $dbh = Bugzilla->dbh;
 my $template = Bugzilla->template;
 my $vars = {};
+$vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
 
 my $requiremilestone = 0;
 
@@ -1278,14 +1280,14 @@ if (defined $cgi->param('keywords')) {
         if ($keyword eq '') {
             next;
         }
-        my $i = GetKeywordIdFromName($keyword);
-        if (!$i) {
+        my $keyword_obj = new Bugzilla::Keyword({name => $keyword});
+        if (!$keyword_obj) {
             ThrowUserError("unknown_keyword",
                            { keyword => $keyword });
         }
-        if (!$keywordseen{$i}) {
-            push(@keywordlist, $i);
-            $keywordseen{$i} = 1;
+        if (!$keywordseen{$keyword_obj->id}) {
+            push(@keywordlist, $keyword_obj->id);
+            $keywordseen{$keyword_obj->id} = 1;
         }
     }
 }
@@ -1297,7 +1299,8 @@ if (!grep($keywordaction eq $_, qw(add delete makeexact))) {
 
 if ($::comma eq ""
     && (! @groupAdd) && (! @groupDel)
-    && (! @::legal_keywords || (0 == @keywordlist && $keywordaction ne "makeexact"))
+    && (!Bugzilla::Keyword::keyword_count() 
+        || (0 == @keywordlist && $keywordaction ne "makeexact"))
     && defined $cgi->param('masscc') && ! $cgi->param('masscc')
     ) {
     if (!defined $cgi->param('comment') || $cgi->param('comment') =~ /^\s*$/) {
@@ -1651,7 +1654,9 @@ foreach my $id (@idlist) {
         $bug_changed = 1;
     }
 
-    if (@::legal_keywords && defined $cgi->param('keywords')) {
+    if (Bugzilla::Keyword::keyword_count() 
+        && defined $cgi->param('keywords')) 
+    {
         # There are three kinds of "keywordsaction": makeexact, add, delete.
         # For makeexact, we delete everything, and then add our things.
         # For add, we delete things we're adding (to make sure we don't
index 33f2d19faaa02ebbd1c013ae5d7aab68befc4906..9bb90e7828cf29418a8fe42af27568f0cdee507c 100755 (executable)
--- a/query.cgi
+++ b/query.cgi
@@ -36,12 +36,12 @@ use Bugzilla::User;
 use Bugzilla::Util;
 use Bugzilla::Product;
 use Bugzilla::Version;
+use Bugzilla::Keyword;
 
 use vars qw(
     @legal_resolution
     @legal_bug_status
     @legal_components
-    @legal_keywords
     @legal_opsys
     @legal_platform
     @legal_priority
@@ -275,7 +275,7 @@ if (Param('usetargetmilestone')) {
     $vars->{'target_milestone'} = \@milestones;
 }
 
-$vars->{'have_keywords'} = scalar(@::legal_keywords);
+$vars->{'have_keywords'} = Bugzilla::Keyword::keyword_count();
 
 push @::legal_resolution, "---"; # Oy, what a hack.
 shift @::legal_resolution; 
index 19aa88573edd624706a2eea6d3f6d52080686680..7f846efeaef4fcf233b46d02d338e63beff61fa8 100755 (executable)
@@ -27,6 +27,7 @@ use lib qw(.);
 use Bugzilla;
 use Bugzilla::Constants;
 use Bugzilla::User;
+use Bugzilla::Keyword;
 
 require "globals.pl";
 
@@ -94,7 +95,7 @@ eval {
 
 $vars->{'bugs'} = \@bugs;
 $vars->{'marks'} = \%marks;
-$vars->{'use_keywords'} = 1 if (@::legal_keywords);
+$vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
 
 # Next bug in list (if there is one)
 my @bug_list;
index 02fb20d6134ccff6f0fdbd636325957256942973..b08ce64328877a0c6934e81bc14125fa1ab60f24 100644 (file)
             </tr>
           [% END %]
 
-          [% IF bug.use_keywords %]
+          [% IF use_keywords %]
             <tr>
               <td align="right">
                 <b>