Silki/Schema/Page.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.0% 38 / 40 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Silki/Formatter/WikiToHTML.pm ↑ Summary Silki/Schema/PageRevision.pm »
1 package Silki::Schema::Page;
2 {
3 $Silki::Schema::Page::VERSION = '0.29';
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Fey::Object::Iterator::FromSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fey::Placeholder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::AllUtils qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Silki::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Silki::I18N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶use Silki::Schema::PageRevision;
Subroutine BEGIN: called CC=1 SCAR=2.6
17 use Silki::Schema;
18 use Silki::Schema::File;
19 use Silki::Schema::PageTag;
20 use Silki::Schema::Tag;
21 use Silki::Schema::Wiki;
22 use Silki::Types qw( ArrayRef Bool Int Str );
23 use URI::Escape qw( uri_escape_utf8 uri_unescape );
24  
25 use Fey::ORM::Table;
26 use MooseX::ClassAttribute;
27 use MooseX::Params::Validate qw( validated_list pos_validated_list );
28  
29 with 'Silki::Role::Schema::URIMaker';
30  
31 with 'Silki::Role::Schema::SystemLogger' => { methods => ['delete'] };
32  
33 with 'Silki::Role::Schema::DataValidator' => {
34 steps => [
35 '_title_is_valid',
36 '_title_is_unique',
37 '_build_uri_path',
38 ],
39 };
40  
41 my $Schema = Silki::Schema->Schema();
42  
43 has_policy 'Silki::Schema::Policy';
44  
45 has_table( $Schema->table('Page') );
46  
47 has_one( $Schema->table('User') );
48  
49 has_one wiki => (
50 table => $Schema->table('Wiki'),
51 handles => ['domain'],
52 );
53  
54 has_many tags => (
55 table => $Schema->table('Tag'),
56 select => __PACKAGE__->_TagsSelect(),
57 bind_params => sub { $_[0]->page_id() },
58 );
59  
60 has revision_count => (
61 metaclass => 'FromSelect',
62 is => 'ro',
63 isa => Int,
64 lazy => 1,
65 select => __PACKAGE__->_RevisionCountSelect(),
66 bind_params => sub { $_[0]->page_id() },
67 clearer => '_clear_revision_count',
68 );
69  
70 class_has _RevisionsSelect => (
71 is => 'ro',
72 isa => 'Fey::SQL::Select',
73 lazy => 1,
74 builder => '_BuildRevisionsSelect',
75 );
76  
77 has_one most_recent_revision => (
78 table => $Schema->table('PageRevision'),
79 select => __PACKAGE__->_MostRecentRevisionSelect(),
80 bind_params => sub { $_[0]->page_id() },
81 handles => {
82 content => 'content',
83 last_modified_datetime => 'creation_datetime',
84 },
85 );
86  
87 has_one first_revision => (
88 table => $Schema->table('PageRevision'),
89 select => __PACKAGE__->_FirstRevisionSelect(),
90 bind_params => sub { $_[0]->page_id(), 1 },
91 handles => {
92 creation_datetime => 'creation_datetime',
93 },
94 );
95  
96 query incoming_link_count => (
97 select => __PACKAGE__->_IncomingLinkCountSelect(),
98 bind_params => sub { $_[0]->page_id() },
99 );
100  
101 has_many incoming_links => (
102 table => $Schema->table('Page'),
103 select => __PACKAGE__->_IncomingLinkSelect(),
104 bind_params => sub { $_[0]->page_id() },
105 );
106  
107 query file_count => (
108 select => __PACKAGE__->_FileCountSelect(),
109 bind_params => sub { $_[0]->page_id() },
110 );
111  
112 has_many files => (
113 table => $Schema->table('File'),
114 select => __PACKAGE__->_FileSelect(),
115 bind_params => sub { $_[0]->page_id() },
116 );
117  
118 has is_front_page => (
119 is => 'ro',
120 isa => Bool,
121 lazy => 1,
122 init_arg => undef,
123 default => sub { $_[0]->title() eq $_[0]->wiki()->front_page_title() },
124 );
125  
126 class_has _PageViewInsert => (
127 is => 'ro',
128 isa => 'Fey::SQL::Insert',
129 lazy => 1,
130 builder => '_BuildPageViewInsert',
131 );
132  
133 class_has _PendingPageLinkSelectSQL => (
134 is => 'ro',
135 isa => 'Fey::SQL::Select',
136 lazy => 1,
137 builder => '_BuildPendingPageLinkSelectSQL',
138 );
139  
140 class_has _PendingPageLinkDeleteSQL => (
141 is => 'ro',
142 isa => 'Fey::SQL::Delete',
143 lazy => 1,
144 builder => '_BuildPendingPageLinkDeleteSQL',
145 );
146  
147 with 'Silki::Role::Schema::Serializes' => {
148 skip => ['cached_content'],
149 };
150  
151 sub _base_uri_path {
152 my $self = shift;
153  
154 return $self->wiki()->_base_uri_path() . '/page/' . $self->uri_path();
155 }
156  
157 around insert => sub {
158 my $orig = shift;
159 my $class = shift;
160  
161 my $page = $class->$orig(@_);
162  
163 my $select = $class->_PendingPageLinkSelectSQL();
164  
165 my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh();
166  
167 # XXX - hack but it should work fine
168 my $select_sql = $select->sql($dbh) . ' FOR UPDATE';
169  
170 my $delete = $class->_PendingPageLinkDeleteSQL();
171  
172 my $update_links = sub {
173 my $links = $dbh->selectcol_arrayref(
174 $select_sql,
175 {},
176 $page->wiki_id(),
177 $page->title(),
178 );
179  
180 return unless @{$links};
181  
182 $dbh->do(
183 $delete->sql($dbh),
184 {},
185 $page->wiki_id(),
186 $page->title(),
187 );
188  
189 my @new_links
190 = map { { from_page_id => $_, to_page_id => $page->page_id(), } }
191 @{$links};
192  
193 Silki::Schema::PageLink->insert_many(@new_links);
194 };
195  
196 Silki::Schema->RunInTransaction($update_links);
197  
198 return $page;
199 };
200  
201 sub _BuildPendingPageLinkSelectSQL {
202 my $select = Silki::Schema->SQLFactoryClass()->new_select();
203  
204 #<<<
205 $select
206 ->select( $Schema->table('PendingPageLink')->column('from_page_id') )
207 ->from( $Schema->table('PendingPageLink') )
208 ->where( $Schema->table('PendingPageLink')->column('to_wiki_id'),
209 '=', Fey::Placeholder->new() )
210 ->and ( $Schema->table('PendingPageLink')->column('to_page_title'),
211 '=', Fey::Placeholder->new() );
212 #>>>
213 return $select;
214 }
215  
216 sub _BuildPendingPageLinkDeleteSQL {
217 my $delete = Silki::Schema->SQLFactoryClass()->new_delete();
218  
219 #<<<
220 $delete
221 ->delete()
222 ->from( $Schema->table('PendingPageLink') )
223 ->where( $Schema->table('PendingPageLink')->column('to_wiki_id'),
224 '=', Fey::Placeholder->new() )
225 ->and ( $Schema->table('PendingPageLink')->column('to_page_title'),
226 '=', Fey::Placeholder->new() );
227 #>>>
228 return $delete;
229 }
230  
231 sub insert_with_content {
232 my $class = shift;
233 my %p = @_;
234  
235 my %page_p = (
236 map { $_ => delete $p{$_} }
237 grep { exists $p{$_} }
238 map { $_->name() } $class->Table()->columns()
239 );
240  
241 my $page;
242 $class->SchemaClass()->RunInTransaction(
243 sub {
244 $page = $class->insert(%page_p);
245  
246 $page->add_revision(
247 %p,
248 user_id => $page->user_id(),
249 );
250 }
251 );
252  
253 return $page;
254 }
255  
256 sub _system_log_values_for_delete {
257 my $self = shift;
258  
259 my $revision = $self->most_recent_revision();
260  
261 my $msg
262 = 'Deleted page, '
263 . $self->title()
264 . ', in wiki '
265 . $self->wiki()->title();
266  
267 return (
268 wiki_id => $self->wiki_id(),
269 message => $msg,
270 data_blob => {
271 title => $self->title(),
272 revisions => ( $revision ? $revision->revision_number() : 0 ),
273 content => ( $revision ? $revision->content() : undef ),
274 },
275 );
276 }
277  
278 sub _title_is_valid {
279 my $self = shift;
280 my $p = shift;
281 my $is_insert = shift;
282  
283 return
284 if !$is_insert
285 && !exists $p->{title};
286  
287 if ( $p->{title} =~ /\)\)/ ) {
288 return {
289 message => loc(
290 q{Page titles cannot contain the characters "))", since this conflicts with the wiki link syntax.}
291 ),
292 };
293 }
294  
295 if ( $p->{title} =~ /\// ) {
296 return {
297 message => loc(
298 q{Page titles cannot contain a slash (/), since this conflicts with the syntax to link to another wiki.}
299 ),
300 };
301 }
302  
303 return;
304 }
305  
306 sub _title_is_unique {
307 my $self = shift;
308 my $p = shift;
309 my $is_insert = shift;
310  
311 return unless exists $p->{title};
312  
313 my $wiki_id = $p->{wiki_id};
314 $wiki_id = $self->wiki_id()
315 unless $wiki_id || $is_insert;
316  
317 return unless $wiki_id;
318  
319 my $page = __PACKAGE__->new( title => $p->{title}, wiki_id => $wiki_id );
320  
321 return unless $page;
322  
323 return if !$is_insert && $page->page_id() == $self->page_id();
324  
325 return {
326 message => loc(
327 q{The page title you have chosen (%1) is already in use in this wiki.},
328 $p->{title}
329 ),
330 };
331 }
332  
333 sub _build_uri_path {
334 my $self = shift;
335 my $p = shift;
336  
337 return unless exists $p->{title};
338  
339 $p->{uri_path} = $self->TitleToURIPath( $p->{title} );
340  
341 return;
342 }
343  
344 sub add_revision {
345 my $self = shift;
346 my %p = @_;
347  
348 my $revision = $self->most_recent_revision();
349 my $revision_number = $revision ? $revision->revision_number() + 1 : 1;
350  
351 $self->_clear_most_recent_revision();
352 $self->_clear_revision_count();
353  
354 return Silki::Schema::PageRevision->insert(
355 %p,
356 revision_number => $revision_number,
357 page_id => $self->page_id(),
358 );
359 }
360  
361 sub add_file {
362 my $self = shift;
363 my ($file) = pos_validated_list( \@_, { isa => 'Silki::Schema::File' } );
364  
365 my $last_rev = $self->most_recent_revision();
366 my $new_content = $last_rev->content();
367  
368 $new_content =~ s/\n*$/\n\n/;
369 $new_content .= '{{file:' . $file->filename() . '}}';
370 $new_content .= "\n";
371  
372 $self->add_revision(
373 content => $new_content,
374 user_id => Silki::Schema::User->SystemUser()->user_id(),
375 comment =>
376 loc( 'Adding a link to a new file: %1', $file->filename() ),
377 );
378  
379 return;
380 }
381  
382 sub rename {
383 my $self = shift;
384 my ($title) = pos_validated_list( \@_, { isa => Str } );
385  
386 return if $title eq $self->title();
387  
388 die 'Cannot rename this page - ', $self->title(), "\n"
389 unless $self->can_be_renamed();
390  
391 my $links = $self->incoming_links();
392  
393 # XXX - there's a small race condition here, because there's a window
394 # between reading the content of the linking pages and updating it. This
395 # could be fixed by moving the read into the transaction and doing a
396 # SELECT ... FOR UPDATE, I think
397 my @pages;
398 while ( my $page = $links->next() ) {
399 push @pages,
400 [
401 $page,
402 $page->rewritten_content_for_rename( $self->title(), $title )
403 ];
404 }
405  
406 my $update_title = sub {
407 my $old_title = $self->title();
408  
409 $self->update( title => $title );
410  
411 for my $pair (@pages) {
412 my ( $page, $content ) = @{$pair};
413  
414 $page->add_revision(
415 content => $content,
416 user_id => Silki::Schema::User->SystemUser()->user_id(),
417 comment => loc(
418 'Updating links because a page is being renamed from %1 to %2',
419 $old_title, $title
420 ),
421 );
422 }
423 };
424  
425 Silki::Schema->RunInTransaction($update_title);
426 }
427  
428 sub rewritten_content_for_rename {
429 my $self = shift;
430 my $old_name = shift;
431 my $new_name = shift;
432  
433 my $content = $self->content();
434  
435 $content =~ s/\Q(($old_name))/(($new_name))/g;
436  
437 return $content;
438 }
439  
440 sub record_view {
441 my $self = shift;
442 my ($user) = pos_validated_list( \@_, { isa => 'Silki::Schema::User' } );
443  
444 my $insert = $self->_PageViewInsert();
445  
446 my $dbh = Silki::Schema->DBIManager()->source_for_sql($insert)->dbh();
447  
448 $dbh->do( $insert->sql($dbh), {}, $self->page_id(), $user->user_id() );
449  
450 return;
451 }
452  
453 sub _BuildPageViewInsert {
454 my $class = shift;
455  
456 my $page_view_t = $Schema->table('PageView');
457  
458 my $insert = Silki::Schema->SQLFactoryClass()->new_insert();
459  
460 #<<<
461 $insert
462 ->insert()
463 ->into( $page_view_t->columns( 'page_id', 'user_id' ) )
464 ->values( page_id => Fey::Placeholder->new(),
465 user_id => Fey::Placeholder->new(),
466 );
467 #>>>
468 return $insert;
469 }
470  
471 sub TitleToURIPath {
472 my $class = shift;
473 my $title = shift;
474  
475 # This is the default list of safe characters, except we also escape
476 # underscores. This lets us replace escaped spaces (%20) with underscores
477 # after URI-escaping, making for much friendlier paths.
478 my $escaped = uri_escape_utf8( $title, q{^A-Za-z0-9-.!~*'()"} );
479  
480 $escaped =~ s/%20/_/g;
481  
482 return $escaped;
483 }
484  
485 sub URIPathToTitle {
486 my $class = shift;
487 my $path = shift;
488  
489 $path =~ s/_/%20/g;
490  
491 return decode( 'utf-8', uri_unescape($path) );
492 }
493  
494 sub _TagsSelect {
495 my $class = shift;
496  
497 my $select = Silki::Schema->SQLFactoryClass()->new_select();
498  
499 #<<<
500 $select->select( $Schema->table('Tag') )
501 ->from( $Schema->tables( 'PageTag', 'Tag' ) )
502 ->where( $Schema->table('PageTag')->column('page_id'), '=',
503 Fey::Placeholder->new() )
504 ->order_by( $Schema->table('Tag')->column('tag') );
505 #>>>
506 return $select;
507 }
508  
509 sub _MostRecentRevisionSelect {
510 my $class = shift;
511  
512 my $select = Silki::Schema->SQLFactoryClass()->new_select();
513  
514 #<<<
515 $select
516 ->select( $Schema->table('PageRevision') )
517 ->from( $Schema->table('PageRevision') )
518 ->where( $Schema->table('PageRevision')->column('page_id'),
519 '=', Fey::Placeholder->new() )
520 ->order_by( $Schema->table('PageRevision')->column('revision_number'),
521 'DESC' )
522 ->limit(1);
523 #>>>
524 return $select;
525 }
526  
527 sub _FirstRevisionSelect {
528 my $self = shift;
529  
530 my $select = Silki::Schema->SQLFactoryClass()->new_select();
531  
532 #<<<
533 $select
534 ->select( $Schema->table('PageRevision') )
535 ->from( $Schema->table('PageRevision') )
536 ->where( $Schema->table('PageRevision')->column('page_id'),
537 '=', Fey::Placeholder->new() )
538 ->and( $Schema->table('PageRevision')->column('revision_number'),
539 '=', Fey::Placeholder->new() );
540 #>>>
541 return $select;
542 }
543  
544 sub _IncomingLinkCountSelect {
545 my $select = Silki::Schema->SQLFactoryClass()->new_select();
546  
547 my $page_link_t = $Schema->table('PageLink');
548  
549 my $count = Fey::Literal::Function->new(
550 'COUNT',
551 $page_link_t->column('from_page_id')
552 );
553  
554 #<<<
555 $select
556 ->select($count)->from($page_link_t)
557 ->where( $page_link_t->column('to_page_id'), '=',
558 Fey::Placeholder->new() );
559 #>>>
560 return $select;
561 }
562  
563 sub _IncomingLinkSelect {
564 my $select = Silki::Schema->SQLFactoryClass()->new_select();
565  
566 my ( $page_t, $page_link_t ) = $Schema->tables( 'Page', 'PageLink' );
567  
568 my ($fk)
569 = first { $_->has_column( $page_link_t->column('from_page_id') ) }
570  
571 $Schema->foreign_keys_between_tables( $page_t, $page_link_t );
572  
573 #<<<
574 $select
575 ->select($page_t)->from( $page_t, $page_link_t, $fk )
576 ->where( $page_link_t->column('to_page_id'), '=',
577 Fey::Placeholder->new() )->order_by( $page_t->column('title') );
578 #>>>
579 return $select;
580 }
581  
582 sub _FileCountSelect {
583 my $select = Silki::Schema->SQLFactoryClass()->new_select();
584  
585 my $file_t = $Schema->table('File');
586 my $count = Fey::Literal::Function->new(
587 'COUNT',
588 $file_t->column('file_id')
589 );
590  
591 #<<<
592 $select
593 ->select($count)
594 ->from($file_t)
595 ->where( $file_t->column('page_id'), '=', Fey::Placeholder->new() );
596 #>>>
597 return $select;
598 }
599  
600 sub _FileSelect {
601 my $select = Silki::Schema->SQLFactoryClass()->new_select();
602  
603 my $file_t = $Schema->table('File');
604  
605 #<<<
606 $select
607 ->select($file_t)
608 ->from( $file_t )
609 ->where( $file_t->column('page_id'), '=', Fey::Placeholder->new() )
610 ->order_by( $file_t->column('filename') );
611 #>>>
612 return $select;
613 }
614  
615 sub _RevisionCountSelect {
616 my $select = Silki::Schema->SQLFactoryClass()->new_select();
617  
618 my $page_revision_t = $Schema->table('PageRevision');
619  
620 my $count = Fey::Literal::Function->new(
621 'COUNT',
622 $page_revision_t->column('page_id')
623 );
624  
625 #<<<
626 $select
627 ->select($count)->from($page_revision_t)
628 ->where( $page_revision_t->column('page_id'), '=',
629 Fey::Placeholder->new() );
630 #>>>
631 return $select;
632 }
633  
634 sub revisions {
635 my $self = shift;
636 my ( $limit, $offset ) = validated_list(
637 \@_,
638 limit => { isa => Int, optional => 1 },
639 offset => { isa => Int, default => 0 },
640 );
641  
642 my $select = $self->_RevisionsSelect()->clone();
643 $select->limit( $limit, $offset );
644  
645 return Fey::Object::Iterator::FromSelect->new(
646 classes => ['Silki::Schema::PageRevision'],
647 select => $select,
648 dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(),
649 bind_params => [ $self->page_id() ],
650 );
651 }
652  
653 sub _BuildRevisionsSelect {
654 my $select = Silki::Schema->SQLFactoryClass()->new_select();
655  
656 my $page_revision_t = $Schema->table('PageRevision');
657  
658 #<<<
659 $select
660 ->select($page_revision_t)->from($page_revision_t)
661 ->where( $page_revision_t->column('page_id'), '=',
662 Fey::Placeholder->new() )
663 ->order_by( $page_revision_t->column('revision_number'), 'DESC' );
664 #>>>
665 return $select;
666 }
667  
668 sub add_tags {
669 my $self = shift;
670 my ($tags) = validated_list(
671 \@_,
672 tags => ArrayRef,
673 );
674  
675 my @tag_ids;
676 for my $tag_name ( @{$tags} ) {
677 my %tag_p = (
678 tag => $tag_name,
679 wiki_id => $self->wiki_id(),
680 );
681  
682 my $tag;
683 if ( $tag = Silki::Schema::Tag->new(%tag_p) ) {
684 next
685 if Silki::Schema::PageTag->new(
686 page_id => $self->page_id(),
687 tag_id => $tag->tag_id(),
688 );
689  
690 push @tag_ids, $tag->tag_id();
691 }
692 else {
693 $tag = Silki::Schema::Tag->insert(%tag_p);
694  
695 push @tag_ids, $tag->tag_id();
696 }
697 }
698  
699 Silki::Schema::PageTag->insert_many(
700 map { { page_id => $self->page_id(), tag_id => $_, } } @tag_ids )
701 if @tag_ids;
702  
703 return;
704 }
705  
706 sub delete_tag {
707 my $self = shift;
708 my ($tag_name) = pos_validated_list(
709 \@_,
710 Str,
711 );
712  
713 my $tag = Silki::Schema::Tag->new(
714 tag => $tag_name,
715 wiki_id => $self->wiki_id()
716 ) or return;
717  
718 my $pt = Silki::Schema::PageTag->new(
719 page_id => $self->page_id(),
720 tag_id => $tag->tag_id(),
721 ) or return;
722  
723 $pt->delete();
724  
725 return;
726 }
727  
728 sub PagesByWikiAndTitle {
729 my $self = shift;
730 my $titles = shift;
731  
732 my $select = Silki::Schema->SQLFactoryClass()->new_select();
733  
734 my $page_t = $Schema->table('Page');
735  
736 #<<<
737 $select
738 ->select($page_t)
739 ->from ($page_t); #>>>
740  
741 my @keys = keys %{$titles};
742  
743 for my $wiki_id (@keys) {
744 #<<<
745 $select->where( '(' )
746 ->and ( $page_t->column('wiki_id'), '=', $wiki_id )
747 ->and ( $page_t->column('title'), 'IN', @{ $titles->{$wiki_id} } )
748 ->and ( ')' );
749 #>>>
750 $select->and('or')
751 unless $wiki_id == $keys[-1];
752 }
753  
754 return Fey::Object::Iterator::FromSelect->new(
755 classes => ['Silki::Schema::Page'],
756 select => $select,
757 dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(),
758 bind_params => [ $select->bind_params() ],
759 );
760 }
761  
762 __PACKAGE__->meta()->make_immutable();
763  
764 1;
765  
766 # ABSTRACT: Represents a page
767  
768 __END__
« Silki/Formatter/WikiToHTML.pm ↑ Summary Silki/Schema/PageRevision.pm »