]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 208761: Move GetFormat() from globals.pl into Bugzilla::Template - Patch by Frédé...
authorlpsolit%gmail.com <>
Thu, 25 Aug 2005 21:02:39 +0000 (21:02 +0000)
committerlpsolit%gmail.com <>
Thu, 25 Aug 2005 21:02:39 +0000 (21:02 +0000)
14 files changed:
Bugzilla/Template.pm
buglist.cgi
chart.cgi
config.cgi
docs/xml/customization.xml
duplicates.cgi
enter_bug.cgi
globals.pl
page.cgi
post_bug.cgi
query.cgi
report.cgi
show_bug.cgi
summarize_time.cgi

index 711144a6fd3c2a50c7e44576b27569f520c892f6..52a1bf150fad6ac63633f387d6140ad0319ae446 100644 (file)
 #                 Tobias Burnus <burnus@net-b.de>
 #                 Myk Melez <myk@mozilla.org>
 #                 Max Kanat-Alexander <mkanat@bugzilla.org>
+#                 Frédéric Buclin <LpSolit@gmail.com>
 
 
 package Bugzilla::Template;
 
 use strict;
 
+use Bugzilla::Constants;
 use Bugzilla::Config qw(:DEFAULT $templatedir $datadir);
 use Bugzilla::Util;
 use Bugzilla::User;
@@ -132,7 +134,6 @@ sub getTemplateIncludePath {
              @usedlanguages)];
 }
 
