Muster/Scanner.pm

filter: Statementstmt 27.7% 30 / 108 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 9 / 18 Podpod 100% 3 / 3 totaltotal 25.7% 42 / 163 CC 19 SCAR 28.0
CC
19
Cov
22%
__ANON__
42.0
__ANON__
30.0
scan_some_pagefiles
20.0
CRAP 16.4
« Muster/LeafFile/mdwn.pm ↑ Summary Muster/Hooks.pm »
1 package Muster::Scanner;
2 2 $Muster::Scanner::VERSION = '0.96';
3 #ABSTRACT: Muster::Scanner - updating meta-data about pages
4 =head1 NAME
5  
6 Muster::Scanner - updating meta-data about pages
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 DESCRIPTION
13  
14 Content Management System
15 keeping meta-data about pages.
16  
17 =cut
18  
19 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Muster::MetaDb;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Module::Pluggable search_path => ['Muster::Hook'], instantiate => 'new';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶has command => sub { croak "command is not defined" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
30  
31 =head1 METHODS
32  
33 =head2 init
34  
35 Set the defaults for the object if they are not defined already.
36  
37 =cut
38 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
39 0 my $self = shift;
40 0 my $app = $self->command->app;
41  
42 0 $self->{page_dirs} = [];
43 0 foreach my $pd (@{$app->config->{page_dirs}})
44 {
45 0 my $pages_dir = File::Spec->rel2abs($pd);
46 0 ▶ if (-d $pages_dir)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $pages_dir) { } 0 0 0
47 {
48 0 push @{$self->{page_dirs}}, $pages_dir;
49 }
50 else
51 {
52 0 croak "pages dir '$pages_dir' not found!";
53 }
54 }
55  
56 0 $self->{metadb} = Muster::MetaDb->new(%{$app->config});
57 0 $self->{metadb}->init();
58  
59 0 $self->{hookmaster} = Muster::Hooks->new();
60 0 $self->{hookmaster}->init($app->config);
61  
62 0 return $self;
63 } # init
64  
65 =head2 scan_some_pagefiles
66  
67 Scan a set of pagefiles; this is NOT going to be all the pages, just some of them.
68 This expects the name of a file relative to the page_dir the file is in.
69  
70 $self->scan_some_pagefiles(@files);
71  
72 =cut
73  
74 0 ▶sub scan_some_pagefiles {
Subroutine scan_some_pagefiles: not called CC=4 SCAR=30.0
Pod documented
75 0 my $self = shift;
76 0 my @files = @_;
77  
78 0 my %the_pages = ();
79 0 foreach my $file (@files)
80 {
81 0 my $leaf = $self->_scan_one_pagefile($file);
82 0 ▶ if (!defined $leaf)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $leaf) { } 0 0 0
83 {
84 # the page was not found, therefore it has been deleted
85 0 my $pagename = $file;
86 0 $pagename =~ s/\.\w+$//; # remove the extension to get the pagename
87 0 ▶ if ($self->{metadb}->delete_one_page($pagename))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'metadb'}->delete_one_page($pagename)) { } 0 0 0
88 {
89 0 warn "DELETED: $file\n";
90 }
91 else
92 {
93 0 warn "UNKNOWN: $file\n";
94 }
95 }
96 else
97 {
98 0 $the_pages{$leaf->pagename} = $leaf->meta;
99 }
100 }
101 0 $self->{metadb}->update_some_pages(%the_pages);
102  
103 0 $self->{metadb}->update_derived_tables(pages=>{%the_pages});
104 } # scan_some_pagefiles
105  
106 =head2 scan_all
107  
108 Scan all pages.
109  
110 =cut
111  
112 0 ▶sub scan_all {
Subroutine scan_all: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114  
115 0 $self->_find_and_scan_all();
116  
117 0 $self->{metadb}->update_derived_tables();
118 } # scan_all
119  
120 =head1 Helper Functions
121  
122 These are private to the module.
123  
124 =head2 _scan_one_pagefile
125  
126 Scan a single pagefile; this expects the name of a file
127 relative to the page_dir the file is in.
128 This returns the leaf with all the relevant data in it.
129  
130 my $leaf = $self->_scan_one_pagefile($filename);
131  
132 =cut
133  
134 0 ▶sub _scan_one_pagefile {
Subroutine _scan_one_pagefile: not called CC=3 SCAR=24.8
135 0 my $self = shift;
136 0 my $filename = shift;
137  
138 0 ▶ return if !$filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
139  
140 0 my $found_leaf;
141 0 foreach my $page_dir (@{$self->{page_dirs}})
142 {
143 0 ▶ my $finder = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
144  
145 # only interested in files with extensions
146 0 ▶ if ($File::Find::name =~ /\.\w+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Find::name =~ /\.\w+$/u) 0 0 0
147 {
148 0 my $pagefile = File::Spec->catfile($page_dir, $filename);
149  
150 0 ▶ if (-f -r $File::Find::name and $File::Find::name eq $pagefile)
Branch: 1 branch
BranchTrueFalseTotal
if (-f -r $File::Find::name and $File::Find::name eq $pagefile) 0 0 0
Condition0%0/3
-f -r $File::Find::name and $File::Find::name eq $pagefile
!ll&&!rl&&r
000
MC/DC0%0/2
-f -r $File::Find::name and $File::Find::name eq $pagefile
-f -r $File::Find::name $File::Find::name eq $pagefile
Truth table0%0/3
A and B
A= -f -r $File::Find::nameB= $File::Find::name eq $pagefile
ABresult
0X0
100
111
151 {
152 0 warn "SCANNING: $File::Find::name\n";
153 0 my $leaf = $self->_create_and_scan_leaf(
154 page_dir=>$page_dir,
155 filename=>$File::Find::name,
156 dir=>$File::Find::dir,
157 );
158 0 ▶ if ($leaf)
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf) 0 0 0
159 {
160 0 ▶ if (!$found_leaf)
Branch: 1 branch
BranchTrueFalseTotal
if (not $found_leaf) 0 0 0
161 {
162 0 $found_leaf = $leaf;
163 }
164 }
165 }
166 }
167 0 };
168 # Using no_chdir because reclassify needs to "require" modules
169 # and the current @INC might just be relative
170 0 find({wanted=>$finder, no_chdir=>1}, $page_dir);
171 }
172  
173 0 return $found_leaf;
174  
175 } # _scan_one_pagefile
176  
177 =head2 _find_and_scan_all
178  
179 Use File::Find to find and scan all page files..
180  
181 =cut
182  
183 0 ▶sub _find_and_scan_all {
Subroutine _find_and_scan_all: not called CC=2 SCAR=17.9
184 0 my $self = shift;
185  
186 0 my %all_pages = ();
187  
188 # We do this in a loop per page-directory
189 # because we need to know what the current page_dir is
190 # in order to calculate what the pagename ought to be
191 # which means we need to define the "wanted" function
192 # inside the loop so that it knows the value of $page_dir
193 #
194 # Note that if a page has already been found, any later pages are ignored
195 0 foreach my $page_dir (@{$self->{page_dirs}})
196 {
197 0 ▶ my $finder = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
198  
199 # skip hidden files
200 0 ▶ if (-f -r $File::Find::name and $File::Find::name !~ /(^\.|\/\.)/)
Branch: 1 branch
BranchTrueFalseTotal
if (-f -r $File::Find::name and not $File::Find::name =~ m[(^\.|/\.)]u) 0 0 0
Condition0%0/3
-f -r $File::Find::name and not $File::Find::name =~ m[(^\.|/\.)]u
!ll&&!rl&&r
000
MC/DC0%0/2
-f -r $File::Find::name and not $File::Find::name =~ m[(^\.|/\.)]u
-f -r $File::Find::name not $File::Find::name =~ m[(^\.|/\.)]u
Truth table0%0/3
A and not(B)
A= -f -r $File::Find::nameB= not $File::Find::name =~ m[(^\.|/\.)]u
ABresult
0X0
100
111
201 {
202 0 warn "SCANNING: $File::Find::name\n";
203 0 my $leaf = $self->_create_and_scan_leaf(
204 page_dir=>$page_dir,
205 filename=>$File::Find::name,
206 dir=>$File::Find::dir,
207 );
208 0 ▶ if ($leaf)
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf) 0 0 0
209 {
210 0 my $page = $leaf->pagename;
211 0 ▶ if (!exists $all_pages{$page})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $all_pages{$page}) 0 0 0
212 {
213 0 $all_pages{$page} = $leaf->meta;
214 }
215 }
216 }
217 0 };
218 # Using no_chdir because reclassify needs to "require" modules
219 # and the current @INC might just be relative
220 0 find({wanted=>$finder, no_chdir=>1}, $page_dir);
221 }
222  
223 0 $self->{metadb}->update_all_pages(%all_pages);
224 } # _find_and_scan_all
225  
226 =head2 _create_and_scan_leaf
227  
228 Create and scan a leaf (which contains meta-data and content).
229
230 $leaf = $self->_create_and_scan_leaf(page_dir=>$page_dir, filename=>$File::Find::name, dir=>$File::Find::dir);
231  
232 =cut
233  
234 0 ▶sub _create_and_scan_leaf {
Subroutine _create_and_scan_leaf: not called CC=2 SCAR=17.9
235 0 my $self = shift;
236 0 my %args = @_;
237  
238 0 my $page_dir = $args{page_dir};
239 0 my $filename = $args{filename};
240 0 my $dir = $args{dir};
241  
242 # -------------------------------------------
243 # Create
244 # -------------------------------------------
245 0 my $parent_page = $dir;
246 0 $parent_page =~ s!${page_dir}!!;
247 0 $parent_page =~ s!^/!!; # remove the leading /
248  
249 0 my $leaf = Muster::LeafFile->new(
250 filename => $filename,
251 parent_page => $parent_page,
252 );
253 0 $leaf = $leaf->reclassify();
254 0 ▶ if (!$leaf)
Branch: 1 branch
BranchTrueFalseTotal
unless ($leaf) 0 0 0
255 {
256 0 croak "ERROR: leaf did not reclassify\n";
257 }
258  
259 0 return $self->{hookmaster}->run_hooks(leaf=>$leaf,
260 command=>$self->command,
261 phase=>$Muster::Hooks::PHASE_SCAN);
262 } # _create_and_scan_leaf
263  
264 2 1; # End of Muster::Scanner
265 __END__
« Muster/LeafFile/mdwn.pm ↑ Summary Muster/Hooks.pm »