Templates.pm 3.51 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9

package Support::Templates;

10 11 12
use strict;

use lib 't';
13
use parent qw(Exporter);
14
@Support::Templates::EXPORT = 
15
         qw(@languages @include_paths @english_default_include_paths
16
         %include_path @referenced_files %actual_files $num_actual_files);
17
use vars qw(@languages @include_paths @english_default_include_paths
18
            %include_path @referenced_files %actual_files $num_actual_files);
19

20
use Bugzilla;
21
use Bugzilla::Constants;
22
use Bugzilla::Install::Util qw(template_include_path);
23 24
use Support::Files;

25
use File::Find;
26
use File::Spec;
27

28 29 30 31 32 33 34 35 36
# The available template languages
@languages = ();

# The colon separated includepath per language
%include_path = ();

# All include paths
@include_paths = ();

37 38
# English default include paths
push @english_default_include_paths,
39 40
    File::Spec->catdir(bz_locations()->{'templatedir'}, 'en', 'default');

41 42 43 44 45 46 47 48
# And the extensions too
foreach my $extension (@Support::Files::extensions) {
    my $dir = File::Spec->catdir($extension, 'template', 'en', 'default');
    if (-e $dir) {
        push @english_default_include_paths, $dir;
    }
}

49
# Files which are referenced in the cgi files
50
@referenced_files = ();
51 52 53 54 55 56 57

# All files sorted by include_path
%actual_files = ();

# total number of actual_files
$num_actual_files = 0;

58 59 60
# Set the template available languages and include paths
@languages = @{ Bugzilla->languages };
@include_paths = @{ template_include_path({ language => Bugzilla->languages }) };
61 62

my @files;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

# Local subroutine used with File::Find
sub find_templates {
    # Prune CVS directories
    if (-d $_ && $_ eq 'CVS') {
        $File::Find::prune = 1;
        return;
    }

    # Only include files ending in '.tmpl'
    if (-f $_ && $_ =~ m/\.tmpl$/i) {
        my $filename;
        my $local_dir = File::Spec->abs2rel($File::Find::dir,
                                            $File::Find::topdir);

78 79 80 81
        # File::Spec 3.13 and newer return "." instead of "" if both
        # arguments of abs2rel() are identical.
        $local_dir = "" if ($local_dir eq ".");

82 83 84 85 86 87
        if ($local_dir) {
            $filename = File::Spec->catfile($local_dir, $_);
        } else {
            $filename = $_;
        }

88
        push(@files, $filename);
89 90
    }
}
91

92 93 94 95 96 97 98 99 100 101 102 103 104
# Scan the given template include path for templates
sub find_actual_files {
  my $include_path = $_[0];
  @files = ();
  find(\&find_templates, $include_path);
  return @files;
}


foreach my $include_path (@include_paths) {
  $actual_files{$include_path} = [ find_actual_files($include_path) ];
  $num_actual_files += scalar(@{$actual_files{$include_path}});
}
105

106 107 108
# Scan Bugzilla's perl code looking for templates used and put them
# in the @referenced_files array to be used by the 004template.t test.
my %seen;
109

110
foreach my $file (@Support::Files::testitems) {
111 112 113 114
    open (FILE, $file);
    my @lines = <FILE>;
    close (FILE);
    foreach my $line (@lines) {
115
        if ($line =~ m/template->process\(\"(.+?)\", .+?\)/) {
116
            my $template = $1;
117 118 119
            # Ignore templates with $ in the name, since they're
            # probably vars, not real files
            next if $template =~ m/\$/;
120 121 122
            next if $seen{$template};
            push (@referenced_files, $template);
            $seen{$template} = 1;
123
        }
124 125 126 127
    }
}

1;