Muster/Hook/Counter.pm

filter: Statementstmt 24.7% 23 / 93 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 46.6% 7 / 15 Podpod 100% 2 / 2 totaltotal 18.2% 32 / 175 CC 26 SCAR 61.2
CC
26
Cov
15%
process
702.0
register
2.0
__ANON__
2.0
CRAP 456.8
« Muster/Hook/DeriveFields.pm ↑ Summary Muster/Hook/SqlReport.pm »
1 package Muster::Hook::Counter;
2 1 $Muster::Hook::Counter::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::Counter - Muster counter directives.
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 DESCRIPTION
12  
13 L<Muster::Hook::Counter> creates directives "counter"
14 which makes a freeform query which returns a number,
15 "wordcount" which gives a wordcount, and
16 "pagecount" which gives a count of pages.
17  
18 =cut
19  
20 1 ▶use Mojo::Base 'Muster::Hook::Directives';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Muster::Hook::Links;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 METHODS
30  
31 L<Muster::Hook::Counter> inherits all methods from L<Muster::Hook::Directives>.
32  
33 =head2 register
34  
35 Do some intialization.
36  
37 =cut
38 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
39 0 my $self = shift;
40 0 my $hookmaster = shift;
41 0 my $conf = shift;
42  
43 0 $self->{metadb} = $hookmaster->{metadb};
44  
45 0 ▶ $hookmaster->add_hook('pagecount' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 my %args = @_;
47  
48 return $self->do_directives(
49 no_scan=>1,
50 directive=>'pagecount',
51 0 ▶ call=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 my %args2 = @_;
53  
54 0 return $self->process(directive=>'pagecount',%args2);
55 },
56 0 %args,
57 );
58 },
59 0 );
60 0 ▶ $hookmaster->add_hook('wordcount' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my %args = @_;
62  
63 return $self->do_directives(
64 no_scan=>1,
65 directive=>'wordcount',
66 0 ▶ call=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 my %args2 = @_;
68  
69 0 return $self->process(directive=>'wordcount',%args2);
70 },
71 0 %args,
72 );
73 },
74 0 );
75 0 ▶ $hookmaster->add_hook('counter' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 my %args = @_;
77  
78 return $self->do_directives(
79 no_scan=>1,
80 directive=>'counter',
81 0 ▶ call=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my %args2 = @_;
83  
84 0 return $self->process(directive=>'counter',%args2);
85 },
86 0 %args,
87 );
88 },
89 0 );
90 0 return $self;
91 } # register
92  
93 =head2 process
94  
95 Process wordcount/pagecount/counter.
96  
97 =cut
98 0 ▶sub process {
Subroutine process: not called CC=26 SCAR=65.5
Pod documented
99 0 my $self = shift;
100 0 my %args = @_;
101  
102 0 my $leaf = $args{leaf};
103 0 my $phase = $args{phase};
104 0 my $directive = $args{directive};
105 0 my @p = @{$args{params}};
106 0 my %params = @p;
107 0 my $pagename = $leaf->pagename;
108 0 my $show = $params{show};
109 0 delete $params{show};
110 0 ▶ my $map_type = (defined $params{map_type} ? $params{map_type} : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'map_type'} ? : 0 0 0
111  
112 0 ▶ if (! exists $params{pages}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"pages"} or exists $params{"titles"} or exists $params{"pagenames"} or exists $params{"where"} or exists $params{"sql"}) 0 0 0
Condition0%0/12
exists $params{"pages"} or exists $params{"titles"} or exists $params{"pagenames"} or exists $params{"where"} or exists $params{"sql"}
l!l&&r!l&&!r
000
exists $params{"pages"} or exists $params{"titles"} or exists $params{"pagenames"} or exists $params{"where"}
l!l&&r!l&&!r
000
exists $params{"pages"} or exists $params{"titles"} or exists $params{"pagenames"}
l!l&&r!l&&!r
000
exists $params{"pages"} or exists $params{"titles"}
l!l&&r!l&&!r
000
MC/DC0%0/5
exists $params{"pages"} or exists $params{"titles"} or exists $params{"pagenames"} or exists $params{"where"} or exists $params{"sql"}
exists $params{"pages"} exists $params{"titles"} exists $params{"pagenames"} exists $params{"where"} exists $params{"sql"}
Truth table0%0/6
A or B or C or D or E
A= exists $params{"pages"}B= exists $params{"titles"}C= exists $params{"pagenames"}D= exists $params{"where"}E= exists $params{"sql"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
113 and ! exists $params{titles}
114 and ! exists $params{pagenames}
115 and ! exists $params{where}
116 and ! exists $params{sql})
117 {
118 0 return "ERROR: missing parameter";
119 }
120 0 ▶ if ($phase ne $Muster::Hooks::PHASE_BUILD)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase ne $Muster::Hooks::PHASE_BUILD) 0 0 0
121 {
122 0 return "";
123 }
124  
125 # pagecount: get the count of pages matching the condition
126 # wordcount: get the sum of the wordcount of pages matching the condition
127 0 my $total = 0;
128 0 ▶ if ($directive eq 'pagecount')
Branch: 3 branches
BranchTrueFalseTotal
if ($directive eq 'pagecount') { } 0 0 0
elsif ($directive eq 'wordcount') { } 0 0 0
elsif (exists $params{'sql'}) { } 0 0 0
129 {
130 0 my @matching_pages = ();
131 0 ▶ if (exists $params{pages})
Branch: 4 branches
BranchTrueFalseTotal
if (exists $params{'pages'}) { } 0 0 0
elsif (exists $params{'titles'} and exists $params{'relto'}) { } 0 0 0
elsif (exists $params{'pagenames'}) { } 0 0 0
elsif (exists $params{'where'}) { } 0 0 0
132 {
133 0 my $pages = $self->{metadb}->query_pagespec($params{pages});
134 0 ▶ @matching_pages = @{$pages} if $pages;
Branch: 1 branch
BranchTrueFalseTotal
if $pages 0 0 0
135 }
136 ▶ elsif (exists $params{titles} and exists $params{relto})
Condition0%0/3
exists $params{"titles"} and exists $params{"relto"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params{"titles"} and exists $params{"relto"}
exists $params{"titles"} exists $params{"relto"}
Truth table0%0/3
A and B
A= exists $params{"titles"}B= exists $params{"relto"}
ABresult
0X0
100
111
137 {
138 # titles are separated by | and may have spaces which should be _
139 0 $params{titles} =~ s/ /_/g;
140 @matching_pages =
141 0 map { $self->{metadb}->bestlink($params{relto}, $_) } split /\|/, $params{titles};
142 }
143 elsif (exists $params{pagenames})
144 {
145 @matching_pages =
146 0 map { $self->{metadb}->bestlink($pagename, $_) } split ' ', $params{pagenames};
147 }
148 elsif (exists $params{where})
149 {
150 0 my $pages = $self->{metadb}->query("SELECT page FROM pagefiles WHERE " . $params{where});
151 0 ▶ @matching_pages = @{$pages} if $pages;
Branch: 1 branch
BranchTrueFalseTotal
if $pages 0 0 0
152 }
153  
154 0 $total = scalar @matching_pages;
155 }
156 elsif ($directive eq 'wordcount')
157 {
158 0 my $where = '';
159 0 ▶ if (exists $params{pages})
Branch: 4 branches
BranchTrueFalseTotal
if (exists $params{'pages'}) { } 0 0 0
elsif (exists $params{'titles'}) { } 0 0 0
elsif (exists $params{'pagenames'}) { } 0 0 0
elsif (exists $params{'where'}) { } 0 0 0
160 {
161 0 $where = $self->{metadb}->pagespec_translate($params{pages});
162 }
163 elsif (exists $params{titles})
164 {
165 # titles are separated by | and may have spaces
166 0 $where = "title REGEXP '(" . $params{titles} . ")'";
167 }
168 elsif (exists $params{pagenames})
169 {
170 # pagenames are separated by spaces
171 0 my $re = $params{pagenames};
172 0 $re =~ s/ /\|/g;
173 0 $where = "page REGEXP '($re)'";
174 }
175 elsif (exists $params{where})
176 {
177 0 $where = $params{where};
178 }
179 0 my $ret = $self->{metadb}->query("SELECT SUM(wordcount) FROM flatfields WHERE " . $where);
180 0 ▶ $total = (scalar @{$ret} ? $ret->[0] : 0);
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$ret;} ? : 0 0 0
181 }
182 elsif (exists $params{sql})
183 {
184 0 my $ret = $self->{metadb}->query($params{sql});
185 0 ▶ $total = (scalar @{$ret} ? $ret->[0] : 0);
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$ret;} ? : 0 0 0
186 }
187  
188 0 my $result = $total;
189 # only prepend if the result isn't zero
190 0 ▶ if ($params{prepend} and $total > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"prepend"} and $total > 0) 0 0 0
Condition0%0/3
$params{"prepend"} and $total > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"prepend"} and $total > 0
$params{"prepend"} $total > 0
Truth table0%0/3
A and B
A= $params{"prepend"}B= $total > 0
ABresult
0X0
100
111
191 {
192 0 $result = $params{prepend} . $result;
193 }
194 0 return $result;
195 } # process
196  
197 1 1;
« Muster/Hook/DeriveFields.pm ↑ Summary Muster/Hook/SqlReport.pm »