Commit 58787923 authored by mkanat%bugzilla.org's avatar mkanat%bugzilla.org

Bug 509053: Implement Bugzilla->feature (feature_enabled in the templates), and…

Bug 509053: Implement Bugzilla->feature (feature_enabled in the templates), and use it to detect when PatchReader is available. Patch by Max Kanat-Alexander <mkanat@bugzilla.org> r=LpSolit, a=LpSolit
parent 832be61c
...@@ -42,6 +42,7 @@ use Bugzilla::Auth::Persist::Cookie; ...@@ -42,6 +42,7 @@ use Bugzilla::Auth::Persist::Cookie;
use Bugzilla::CGI; use Bugzilla::CGI;
use Bugzilla::DB; use Bugzilla::DB;
use Bugzilla::Install::Localconfig qw(read_localconfig); use Bugzilla::Install::Localconfig qw(read_localconfig);
use Bugzilla::Install::Requirements qw(OPTIONAL_MODULES);
use Bugzilla::JobQueue; use Bugzilla::JobQueue;
use Bugzilla::Template; use Bugzilla::Template;
use Bugzilla::User; use Bugzilla::User;
...@@ -187,6 +188,40 @@ sub template_inner { ...@@ -187,6 +188,40 @@ sub template_inner {
return $class->request_cache->{"template_inner_$lang"}; return $class->request_cache->{"template_inner_$lang"};
} }
sub feature {
my ($class, $feature) = @_;
my $cache = $class->request_cache;
return $cache->{feature}->{$feature}
if exists $cache->{feature}->{$feature};
my $feature_map = $cache->{feature_map};
if (!$feature_map) {
foreach my $package (@{ OPTIONAL_MODULES() }) {
foreach my $f (@{ $package->{feature} }) {
$feature_map->{$f} ||= [];
push(@{ $feature_map->{$f} }, $package->{module});
}
}
$cache->{feature_map} = $feature_map;
}
if (!$feature_map->{$feature}) {
ThrowCodeError('invalid_feature', { feature => $feature });
}
my $success = 1;
foreach my $module (@{ $feature_map->{$feature} }) {
# We can't use a string eval and "use" here (it kills Template-Toolkit,
# see https://rt.cpan.org/Public/Bug/Display.html?id=47929), so we have
# to do a block eval.
$module =~ s{::}{/}g;
$module .= ".pm";
eval { require $module; 1; } or $success = 0;
}
$cache->{feature}->{$feature} = $success;
return $success;
}
sub cgi { sub cgi {
my $class = shift; my $class = shift;
$class->request_cache->{cgi} ||= new Bugzilla::CGI(); $class->request_cache->{cgi} ||= new Bugzilla::CGI();
...@@ -759,4 +794,9 @@ Returns a L<Bugzilla::JobQueue> that you can use for queueing jobs. ...@@ -759,4 +794,9 @@ Returns a L<Bugzilla::JobQueue> that you can use for queueing jobs.
Will throw an error if job queueing is not correctly configured on Will throw an error if job queueing is not correctly configured on
this Bugzilla installation. this Bugzilla installation.
=item C<feature>
Tells you whether or not a specific feature is enabled. For names
of features, see C<OPTIONAL_MODULES> in C<Bugzilla::Install::Requirements>.
=back =back
...@@ -281,12 +281,8 @@ sub get_attachment_link { ...@@ -281,12 +281,8 @@ sub get_attachment_link {
# If the attachment is a patch, try to link to the diff rather # If the attachment is a patch, try to link to the diff rather
# than the text, by default. # than the text, by default.
my $patchlink = ""; my $patchlink = "";
if ($is_patch) { if ($is_patch and Bugzilla->feature('patch_viewer')) {
# Determine if PatchReader is installed $patchlink = '&amp;action=diff';
my $patchviewer_installed = eval { require PatchReader; };
if ($patchviewer_installed) {
$patchlink = '&amp;action=diff';
}
} }
# Whitespace matters here because these links are in <pre> tags. # Whitespace matters here because these links are in <pre> tags.
...@@ -745,6 +741,8 @@ sub create { ...@@ -745,6 +741,8 @@ sub create {
return \@bug_list; return \@bug_list;
}, },
'feature_enabled' => sub { return Bugzilla->feature(@_); },
# These don't work as normal constants. # These don't work as normal constants.
DB_MODULE => \&Bugzilla::Constants::DB_MODULE, DB_MODULE => \&Bugzilla::Constants::DB_MODULE,
REQUIRED_MODULES => REQUIRED_MODULES =>
......
...@@ -86,12 +86,6 @@ if ($action ne 'view') { ...@@ -86,12 +86,6 @@ if ($action ne 'view') {
Bugzilla->login(); Bugzilla->login();
} }
# Determine if PatchReader is installed
eval {
require PatchReader;
$vars->{'patchviewerinstalled'} = 1;
};
# When viewing an attachment, do not request credentials if we are on # When viewing an attachment, do not request credentials if we are on
# the alternate host. Let view() decide when to call Bugzilla->login. # the alternate host. Let view() decide when to call Bugzilla->login.
if ($action eq "view") if ($action eq "view")
......
...@@ -240,12 +240,6 @@ if (defined($cgi->upload('data')) || $cgi->param('attachurl')) { ...@@ -240,12 +240,6 @@ if (defined($cgi->upload('data')) || $cgi->param('attachurl')) {
else { else {
$vars->{'message'} = 'attachment_creation_failed'; $vars->{'message'} = 'attachment_creation_failed';
} }
# Determine if Patch Viewer is installed, for Diff link
eval {
require PatchReader;
$vars->{'patchviewerinstalled'} = 1;
};
} }
# Set bug flags. # Set bug flags.
......
...@@ -634,13 +634,6 @@ foreach my $bug (@bug_objects) { ...@@ -634,13 +634,6 @@ foreach my $bug (@bug_objects) {
} }
} }
# Determine if Patch Viewer is installed, for Diff link
# (NB: Duplicate code with show_bug.cgi.)
eval {
require PatchReader;
$vars->{'patchviewerinstalled'} = 1;
};
if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) { if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
# Do nothing. # Do nothing.
} }
......
...@@ -91,12 +91,6 @@ if ($single) { ...@@ -91,12 +91,6 @@ if ($single) {
} }
} }
# Determine if Patch Viewer is installed, for Diff link
eval {
require PatchReader;
$vars->{'patchviewerinstalled'} = 1;
};
$vars->{'bugs'} = \@bugs; $vars->{'bugs'} = \@bugs;
$vars->{'marks'} = \%marks; $vars->{'marks'} = \%marks;
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
%] %]
[%# No need to display the Diff button and iframe if the attachment is not a patch. %] [%# No need to display the Diff button and iframe if the attachment is not a patch. %]
[% patchviewerinstalled = (patchviewerinstalled && attachment.ispatch) %] [% use_patchviewer = (feature_enabled('patch_viewer') && attachment.ispatch) %]
<form method="post" action="attachment.cgi" onsubmit="normalizeComments();"> <form method="post" action="attachment.cgi" onsubmit="normalizeComments();">
<input type="hidden" name="id" value="[% attachment.id %]"> <input type="hidden" name="id" value="[% attachment.id %]">
...@@ -127,7 +127,7 @@ ...@@ -127,7 +127,7 @@
<input type="submit" value="Submit" id="update"><br><br> <input type="submit" value="Submit" id="update"><br><br>
<strong>Actions:</strong> <strong>Actions:</strong>
<a href="attachment.cgi?id=[% attachment.id %]">View</a> <a href="attachment.cgi?id=[% attachment.id %]">View</a>
[% IF attachment.ispatch && patchviewerinstalled %] [% IF use_patchviewer %]
| <a href="attachment.cgi?id=[% attachment.id %]&amp;action=diff">Diff</a> | <a href="attachment.cgi?id=[% attachment.id %]&amp;action=diff">Diff</a>
[% END %] [% END %]
[% IF Param("allow_attachment_deletion") [% IF Param("allow_attachment_deletion")
...@@ -183,14 +183,14 @@ ...@@ -183,14 +183,14 @@
var patchviewerinstalled = 0; var patchviewerinstalled = 0;
var attachment_id = [% attachment.id %]; var attachment_id = [% attachment.id %];
if (typeof document.getElementById == "function") { if (typeof document.getElementById == "function") {
[% IF patchviewerinstalled %] [% IF use_patchviewer %]
var patchviewerinstalled = 1; var patchviewerinstalled = 1;
document.write('<iframe id="viewDiffFrame" style="height: 400px; width: 100%; display: none;"><\/iframe>'); document.write('<iframe id="viewDiffFrame" style="height: 400px; width: 100%; display: none;"><\/iframe>');
[% END %] [% END %]
document.write('<button type="button" id="editButton" onclick="editAsComment(patchviewerinstalled);">Edit Attachment As Comment<\/button>'); document.write('<button type="button" id="editButton" onclick="editAsComment(patchviewerinstalled);">Edit Attachment As Comment<\/button>');
document.write('<button type="button" id="undoEditButton" onclick="undoEditAsComment(patchviewerinstalled);" style="display: none;">Undo Edit As Comment<\/button>'); document.write('<button type="button" id="undoEditButton" onclick="undoEditAsComment(patchviewerinstalled);" style="display: none;">Undo Edit As Comment<\/button>');
document.write('<button type="button" id="redoEditButton" onclick="redoEditAsComment(patchviewerinstalled);" style="display: none;">Redo Edit As Comment<\/button>'); document.write('<button type="button" id="redoEditButton" onclick="redoEditAsComment(patchviewerinstalled);" style="display: none;">Redo Edit As Comment<\/button>');
[% IF patchviewerinstalled %] [% IF use_patchviewer %]
document.write('<button type="button" id="viewDiffButton" onclick="viewDiff(attachment_id, patchviewerinstalled);">View Attachment As Diff<\/button>'); document.write('<button type="button" id="viewDiffButton" onclick="viewDiff(attachment_id, patchviewerinstalled);">View Attachment As Diff<\/button>');
[% END %] [% END %]
document.write('<button type="button" id="viewRawButton" onclick="viewRaw(patchviewerinstalled);" style="display: none;">View Attachment As Raw<\/button>'); document.write('<button type="button" id="viewRawButton" onclick="viewRaw(patchviewerinstalled);" style="display: none;">View Attachment As Raw<\/button>');
......
...@@ -117,7 +117,7 @@ function toggle_display(link) { ...@@ -117,7 +117,7 @@ function toggle_display(link) {
<td valign="top"> <td valign="top">
<a href="attachment.cgi?id=[% attachment.id %]&amp;action=edit">Details</a> <a href="attachment.cgi?id=[% attachment.id %]&amp;action=edit">Details</a>
[% IF attachment.ispatch && patchviewerinstalled %] [% IF attachment.ispatch && feature_enabled('patch_viewer') %]
| <a href="attachment.cgi?id=[% attachment.id %]&amp;action=diff">Diff</a> | <a href="attachment.cgi?id=[% attachment.id %]&amp;action=diff">Diff</a>
[% END %] [% END %]
[% Hook.process("action") %] [% Hook.process("action") %]
......
...@@ -198,6 +198,12 @@ ...@@ -198,6 +198,12 @@
[% title = "Invalid Dimensions" %] [% title = "Invalid Dimensions" %]
The width or height specified is not a positive integer. The width or height specified is not a positive integer.
[% ELSIF error == "invalid_feature" %]
[% title = "Invalid Feature Name" %]
[% feature FILTER html %] is not a valid feature name. See
<code>OPTIONAL_MODULES</code> in
<code>Bugzilla::Install::Requirements</code> for valid names.
[% ELSIF error == "invalid_flag_association" %] [% ELSIF error == "invalid_flag_association" %]
[% title = "Invalid Flag Association" %] [% title = "Invalid Flag Association" %]
Some flags do not belong to Some flags do not belong to
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment