From 74060782dd67e3e960fac20c759bc025c91caece Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Wed, 25 Nov 2009 03:37:08 +0000 Subject: Bug 530960: Put hooks into template/default/hook instead of template/hook Patch by Max Kanat-Alexander (module owner) a=mkanat --- Bugzilla/Install/Util.pm | 39 +++++++++-------- Bugzilla/Template.pm | 8 ++++ Bugzilla/Template/Plugin/Hook.pm | 50 ++++++---------------- contrib/extension-convert.pl | 17 ++++---- .../admin/sanitycheck/messages-statuses.html.tmpl | 35 +++++++++++++++ .../hook/global/user-error-errors.html.tmpl | 12 ++++++ 6 files changed, 96 insertions(+), 65 deletions(-) create mode 100644 extensions/Example/template/en/default/hook/admin/sanitycheck/messages-statuses.html.tmpl create mode 100644 extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl diff --git a/Bugzilla/Install/Util.pm b/Bugzilla/Install/Util.pm index 254cc237b..107f91a9b 100644 --- a/Bugzilla/Install/Util.pm +++ b/Bugzilla/Install/Util.pm @@ -43,8 +43,6 @@ our @EXPORT_OK = qw( install_string include_languages template_include_path - template_base_directories - template_lang_directories vers_cmp get_console_locale init_console @@ -300,20 +298,14 @@ sub include_languages { return @usedlanguages; } -# Used by template_include_path and Bugzilla::Template::Plugin::Hook. -sub template_lang_directories { - my ($languages, $templatedir, $subdir_name) = @_; +# Used by template_include_path +sub _template_lang_directories { + my ($languages, $templatedir) = @_; - my @add; + my @add = qw(custom default); my $project = bz_locations->{'project'}; - if ($subdir_name) { - @add = ("$subdir_name.custom", $subdir_name); - unshift(@add, "$subdir_name.$project") if $project; - } - else { - @add = ("custom", "default"); - unshift(@add, $project) if $project; - } + unshift(@add, $project) if $project; + my @result; foreach my $lang (@$languages) { foreach my $dir (@add) { @@ -327,8 +319,8 @@ sub template_lang_directories { return @result; } -# Used by template_include_path and Bugzilla::Template::Plugin::Hook. -sub template_base_directories { +# Used by template_include_path. +sub _template_base_directories { # First, we add extension template directories, because extension templates # override standard templates. Extensions may be localized in the same way # that Bugzilla templates are localized. @@ -339,14 +331,23 @@ sub template_base_directories { } sub template_include_path { + my ($params) = @_; my @used_languages = include_languages(@_); # Now, we add template directories in the order they will be searched: - my $template_dirs = template_base_directories(); + my $template_dirs = _template_base_directories(); my @include_path; foreach my $template_dir (@$template_dirs) { - push(@include_path, - template_lang_directories(\@used_languages, $template_dir)); + my @lang_dirs = _template_lang_directories(\@used_languages, + $template_dir); + # Hooks get each set of extension directories separately. + if ($params->{hook}) { + push(@include_path, \@lang_dirs); + } + # Whereas everything else just gets a whole INCLUDE_PATH. + else { + push(@include_path, @lang_dirs); + } } return \@include_path; } diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index c3c7a9fac..f714f04d1 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -475,6 +475,14 @@ sub create { PRE_CHOMP => 1, TRIM => 1, + # Bugzilla::Template::Plugin::Hook uses the absolute (in mod_perl) + # or relative (in mod_cgi) paths of hook files to explicitly compile + # a specific file. Also, these paths may be absolute at any time + # if a packager has modified bz_locations() to contain absolute + # paths. + ABSOLUTE => 1, + RELATIVE => $ENV{MOD_PERL} ? 0 : 1, + COMPILE_DIR => bz_locations()->{'datadir'} . "/template", # Initialize templates (f.e. by loading plugins like Hook). diff --git a/Bugzilla/Template/Plugin/Hook.pm b/Bugzilla/Template/Plugin/Hook.pm index fb59a495b..9c292d726 100644 --- a/Bugzilla/Template/Plugin/Hook.pm +++ b/Bugzilla/Template/Plugin/Hook.pm @@ -27,8 +27,7 @@ use strict; use base qw(Template::Plugin); use Bugzilla::Constants; -use Bugzilla::Install::Util qw(include_languages template_base_directories - template_lang_directories); +use Bugzilla::Install::Util qw(include_languages template_include_path); use Bugzilla::Util; use Bugzilla::Error; @@ -58,16 +57,18 @@ sub process { my $type = $2; # Hooks are named like this: - my $extension_template = "$path/$template_name-$hook_name.$type.tmpl"; + my $extension_template = "$path$template_name-$hook_name.$type.tmpl"; # Get the hooks out of the cache if they exist. Otherwise, read them # from the disk. my $cache = Bugzilla->request_cache->{template_plugin_hook_cache} ||= {}; - $cache->{$extension_template} ||= $self->_get_hooks($extension_template); + my $lang = $cache->{language} || ''; + $cache->{"${lang}__$extension_template"} + ||= $self->_get_hooks($extension_template); # process() accepts an arrayref of templates, so we just pass the whole # arrayref. - return $context->process($cache->{$extension_template}); + return $context->process($cache->{"${lang}__$extension_template"}); } sub _get_hooks { @@ -76,21 +77,10 @@ sub _get_hooks { my $template_sets = _template_hook_include_path(); my @hooks; foreach my $dir_set (@$template_sets) { - foreach my $lang_dir (@$dir_set) { - my $file = File::Spec->catdir($lang_dir, $extension_template); + foreach my $template_dir (@$dir_set) { + my $file = "$template_dir/hook/$extension_template"; if (-e $file) { - # TT won't accept a file that isn't in its include path. - # So we open a file handle, compile it to a template, and - # then pass the compiled template to process(). - # - # This doesn't cache the hook template on disk - # (which is nearly impossible for us to do, with TT's - # architecture), but we do cache this compiled template - # per-request (in process() above) so that it doesn't have - # to be recompiled over and over within one request. - open(my $fh, '<', $file) or die "$file: $!"; - my $template = $self->_context->template($fh); - close($fh); + my $template = $self->_context->template($file); push(@hooks, $template); # Don't run the hook for more than one language. last; @@ -105,25 +95,11 @@ sub _template_hook_include_path { my $cache = Bugzilla->request_cache; my $language = $cache->{language} || ''; my $cache_key = "template_plugin_hook_include_path_$language"; - return $cache->{$cache_key} if defined $cache->{$cache_key}; - - my @used_languages = include_languages({ + $cache->{$cache_key} ||= template_include_path({ use_languages => Bugzilla->languages, - only_language => $language }); - my $template_dirs = template_base_directories(); - - # We create an array of arrayrefs, with each arrayref being a single - # extension's "language" directories. In addition to the extensions/ - # directory, this also includes a set for the base template/ directory. - my @template_sets; - foreach my $template_dir (@$template_dirs) { - my @language_dirs = template_lang_directories(\@used_languages, - $template_dir, 'hook'); - if (scalar @language_dirs) { - push(@template_sets, \@language_dirs); - } - } - $cache->{$cache_key} = \@template_sets; + only_language => $language, + hook => 1, + }); return $cache->{$cache_key}; } diff --git a/contrib/extension-convert.pl b/contrib/extension-convert.pl index 1f29db3da..88718cf83 100644 --- a/contrib/extension-convert.pl +++ b/contrib/extension-convert.pl @@ -261,23 +261,22 @@ sub move_template_hooks { my ($dir) = @_; foreach my $lang (glob("$dir/template/*")) { next if !_file_matters($lang); - mkpath("$lang/hook") || die "$lang/hook: $!"; + my $hook_container = "$lang/default/hook"; + mkpath($hook_container) || warn "$hook_container: $!"; # Hooks can be in all sorts of weird places, including # template/default/hook. - foreach my $hooks_container ($lang, "$lang/default/hook") { - foreach my $file (glob("$hooks_container/*")) { - next if !_file_matters($file, 1); - my $dirname = basename($file); - print "Moving $file to $lang/hook/$dirname...\n"; - rename($file, "$lang/hook/$dirname") || die "move failed: $!"; - } + foreach my $file (glob("$lang/*")) { + next if !_file_matters($file, 1); + my $dirname = basename($file); + print "Moving $file to $hook_container/$dirname...\n"; + rename($file, "$hook_container/$dirname") || die "move failed: $!"; } } } sub _file_matters { my ($path, $tmpl) = @_; - my @ignore = qw(default custom CVS hook); + my @ignore = qw(default custom CVS); my $file = basename($path); return 0 if grep(lc($_) eq lc($file), @ignore); # Hidden files diff --git a/extensions/Example/template/en/default/hook/admin/sanitycheck/messages-statuses.html.tmpl b/extensions/Example/template/en/default/hook/admin/sanitycheck/messages-statuses.html.tmpl new file mode 100644 index 000000000..8a825e57c --- /dev/null +++ b/extensions/Example/template/en/default/hook/admin/sanitycheck/messages-statuses.html.tmpl @@ -0,0 +1,35 @@ +[%# -*- Mode: perl; indent-tabs-mode: nil -*- + # + # The contents of this file are subject to the Mozilla Public + # License Version 1.1 (the "License"); you may not use this file + # except in compliance with the License. You may obtain a copy of + # the License at http://www.mozilla.org/MPL/ + # + # Software distributed under the License is distributed on an "AS + # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + # implied. See the License for the specific language governing + # rights and limitations under the License. + # + # The Original Code is the Bugzilla Example Plugin. + # + # The Initial Developer of the Original Code is ITA Software + # Portions created by the Initial Developer are Copyright (C) 2009 + # the Initial Developer. All Rights Reserved. + # + # Contributor(s): Bradley Baetz + #%] + +[% IF san_tag == "example_check_au_user" %] + EXAMPLE PLUGIN - Checking for non-Australian users. +[% ELSIF san_tag == "example_check_au_user_alert" %] + User <[% login FILTER html %]> isn't Australian. + [% IF user.in_group('editusers') %] + Edit this user. + [% END %] +[% ELSIF san_tag == "example_check_au_user_prompt" %] + Fix these users. +[% ELSIF san_tag == "example_repair_au_user_start" %] + EXAMPLE PLUGIN - OK, would now make users Australian. +[% ELSIF san_tag == "example_repair_au_user_end" %] + EXAMPLE PLUGIN - Users would now be Australian. +[% END %] diff --git a/extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl b/extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl new file mode 100644 index 000000000..df5a203dd --- /dev/null +++ b/extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl @@ -0,0 +1,12 @@ +[%# Note that error messages should generally be indented four spaces, like + # below, because when Bugzilla translates an error message into plain + # text, it takes four spaces off the beginning of the lines. + # + # Note also that I prefixed my error name with "example", the name of my + # extension, so that I wouldn't conflict with other error names in + # Bugzilla or other extensions. + #%] +[% IF error == "example_my_error" %] + [% title = "Example Error Title" %] + This is the error message! It contains some html. +[% END %] -- cgit v1.2.3-24-g4f1b