File Coverage

blib/lib/XAO/Templates.pm
Criterion Covered Total %
statement 52 83 62.6
branch 22 56 39.2
condition 10 30 33.3
subroutine 7 9 77.7
pod 2 4 50.0
total 93 182 51.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             XAO::Templates - templates caching and retrieving module
4              
5             =head1 DESCRIPTION
6              
7             Templates retriever. Uses persistent cache to store once retrieved
8             templates.
9              
10             =over
11              
12             =cut
13              
14             ###############################################################################
15             package XAO::Templates;
16 22     22   1075 use strict;
  22         40  
  22         765  
17 22     22   541 use XAO::Base qw($homedir $projectsdir);
  22         6571  
  22         1961  
18 22     22   615 use XAO::Utils;
  22         17792  
  22         1318  
19 22     22   594 use XAO::Projects qw(get_current_project_name);
  22         1378  
  22         1596  
20              
21             our $VERSION='2.001';
22              
23             # Cache for templates.
24             #
25 22     22   163 use vars qw(%cache);
  22         50  
  22         20365  
26              
27             # Getting the text of given template.
28             #
29             sub get (%) {
30 40     40 0 249 my %args=@_;
31 40         83 my $path=$args{path};
32              
33 40         115 my $sitename=get_current_project_name();
34              
35 40 50       606 if($path =~ /\.\.\//) {
36 0         0 eprint "Bad template path -- sitename=",$sitename,", path=$path";
37 0         0 return undef;
38             }
39              
40             ##
41             # Checking in the memory cache. If there is a record, but it's
42             # 'undef' then the template does not exist in that site's local
43             # tree.
44             #
45 40 50 66     337 if(!defined $sitename) {
    100          
46 0 0 0     0 return $cache{'/'}->{$path} if exists($cache{'/'}) && exists($cache{'/'}->{$path});
47             }
48             elsif(exists $cache{$sitename} && exists $cache{$sitename}->{$path}) {
49 7         16 my $template=$cache{$sitename}->{$path};
50 7 100       39 return $template if defined $template;
51              
52             ##
53             # Otherwise we know we already tried, but failed and it's safe
54             # to return the one from the system cache.
55             #
56 1 50 33     15 return $cache{'/'}->{$path} if exists($cache{'/'}) && exists($cache{'/'}->{$path});
57             }
58              
59             ##
60             # Retrieving from disk. We only get here if cache was a miss.
61             #
62 33         81 my $system;
63             my $tpath;
64 33 50       78 if(defined $sitename) {
65 33         119 $tpath="$projectsdir/$sitename/templates/$path";
66 33         81 $system=0;
67             }
68 33 100 66     1017 if(! $tpath || ! -r $tpath) {
69 16         65 $tpath="$homedir/templates/$path";
70 16         31 $system=1;
71             }
72 33         159 local *F;
73 33         66 my $text;
74 33 50       1587 if(open(F,$tpath)) {
75 33         218 local $/;
76 33         1320 $text=;
77 33         461 close(F);
78             }
79              
80             ##
81             # Storing into cache.
82             #
83 33 50       281 if(!defined $text) {
    50          
84 0         0 $cache{'/'}->{$path}=undef;
85 0 0       0 $cache{$sitename}->{$path}=undef if defined $sitename;
86             }
87             elsif(length($text) < 100000) {
88 33 100       87 if($system) {
89 16         87 $cache{'/'}->{$path}=$text;
90 16 50       101 $cache{$sitename}->{$path}=undef if defined $sitename;
91             }
92             else {
93 17         84 $cache{$sitename}->{$path}=$text;
94             }
95             }
96              
97 33         207 return $text;
98             }
99              
100             ###############################################################################
101              
102             =item filename ($;$)
103              
104             Checks if given path (first argument) exists and returns template's
105             filename if it does and 'undef' if there is no template. Optional second
106             argument refers to a sitename (project name), by default the current
107             active project name is used.
108              
109             =cut
110              
111             sub filename ($;$) {
112 81     81 1 151 my ($path,$sitename)=@_;
113              
114 81   33     300 $sitename||=get_current_project_name();
115              
116 81 50       433 if($path =~ /\.\.\//) {
117 0         0 eprint "Bad template path -- sitename=",$sitename,", path=$path";
118 0         0 return 0;
119             }
120              
121 81 50 33     334 return undef if !defined($path) || $path eq '';
122              
123 81 50       155 if(defined $sitename) {
124 81         172 my $tn="$projectsdir/$sitename/templates/$path";
125 81 100 66     2305 return $tn if -f $tn && -r _;
126             }
127              
128 1         17 my $tn="$homedir/templates/$path";
129 1 50 33     39 return $tn if -f $tn && -r _;
130              
131 1         8 return undef;
132             }
133              
134             ###############################################################################
135              
136             =item check (%)
137              
138             Deprecated method, do not use.
139              
140             =cut
141              
142             sub check (%) {
143 0     0 1   my %args=@_;
144 0           dprint "XAO::Templates::check - deprecated, use filename() instead";
145 0 0         return filename($args{path}) ? 1 : 0;
146             }
147              
148             ###############################################################################
149              
150             # Complete list of all available templates in random order.
151             #
152             # Returns list in array context and array reference in scalar context.
153              
154             sub list (%) {
155 0     0 0   eprint "XAO::Templates::list - is not supported any more";
156 0           my %args=@_;
157 0           my $tpath;
158 0           my $sitename=get_current_project_name();
159 0 0         if(defined $sitename) {
160 0           $tpath="$projectsdir/$sitename/templates/";
161             }
162 0 0 0       if(! $tpath || ! -r $tpath) {
163 0           $tpath="$homedir/templates/";
164             }
165 0 0 0       if(! $tpath || ! -r $tpath) {
166 0           eprint "Templates::list - can't get list";
167 0 0         return wantarray ? () : undef;
168             }
169 0           local *F;
170 0 0         if(!open(F,"/usr/bin/find $tpath -type f |")) {
171 0           eprint "Templates::list - can't get list: $!\n";
172 0 0         return wantarray ? () : undef;
173             }
174 0           my @list=map { chomp; s/^$tpath//; $_ } ;
  0            
  0            
  0            
175 0           close(F);
176 0 0         wantarray ? @list : (@list ? \@list : undef);
    0          
177             }
178              
179             ###############################################################################
180             1;
181             __END__