-# Write the header for non yet templatized .cgi files.
 sub put_header {
     my $self = shift;
     ($vars->{'title'}, $vars->{'h1'}, $vars->{'h2'}) = (@_);
@@ -142,13 +143,51 @@ sub put_header {
     $vars->{'header_done'} = 1;
 }
 
-# Write the footer for non yet templatized .cgi files.
 sub put_footer {
     my $self = shift;
     $self->process("global/footer.html.tmpl", $vars)
       || ThrowTemplateError($self->error());
 }
 
+sub get_format {
+    my $self = shift;
+    my ($template, $format, $ctype) = @_;
+
+    $ctype ||= 'html';
+    $format ||= '';
+
+    # Security - allow letters and a hyphen only
+    $ctype =~ s/[^a-zA-Z\-]//g;
+    $format =~ s/[^a-zA-Z\-]//g;
+    trick_taint($ctype);
+    trick_taint($format);
+
+    $template .= ($format ? "-$format" : "");
+    $template .= ".$ctype.tmpl";
+
+    # Now check that the template actually exists. We only want to check
+    # if the template exists; any other errors (eg parse errors) will
+    # end up being detected later.
+    eval {
+        $self->context->template($template);
+    };
+    # This parsing may seem fragile, but its OK:
+    # http://lists.template-toolkit.org/pipermail/templates/2003-March/004370.html
+    # Even if it is wrong, any sort of error is going to cause a failure
+    # eventually, so the only issue would be an incorrect error message
+    if ($@ && $@->info =~ /: not found$/) {
+        ThrowUserError('format_not_found', {'format' => $format,
+                                            'ctype'  => $ctype});
+    }
+
+    # Else, just return the info
+    return
+    {
+        'template'    => $template,
+        'extension'   => $ctype,
+        'ctype'       => Bugzilla::Constants::contenttypes->{$ctype}
+    };
+}
 
 ###############################################################################
 # Templatization Code
@@ -449,12 +488,19 @@ __END__
 
 =head1 NAME
 
-Bugzilla::Template - Wrapper arround the Template Toolkit C<Template> object
+Bugzilla::Template - Wrapper around the Template Toolkit C<Template> object
 
 =head1 SYNOPSYS
 
   my $template = Bugzilla::Template->create;
 
+  $template->put_header($title, $h1, $h2);
+  $template->put_footer();
+
+  my $format = $template->get_format("foo/bar",
+                                     scalar($cgi->param('format')),
+                                     scalar($cgi->param('ctype')));
+
 =head1 DESCRIPTION
 
 This is basically a wrapper so that the correct arguments get passed into
@@ -463,6 +509,41 @@ the C<Template> constructor.
 It should not be used directly by scripts or modules - instead, use
 C<Bugzilla-E<gt>instance-E<gt>template> to get an already created module.
 
+=head1 METHODS
+
+=over
+
+=item C<put_header($title, $h1, $h2)>
+
+ Description: Display the header of the page.
+
+ Params:      $title - Page title.
+              $h1    - Main page header.
+              $h2    - Page subheader.
+
+ Returns:     nothing
+
+=item C<put_footer()>
+
+ Description: Display the footer of the page.
+
+ Params:      none
+
+ Returns:     nothing
+
+=item C<get_format($file, $format, $ctype)>
+
+ Description: Construct a format object from URL parameters.
+
+ Params:      $file   - Name of the template to display.
+              $format - When the template exists under several formats
+                        (e.g. table or graph), specify the one to choose.
+              $ctype  - Content type, see Bugzilla::Constants::contenttypes.
+
+ Returns:     A format object.
+
+=back
+
 =head1 SEE ALSO
 
 L<Bugzilla>, L<Template>
index 70cc46bae4b72f6b1ddbbcadb93967f53da3afb3..44854b237fa9b9227a24a879db0bbea631144d69 100755 (executable)
@@ -115,8 +115,8 @@ if ((defined $cgi->param('ctype')) && ($cgi->param('ctype') eq "js")) {
 # Determine the format in which the user would like to receive the output.
 # Uses the default format if the user did not specify an output format;
 # otherwise validates the user's choice against the list of available formats.
-my $format = GetFormat("list/list", scalar $cgi->param('format'),
-                       scalar $cgi->param('ctype'));
+my $format = $template->get_format("list/list", scalar $cgi->param('format'),
+                                   scalar $cgi->param('ctype'));
 
 # Use server push to display a "Please wait..." message for the user while
 # executing their query if their browser supports it and they are viewing
index 812803199dc18d1b0113c9bb75706b1a2150350d..2b8d39b564ccb5f4e96fd7c05fe2013525f1b9c9 100755 (executable)
--- a/chart.cgi
+++ b/chart.cgi
@@ -265,7 +265,7 @@ sub plot {
     validateWidthAndHeight();
     $vars->{'chart'} = new Bugzilla::Chart($cgi);
 
-    my $format = &::GetFormat("reports/chart", "", scalar($cgi->param('ctype')));
+    my $format = $template->get_format("reports/chart", "", scalar($cgi->param('ctype')));
 
     # Debugging PNGs is a pain; we need to be able to see the error messages
     if ($cgi->param('debug')) {
index bbffe20d85eacfdf5bbbeb3302ecea4d111d8e87..e3ecef3fff4166acefae8c7198ae0f88d2ff1a9d 100755 (executable)
@@ -87,8 +87,8 @@ $vars->{'field'} = [Bugzilla->dbh->bz_get_field_defs()];
 # Determine how the user would like to receive the output; 
 # default is JavaScript.
 my $cgi = Bugzilla->cgi;
-my $format = GetFormat("config", scalar($cgi->param('format')),
-                       scalar($cgi->param('ctype')) || "js");
+my $format = $template->get_format("config", scalar($cgi->param('format')),
+                                   scalar($cgi->param('ctype')) || "js");
 
 # Return HTTP headers.
 print "Content-Type: $format->{'ctype'}\n\n";
index 49b73319e24e498cc8e7beb6dcf8f603a757c0ab..37c64cca0d4410b34835e0ee4caecf620f35d1b8 100644 (file)
       
       <para>
         To see if a CGI supports multiple output formats and types, grep the
-        CGI for <quote>GetFormat</quote>. If it's not present, adding
+        CGI for <quote>get_format</quote>. If it's not present, adding
         multiple format/type support isn't too hard - see how it's done in
         other CGIs, e.g. config.cgi.
       </para>
index 92c697f53454501f8b193cb14150e1d363477fe9..2aa0df263700e374a6f897b248896565657118fe 100755 (executable)
@@ -269,8 +269,9 @@ my @selectable_products = GetSelectableProducts();
 $vars->{'products'} = \@selectable_products;
 
 
-my $format = GetFormat("reports/duplicates", scalar($cgi->param('format')),
-                       scalar($cgi->param('ctype')));
+my $format = $template->get_format("reports/duplicates",
+                                   scalar($cgi->param('format')),
+                                   scalar($cgi->param('ctype')));
 
 print $cgi->header($format->{'ctype'});
 
index d60ac7bc7c5a4abab50db1d20a67aaaebc69b89f..fd009e518bedea9f3470c7c2bc30f7f267051b03 100755 (executable)
@@ -581,9 +581,9 @@ $vars->{'group'} = \@groups;
 
 $vars->{'default'} = \%default;
 
-my $format = 
-  GetFormat("bug/create/create", scalar $cgi->param('format'), 
-            scalar $cgi->param('ctype'));
+my $format = $template->get_format("bug/create/create",
+                                   scalar $cgi->param('format'), 
+                                   scalar $cgi->param('ctype'));
 
 print $cgi->header($format->{'ctype'});
 $template->process($format->{'template'}, $vars)
index 17174540a3a1201c83d974ceebc2d7f7dff2c888..6e9dcbeba09eb11fe5857f18dbb3d12507aed3f4 100644 (file)
@@ -1082,54 +1082,6 @@ sub OpenStates {
     return ('NEW', 'REOPENED', 'ASSIGNED', 'UNCONFIRMED');
 }
 
-
-###############################################################################
-
-# Constructs a format object from URL parameters. You most commonly call it 
-# like this:
-# my $format = GetFormat("foo/bar", scalar($cgi->param('format')),
-#                        scalar($cgi->param('ctype')));
-
-sub GetFormat {
-    my ($template, $format, $ctype) = @_;
-
-    $ctype ||= "html";
-    $format ||= "";
-
-    # Security - allow letters and a hyphen only
-    $ctype =~ s/[^a-zA-Z\-]//g;
-    $format =~ s/[^a-zA-Z\-]//g;
-    trick_taint($ctype);
-    trick_taint($format);
-
-    $template .= ($format ? "-$format" : "");
-    $template .= ".$ctype.tmpl";
-
-    # Now check that the template actually exists. We only want to check
-    # if the template exists; any other errors (eg parse errors) will
-    # end up being detected later.
-    eval {
-        Bugzilla->template->context->template($template);
-    };
-    # This parsing may seem fragile, but its OK:
-    # http://lists.template-toolkit.org/pipermail/templates/2003-March/004370.html
-    # Even if it is wrong, any sort of error is going to cause a failure
-    # eventually, so the only issue would be an incorrect error message
-    if ($@ && $@->info =~ /: not found$/) {
-        ThrowUserError("format_not_found", { 'format' => $format,
-                                             'ctype' => $ctype,
-                                           });
-    }
-
-    # Else, just return the info
-    return
-    {
-        'template'    => $template ,
-        'extension'   => $ctype ,
-        'ctype'       => Bugzilla::Constants::contenttypes->{$ctype} ,
-    };
-}
-
 ############# Live code below here (that is, not subroutine defs) #############
 
 use Bugzilla;
index 9954c64a8f271cbae1cac6420f030542bc3e7c9b..97f425ba22fd1dc3ae83e2de79ccee0ff00281db 100755 (executable)
--- a/page.cgi
+++ b/page.cgi
@@ -52,7 +52,7 @@ if ($id) {
         ThrowCodeError("bad_page_cgi_id", { "page_id" => $id });
     }
 
-    my $format = GetFormat("pages/$1", undef, $2);
+    my $format = $template->get_format("pages/$1", undef, $2);
     
     $cgi->param('id', $id);
 
index f0c2de65a57ef3081faddfb3626a65b35c373e96..77f076139504f7d5c915fe84d67652fca629755d 100755 (executable)
@@ -67,8 +67,8 @@ my $dbh = Bugzilla->dbh;
 # enter_bug template and then referencing them in the comment template.
 my $comment;
 
-my $format = GetFormat("bug/create/comment",
-                       scalar($cgi->param('format')), "txt");
+my $format = $template->get_format("bug/create/comment",
+                                   scalar($cgi->param('format')), "txt");
 
 $template->process($format->{'template'}, $vars, \$comment)
   || ThrowTemplateError($template->error());
index 51f9832f33b482e96cd84786669bd0a19135a58a..2f73f06024aa9efa5bbadf4f6f974dac07a1588d 100755 (executable)
--- a/query.cgi
+++ b/query.cgi
@@ -454,9 +454,9 @@ if (defined($vars->{'format'}) && IsValidQueryType($vars->{'format'})) {
 # If we submit back to ourselves (for e.g. boolean charts), we need to
 # preserve format information; hence query_format taking priority over
 # format.
-my $format = GetFormat("search/search", 
-                       $vars->{'query_format'} || $vars->{'format'}, 
-                       scalar $cgi->param('ctype'));
+my $format = $template->get_format("search/search", 
+                                   $vars->{'query_format'} || $vars->{'format'}, 
+                                   scalar $cgi->param('ctype'));
 
 print $cgi->header($format->{'ctype'});
 
index 6effd485eeec812e355c5b2f5bb23d8f72648d29..f9a56588f5d930a5ded9a62c676259e4de3347ba 100755 (executable)
@@ -293,7 +293,8 @@ else {
     ThrowUserError("unknown_action", {action => $cgi->param('action')});
 }
 
-my $format = GetFormat("reports/report", $formatparam, scalar($cgi->param('ctype')));
+my $format = $template->get_format("reports/report", $formatparam,
+                                   scalar($cgi->param('ctype')));
 
 # If we get a template or CGI error, it comes out as HTML, which isn't valid
 # PNG data, and the browser just displays a "corrupt PNG" message. So, you can
index 76bf353d996ac6bca0f3f8ce13694ee7720b89d7..ddb6b2313eafa2795ce454060e388b74c9857b3f 100755 (executable)
@@ -54,8 +54,8 @@ if (!$cgi->param('id') && $single) {
     exit;
 }
 
-my $format = GetFormat("bug/show", scalar $cgi->param('format'), 
-                       scalar $cgi->param('ctype'));
+my $format = $template->get_format("bug/show", scalar $cgi->param('format'), 
+                                   scalar $cgi->param('ctype'));
 
 GetVersionTable();
 
index 31aedd9e1ef62d482667e23e602a242c17634357..c04a956d763e78850f776a8e27f913e57f3b92c5 100755 (executable)
@@ -485,10 +485,9 @@ $vars->{'check_time'} = \&check_time;
 $vars->{'sort_bug_keys'} = \&sort_bug_keys;
 $vars->{'GetBugLink'} = \&GetBugLink;
 
-$ctype = "html" if !$ctype;
-my $format = GetFormat("bug/summarize-time", undef, $ctype);
+my $format = $template->get_format("bug/summarize-time", undef, $ctype);
 
 # Get the proper content-type
-print $cgi->header(-type=> Bugzilla::Constants::contenttypes->{$ctype});
+print $cgi->header(-type=> $format->{'ctype'});
 $template->process("$format->{'template'}", $vars)
   || ThrowTemplateError($template->error());