Skip to content

Commit 385632f

Browse files
Bug 2059621: Add simpler method to check if an extension is present (#66)
r=mrenvoize Co-authored-by: Martin Renvoize <martin.renvoize@openfifth.co.uk> r=justdave
1 parent 4a77381 commit 385632f

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎Bugzilla.pm‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,19 @@ sub template_inner {
122122
||= Bugzilla::Template->create(%options);
123123
}
124124

125+
our $in_extensions = 0;
126+
125127
sub extensions {
126128
state $extensions;
127129
return $extensions if $extensions;
130+
131+
# Guard against extensions querying the extension list during initialization
132+
# (through this method or has_extension).
133+
# The extension list is not fully populated at that point,
134+
# so the results would not be meaningful.
135+
die "Recursive attempt to load/query extensions" if $in_extensions > 0;
136+
local $in_extensions = $in_extensions + 1;
137+
128138
my $extension_packages = Bugzilla::Extension->load_all();
129139
$extensions = [];
130140
foreach my $package (@$extension_packages) {
@@ -135,6 +145,16 @@ sub extensions {
135145
return $extensions;
136146
}
137147

148+
sub has_extension {
149+
my ($class, $name) = @_;
150+
my $cache = $class->request_cache;
151+
if (!$cache->{extensions_hash}) {
152+
my %extensions = map { $_->NAME => 1 } @{Bugzilla->extensions};
153+
$cache->{extensions_hash} = \%extensions;
154+
}
155+
return exists $cache->{extensions_hash}{$name};
156+
}
157+
138158
sub cgi {
139159
return request_cache->{cgi} ||= Bugzilla::CGI->new;
140160
}

‎votes.cgi‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use lib qw(. lib local/lib/perl5);
3030
use Bugzilla;
3131
use Bugzilla::Error;
3232

33-
my $is_enabled = grep { $_->NAME eq 'Voting' } @{Bugzilla->extensions};
34-
$is_enabled || ThrowCodeError('extension_disabled', {name => 'Voting'});
33+
Bugzilla->has_extension('Voting')
34+
|| ThrowCodeError('extension_disabled', {name => 'Voting'});
3535

3636
my $cgi = Bugzilla->cgi;
3737
my $action = $cgi->param('action') || 'show_user';

0 commit comments

Comments
 (0)