File: PageVersion.pm

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 4,554 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package MojoMojo::Schema::Result::PageVersion;

use strict;
use warnings;

use parent qw/MojoMojo::Schema::Base::Result/;

=head1 NAME

MojoMojo::Schema::Result::PageVersion - Versioned page metadata

=head1 DESCRIPTION

This table implements versioning of page metadata (not content, see
L<MojoMojo::Schema::Result::Content> for that). It has a composite
primary key C<(page, version)>.

When renaming a page, a new version is created in this table, with
C<version> set to 1 + the maximum version for that C<page>. The
C<status> of the new C<page_version> is set to "released", its 
C<release_date> is set to C<< DateTime->now >>, while the old
C<page_version>'s status is set to 'removed' and its C<remove_date>
is set to C<< DateTime->now >>.

=head2 TODO

=over 4

=item * document the relationships

=item * in order to support proper rollback, meaning creating a new
version for the rollback operation itself, a C<content_version>
field needs to be added.

=item * C<created> is apparently unused: set to 0 for pages populated when
creating the database, and NULL for all normal pages.

=back

=cut

__PACKAGE__->load_components( "Core" );
__PACKAGE__->table("page_version");
__PACKAGE__->add_columns(
    "page",                  { data_type => "INTEGER", is_nullable => 0, size => undef },
    "version",               { data_type => "INTEGER", is_nullable => 0, size => undef },
    "parent",                { data_type => "INTEGER", is_nullable => 1, size => undef },
    "parent_version",        { data_type => "INTEGER", is_nullable => 1, size => undef },
    "name",                  { data_type => "VARCHAR", is_nullable => 0, size => 200 },
    "name_orig",             { data_type => "VARCHAR", is_nullable => 0, size => 200 },
    "depth",                 { data_type => "INTEGER", is_nullable => 0, size => undef },
    "creator",               { data_type => "INTEGER", is_nullable => 0, size => undef },
    "created",               { data_type => "VARCHAR", is_nullable => 1, size => 100 },
    "status",                { data_type => "VARCHAR", is_nullable => 0, size => 20 },
    "release_date",          { data_type => "VARCHAR", is_nullable => 0, size => 100 },
    "remove_date",           { data_type => "VARCHAR", is_nullable => 1, size => 100 },
    "comments",              { data_type => "TEXT",    is_nullable => 1, size => 4000 },

    # FIXME: in a wiki in which I had never rolled back a page (that is, never made a second
    # revision current, I see in the page_version table that some pages have
    # content_version_first and content_version_last (1, 1) and others (NULL, NULL). --dandv
    "content_version_first", { data_type => "INTEGER", is_nullable => 1, size => undef },
    "content_version_last",  { data_type => "INTEGER", is_nullable => 1, size => undef },
);
__PACKAGE__->set_primary_key( "page", "version" );
__PACKAGE__->has_many(
    pages => "MojoMojo::Schema::Result::Page",
    { 
        "foreign.id" => "self.page", 
        "foreign.version" => "self.version" 
    },
);
__PACKAGE__->belongs_to( "creator", "MojoMojo::Schema::Result::Person", { id => "creator" } );
__PACKAGE__->belongs_to( "page",    "MojoMojo::Schema::Result::Page",   { id => "page" }, );
__PACKAGE__->belongs_to(
    content => "MojoMojo::Schema::Result::Content",
    { 
        page => "page",
        version => "content_version_first" 
    },
);
__PACKAGE__->belongs_to(
    content => "MojoMojo::Schema::Result::Content",
    { 
        page => "page", 
        version => "content_version_last"
    },
);
__PACKAGE__->belongs_to(
    page_version => "MojoMojo::Schema::Result::PageVersion",
    { 
        page => "parent", 
        version => "parent_version" 
    },
);
__PACKAGE__->has_many(
    "page_versions",
    "MojoMojo::Schema::Result::PageVersion",
    {
        "foreign.parent"         => "self.page",
        "foreign.parent_version" => "self.version",
    },
);

=head1 METHODS

=cut

=head2 latest_version

Return the L<PageVersion|MojoMojo::Schema::Result::PageVersion> object
having the latest version of this page.

=cut

sub latest_version {
    my $self = shift;
    my $latest = $self->result_source->resultset->search(
        {
            page => $self->page->id
        },
        {
            order_by => 'version DESC',
            rows => 1
        }    
    )->single;
    
    return $latest;
}

=head1 AUTHOR

Marcus Ramberg <mramberg@cpan.org>

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut


1;