diff --git a/Bugzilla.pm b/Bugzilla.pm index 42b56ec03f..0d24850d38 100644 --- a/Bugzilla.pm +++ b/Bugzilla.pm @@ -123,9 +123,19 @@ sub template_inner { ||= Bugzilla::Template->create(%options); } +our $in_extensions = 0; + sub extensions { state $extensions; return $extensions if $extensions; + + # Guard against extensions querying the extension list during initialization + # (through this method or has_extension). + # The extension list is not fully populated at that point, + # so the results would not be meaningful. + die "Recursive attempt to load/query extensions" if $in_extensions > 0; + local $in_extensions = $in_extensions + 1; + my $extension_packages = Bugzilla::Extension->load_all(); $extensions = []; foreach my $package (@$extension_packages) { @@ -136,6 +146,16 @@ sub extensions { return $extensions; } +sub has_extension { + my ($class, $name) = @_; + my $cache = $class->request_cache; + if (!$cache->{extensions_hash}) { + my %extensions = map { $_->NAME => 1 } @{Bugzilla->extensions}; + $cache->{extensions_hash} = \%extensions; + } + return exists $cache->{extensions_hash}{$name}; +} + sub cgi { return request_cache->{cgi} ||= Bugzilla::CGI->new; } diff --git a/votes.cgi b/votes.cgi index 57912cd771..3cebfad37c 100755 --- a/votes.cgi +++ b/votes.cgi @@ -30,8 +30,7 @@ use lib qw(. lib local/lib/perl5); use Bugzilla; use Bugzilla::Error; -my $is_enabled = grep { $_->NAME eq 'Voting' } @{Bugzilla->extensions}; -$is_enabled || ThrowCodeError('extension_disabled', {name => 'Voting'}); +Bugzilla->has_extension('Voting') || ThrowCodeError('extension_disabled', { name => 'Voting' }); my $cgi = Bugzilla->cgi; my $action = $cgi->param('action') || 'show_user';