blib/lib/CatalystX/CMS.pm

filter: Statementstmt 38.6% 106 / 274 Branchbran 13.6% 18 / 132 Conditioncond 17.1% 6 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 65.5% 19 / 29 Podpod 100% 16 / 16 totaltotal 32.5% 165 / 507 CC 83 SCAR 43.1
CC
83
Cov
29%
delete
272.0
_copy_if_required
132.0
edit
90.0
CRAP 74.1
↑ Summary lib/CatalystX/CMS/File.pm »
1 package CatalystX::CMS;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use base qw( Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 __PACKAGE__->mk_accessors(qw( model_name view_name strict_html ));
11  
12 3 our $VERSION = '0.011';
13  
14 3 my $DEBUG = 0;
15  
16 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 $XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX';
19  
20 =head1 NAME
21  
22 CatalystX::CMS - drop-in content management system
23  
24 =head1 SYNOPSIS
25  
26 package MyApp;
27
28 # ...
29
30 MyApp->config(
31 cms => {
32 model_name => 'CMS',
33 view_name => 'CMS',
34 actionclass_per_action => 0,
35 use_editor => 1,
36 use_layout => 1,
37 editor => {
38 height => '300',
39 width => '550',
40 },
41 default_type => 'html',
42 default_flavour => 'default',
43 lock_period => 3600,
44 root => {
45 r => [
46 MyApp->path_to('root')
47 ],
48 rw => [
49 '/path/to/svn/workdir'
50 ]
51 }
52
53 }
54 );
55
56 MyApp->setup;
57  
58 # elsewhere, in a controller
59
60 package MyApp::Controller::Foo;
61 use strict;
62 use base qw( CatalystX::CMS::Controller );
63
64 sub bar : Local {
65 # ...
66 }
67
68 1;
69  
70 =head1 DESCRIPTION
71  
72 CatalystX::CMS is a drop-in content management system that allows
73 you to manage your Catalyst app templates via a web-based editor.
74
75 =head1 METHODS
76  
77 Only new or overridden method are documented here.
78  
79 =cut
80  
81 =head2 PUT( I<c>, I<controller>, I<cmspage> )
82  
83 Calls do_action().
84  
85 =cut
86  
87 1 ▶sub PUT {
Subroutine PUT: called CC=1 SCAR=0.0
Pod documented
88 1 my ( $self, $c, $controller, $cmspage ) = @_;
89 1 $self->do_action( $c, $controller, $cmspage );
90 }
91  
92 =head2 POST( I<c>, I<controller>, I<cmspage> )
93  
94 Calls create().
95  
96 =cut
97  
98 1 ▶sub POST {
Subroutine POST: called CC=1 SCAR=0.0
Pod documented
99 1 my ( $self, $c, $controller, $cmspage ) = @_;
100 1 $self->create( $c, $controller, $cmspage );
101 }
102  
103 =head2 GET( I<c>, I<controller>, I<cmspage> )
104  
105 Calls the method named in the stash() in I<c> under the key B<cms_mode>.
106 B<cms_mode> is typically set by CatalystX::CMS::Action->execute().
107  
108 =cut
109  
110 1 ▶sub GET {
Subroutine GET: called CC=2 SCAR=7.1
Pod documented
111 1 my ( $self, $c, $controller, $cmspage ) = @_;
112 my $method = $c->stash->{cms_mode}
113 1 ▶ or croak("no cxcms action specified");
Branch: 1 branch
BranchTrueFalseTotal
my $method = $c->stash->{"cms_mode"} or croak("no cxcms action specified") 1 0 1
114 1 $self->$method( $c, $controller, $cmspage );
115 }
116  
117 =head2 DELETE( I<c>, I<controller>, I<cmspage> )
118  
119 Calls delete().
120  
121 =cut
122  
123 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
124 0 my ( $self, $c, $controller, $cmspage ) = @_;
125 0 $self->delete( $c, $controller, $cmspage );
126 }
127  
128 =head2 unlock
129  
130 TODO
131
132 =cut
133  
134 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=0.0
Pod documented
135  
136 }
137  
138 =head2 get_user( I<c> )
139  
140 Returns a username to associate with the lock on a file.
141  
142 If I<c> has a C<user> method (as if using Catalyst::Plugin::Authentication),
143 calls the user->id method chain.
144  
145 Otherwise, returns anonymous.
146  
147 =cut
148  
149 3 ▶sub get_user {
Subroutine get_user: called CC=4 SCAR=15.6
Pod documented
150 3 my ( $self, $c ) = @_;
151 3 ▶ my $user
Branch: 1 branch
BranchTrueFalseTotal
$c->can('user') && defined $c->user ? : 0 3 3
Condition33%1/3
$c->can("user") and defined $c->user
!ll&&!rl&&r
300
MC/DC0%0/2
$c->can("user") and defined $c->user
$c->can("user") defined $c->user
Truth table33%1/3
A and B
A= $c->can("user")B= defined $c->user
ABresult
0X0
100
111
152 = ( $c->can('user') && defined $c->user )
153 ? $c->user->id
154 : 'anonymous';
155 3 ▶ $c->log->debug("CMS user = $user") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 3 3
156 3 return $user;
157 }
158  
159 =head2 history( I<c>, I<controller>, I<cmspage> )
160  
161 Available as a B<cms_mode> method.
162  
163 Display svn history of I<cmspage>.
164  
165 =cut
166  
167 0 ▶sub history {
Subroutine history: not called CC=2 SCAR=17.9
Pod documented
168 0 my ( $self, $c, $controller, $cmspage ) = @_;
169  
170 0 ▶ if ( my $log = $cmspage->log( ['--xml'] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $cmspage->log(['--xml'])) { } 0 0 0
171  
172 0 my %res;
173 $res{history}
174 = XMLin( join( '', @$log ), ForceArray => [qw( logentry )] )
175 0 ->{logentry};
176 0 $res{template} = 'cms/svn/history.tt';
177  
178 0 return \%res;
179  
180 }
181 else {
182 0 croak( "could not get svn log for " . $cmspage->url );
183 }
184 }
185  
186 =head2 diff( I<c>, I<controller>, I<cmspage> )
187  
188 Available as a B<cms_mode> method.
189  
190 Display svn diff of I<cmspage>.
191 If the C<rev> request param is present, the difference between
192 that value and the previous revision is used.
193  
194 =cut
195  
196 0 ▶sub diff {
Subroutine diff: not called CC=5 SCAR=34.0
Pod documented
197 0 my ( $self, $c, $controller, $cmspage ) = @_;
198  
199 0 my $rev = $c->req->params->{rev};
200  
201 0 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) { } 0 0 0
202 0 ▶ if ( $rev =~ m/\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev =~ /\D/) 0 0 0
203 0 croak("bad changeset value $rev");
204 0 return;
205 }
206  
207 0 my $start = $rev - 1;
208  
209 0 ▶ $cmspage->diff( ["-r$start:$rev"] )
Branch: 1 branch
BranchTrueFalseTotal
$cmspage->diff(["-r${start}:$rev"]) or croak("could not diff for ${start}:$rev") 0 0 0
210 or croak("could not diff for $start:$rev");
211 }
212 else {
213 0 ▶ $cmspage->diff or croak("could not diff $cmspage");
Branch: 1 branch
BranchTrueFalseTotal
$cmspage->diff or croak("could not diff $cmspage") 0 0 0
214 }
215  
216 0 my %res;
217 0 $res{diff} = $cmspage->stdout;
218 0 $res{template} = 'cms/svn/diff.tt';
219 0 return \%res;
220 }
221  
222 =head2 blame( I<c>, I<controller>, I<cmspage> )
223  
224 Available as a B<cms_mode> method.
225  
226 Display svn blame of I<cmspage>.
227  
228 =cut
229  
230 0 ▶sub blame {
Subroutine blame: not called CC=7 SCAR=40.3
Pod documented
231 0 my ( $self, $c, $controller, $cmspage ) = @_;
232 0 ▶ $cmspage->blame or croak("Can't blame for $cmspage");
Branch: 1 branch
BranchTrueFalseTotal
$cmspage->blame or croak("Can't blame for $cmspage") 0 0 0
233 0 my $buf = $cmspage->stdout;
234 0 my @blame;
235 0 my $oldest_rev = 0;
236 0 for my $line (@$buf) {
237 0 my ( $rev, $who, $txt ) = ( $line =~ m/^\s+(\d+)\s+(\S+)\s+(.+)$/ );
238  
239 # TODO $who is always going to be the webserver user
240 # so we need to get the log for that rev to correctly report
241  
242 0 ▶ next unless $rev;
Branch: 1 branch
BranchTrueFalseTotal
unless $rev 0 0 0
243 0 ▶ $oldest_rev ||= $rev;
Condition0%0/3
$oldest_rev ||= $rev
l!l&&r!l&&!r
000
MC/DC0%0/2
$oldest_rev ||= $rev
$oldest_rev $rev
Truth table0%0/3
A ||= B
A= $oldest_revB= $rev
ABresult
1X1
011
000
244 0 ▶ $oldest_rev = $rev if ( $oldest_rev && $oldest_rev > $rev );
Branch: 1 branch
BranchTrueFalseTotal
if $oldest_rev and $oldest_rev > $rev 0 0 0
Condition0%0/3
$oldest_rev and $oldest_rev > $rev
!ll&&!rl&&r
000
MC/DC0%0/2
$oldest_rev and $oldest_rev > $rev
$oldest_rev $oldest_rev > $rev
Truth table0%0/3
A and B
A= $oldest_revB= $oldest_rev > $rev
ABresult
0X0
100
111
245 0 push( @blame, { rev => $rev, who => $who, txt => $txt } );
246 }
247 0 my %res;
248 0 $res{blame} = \@blame;
249 0 $res{oldest_rev} = $oldest_rev;
250 0 $res{template} = 'cms/svn/blame.tt';
251 0 return \%res;
252 }
253  
254 =head2 create( I<c>, I<controller>, I<cmspage> )
255  
256 Available only via HTTP POST.
257  
258 Calls create() method on I<cmspage> passing get_user() for lock owner.
259  
260 Sets redirect uri for edit().
261  
262 =cut
263  
264 2 ▶sub create {
Subroutine create: called CC=3 SCAR=11.3
Pod documented
265 2 my ( $self, $c, $controller, $cmspage ) = @_;
266 2 my %res;
267  
268 # must be a POST request
269 2 ▶ unless ( uc( $c->req->method ) eq 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (uc $c->req->method eq "POST") 1 1 2
270 1 $res{body} = 'Bad HTTP request. Must POST to create a new CMS page.';
271 1 $res{status} = 400;
272 1 return \%res;
273 }
274  
275 1 eval { $cmspage->create( $self->get_user($c) ) };
276 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
277 0 $c->log->error($@);
278 0 $c->error($@);
279 0 return;
280 }
281  
282 # "pure" REST response would be status 201
283 # but we're dealing with browsers so return the 30x redirect URI
284 1 $res{uri} = $c->uri_for( $cmspage->url, { 'cxcms' => 'edit' } );
285 1 return \%res;
286 }
287  
288 3 my %wrappers = map { $_ => 1 } qw( wrapper header footer body );
289  
290 0 ▶sub _copy_if_required {
Subroutine _copy_if_required: not called CC=11 SCAR=48.8
291 0 my ( $self, $c, $controller, $cmspage ) = @_;
292  
293 0 my $file = $cmspage->bare_file;
294  
295 0 ▶ $DEBUG and warn "copy_if_required $file";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "copy_if_required $file" 0 0 0
296  
297 0 ▶ $c->log->debug("checking if we should make copy of $file")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
298 if $c->debug;
299  
300 0 ▶ if ( ( exists $wrappers{$file} and !-s $cmspage )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $wrappers{$file} and not -s $cmspage or $cmspage->copy) 0 0 0
Condition0%0/6
exists $wrappers{$file} and not -s $cmspage or $cmspage->copy
l!l&&r!l&&!r
000
exists $wrappers{$file} and not -s $cmspage
!ll&&!rl&&r
000
MC/DC0%0/3
exists $wrappers{$file} and not -s $cmspage or $cmspage->copy
exists $wrappers{$file} not -s $cmspage $cmspage->copy
Truth table0%0/5
A and not(B) or C
A= exists $wrappers{$file}B= not -s $cmspageC= $cmspage->copy
ABCresult
11X1
0X11
1011
0X00
1000
301 or $cmspage->copy )
302 {
303  
304 #carp dump $cmspage;
305  
306 # make a local copy to edit
307 0 ▶ $c->log->debug("making local copy of $file") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
308 0 my $class = $cmspage->delegate_class;
309 0 my $view = $c->view( $self->view_name );
310 0 my $tt_root = $view->cms_template_base;
311 0 my $model = $c->model( $self->model_name );
312 0 my $orig = $model->new_object(
313 file => $cmspage->file,
314 ext => $cmspage->ext
315 );
316 0 $orig->{type} = $cmspage->type;
317 0 $orig->{flavour} = $cmspage->flavour;
318  
319 0 ▶ if ( !-s $cmspage ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -s $cmspage) { } 0 0 0
320 0 $orig = $model->find_page_in_inc( $orig,
321 $tt_root->subdir( 'cms', 'wrappers' ) );
322  
323 0 ▶ if ( !-s $orig ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $orig) 0 0 0
324 0 croak("no such original file at $orig");
325 }
326  
327 }
328 else {
329  
330 # cmspage exists, we just want a local copy to edit
331  
332 #carp dump $cmspage;
333 0 $orig->{delegate} = $cmspage->{delegate};
334 $cmspage->{delegate} = $cmspage->delegate_class->new(
335 path => Path::Class::file(
336 $c->config->{cms}->{root}->{rw}->[0],
337 $orig->{type},
338 $orig->{flavour},
339 0 $cmspage->file . $cmspage->ext
340 )
341 );
342 0 $cmspage->{delegate}->dir->mkpath;
343  
344 }
345  
346 # make the copy
347 0 ▶ $c->log->debug("orig: $orig") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
348 0 ▶ $c->log->debug("copy: $cmspage") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
349 0 my $fh = $cmspage->delegate->openw;
350 0 my $buf = $orig->slurp;
351 0 print $fh $buf;
352 0 $fh->close;
353 0 $cmspage->delegate->_parse_page($buf);
354  
355 }
356  
357 }
358  
359 =head2 edit( I<c>, I<controller>, I<cmspage> )
360  
361 Available as a B<cms_mode> method.
362  
363 Verifies lock on I<cmspage>, extending it if necessary, and
364 displays the cms editor template.
365  
366 =cut
367  
368 0 ▶sub edit {
Subroutine edit: not called CC=9 SCAR=45.0
Pod documented
369 0 my ( $self, $c, $controller, $cmspage ) = @_;
370  
371 # can't edit a file we haven't yet created
372 # unless it is one of our own CMS/tt/cms/wrappers/* files
373 0 $self->_copy_if_required( $c, $controller, $cmspage );
374  
375 0 ▶ unless ( -s $cmspage ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $cmspage) 0 0 0
376 0 croak( $cmspage->file . " does not exist" );
377 }
378  
379 # lock the file immediately so no one else can edit it.
380 # only the current user can unlock it, either by saving
381 # changes or by explicitly calling the cancel url.
382  
383 # could be a redirect from create() so check that owner == $user
384 0 my $user = $self->get_user($c);
385 0 ▶ if ( $cmspage->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmspage->locked) { } 0 0 0
386  
387 # how old is the lock?
388 0 ▶ my $lock_period = $c->config->{cms}->{lock_period} || 3600;
Condition0%0/2
$c->config->{'cms'}{'lock_period'} || 3600
l!l
00
MC/DC0%0/1
$c->config->{'cms'}{'lock_period'} || 3600
$c->config->{'cms'}{'lock_period'}
Truth table0%0/2
A
A= $c->config->{'cms'}{'lock_period'}
Aresult
11
00
389 0 ▶ if ( ( $cmspage->lock_time + $lock_period ) < time() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmspage->lock_time + $lock_period < time) { } 0 0 0
elsif ($cmspage->lock_owner ne $user) { } 0 0 0
390 0 $c->log->debug("lock expired on $cmspage");
391 0 $cmspage->lock($user);
392 }
393 elsif ( $cmspage->lock_owner ne $user ) {
394 0 croak( "$cmspage is already locked by " . $cmspage->lock_owner );
395 }
396 else {
397  
398 # extend the lock every time we hit this url
399 0 ▶ if ( $cmspage->unlock && $cmspage->lock($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmspage->unlock and $cmspage->lock($user)) { } 0 0 0
Condition0%0/3
$cmspage->unlock and $cmspage->lock($user)
!ll&&!rl&&r
000
MC/DC0%0/2
$cmspage->unlock and $cmspage->lock($user)
$cmspage->unlock $cmspage->lock($user)
Truth table0%0/3
A and B
A= $cmspage->unlockB= $cmspage->lock($user)
ABresult
0X0
100
111
400 0 $c->log->debug("$cmspage lock re-acquired by $user");
401 }
402 else {
403 0 croak("failed to re-acquire $cmspage lock for $user");
404 }
405 }
406 }
407 else {
408 0 $cmspage->lock($user);
409 }
410  
411 0 ▶ $DEBUG and warn "svn update $cmspage";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "svn update $cmspage" 0 0 0
412  
413 # make sure we're working on the latest copy,
414 # in case the repository is non-local and we're
415 # behind a load balancer (for example)
416 #$cmspage->up; # **not** ->update
417  
418 0 my %res;
419 0 $res{template} = 'cms/yui/editor.tt';
420 0 return \%res;
421 }
422  
423 1 ▶sub _filter_text {
Subroutine _filter_text: called CC=1 SCAR=0.0
424 1 my ( $self, $text_ref ) = @_;
425  
426 # return number of s/// changes to $$text_ref
427 1 my $n = 0;
428  
429 1 $n += $$text_ref =~ s/<code class="tt">//g;
430 1 $n += $$text_ref =~ s/<\/code>//g;
431 1 $n += $$text_ref =~ s/<br>/\n/g;
432  
433 # pesky Win32 line-endings
434 1 $n += $$text_ref =~ s/\r\n/\n/g;
435  
436 1 return $n;
437 }
438  
439 1 ▶sub _get_attrs {
Subroutine _get_attrs: called CC=9 SCAR=33.0
440 1 my ( $self, $c, $cmspage ) = @_;
441  
442 1 for my $key ( sort keys %{ $cmspage->attrs } ) {
443 1 ▶ if ( defined $c->req->params->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->req->params->{$key}) 0 1 1
444 0 $cmspage->attrs->{$key} = $c->req->params->{$key};
445 }
446 }
447  
448 # new ad hoc attrs
449 1 for my $new_attrs_name ( grep {m/^new_attr_name_\d+$/} $c->req->param ) {
450 0 my ($id) = ( $new_attrs_name =~ m/_(\d+)$/ );
451 0 ▶ my $key = $c->req->params->{$new_attrs_name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $c->req->params->{$new_attrs_name} or next 0 0 0
452 0 ▶ my $val = $c->req->params->{ 'new_attr_val_' . $id } || '';
Condition0%0/2
$c->req->params->{'new_attr_val_' . $id} || ''
l!l
00
MC/DC0%0/1
$c->req->params->{'new_attr_val_' . $id} || ''
$c->req->params->{'new_attr_val_' . $id}
Truth table0%0/2
A
A= $c->req->params->{'new_attr_val_' . $id}
Aresult
11
00
453 0 $key =~ s/\W/_/g;
454 0 ▶ next if $key eq 'attrs_name'; # generic name
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "attrs_name" 0 0 0
455 0 $cmspage->attrs->{$key} = $val;
456 }
457  
458 # some minimal attrs required.
459 1 ▶ $cmspage->attrs->{owner} ||= $self->get_user($c);
Condition33%1/3
$cmspage->attrs->{"owner"} ||= $self->get_user($c)
l!l&&r!l&&!r
100
MC/DC0%0/2
$cmspage->attrs->{"owner"} ||= $self->get_user($c)
$cmspage->attrs->{"owner"} $self->get_user($c)
Truth table33%1/3
A ||= B
A= $cmspage->attrs->{"owner"}B= $self->get_user($c)
ABresult
1X1
011
000
460 1 ▶ $cmspage->attrs->{title} ||= 'no title';
Condition50%1/2
$cmspage->attrs->{"title"} ||= "no title"
l!l
01
MC/DC0%0/1
$cmspage->attrs->{"title"} ||= "no title"
$cmspage->attrs->{"title"}
Truth table50%1/2
A
A= $cmspage->attrs->{"title"}
Aresult
11
00
461 }
462  
463 =head2 do_action( I<c>, I<controller>, I<cmspage> )
464  
465 Called by PUT(). Calls B<cms_mode> like GET() does,
466 but checks lock on I<cmspage> first.
467  
468 do_action() is typically called for save()ing a I<cmspage>.
469  
470 =cut
471  
472 1 ▶sub do_action {
Subroutine do_action: called CC=7 SCAR=22.0
Pod documented
473 1 my ( $self, $c, $controller, $cmspage ) = @_;
474  
475 1 my $text = $c->req->params->{text};
476 1 ▶ if ( !defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 1 1
477 0 croak('text param required');
478 }
479 1 ▶ my $action = $c->stash->{cms_mode} || 'save';
Condition50%1/2
$c->stash->{'cms_mode'} || 'save'
l!l
10
MC/DC0%0/1
$c->stash->{'cms_mode'} || 'save'
$c->stash->{'cms_mode'}
Truth table50%1/2
A
A= $c->stash->{'cms_mode'}
Aresult
11
00
480 1 my $user = $self->get_user($c);
481  
482 # is this file locked by this user?
483 1 ▶ if ( !$cmspage->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->locked) 0 1 1
484 0 croak( $cmspage->url . " is not locked for editing." );
485 }
486  
487 1 ▶ if ( $cmspage->lock_owner ne $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmspage->lock_owner ne $user) 0 1 1
488 0 croak( $cmspage->url . " is not locked by $user." );
489 }
490  
491 # YUI editor adds HTML <br> to represent \n
492 # so filter those and any others.
493 1 my $filter += $self->_filter_text( \$text );
494  
495 1 $self->_get_attrs( $c, $cmspage );
496 1 $cmspage->content($text);
497  
498 1 ▶ $c->log->debug("Action = $action") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
499 1 ▶ if ( $self->can($action) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($action)) { } 1 0 1
500 1 return $self->$action( $c, $controller, $cmspage );
501 }
502 else {
503 0 croak("Bad action: $action");
504 }
505  
506 }
507  
508 =head2 cancel( I<c>, I<controller>, I<cmspage> )
509  
510 Available as a B<cms_mode> method.
511  
512 Reverts any local changes to I<cmspage> and unlocks the page.
513  
514 If I<cmspage> has never been committed to the svn repository,
515 the file will be removed.
516  
517 =cut
518  
519 0 ▶sub cancel {
Subroutine cancel: not called CC=8 SCAR=42.8
Pod documented
520 0 my ( $self, $c, $controller, $cmspage ) = @_;
521  
522 0 my $stat = $cmspage->status;
523  
524 # if file has local mods (or flagged for addition)
525 # then remove/revert it and update.
526 0 ▶ if ( $stat ne '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat ne '?') { } 0 0 0
527 0 ▶ if ( $stat eq 'A' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stat eq 'A') { } 0 0 0
elsif ($stat eq 'M') { } 0 0 0
528 0 ▶ unless ( $cmspage->revert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->revert) 0 0 0
529 0 croak("Could not revert $cmspage");
530 0 return 0;
531 }
532  
533 # TODO should page be removed too?
534  
535 }
536 elsif ( $stat eq 'M' ) {
537 0 ▶ unless ( $cmspage->remove ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->remove) 0 0 0
538 0 croak("Could not remove modified $cmspage");
539 0 return 0;
540 }
541  
542 0 ▶ unless ( $cmspage->up ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->up) 0 0 0
543 0 croak("Count not svn update $cmspage");
544 0 return 0;
545 }
546 }
547 }
548 else {
549  
550 # if svn knows nothing about the file, just remove it
551 0 ▶ unless ( $cmspage->remove ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->remove) 0 0 0
552 0 croak("Could not remove $cmspage");
553 0 return 0;
554 }
555 }
556  
557 # unlock the page
558 0 $cmspage->unlock;
559  
560 # show the original
561 0 my %res;
562 0 $res{uri} = $c->uri_for( $cmspage->url );
563 0 return \%res;
564 }
565  
566 =head2 validate( I<c>, I<controller>, I<cmspage> )
567  
568 Called by save() to verify that I<cmspage> has correct
569 TT syntax.
570  
571 If C<strict_html> mode is set and the type() of I<cmspage> is C<html>
572 XML::LibXML is used to verify that I<cmspage> parses correctly.
573  
574 =cut
575  
576 1 ▶sub validate {
Subroutine validate: called CC=6 SCAR=23.9
Pod documented
577 1 my ( $self, $c, $controller, $cmspage ) = @_;
578  
579 # attempt to render the file to test TT syntax
580 1 my $view = $c->view( $self->view_name );
581 1 my $buf = $cmspage->content;
582 1 $c->stash( cmspage => $cmspage );
583  
584 # turn off debugging statement since we pass scalar ref instead
585 # of file name
586 1 ▶ $c->log->debug("Validating content of $cmspage") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
587 1 $c->log->disable('debug');
588 1 my $out = $view->render( $c, \$buf );
589 1 $c->log->enable('debug');
590  
591 #$c->log->debug("test render returned: $out") if $c->debug;
592  
593 1 ▶ if ( UNIVERSAL::isa( $out, 'Template::Exception' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($out, "Template::Exception")) 0 1 1
594 0 $c->error("Failed to render $cmspage");
595 0 $c->error("Template error: $out");
596 0 return;
597 }
598  
599 # optional: attempt to parse the file with an HTML parser
600 1 ▶ if ( $cmspage->type eq 'html' && $self->strict_html ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmspage->type eq "html" and $self->strict_html) 0 1 1
Condition33%1/3
$cmspage->type eq "html" and $self->strict_html
!ll&&!rl&&r
010
MC/DC0%0/2
$cmspage->type eq "html" and $self->strict_html
$cmspage->type eq "html" $self->strict_html
Truth table33%1/3
A and B
A= $cmspage->type eq "html"B= $self->strict_html
ABresult
0X0
100
111
601 0 my $parser = XML::LibXML->new();
602 0 eval { $parser->parse_html_string( $out, { recover => 1 } ); };
603 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
604 0 $c->error("strict HTML mode failed: $@");
605 0 return;
606 }
607 }
608  
609 1 return 1;
610 }
611  
612 =head2 save( I<c>, I<controller>, I<cmspage> )
613  
614 Available as a B<cms_mode> method.
615  
616 Typically called by do_action() via PUT().
617  
618 =cut
619  
620 1 ▶sub save {
Subroutine save: called CC=6 SCAR=20.7
Pod documented
621 1 my ( $self, $c, $controller, $cmspage ) = @_;
622  
623 1 ▶ return unless $self->validate( $c, $controller, $cmspage );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate($c, $controller, $cmspage) 0 1 1
624  
625 1 my $user = $self->get_user($c);
626 1 my $stat = $cmspage->save("edit by $user");
627 1 my $url = $cmspage->url;
628  
629 1 my %res;
630 1 ▶ if ( $stat && $stat > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat and $stat > 0) { } 0 1 1
Condition33%1/3
$stat and $stat > 0
!ll&&!rl&&r
010
MC/DC0%0/2
$stat and $stat > 0
$stat $stat > 0
Truth table33%1/3
A and B
A= $statB= $stat > 0
ABresult
0X0
100
111
631 0 ▶ $c->log->debug("save returned $stat") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
632 0 $res{message} = "Committed $url as change $stat";
633 }
634 else {
635 1 ▶ $c->log->debug("save stat $stat indicates no change to $cmspage")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
636 if $c->debug;
637 1 $res{message} = "No changes made to $url";
638 }
639  
640 # TODO what if $url is a wrapper part?
641 1 $res{uri} = $c->uri_for($url);
642 1 return \%res;
643 }
644  
645 =head2 preview( I<c>, I<controller>, I<cmspage> )
646  
647 Available as a B<cms_mode> method.
648  
649 Like save() but with no commit() and preserves lock
650  
651 =cut
652  
653 0 ▶sub preview {
Subroutine preview: not called CC=2 SCAR=17.9
Pod documented
654 0 my ( $self, $c, $controller, $cmspage ) = @_;
655  
656 0 ▶ return unless $self->validate( $c, $controller, $cmspage );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate($c, $controller, $cmspage) 0 0 0
657  
658 # force write since we own lock and want to keep it.
659 0 $cmspage->update(1);
660  
661 0 return { template => $cmspage };
662  
663 }
664  
665 =head2 delete( I<c>, I<controller>, I<cmspage> )
666  
667 Available as a B<cms_mode> method.
668  
669 Called by DELETE(). Removes lock on I<cmspage> and
670 deletes the file from the local workspace B<and from the svn
671 repository>. See also cancel().
672  
673 =cut
674  
675 0 ▶sub delete {
Subroutine delete: not called CC=16 SCAR=56.1
Pod documented
676 0 my ( $self, $c, $controller, $cmspage ) = @_;
677  
678 0 ▶ if ( $cmspage->locked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmspage->locked) 0 0 0
679 0 ▶ $cmspage->unlock or croak("can't unlock $cmspage");
Branch: 1 branch
BranchTrueFalseTotal
$cmspage->unlock or croak("can't unlock $cmspage") 0 0 0
680 }
681  
682 0 my $user = $self->get_user($c);
683 0 my $stat = $cmspage->status;
684 0 ▶ if ($stat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat) { } 0 0 0
685  
686 # if the page has local mods, then remove working copy,
687 # svn update, and then svn delete
688  
689 0 ▶ if ( $stat eq 'M' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat eq "M") 0 0 0
690 0 ▶ unless ( $cmspage->remove ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->remove) 0 0 0
691 0 croak("Could not remove modified $cmspage");
692 }
693  
694 0 ▶ unless ( $cmspage->up ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->up) 0 0 0
695 0 croak("Could not svn update $cmspage");
696 }
697  
698 0 ▶ unless ( $cmspage->delete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->delete) 0 0 0
699 0 croak("Could not svn delete $cmspage");
700 }
701  
702 0 ▶ unless ( $cmspage->commit("deleted by $user") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->commit("deleted by $user")) 0 0 0
703 0 croak("Could not commit delete of $cmspage");
704 }
705 }
706  
707 # if the page is new, revert it and then remove
708 0 ▶ if ( $stat eq 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat eq "A") 0 0 0
709 0 ▶ unless ( $cmspage->revert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->revert) 0 0 0
710 0 croak("Could not revert $cmspage");
711 0 return 0;
712 }
713  
714 0 ▶ unless ( $cmspage->remove ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->remove) 0 0 0
715 0 croak("Could not remove $cmspage");
716 0 return 0;
717 }
718 }
719  
720 # if svn knows nothing about the file, just remove it
721 0 ▶ if ( $stat eq '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat eq "?") 0 0 0
722 0 ▶ unless ( $cmspage->remove ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->remove) 0 0 0
723 0 croak("Could not remove $cmspage");
724 0 return 0;
725 }
726 }
727 }
728 else {
729  
730 # no local mods. svn delete it.
731 0 ▶ unless ( $cmspage->delete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->delete) 0 0 0
732 0 croak("Count not svn delete $cmspage");
733 0 return 0;
734 }
735  
736 0 ▶ unless ( $cmspage->commit("deleted by $user") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmspage->commit("deleted by $user")) 0 0 0
737 0 croak("Could not commit delete of $cmspage");
738 0 return 0;
739 }
740 }
741  
742 0 my %res;
743 0 $res{message} = $cmspage->url . " deleted";
744 0 return \%res;
745 }
746  
747 3 1;
748  
749 __END__
↑ Summary lib/CatalystX/CMS/File.pm »