lib/MojoMojo/Schema/Result/Page.pm

filter: Statementstmt 87.0% 87 / 100 Branchbran 81.8% 18 / 22 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 14 / 16 Podpod 100% 11 / 11 totaltotal 86.1% 131 / 152 CC 14 SCAR 13.0
CC
14
Cov
85%
update_content
7.0
add_version
6.0
descendants
4.0
CRAP 3.7
« lib/MojoMojo/Formatter/YouTube.pm ↑ Summary lib/MojoMojo/WordDiff.pm »
1 package MojoMojo::Schema::Result::Page;
2  
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use parent qw/MojoMojo::Schema::Base::Result/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 __PACKAGE__->load_components("Core");
10 40 __PACKAGE__->table("page");
11 40 __PACKAGE__->add_columns(
12 "id",
13 {
14 data_type => "INTEGER",
15 is_nullable => 0,
16 size => undef,
17 is_auto_increment => 1
18 },
19 "version",
20 { data_type => "INTEGER", is_nullable => 1, size => undef },
21 "parent",
22 { data_type => "INTEGER", is_nullable => 1, size => undef },
23 "name",
24 { data_type => "VARCHAR", is_nullable => 1, size => 200 },
25 "name_orig",
26 { data_type => "VARCHAR", is_nullable => 1, size => 200 },
27 "depth",
28 { data_type => "INTEGER", is_nullable => 1, size => undef },
29 "lft",
30 { data_type => "INTEGER", is_nullable => 1, size => undef },
31 "rgt",
32 { data_type => "INTEGER", is_nullable => 1, size => undef },
33 "content_version",
34 { data_type => "INTEGER", is_nullable => 1, size => undef },
35 );
36 40 __PACKAGE__->set_primary_key("id");
37 40 __PACKAGE__->add_unique_constraint( "page_unique_child_index",
38 [ "parent", "name" ] );
39 40 __PACKAGE__->has_many(
40 "wantedpages",
41 "MojoMojo::Schema::Result::WantedPage",
42 { "foreign.from_page" => "self.id" }
43 );
44 40 __PACKAGE__->belongs_to(
45 "parent",
46 "MojoMojo::Schema::Result::Page",
47 { id => "parent" }
48 );
49 40 __PACKAGE__->has_many(
50 "children",
51 "MojoMojo::Schema::Result::Page",
52 { "foreign.parent" => "self.id" }
53 );
54 40 __PACKAGE__->belongs_to(
55 "content",
56 "MojoMojo::Schema::Result::Content",
57 { page => "id", version => "content_version" }
58 );
59 40 __PACKAGE__->has_many(
60 "versions",
61 "MojoMojo::Schema::Result::Content",
62 { "foreign.page" => "self.id" },
63 { order_by => 'version desc' }
64 );
65 40 __PACKAGE__->belongs_to(
66 "page_version",
67 "MojoMojo::Schema::Result::PageVersion",
68 { page => "id", version => "version" }
69 );
70 40 __PACKAGE__->has_many(
71 "tags",
72 "MojoMojo::Schema::Result::Tag",
73 { "foreign.page" => "self.id" }
74 );
75 40 __PACKAGE__->has_many(
76 "links_from",
77 "MojoMojo::Schema::Result::Link",
78 { "foreign.from_page" => "self.id" }
79 );
80 40 __PACKAGE__->has_many(
81 "links_to",
82 "MojoMojo::Schema::Result::Link",
83 { "foreign.to_page" => "self.id" }
84 );
85 40 __PACKAGE__->has_many(
86 "roleprivileges",
87 "MojoMojo::Schema::Result::RolePrivilege",
88 { "foreign.page" => "self.id" }
89 );
90 40 __PACKAGE__->has_many(
91 "attachments",
92 "MojoMojo::Schema::Result::Attachment",
93 { "foreign.page" => "self.id" },
94 { order_by => 'name asc' }
95 );
96 40 __PACKAGE__->has_many(
97 "comments",
98 "MojoMojo::Schema::Result::Comment",
99 { "foreign.page" => "self.id" }
100 );
101 40 __PACKAGE__->has_many(
102 "journals",
103 "MojoMojo::Schema::Result::Journal",
104 { "foreign.pageid" => "self.id" }
105 );
106  
107 =head1 NAME
108  
109 MojoMojo::Schema::Result::Page - store pages
110  
111 =head1 METHODS
112  
113 =cut
114  
115 =head2 update_content <%args>
116  
117 Create a new content version for this page.
118  
119 %args is each column of L<MojoMojo::Schema::Result::Content>.
120  
121 =cut
122  
123 # update_content: this whole method may need work to deal with workflow.
124 # maybe it can't even be called if the site uses workflow...
125 # may need fixing for better conflict handling, too. maybe use a transaction?
126  
127 6 ▶sub update_content {
Subroutine update_content: called CC=7 SCAR=19.5
Pod documented
128 6 my ( $self, %args ) = @_;
129  
130 6 ▶ my $content_version = (
Branch: 1 branch
BranchTrueFalseTotal
$self->content ? : 1 5 6
131 $self->content
132 ? $self->content->max_version()
133 : undef
134 );
135 my %content_data =
136 6 map { $_ => $args{$_} }
137 $self->result_source->related_source('content')->columns;
138 6 my $now = DateTime->now;
139 6 ▶ @content_data{qw/page version status release_date/} = (
Branch: 1 branch
BranchTrueFalseTotal
$content_version ? : 1 5 6
140 $self->id, ( $content_version ? $content_version + 1 : 1 ),
141 'released', $now,
142 );
143 6 my $content =
144 $self->result_source->related_source('content')
145 ->resultset->create( \%content_data );
146 6 $self->content_version( $content->version );
147 6 $self->update;
148  
149 6 ▶ $self->page_version->content_version_first($content_version)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->page_version->content_version_first 6 0 6
150 unless defined $self->page_version->content_version_first;
151 6 $self->page_version->content_version_last($content_version);
152 6 $self->page_version->update;
153  
154 6 ▶ if ( my $previous_content = $content->previous ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $previous_content = $content->previous) { } 1 5 6
155 1 $previous_content->remove_date($now);
156 1 $previous_content->status('removed');
157 1 $previous_content->comments("Replaced by version $content_version.");
158 1 $previous_content->update;
159 }
160 else {
161 5 $self->result_source->resultset->set_paths($self);
162 }
163 6 foreach my $want_me ( $self->result_source->schema->resultset('WantedPage')
164 ->search( { to_path => $self->path } ) )
165 {
166 1 my $wantme_page = $want_me->from_page;
167  
168 # convert the wanted into links
169 1 $self->result_source->schema->resultset('Link')->create(
170 {
171 from_page => $wantme_page,
172 to_page => $self,
173 }
174 );
175  
176 # clear the precompiled (will be recompiled on view)
177 1 ▶ if ( my $wantme_content = $wantme_page->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wantme_content = $wantme_page->content) 1 0 1
178 1 $wantme_content->precompiled(undef);
179 1 $wantme_content->update;
180 }
181  
182 # ok, she don't want me anymore ;)
183 1 $want_me->delete();
184 }
185  
186 } # end sub update_content
187  
188 =head2 add_version
189  
190 my $page_version_new = $page->add_version(
191 creator => $user_id,
192 name_orig => $page_new_name,
193 );
194  
195 Arguments: %replacementdata
196  
197 Returns: The new L<PageVersion|MojoMojo::Schema::Result::PageVersion>
198 object.
199
200 Creates a new page version by cloning the latest version (hence pointing
201 to the same content), and replacing its values with data in the replacement
202 hash.
203  
204 Used for renaming pages.
205  
206 =cut
207  
208 0 ▶sub add_version {
Subroutine add_version: not called CC=2 SCAR=17.9
Pod documented
209 0 my ( $self, %args ) = @_;
210 0 my $now = DateTime->now;
211  
212 0 my $page_version_last = $self->page_version->latest_version();
213  
214 # clone the last version and update fields passed in %args
215 my %page_version_data = map {
216 0 exists $args{$_}
217 0 ▶ ? ( $_ => $args{$_} )
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} ? : 0 0 0
218 : ( $_ => $page_version_last->$_ )
219 } $self->result_source->related_source('page_version')->columns;
220  
221 0 delete $args{creator}; # creator is a field in page_version, not in page
222  
223 # for the new version, set the version number, status, and release date
224 @page_version_data{
225 0 qw/
226 version status release_date/
227 }
228 = ( $page_version_last->version + 1, 'released', $now );
229  
230 0 my $page_version_new;
231  
232 # commit the new version to the database and update the previously last version to indicate its removal
233 $self->result_source->schema->txn_do(
234 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
235  
236 0 $page_version_new =
237 $self->result_source->related_source('page_version')
238 ->resultset->create( \%page_version_data );
239  
240 $page_version_last->update(
241 {
242 remove_date => $now,
243 status => 'removed',
244 comments => 'Replaced by version '
245 . $page_version_data{version}
246 }
247 0 );
248  
249 0 $self->update( \%args );
250 }
251 0 );
252  
253 0 return $page_version_new;
254 }
255  
256 =head2 tagged_descendants($tag)
257  
258 Return descendants with the given tag, ordered by name.
259  
260 =cut
261  
262 2 ▶sub tagged_descendants {
Subroutine tagged_descendants: called CC=1 SCAR=0.0
Pod documented
263 2 my ( $self, $tag ) = @_;
264 2 my (@pages) = $self->result_source->resultset->search(
265 {
266 'ancestor.id' => $self->id,
267 'tag' => $tag,
268 -or => [
269 'me.id' => \'=ancestor.id',
270 -and =>
271 [ 'me.lft', \'> ancestor.lft', 'me.rgt', \'< ancestor.rgt', ],
272 ],
273  
274 'me.id' => \'=tag.page',
275 'content.page' => \'=me.id',
276 'content.version' => \'=me.content_version',
277 },
278 {
279 distinct => 1,
280 from => "page as me, page as ancestor, tag, content",
281 order_by => 'me.name',
282 }
283 )->all;
284 2 return $self->result_source->resultset->set_paths(@pages);
285 }
286  
287 =head2 tagged_descendants_by_date
288  
289 Return descendants with the given tag, ordered by creation time, most
290 recent first.
291  
292 =cut
293  
294 1 ▶sub tagged_descendants_by_date {
Subroutine tagged_descendants_by_date: called CC=1 SCAR=0.0
Pod documented
295 1 my ( $self, $tag ) = @_;
296 1 my (@pages) = $self->result_source->resultset->search(
297 {
298 'ancestor.id' => $self->id,
299 'tag' => $tag,
300 -or => [
301 'me.id' => \'=ancestor.id',
302 -and =>
303 [ 'me.lft', \'> ancestor.lft', 'me.rgt', \'< ancestor.rgt', ],
304 ],
305 'me.id' => \'=tag.page',
306 'content.page' => \'=me.id',
307 'content.version' => \'=me.content_version',
308 },
309 {
310 columns => [
311 'me.id', 'me.version',
312 'me.parent', 'me.name',
313 'me.name_orig', 'me.depth',
314 'me.lft', 'me.rgt',
315 'me.content_version', 'content.created'
316 ],
317 distinct => 1,
318 from => "page as me, page as ancestor, tag, content",
319 order_by => 'content.created DESC',
320 }
321 );
322 1 return $self->result_source->resultset->set_paths(@pages);
323 }
324  
325 =head2 descendants
326  
327 @descendants = $page->descendants( [$resultset_page] );
328  
329 In list context, returns all descendants of this page (no paging), including
330 the page itself. In scalar context, returns the resultset object.
331  
332 If the optional $resultset_page is passed, returns that page from the
333 L<resultset|DBIx::Class::ResultSet>.
334  
335 =cut
336  
337 7 ▶sub descendants {
Subroutine descendants: called CC=4 SCAR=13.9
Pod documented
338 7 my ( $self, $resultset_page ) = @_;
339  
340 7 ▶ my $rs = $self->result_source->resultset->search(
Branch: 1 branch
BranchTrueFalseTotal
$resultset_page ? : 1 6 7
Condition50%1/2
$resultset_page or 1
l!l
10
MC/DC0%0/1
$resultset_page or 1
$resultset_page
Truth table50%1/2
A
A= $resultset_page
Aresult
11
00
341 {
342 'ancestor.id' => $self->id,
343 -or => [
344 'ancestor.id' => \'=me.id',
345 -and => [
346 'me.lft' => \'> ancestor.lft',
347 'me.rgt' => \'< ancestor.rgt',
348 ]
349 ],
350 },
351 {
352 $resultset_page ? ( page => $resultset_page || 1, rows => 20 ) : (),
353 from => 'page me, page ancestor',
354 order_by => ['me.name']
355 }
356 )
357 ; # an empty arrayref if there are no results because we'll dereference in the 'return'
358  
359 return wantarray
360 7 ▶ ? $self->result_source->resultset->set_paths( $rs->all )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 1 7
361 : $rs;
362 }
363  
364 =head2 descendants_by_date
365  
366 @descendants = $page->descendants_by_date;
367  
368 Like L</descendants>, but returns pages sorted by the dates of their
369 last content release dates and pages results (20 per page).
370  
371 =cut
372  
373 5 ▶sub descendants_by_date {
Subroutine descendants_by_date: called CC=1 SCAR=0.0
Pod documented
374 5 my $self = shift;
375 5 my @pages = $self->result_source->resultset->search(
376 {
377 'ancestor.id' => $self->id,
378 'content.page' => \'= me.id',
379 'content.version' => \'= me.content_version',
380 -or => [
381 -and => [
382 'me.lft' => \'> ancestor.lft',
383 'me.rgt' => \'< ancestor.rgt'
384 ],
385 'ancestor.id' => \'= me.id',
386 ]
387 },
388 {
389 rows => 20,
390 page => 1,
391 from => 'page as me, page as ancestor, content',
392 order_by => 'content.created DESC'
393 }
394 );
395 5 return $self->result_source->resultset->set_paths(@pages);
396 }
397  
398 =head2 user_tags($user)
399  
400 Return popular tags for this page used C<$user>.
401  
402 =cut
403  
404 13 ▶sub user_tags {
Subroutine user_tags: called CC=1 SCAR=0.0
Pod documented
405 13 my ( $self, $user ) = @_;
406 13 my (@tags) =
407 $self->result_source->related_source('tags')->resultset->search(
408 {
409 page => $self->id,
410 person => $user,
411 },
412 {
413 select => [ 'me.tag', 'count(me.tag) as refcount' ],
414 as => [ 'tag', 'refcount' ],
415 order_by => ['refcount'],
416 group_by => ['me.tag'],
417 }
418 );
419 13 return @tags;
420 }
421  
422 =head2 others_tags($user)
423  
424 Return popular tags for this page used by other people than C<$user>.
425  
426 =cut
427  
428 13 ▶sub others_tags {
Subroutine others_tags: called CC=1 SCAR=0.0
Pod documented
429 13 my ( $self, $user ) = @_;
430 13 my (@tags) =
431 $self->result_source->related_source('tags')->resultset->search(
432 {
433 page => $self->id,
434 person => { '!=', $user }
435 },
436 {
437 select => [ 'me.tag', 'count(me.tag) as refcount' ],
438 as => [ 'tag', 'refcount' ],
439 order_by => ['refcount'],
440 group_by => ['me.tag'],
441 }
442 );
443 13 return @tags;
444 }
445  
446 =head2 tags_with_counts($user)
447  
448 Return an array of {id, tag, refcount} for the C<$user>'s tags.
449  
450 =cut
451  
452 22 ▶sub tags_with_counts {
Subroutine tags_with_counts: called CC=1 SCAR=0.0
Pod documented
453 22 my ( $self, $user ) = @_;
454 22 my (@tags) =
455 $self->result_source->related_source('tags')->resultset->search(
456 { page => $self->id, },
457 {
458 select => [ 'me.tag', 'count(me.tag) as refcount' ],
459 as => [ 'tag', 'refcount' ],
460 order_by => ['refcount'],
461 group_by => ['me.tag'],
462 }
463 );
464 22 return @tags;
465 }
466  
467 =head2 path( [$path] )
468  
469 TODO Accessor?
470  
471 =cut
472  
473 706 ▶sub path {
Subroutine path: called CC=4 SCAR=13.9
Pod documented
474 706 my ( $self, $path ) = @_;
475 706 require Carp;
476 706 ▶ if ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 383 323 706
477 383 $self->{path} = $path;
478 }
479 706 ▶ unless ( defined $self->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"path"}) 117 589 706
480 117 ▶ return '/' if ( $self->depth == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->depth == 0 107 10 117
481 10 $self->result_source->resultset->set_paths($self);
482  
483 # croak 'path is not set on the page object: ' . $self->name;
484 }
485 599 return $self->{path};
486 }
487  
488 =head2 has_photos
489  
490 Return the number of photos attached to this page. Use for galleries.
491  
492 =cut
493  
494 58 ▶sub has_photos {
Subroutine has_photos: called CC=1 SCAR=0.0
Pod documented
495 58 my $self = shift;
496 58 return $self->result_source->schema->resultset('Photo')
497 ->search( { 'attachment.page' => $self->id },
498 { join => [qw/attachment/] } )->count;
499 }
500  
501 =head1 AUTHOR
502  
503 Marcus Ramberg <mramberg@cpan.org>
504  
505 =head1 LICENSE
506  
507 This library is free software. You can redistribute it and/or modify
508 it under the same terms as Perl itself.
509  
510 =cut
511  
512 40 1;
« lib/MojoMojo/Formatter/YouTube.pm ↑ Summary lib/MojoMojo/WordDiff.pm »