Muster/PagesHelper.pm

filter: Statementstmt 22.8% 32 / 140 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 41.6% 10 / 24 Podpod 100% 1 / 1 totaltotal 22.2% 43 / 193 CC 13 SCAR 24.6
CC
13
Cov
20%
_make_page_attachments_list
42.0
_make_page_related_list
20.0
_rightbar
12.0
CRAP 11.7
« Muster/Hooks.pm ↑ Summary Muster/LeafFile.pm »
1 package Muster::PagesHelper;
2 1 $Muster::PagesHelper::VERSION = '0.96';
3 #ABSTRACT: Muster::PagesHelper - helping with pages
4 =head1 NAME
5  
6 Muster::PagesHelper - helping with pages
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 SYNOPSIS
13  
14 use Muster::PagesHelper;
15  
16 =head1 DESCRIPTION
17  
18 Content management system; getting and showing pages.
19  
20 =cut
21  
22 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Muster::MetaDb;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use HTML::LinkList;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 REGISTER
34  
35 =cut
36  
37 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
38 0 my ( $self, $app, $conf ) = @_;
39  
40 0 $self->_init($app,$conf);
41  
42 0 ▶ $app->helper( 'muster_sidebar' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 0 my $c = shift;
44 0 my %args = @_;
45  
46 0 return $self->_sidebar($c,%args);
47 0 } );
48 0 ▶ $app->helper( 'muster_rightbar' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my $c = shift;
50 0 my %args = @_;
51  
52 0 return $self->_rightbar($c,%args);
53 0 } );
54  
55 0 ▶ $app->helper( 'muster_total_pages' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my $c = shift;
57 0 my %args = @_;
58  
59 0 return $self->_total_pages($c,%args);
60 0 } );
61  
62 0 ▶ $app->helper( 'muster_page_related_list' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 my $c = shift;
64 0 my %args = @_;
65  
66 0 return $self->_make_page_related_list($c,%args);
67 0 } );
68 0 ▶ $app->helper( 'muster_page_attachments_list' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my $c = shift;
70 0 my %args = @_;
71  
72 0 return $self->_make_page_attachments_list($c,%args);
73 0 } );
74  
75 0 ▶ $app->helper( 'muster_pagelist' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 my $c = shift;
77 0 my %args = @_;
78  
79 0 return $self->_pagelist($c,%args);
80 0 } );
81 }
82  
83 =head1 Helper Functions
84  
85 These are functions which are NOT exported by this plugin.
86  
87 =cut
88  
89 =head2 _init
90  
91 Initialize.
92  
93 =cut
94 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
95 0 my $self = shift;
96 0 my $app = shift;
97 0 my $conf = shift;
98  
99 0 $self->{metadb} = Muster::MetaDb->new(%{$app->config});
100 0 $self->{metadb}->init();
101 0 $self->{hookmaster} = Muster::Hooks->new();
102 0 $self->{hookmaster}->init($app->config);
103 0 return $self;
104 } # _init
105  
106 =head2 _sidebar
107  
108 Fill in the sidebar.
109 I have decided, for the sake of speed, to "hardcode" the contents of the sidebar,
110 considering that for some pages it too 2.5 seconds to load the page with an external sidebar,
111 and only .6 seconds when using the internal default.
112  
113 =cut
114  
115 0 ▶sub _sidebar {
Subroutine _sidebar: not called CC=1 SCAR=6.9
116 0 my $self = shift;
117 0 my $c = shift;
118  
119 0 my $pagename = $c->param('cpath');
120 0 $pagename =~ s!/$!!; # remove trailing slash
121  
122 0 my $out = $self->_make_page_related_list($c);
123 0 return "<nav>$out</nav>\n";
124 } # _sidebar
125  
126 =head2 _rightbar
127  
128 Fill in the rightbar.
129 I have decided, for the sake of speed, to "hardcode" the contents of the rightbar also.
130  
131 =cut
132  
133 0 ▶sub _rightbar {
Subroutine _rightbar: not called CC=3 SCAR=24.8
134 0 my $self = shift;
135 0 my $c = shift;
136  
137 0 my $pagename = $c->param('cpath');
138 0 $pagename =~ s!/$!!; # remove trailing slash
139 0 my $info = $self->{metadb}->page_or_file_info($pagename);
140 0 $pagename = $info->{pagename};
141  
142 # The source-file relative to the "page" is reached by going up
143 # because the page URL is foo/bar/page/
144 # and the page source URL is foo/bar/page.ext
145 # so the relative URL is foo/bar/page/../page.ext
146 0 my $src_file_url = '../' . $info->{hairy_name};
147 0 my $src_file_label = $info->{hairy_name};
148  
149 0 my $current_url = $c->req->url->to_abs;
150 0 my $meta_dest_url = $c->url_for("/_meta/$pagename/");
151 0 my $meta_dest_label = 'Meta';
152 0 ▶ if (defined $current_url
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_url and $current_url =~ /_meta/u) 0 0 0
Condition0%0/3
defined $current_url and $current_url =~ /_meta/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current_url and $current_url =~ /_meta/u
defined $current_url $current_url =~ /_meta/u
Truth table0%0/3
A and B
A= defined $current_urlB= $current_url =~ /_meta/u
ABresult
0X0
100
111
153 and $current_url =~ /_meta/) # we're already looking at Meta
154 {
155 0 $meta_dest_url =~ s/_meta\///;
156 0 $meta_dest_label = "Page";
157 }
158 0 my $total = $self->_total_pages($c);
159 0 my $atts = $self->_make_page_attachments_list($c);
160 0 my $out=<<EOT;
161 <p class="total">$total pages</p>
162 <p class="srcfile"><a href="$src_file_url">$src_file_label</a></p>
163 <p class="metadest"><a href="$meta_dest_url">$meta_dest_url</a></p>
164 $atts
165 EOT
166 0 return $out;
167 } # _rightbar
168  
169 =head2 _total_pages
170  
171 Return the total number of records in this db
172  
173 =cut
174  
175 0 ▶sub _total_pages {
Subroutine _total_pages: not called CC=2 SCAR=17.9
176 0 my $self = shift;
177 0 my $c = shift;
178  
179 0 my $total = $self->{metadb}->total_pages();
180 0 ▶ if (!defined $total)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $total) 0 0 0
181 {
182 0 $c->render(template => 'apperror',
183 errormsg=>"UNKNOWN");
184 0 return undef;
185 }
186 0 return $total;
187 } # _total_pages
188  
189 =head2 _make_page_attachments_list
190  
191 Make a list of related files to this page.
192  
193 =cut
194  
195 0 ▶sub _make_page_attachments_list {
Subroutine _make_page_attachments_list: not called CC=6 SCAR=37.4
196 0 my $self = shift;
197 0 my $c = shift;
198  
199 0 my $pagename = $c->param('cpath');
200 0 $pagename =~ s!/$!!; # remove trailing slash
201  
202 0 my $info = $self->{metadb}->page_or_file_info($pagename);
203 0 my $att_list = '';
204 0 ▶ if ($info and $info->{attachments})
Branch: 1 branch
BranchTrueFalseTotal
if ($info and $info->{"attachments"}) 0 0 0
Condition0%0/3
$info and $info->{"attachments"}
!ll&&!rl&&r
000
MC/DC0%0/2
$info and $info->{"attachments"}
$info $info->{"attachments"}
Truth table0%0/3
A and B
A= $infoB= $info->{"attachments"}
ABresult
0X0
100
111
205 {
206 0 my @att = ();
207 0 my %labels = ();
208 # If this is not a binary-file page
209 # just link to the basenames, since this should be relative
210 # But with a binary-file page, the "attachment" is the binary-file itself.
211 0 ▶ if ($info->{is_binary})
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'is_binary'}) { } 0 0 0
212 {
213 0 my $src_link = '../'.$info->{hairy_name};
214 0 push @att, $src_link;
215 0 $labels{$src_link} = $info->{hairy_name};
216 }
217 else
218 {
219 0 foreach my $att (@{$info->{attachments}})
220 {
221 0 my $bn = basename($att);
222 0 push @att, $bn;
223 0 $labels{$bn} = $bn;
224 }
225 }
226 0 $att_list = HTML::LinkList::link_list(
227 urls=>\@att,
228 labels=>\%labels,
229 );
230 0 ▶ $att_list = "<div><p><b>Attachments:</b></p>$att_list</div>" if $att_list;
Branch: 1 branch
BranchTrueFalseTotal
if $att_list 0 0 0
231 }
232
233 0 return $att_list;
234 } # _make_page_attachments_list
235  
236 =head2 _make_page_related_list
237  
238 Make a list of related pages to this page.
239  
240 =cut
241  
242 0 ▶sub _make_page_related_list {
Subroutine _make_page_related_list: not called CC=4 SCAR=30.0
243 0 my $self = shift;
244 0 my $c = shift;
245  
246 0 ▶ my $pagename = $c->param('cpath') || 'index';
Condition0%0/2
$c->param('cpath') || 'index'
l!l
00
MC/DC0%0/1
$c->param('cpath') || 'index'
$c->param('cpath')
Truth table0%0/2
A
A= $c->param('cpath')
Aresult
11
00
247 0 $pagename =~ s!/$!!; # remove trailing slash
248 0 my $info = $self->{metadb}->page_or_file_info($pagename);
249  
250 # get the links to the pages
251 0 my @pages = $self->{metadb}->non_hidden_pagelist();
252 # need to add slashes to ends of paths
253 0 my @paths = ();
254 0 foreach my $pn (@pages)
255 {
256 0 ▶ if ($pn !~ m{/$})
Branch: 1 branch
BranchTrueFalseTotal
unless ($pn =~ m[/$]u) 0 0 0
257 {
258 0 $pn = "/${pn}/";
259 }
260 0 push @paths, $pn;
261 }
262 0 my $link_list = HTML::LinkList::nav_tree(
263 current_url=>"/${pagename}/",
264 paths=>\@paths,
265 );
266 # figure out how get to the top page from this page
267 0 my $rel_str = File::Spec->abs2rel('/', '/'.$info->{pagename});
268 # alter the links to be relative to this page
269 0 $link_list =~ s!href="!href="${rel_str}!g;
270  
271 0 return $link_list;
272 } # _make_page_related_list
273  
274 =head2 _pagelist
275  
276 Make a pagelist
277  
278 =cut
279  
280 0 ▶sub _pagelist {
Subroutine _pagelist: not called CC=2 SCAR=17.9
281 0 my $self = shift;
282 0 my $c = shift;
283  
284 0 ▶ my $location = $c->url_for('pagelist') // "";
Condition0%0/2
$c->url_for("pagelist") // ""
l!l
00
MC/DC0%0/1
$c->url_for("pagelist") // ""
$c->url_for("pagelist")
Truth table0%0/2
A
A= $c->url_for("pagelist")
Aresult
11
00
285 # get the links to the pages
286 0 my @paths = $self->{metadb}->non_hidden_pagelist();
287  
288 0 my $link_list = HTML::LinkList::full_tree(
289 current_url=>$location,
290 paths=>\@paths,
291 );
292 0 return $link_list;
293 } # _pagelist
294  
295 1 1; # End of Muster::PagesHelper
296 __END__
« Muster/Hooks.pm ↑ Summary Muster/LeafFile.pm »