ZofCMS/Plugin/YouTube.pm

filter: Statementstmt 21.1% 23 / 109 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 43.7% 7 / 16 Podpod n/a totaltotal 13.1% 30 / 229 CC 39 SCAR 46.1
CC
39
Cov
12%
_do
156.0
_process_add_form
156.0
_get_video_list
110.0
CRAP 100.7
« ZofCMS/Template.pm ↑ Summary ZofCMS/Plugin/DateSelector.pm »
1 package App::ZofCMS::Plugin::YouTube;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.001010'; # VERSION
7  
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use base 'App::ZofCMS::Plugin::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub _key { 'plug_youtube' }
Subroutine _key: not called CC=1 SCAR=6.9
15  
16 0 ▶sub _defaults {
Subroutine _defaults: not called CC=1 SCAR=6.9
17 return (
18 #dsn => "DBI:mysql:database=test;host=localhost",
19 0 user => '',
20 pass => '',
21 opt => { RaiseError => 1, AutoCommit => 1 },
22 t_name => 'plug_youtube',
23 table => 'videos',
24 create_table => 0,
25 h_level => 3,
26 size => 1,
27 no_form => 0,
28 no_list => 0,
29 allow_edit => 0,
30 ua_args => [
31 agent => 'Opera 9.2',
32 timeout => 30,
33 ],
34 # filter => {
35 # title => qr/Foo/,
36 # description => qr/Bar/,
37 # link => qr/234fd343/,
38 # },
39 );
40 }
41  
42 0 ▶sub _do {
Subroutine _do: not called CC=12 SCAR=50.5
43 0 my ( $self, $conf, $template, $query ) = @_;
44  
45 return if
46 $conf->{no_form}
47 0 ▶ and $conf->{no_list};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"no_form"} and $conf->{"no_list"} 0 0 0
Condition0%0/3
$conf->{"no_form"} and $conf->{"no_list"}
!ll&&!rl&&r
000
MC/DC0%0/2
$conf->{"no_form"} and $conf->{"no_list"}
$conf->{"no_form"} $conf->{"no_list"}
Truth table0%0/3
A and B
A= $conf->{"no_form"}B= $conf->{"no_list"}
ABresult
0X0
100
111
48  
49 0 ▶ if ( not ref $conf->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $conf->{"size"}) 0 0 0
50 0 my @sizes = (
51 [ 320, 265 ],
52 [ 425, 344 ],
53 [ 480, 385 ],
54 [ 640, 505 ],
55 );
56 0 ▶ if ( not defined $conf->{size} or $conf->{size} > $#sizes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $conf->{'size'} or $conf->{'size'} > $#sizes) { } 0 0 0
Condition0%0/3
not defined $conf->{"size"} or $conf->{"size"} > $#sizes
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $conf->{"size"} or $conf->{"size"} > $#sizes
not defined $conf->{"size"} $conf->{"size"} > $#sizes
Truth table0%0/3
not(A) or B
A= not defined $conf->{"size"}B= $conf->{"size"} > $#sizes
ABresult
1X1
011
000
57 0 $conf->{size} = [ 320, 262 ];
58 }
59 else {
60 0 $conf->{size} = $sizes[ $conf->{size} ];
61 }
62 }
63  
64 0 ▶ if ( $conf->{create_table} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"create_table"}) 0 0 0
65 my $dbh = DBI->connect_cached(
66 0 @$conf{ qw/dsn user pass opt/ },
67 );
68  
69 0 $dbh->do(
70 "CREATE TABLE $conf->{table} (
71 title TEXT,
72 link TEXT,
73 description TEXT,
74 embed TEXT,
75 time VARCHAR(10),
76 id TEXT
77 );",
78 );
79 }
80  
81 0 my %t_names = (
82 form => "$conf->{t_name}_form",
83 list => "$conf->{t_name}_list",
84 );
85  
86 $conf->{no_form}
87 0 ▶ or $template->{t}{ $t_names{form} } = $self->_process_add_form( $conf, $query );
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"no_form"} or $template->{"t"}{$t_names{"form"}} = $self->_process_add_form($conf, $query) 0 0 0
88  
89 0 ▶ if ( $query->{plug_youtube_vid_edit_action} and $conf->{allow_edit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"plug_youtube_vid_edit_action"} and $conf->{"allow_edit"}) 0 0 0
Condition0%0/3
$query->{"plug_youtube_vid_edit_action"} and $conf->{"allow_edit"}
!ll&&!rl&&r
000
MC/DC0%0/2
$query->{"plug_youtube_vid_edit_action"} and $conf->{"allow_edit"}
$query->{"plug_youtube_vid_edit_action"} $conf->{"allow_edit"}
Truth table0%0/3
A and B
A= $query->{"plug_youtube_vid_edit_action"}B= $conf->{"allow_edit"}
ABresult
0X0
100
111
90 $query->{plug_youtube_vid_edit_action} eq 'Delete'
91 0 ▶ and $self->_delete_video( $conf, $query->{plug_youtube_vid_edit_id} );
Branch: 1 branch
BranchTrueFalseTotal
$query->{"plug_youtube_vid_edit_action"} eq "Delete" and $self->_delete_video($conf, $query->{"plug_youtube_vid_edit_id"}) 0 0 0
92 }
93  
94 $conf->{no_list}
95 0 ▶ or $template->{t}{ $t_names{list} } = $self->_get_video_list( $conf, $query );
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"no_list"} or $template->{"t"}{$t_names{"list"}} = $self->_get_video_list($conf, $query) 0 0 0
96 }
97  
98 0 ▶sub _delete_video {
Subroutine _delete_video: not called CC=1 SCAR=6.9
99 0 my ( $self, $conf, $id ) = @_;
100  
101 my $dbh = DBI->connect_cached(
102 0 @$conf{ qw/dsn user pass opt/ },
103 );
104  
105 0 $dbh->do(
106 "DELETE FROM $conf->{table} WHERE id = ?;",
107 undef,
108 $id,
109 );
110 }
111  
112 0 ▶sub _get_video_list {
Subroutine _get_video_list: not called CC=10 SCAR=47.0
113 0 my ( $self, $conf, $query ) = @_;
114  
115 0 my $t = HTML::Template->new_scalar_ref( \ _list_template() );
116  
117 my $dbh = DBI->connect_cached(
118 0 @$conf{ qw/dsn user pass opt/ },
119 );
120  
121 0 my $vids = $dbh->selectall_arrayref(
122 "SELECT * FROM $conf->{table};",
123 { Slice => {} },
124 );
125  
126 0 ▶ $vids ||= [];
Condition0%0/2
$vids ||= []
l!l
00
MC/DC0%0/1
$vids ||= []
$vids
Truth table0%0/2
A
A= $vids
Aresult
11
00
127  
128 0 @$vids = sort { $b->{time} <=> $a->{time} } @$vids;
129  
130 0 for ( @$vids ) {
131 0 $_->{time} = localtime $_->{time};
132 0 encode_entities $_->{description};
133 0 $_->{description} =~ s/\n/<br>/g;
134  
135 $_->{embed}
136 0 =~ s|(width=")\d+("\s+height=")\d+"|$1$conf->{size}[0]$2$conf->{size}[1]"|g;
137 }
138  
139 0 ▶ if ( $conf->{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"filter"}) 0 0 0
140 0 ▶ if ( defined $conf->{filter}{title} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"filter"}{"title"}) 0 0 0
141 0 @$vids = grep $_ =~ /$conf->{filter}{title}/, @$vids;
142 }
143 0 ▶ if ( defined $conf->{filter}{description} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"filter"}{"description"}) 0 0 0
144 0 @$vids = grep $_ =~ /$conf->{filter}{description}/, @$vids;
145 }
146 0 ▶ if ( defined $conf->{filter}{link} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"filter"}{"link"}) 0 0 0
147 0 @$vids = grep $_ =~ /$conf->{filter}{link}/, @$vids;
148 }
149 }
150  
151 0 ▶ if ( @$vids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$vids) 0 0 0
152 my @loop = map +{
153 ( $_%2 ? ( alt_class => 1 ) : () ),
154 h_level => $conf->{h_level},
155 link => $vids->[$_]{link},
156 title => $vids->[$_]{title},
157 time => $vids->[$_]{time},
158 embed => $vids->[$_]{embed},
159 description => $vids->[$_]{description},
160 id => $vids->[$_]{id},
161 page => $query->{page},
162 dir => $query->{dir},
163 0 ▶ ( $conf->{allow_edit} ? ( edit => 1 ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$_ % 2 ? : 0 0 0
$conf->{'allow_edit'} ? : 0 0 0
164 }, 0.. $#$vids;
165  
166 0 $t->param(
167 has_vids => 1,
168 vids => \@loop,
169 );
170  
171 }
172  
173 0 return $t->output;
174 }
175  
176 0 ▶sub _process_add_form {
Subroutine _process_add_form: not called CC=12 SCAR=50.5
177 0 my ( $self, $conf, $query ) = @_;
178  
179 0 my $t = HTML::Template->new_scalar_ref( \ _form_template() );
180  
181 0 ▶ if ( defined $query->{plug_youtube_vid_edit_action}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query->{"plug_youtube_vid_edit_action"} and $conf->{"allow_edit"} and $query->{"plug_youtube_vid_edit_action"} eq "Edit") 0 0 0
Condition0%0/6
defined $query->{"plug_youtube_vid_edit_action"} and $conf->{"allow_edit"} and $query->{"plug_youtube_vid_edit_action"} eq "Edit"
!ll&&!rl&&r
000
defined $query->{"plug_youtube_vid_edit_action"} and $conf->{"allow_edit"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $query->{"plug_youtube_vid_edit_action"} and $conf->{"allow_edit"} and $query->{"plug_youtube_vid_edit_action"} eq "Edit"
defined $query->{"plug_youtube_vid_edit_action"} $conf->{"allow_edit"} $query->{"plug_youtube_vid_edit_action"} eq "Edit"
Truth table0%0/4
A and B and C
A= defined $query->{"plug_youtube_vid_edit_action"}B= $conf->{"allow_edit"}C= $query->{"plug_youtube_vid_edit_action"} eq "Edit"
ABCresult
0XX0
10X0
1100
1111
182 and $conf->{allow_edit}
183 and $query->{plug_youtube_vid_edit_action} eq 'Edit'
184 ) {
185 my $dbh = DBI->connect_cached(
186 0 @$conf{ qw/dsn user pass opt/ },
187 );
188  
189 my $vids = $dbh->selectall_arrayref(
190 "SELECT * FROM $conf->{table} WHERE id = ?",
191 { Slice => {} },
192 $query->{plug_youtube_vid_edit_id},
193 0 );
194  
195 0 ▶ if ( @$vids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$vids) 0 0 0
196 $dbh->do(
197 "DELETE FROM $conf->{table} WHERE id = ?",
198 undef,
199 $query->{plug_youtube_vid_edit_id},
200 0 );
201 @$query{ qw/plug_youtube_title plug_youtube_link plug_youtube_description/ }
202 0 = @{ $vids->[0] }{ qw/title link description/ };
203 }
204 }
205  
206 $t->param(
207 0 map +( $_ => $query->{$_} ), qw/
208 page
209 dir
210 plug_youtube_title
211 plug_youtube_link
212 plug_youtube_description
213 /
214 );
215  
216 0 ▶ if ( $query->{plug_youtube_submit}
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"plug_youtube_submit"} and $self->_check_add_form($query, $t)) 0 0 0
Condition0%0/3
$query->{"plug_youtube_submit"} and $self->_check_add_form($query, $t)
!ll&&!rl&&r
000
MC/DC0%0/2
$query->{"plug_youtube_submit"} and $self->_check_add_form($query, $t)
$query->{"plug_youtube_submit"} $self->_check_add_form($query, $t)
Truth table0%0/3
A and B
A= $query->{"plug_youtube_submit"}B= $self->_check_add_form($query, $t)
ABresult
0X0
100
111
217 and $self->_check_add_form( $query, $t )
218 ) {
219 0 my $uri = $query->{plug_youtube_link};
220 0 ▶ $uri = "http://$uri"
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^http://]i 0 0 0
221 unless $uri =~ m{^http://}i;
222  
223 0 ▶ my $ua = LWP::UserAgent->new( @{ $conf->{ua_args} || [] } );
Condition0%0/2
$conf->{'ua_args'} || []
l!l
00
MC/DC0%0/1
$conf->{'ua_args'} || []
$conf->{'ua_args'}
Truth table0%0/2
A
A= $conf->{'ua_args'}
Aresult
11
00
224 0 my $response = $ua->get( $uri );
225  
226 0 ▶ if ( not $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success) { } 0 0 0
227 0 $t->param( error => $response->status_line );
228 }
229 else {
230 0 ▶ my ( $code ) = $response->content =~ /
Branch: 1 branch
BranchTrueFalseTotal
my($code) = $response->content =~ / <input\s+id="embed_code"\s+name="embed_code"\s+type="text"\s+value=' (&lt;object[^']+) /xi or $t->param("error", "Server Error") and return $t->output 0 0 0
Condition0%0/5
(my($code) = $response->content =~ / <input\s+id="embed_code"\s+name="embed_code"\s+type="text"\s+value=' (&lt;object[^']+) /xi) || $t->param('error', 'Server Error') && (return $t->output)
l!l&&r!l&&!r
000
$t->param("error", "Server Error") and return $t->output
!ll
00
MC/DC0%0/2
(my($code) = $response->content =~ / <input\s+id="embed_code"\s+name="embed_code"\s+type="text"\s+value=' (&lt;object[^']+) /xi) || $t->param('error', 'Server Error') && (return $t->output)
(my($code) = $response->content =~ / <input\s+id="embed_code"\s+name="embed_code"\s+type="text"\s+value=' (&lt;object[^']+) /xi) $t->param("error", "Server Error")
Truth table0%0/3
A || B
A= (my($code) = $response->content =~ / <input\s+id="embed_code"\s+name="embed_code"\s+type="text"\s+value=' (&lt;object[^']+) /xi)B= $t->param("error", "Server Error")
ABresult
1X1
011
000
231 <input\s+id="embed_code"\s+name="embed_code"\s+type="text"\s+value='
232 (&lt;object[^']+)
233 /xi
234 or $t->param( error => 'Server Error' )
235 and return $t->output;
236  
237 0 decode_entities $code;
238  
239 my $dbh = DBI->connect_cached(
240 0 @$conf{ qw/dsn user pass opt/ },
241 );
242  
243 $dbh->do(
244 "INSERT INTO $conf->{table} VALUES(?, ?, ?, ?, ?, ?);",
245 undef,
246 @$query{ qw/
247 plug_youtube_title
248 plug_youtube_link
249 plug_youtube_description
250 /},
251 $code,
252 time(),
253 0 do { my $id = rand() . time() . rand(); $id =~ tr/.//d; $id },
254 );
255  
256 0 $t->param( form_ok => 1 );
257 }
258 }
259  
260 0 return $t->output;
261 }
262  
263 0 ▶sub _check_add_form {
Subroutine _check_add_form: not called CC=8 SCAR=42.8
264 0 my ( $self, $query, $t ) = @_;
265  
266 0 my $error;
267 0 ▶ unless ( defined $query->{plug_youtube_title}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query->{"plug_youtube_title"} and length $query->{"plug_youtube_title"}) 0 0 0
Condition0%0/3
defined $query->{"plug_youtube_title"} and length $query->{"plug_youtube_title"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $query->{"plug_youtube_title"} and length $query->{"plug_youtube_title"}
defined $query->{"plug_youtube_title"} length $query->{"plug_youtube_title"}
Truth table0%0/3
A and B
A= defined $query->{"plug_youtube_title"}B= length $query->{"plug_youtube_title"}
ABresult
0X0
100
111
268 and length $query->{plug_youtube_title}
269 ) {
270 0 $error = q|Missing 'Title' parameter|;
271 }
272  
273 0 ▶ unless ( defined $query->{plug_youtube_link}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query->{"plug_youtube_link"} and length $query->{"plug_youtube_link"}) 0 0 0
Condition0%0/3
defined $query->{"plug_youtube_link"} and length $query->{"plug_youtube_link"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $query->{"plug_youtube_link"} and length $query->{"plug_youtube_link"}
defined $query->{"plug_youtube_link"} length $query->{"plug_youtube_link"}
Truth table0%0/3
A and B
A= defined $query->{"plug_youtube_link"}B= length $query->{"plug_youtube_link"}
ABresult
0X0
100
111
274 and length $query->{plug_youtube_link}
275 ) {
276 0 $error = q|Missing 'Link' parameter|;
277 }
278  
279 0 ▶ unless ( $query->{plug_youtube_link} =~ /youtube[.]com/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query->{"plug_youtube_link"} =~ /youtube[.]com/) 0 0 0
280 0 $error = q|Incorrect YouTube link|;
281 }
282  
283 0 ▶ unless ( defined $query->{description} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query->{"description"}) 0 0 0
284 0 $query->{description} = '';
285 }
286  
287 0 ▶ if ( defined $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
288 0 $t->param( error => $error );
289 0 return;
290 }
291 0 return 1;
292 }
293  
294 0 ▶sub _form_template {
Subroutine _form_template: not called CC=1 SCAR=6.9
295 0 return <<'END_HTML_TEMPLATE_CODE';
296 <tmpl_if name="form_ok">
297 <p>Video was successfully added.</p>
298 <p><a href="/index.pl?page=<tmpl_var escape='html' name='page'>&dir=<tmpl_var escape='html' name='dir'>">Add another video</a></p>
299 <tmpl_else>
300 <form action="" method="POST" id="plug_youtube_form">
301 <div>
302 <tmpl_if name="error"><p class="error"><tmpl_var escape='html' name='error'></tmpl_if>
303 <input type="hidden" name="page" value="<tmpl_var escape='html' name='page'>">
304 <input type="hidden" name="dir" value="<tmpl_var escape='html' name='dir'>">
305 <ul>
306 <li>
307 <label for="plug_youtube_title">Title: </label
308 ><input type="text" id="plug_youtube_title" name="plug_youtube_title" value="<tmpl_var escape='html' name='plug_youtube_title'>">
309 </li>
310 <li>
311 <label for="plug_youtube_link">Link: </label
312 ><input type="text" id="plug_youtube_link" name="plug_youtube_link" value="<tmpl_var escape='html' name='plug_youtube_link'>">
313 </li>
314 <li>
315 <label for="plug_youtube_description">Description: </label
316 ><textarea id="plug_youtube_description" name="plug_youtube_description" cols="60" rows="10"><tmpl_var escape='html' name='plug_youtube_description'></textarea>
317 </li>
318 </ul>
319 <input type="submit" name="plug_youtube_submit" value="Add">
320 </div>
321 </form>
322 </tmpl_if>
323 END_HTML_TEMPLATE_CODE
324  
325 }
326  
327 0 ▶sub _list_template {
Subroutine _list_template: not called CC=1 SCAR=6.9
328 0 return <<'END_HTML_TEMPLATE_CODE';
329 <tmpl_if name='has_vids'>
330 <ul id="plug_youtube_list">
331 <tmpl_loop name='vids'>
332 <li<tmpl_if name='alt_class'> class="alt"</tmpl_if>>
333 <h<tmpl_var name='h_level'>><a href="<tmpl_var escape='html' name='link'>"><tmpl_var escape='html' name='title'></a></h<tmpl_var name='h_level'>>
334 <p class="plug_youtube_time">Posted on: <tmpl_var name='time'></p>
335 <div class="plug_youtube_video"><tmpl_var name='embed'></div>
336 <p class="plug_youtube_description"><tmpl_var name='description'></p>
337 <tmpl_if name='edit'>
338 <form action="" method="POST">
339 <div>
340 <input type="hidden" name="plug_youtube_vid_edit_id" value="<tmpl_var escape='html' name='id'>">
341 <input type="hidden" name="page" value="<tmpl_var escape='html' name='page'>">
342 <input type="hidden" name="dir" value="<tmpl_var escape='html' name='dir'>">
343 <input type="submit" class="input_submit submit_button_edit" name="plug_youtube_vid_edit_action" value="Edit">
344 <input type="submit" class="input_submit submit_button_delete" name="plug_youtube_vid_edit_action" value="Delete">
345 </div>
346 </form>
347 </tmpl_if>
348 </li>
349 </tmpl_loop>
350 </ul>
351 <tmpl_else>
352 <p class="plug_youtube_no_vids">Currently there are no videos.</p>
353 </tmpl_if>
354 END_HTML_TEMPLATE_CODE
355 }
356  
357 1 1;
358 __END__
« ZofCMS/Template.pm ↑ Summary ZofCMS/Plugin/DateSelector.pm »