Templates.pm 3.23 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
use 5.10.1;
11
use strict;
12
use warnings;
13 14

use lib 't';
15
use parent qw(Exporter);
16 17 18
@Support::Templates::EXPORT =
    qw(@languages @include_paths @english_default_include_paths
       @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
# English default include paths
29 30
our @english_default_include_paths =
    (File::Spec->catdir(bz_locations()->{'templatedir'}, 'en', 'default'));
31

32 33 34 35 36 37 38 39
# 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;
    }
}

40
# Files which are referenced in the cgi files
41
our @referenced_files = ();
42 43

# All files sorted by include_path
44
our %actual_files = ();
45 46

# total number of actual_files
47
our $num_actual_files = 0;
48

49
# Set the template available languages and include paths
50 51
our @languages = @{ Bugzilla->languages };
our @include_paths = @{ template_include_path({ language => Bugzilla->languages }) };
52

53
our @files;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

# 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);

69 70 71 72
        # File::Spec 3.13 and newer return "." instead of "" if both
        # arguments of abs2rel() are identical.
        $local_dir = "" if ($local_dir eq ".");

73 74 75 76 77 78
        if ($local_dir) {
            $filename = File::Spec->catfile($local_dir, $_);
        } else {
            $filename = $_;
        }

79
        push(@files, $filename);
80 81
    }
}
82

83 84 85 86 87 88 89 90 91 92 93 94 95
# 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}});
}
96

97 98 99
# 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;
100

101
foreach my $file (@Support::Files::testitems) {
102 103 104 105
    open (FILE, $file);
    my @lines = <FILE>;
    close (FILE);
    foreach my $line (@lines) {
106
        if ($line =~ m/template->process\(\"(.+?)\", .+?\)/) {
107
            my $template = $1;
108 109 110
            # Ignore templates with $ in the name, since they're
            # probably vars, not real files
            next if $template =~ m/\$/;
111 112 113
            next if $seen{$template};
            push (@referenced_files, $template);
            $seen{$template} = 1;
114
        }
115 116 117 118
    }
}

1;