From b0f0bda8b589d629b2d681f6e3026da274d99762 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Sun, 8 Apr 2018 06:57:06 +0300 Subject: [PATCH 1/2] Bug 1446236 - Add simpler method to check if an extension is present --- Bugzilla.pm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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; } From 60c7e9468023972180a850e1836140a6549c43e0 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Sun, 4 Oct 2020 21:34:24 +0000 Subject: [PATCH 2/2] votes.cgi: Use has_extension --- votes.cgi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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';