blib/lib/WWW/CMS.pm

filter: Statementstmt 17.5% 10 / 57 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 33.3% 3 / 9 Podpod 0.0% 0 / 4 totaltotal 12.7% 13 / 102 CC 11 SCAR 29.9
CC
11
Cov
12%
new
42.0
publicize
20.0
tempeval
12.0
CRAP 19.8
↑ Summary
1 # ShellShark's CMS
2 # Content Management System engine for ShellShark Networks, Inc.
3 # Copyright (c)2005 ShellShark Networks, Inc. All rights reserved.
4  
5 package WWW::CMS;
6  
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
12 # Create a new WWW::CMS instance
13 # Takes a hash of arguments containing Base and Module
14 # Returns an object reference on success, nothing on failure
15  
16 0 my $self = { };
17 0 my ( $class, $args ) = @_;
18  
19 # Bless me father, for I have OOP'ed
20 0 bless $self, $class;
21  
22 # Is our template basedir defined, and does it exist?
23 0 ▶ if ( !$args->{ TemplateBase } || !-d $args->{ TemplateBase } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"TemplateBase"} and -d $args->{"TemplateBase"}) 0 0 0
Condition0%0/3
$args->{"TemplateBase"} and -d $args->{"TemplateBase"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"TemplateBase"} and -d $args->{"TemplateBase"}
$args->{"TemplateBase"} -d $args->{"TemplateBase"}
Truth table0%0/3
A and B
A= $args->{"TemplateBase"}B= -d $args->{"TemplateBase"}
ABresult
0X0
100
111
24 0 print STDERR "WWW::CMS: Fatal error: Template base directory not defined or does not exist!\n";
25 0 return;
26 }
27  
28 # Strip trailing slash from template basedir
29 0 $args->{ TemplateBase } =~ s/\/$//;
30  
31 # Is our template module defined, and does it exist?
32 0 ▶ if ( !$args->{ Module } || !-f "$args->{ TemplateBase }/$args->{ Module }" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"Module"} and -f qq[$args->{"TemplateBase"}/$args->{"Module"}]) 0 0 0
Condition0%0/3
$args->{"Module"} and -f qq[$args->{"TemplateBase"}/$args->{"Module"}]
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"Module"} and -f qq[$args->{"TemplateBase"}/$args->{"Module"}]
$args->{"Module"} -f qq[$args->{"TemplateBase"}/$args->{"Module"}]
Truth table0%0/3
A and B
A= $args->{"Module"}B= -f qq[$args->{"TemplateBase"}/$args->{"Module"}]
ABresult
0X0
100
111
33 0 print STDERR "WWW::CMS: Fatal error: Template module not defined or not found\n";
34 0 return;
35 }
36  
37 # Open the template and shove it in memory
38 0 ▶ open my $fh, "<", "$args->{ TemplateBase }/$args->{ Module }" || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 $self->{ ERRMSG } = "Failed to open template module '$args->{ TemplateBase }/$args->{ Module }': $!";
40 0 return;
41 0 ▶ };
Condition0%0/3
"$args->{'TemplateBase'}/$args->{'Module'}" || sub { $self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!"; return; }
l!l&&r!l&&!r
000
MC/DC0%0/2
"$args->{'TemplateBase'}/$args->{'Module'}" || sub { $self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!"; return; }
"$args->{'TemplateBase'}/$args->{'Module'}" sub { $self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!"; return; }
Truth table0%0/3
A || B
A= "$args->{'TemplateBase'}/$args->{'Module'}"B= sub { $self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!"; return; }
ABresult
1X1
011
000
42  
43 # Slurpy, slurpy....
44 0 $self->{ template } = do { local $/; <$fh> };
45  
46 0 close $fh;
47  
48 # Stuff our arguments into our namespace
49 0 $self->{ TemplateBase } = $args->{ TemplateBase };
50  
51 # Give this instance the template module's name for later identification, if needed
52 0 $self->{ instance } = $args->{ Module };
53
54 # Setup a heap for arbitrary variable storage for IF/ELSE operators in templates
55 0 $self->{ heap } = { };
56  
57 0 return $self;
58 }
59  
60 0 ▶sub publicize {
Subroutine publicize: not called CC=4 SCAR=30.0
Pod undocumented
61 # Find / replace all instances of built-in tags with output of respective code
62 0 my $self = shift;
63
64 # Pass in an existing CGI object
65 0 ▶ my $query = shift || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 $self->{ ERRSTR } = 'CGI object not passed into publicize()';
67 0 return;
68 0 ▶ };
Condition0%0/3
shift() || sub { $self->{'ERRSTR'} = 'CGI object not passed into publicize()'; return; }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { $self->{'ERRSTR'} = 'CGI object not passed into publicize()'; return; }
shift() sub { $self->{'ERRSTR'} = 'CGI object not passed into publicize()'; return; }
Truth table0%0/3
A || B
A= shift()B= sub { $self->{'ERRSTR'} = 'CGI object not passed into publicize()'; return; }
ABresult
1X1
011
000
69  
70 # Many thanks to revdiablo for making this uberleet regex
71 0 $self->{ template } =~ s{
72 \Q^^[\E # start operator block
73 (IF|WHILE) # operator name
74 \Q]=\E # start condition
75 ([^\n]*) # condition expression
76 \Q^^\E # end condition
77 ((?:.(?!\^\^))*.) # contents
78 (?: \Q^^[ELSE]^^\E # start optional else block
79 ((?:.(?!\^\^))*.) # contents
80 )? # ensure it is optional
81 \Q^^[END]^^\E # end operator block
82 }
83 {
84 0 $self->tempeval($1, $2, $3, $4)
85 }gisex;
86  
87  
88 0 ▶ $self->{ PageName } = '' unless $self->{ PageName };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PageName"} 0 0 0
89 0 $self->{ template } =~ s/%%PAGE%%/$self->{ PageName }/gi;
90  
91 0 my $ts = strftime ( "%A, %B %e, %G - %I:%M%p", localtime );
92 0 $self->{ template } =~ s/%%DATETIME%%/$ts/gi;
93  
94 0 ▶ if ( $self->{ content } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"content"}) 0 0 0
95 0 my $tmpcont = join ( "\n", @{ $self->{ content } } );
96 0 $self->{ template } =~ s/%%CONTENT%%/$tmpcont/gi;
97 }
98  
99 0 my $url = $query->url();
100 0 $self->{ template } =~ s/%%MYURL%%/$url/gi;
101  
102 0 return $self->{ template };
103 }
104  
105 0 ▶sub repl_tag {
Subroutine repl_tag: not called CC=1 SCAR=6.9
Pod undocumented
106 # Find / replace all instances of a given tag with the output of a given coderef
107 0 my ( $self, $var, $coderef ) = @_;
108  
109 # See how many find/replace operations will have to take place
110  
111 # Evaluate the code once, and cache the output to save overhead
112 0 my $output = $coderef->();
113  
114 # In the template, find $var (CaSE sEnsiTiVE!) and replace it with $output
115 0 $self->{ template } =~ s/$var/$output/g;
116  
117 0 return 1;
118 }
119  
120 0 ▶sub tempeval {
Subroutine tempeval: not called CC=3 SCAR=24.8
Pod undocumented
121 # Many thanks again to revdiablo for creating this initially as a stub
122 # to show me how the hell his funky regex works ;-)
123 0 my ( $self, $op, $exp, $true, $false ) = @_;
124  
125 0 ▶ if ( $op =~ /^IF$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /^IF$/i) 0 0 0
126 0 ▶ if ( $self->{heap}->{$exp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'heap'}{$exp}) { } 0 0 0
127 0 return "<!-- $op ($exp => $self->{heap}->{$exp}) succeeded -->\n$true";
128 }
129 else {
130 0 return "<!-- $op ($exp => $self->{heap}->{$exp}) failed -->\n$false";
131 }
132 }
133 }
134  
135 1 1;
↑ Summary