File: Content.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 (359 lines) | stat: -rw-r--r-- 9,976 bytes parent folder | download | duplicates (2)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package MojoMojo::Schema::Result::Content;

use strict;
use warnings;

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

=head1 NAME

MojoMojo::Schema::Result::Content - Versioned page content

=head1 DESCRIPTION

This table stores the actual page content; in other words, it's a table of
page versions (revisions). It has a composite primary key C<(page, version)>,
where C<page> is the id of a page, and C<version> is its version number. Each
version has a content C<body>, a C<status> ("released" or "removed"), and a
C<release_date>. Revisions that have been replaced by a newer revision have a
C<remove_date> and a C<comments> set to "Replaced by version x.".

The C<type>, C<abstract> and C<precompiled> columns are for future use.

C<created> is essentially equal to C<release_date> (there can be a 1-second
difference), and is used externally by other modules and in templates.
C<release_date> and C<remove_date> are used internally.

=cut

use DateTime::Format::Mail;

use Algorithm::Diff;
use Algorithm::Merge qw/merge/;
use MojoMojo::WordDiff;
use HTML::Entities qw/encode_entities_numeric/;

__PACKAGE__->load_components(qw/DateTime::Epoch TimeStamp Core/);
__PACKAGE__->table("content");
__PACKAGE__->add_columns(
    "page",
    { data_type => "INTEGER", is_nullable => 0, size => undef },
    "version",
    { data_type => "INTEGER", is_nullable => 0, size => undef },
    "creator",
    { data_type => "INTEGER", is_nullable => 0, size => undef },
    "created",
    {
        data_type        => "BIGINT",
        is_nullable      => 0,
        size             => 100,
        inflate_datetime => 'epoch',
        set_on_create    => 1,
    },
    "status",
    { data_type => "VARCHAR", is_nullable => 0, size => 20 },
    "release_date",
    {
        data_type                 => "BIGINT",
        is_nullable               => 0,
        size                      => 100,
        default_value             => undef,
        inflate_datetime          => 'epoch',
        datetime_undef_if_invalid => 1,
    },
    "remove_date",
    {
        data_type                 => "BIGINT",
        is_nullable               => 1,
        size                      => 100,
        default_value             => undef,
        inflate_datetime          => 'epoch',
        datetime_undef_if_invalid => 1,
    },
    "type",
    { data_type => "VARCHAR", is_nullable => 1, size => 200 },
    "abstract",
    { data_type => "TEXT", is_nullable => 1, size => 4000 },
    "comments",
    { data_type => "TEXT", is_nullable => 1, size => 4000 },
    "body",
    { data_type => "TEXT", is_nullable => 0, size => undef },
    "precompiled",
    { data_type => "TEXT", is_nullable => 1, size => undef },

);
__PACKAGE__->set_primary_key( "version", "page" );
__PACKAGE__->has_many(
    "pages",
    "MojoMojo::Schema::Result::Page",
    {
        "foreign.content_version" => "self.version",
        "foreign.id"              => "self.page",
    },
);
__PACKAGE__->has_many(
    "page_version_page_content_version_firsts",
    "MojoMojo::Schema::Result::PageVersion",
    {
        "foreign.content_version_first" => "self.version",
        "foreign.page"                  => "self.page",
    },
);
__PACKAGE__->has_many(
    "page_version_page_content_version_lasts",
    "MojoMojo::Schema::Result::PageVersion",
    {
        "foreign.content_version_last" => "self.version",
        "foreign.page"                 => "self.page",
    },
);
__PACKAGE__->belongs_to( "creator", "MojoMojo::Schema::Result::Person", { id => "creator" } );
__PACKAGE__->belongs_to( "page",    "MojoMojo::Schema::Result::Page",   { id => "page" } );

=head1 COLUMNS

=head2 page

References L<MojoMojo::Schema::Result::Page>.

=head2 creator

References L<MojoMojo::Schema::Result::Person>.


=head1 METHODS

=head2 highlight

Returns an HTML string highlighting the changes between this version and the
previous version. The changes are in C<< <span> >> or C<< <div> >> tags with
the class C<fade>.

=cut

sub highlight {
    my ( $self, $c ) = @_;
    my $this_content = $self->formatted($c);

    my $previous_content = (
        defined $self->previous
        ? $self->previous->formatted($c)
        : $this_content
    );

    my $this = [ split /\n/,                  $this_content ];
    my $prev = [ split /\n/,                  $previous_content ];
    my @diff = Algorithm::Diff::sdiff( $prev, $this );
    my $diff;
    my $hi           = 0;
    my $pre_tag_open = 0;
    for my $line (@diff) {
        $pre_tag_open = 1 if $$line[2] =~ qr{<pre>} and $$line[2] !~ qr{</pre>};
        my $tag = $pre_tag_open ? 'span' : 'div';
        $hi++;
        if ( $$line[0] eq "+" ) {
            $diff .= qq(<$tag id="hi$hi" class="fade">) . $$line[2] . "</$tag>";
        }
        elsif ( $$line[0] eq "c" ) {
            $diff .= qq(<$tag id="hi$hi" class="fade">) . $$line[2] . "</$tag>";
        }
        elsif ( $$line[0] eq "-" ) { }
        else { $diff .= $$line[1] }
        $diff .= "\n";
        $pre_tag_open = 0 if $$line[2] =~ qr{</pre>} and $$line[2] !~ qr{<pre>};
    }
    return $diff;
}

=head2 formatted_diff <context> <old_content>

Compare this content version to <old_content>, using L<Algorithm::Diff>.
Sets a C<diffins> CSS class for added lines, and a C<diffdel> CSS class
for deleted lines. The C<< <ins> >> and C<< <del> >> HTML tags are also used.

=cut

sub formatted_diff {
    my ( $self, $c, $to, $sparse ) = @_;
    my $this = [
        $sparse
        ? split /\n/, ( $self->encoded_body )
        : split /\n\n/, ( $self->formatted($c) )
    ];
    my $prev = [
        $sparse
        ? split /\n/, ( $to->encoded_body )
        : split /\n\n/, ( $to->formatted($c) )
    ];
    my @diff = Algorithm::Diff::sdiff( $prev, $this );
    my $diff;
    for my $line (@diff) {
        if ( $$line[0] eq "+" ) {
            $diff .= qq(<div class="diffins">) . $$line[2] . "</div>";
        }
        elsif ( $$line[0] eq "-" ) {
            $diff .= qq(<div class="diffdel">) . $$line[1] . "</div>";
        }
        elsif ( $$line[0] eq "c" ) {

            $diff .= (
                $sparse
                ? qq(<div class="diffdel">)
                  . $$line[1]
                  . "</div>"
                  . qq(<div class="diffins">)
                  . $$line[2]
                  . "</div>"
                : word_diff( $$line[1], $$line[2] )
            );
        }
        elsif ( $$line[0] eq "u" ) {
            $diff .= ( $sparse ? '<div> ' . $$line[1] . '<div>' : $$line[1] );
        }
        else { $diff .= "Unknown operator " . $$line[0] }
    }
    return $diff;
}

=head2 formatted

Return the content after being run through MojoMojo::Formatter::*.

=cut

sub formatted {
    my ( $self, $c ) = @_;
    my $result = $self->result_source->resultset->format_content( $c, $self->body, $self );
    return $result;
}

=head2 merge_content

Show the merge conflict of the content for two different edit sessions of the same page.

=cut

sub merge_content {
    my ( $self, $saved, $content, $h1, $h2, $h3 ) = @_;

    my $source = [ split /\n/, $self->encoded_body ];
    my $a      = [ split /\n/, $saved->encoded_body ];
    my $b      = [ split /\n/, $content ];
    my @merged = merge(
        $source, $a, $b,
        {
            CONFLICT => sub ($$) {
                (
                    "<!-- $h1  -->\n",
                    ( @{ $_[0] } ),
                    "<!-- $h2  -->\n",
                    ( @{ $_[1] } ),
                    "<!-- $h3 -->\n",
                );
              }
        }
    );
    return join( '', @merged );
}

=head2 max_version

Return the highest numbered revision.

=cut

sub max_version {
    my $self = shift;
    my $max  = $self->result_source->resultset->search(
        { page => $self->page->id },
        {
            select => [ { max => 'me.version' } ],
            as     => ['max_ver']
        }
    );
    return 0 unless $max->count;
    return $max->next->get_column('max_ver');
}

=head2 previous

Return the previous version of this content, or undef for the first version.

=cut

sub previous {
    my $self = shift;
    return $self->result_source->resultset->search(
        {
            page    => $self->page->id,
            version => $self->version - 1
        }
    )->next;
}

=head2 pub_date

Return the publishing date of this version in a format suitable for RSS 2.0.

=cut

sub pub_date {
    my $self = shift;
    return DateTime::Format::Mail->format_datetime( $self->created );
}

=head2 store_links

Extract and store all links and wanted paged from a given content
version.

=cut

sub store_links {
    my ($self) = @_;

    return unless ( $self->status eq 'released' );
    my $content = $self->body;
    my $page    = $self->page;
    $page->result_source->resultset->set_paths($page);
    $page->links_from->delete();
    $page->wantedpages->delete();
    require MojoMojo::Formatter::Wiki;
    my ( $linked_pages, $wanted_pages ) = MojoMojo::Formatter::Wiki->find_links( \$content, $page );
    return unless ( @$linked_pages || @$wanted_pages );

    for (@$linked_pages) {
        my $link =
          $self->result_source->schema->resultset('Link')
          ->find_or_create( { from_page => $self->page->id, to_page => $_->id } );
    }
    for (@$wanted_pages) {
        $_->{path} = join( '/',
            map { ( $self->result_source->schema->resultset('Page')->normalize_name($_) )[1]; }
              split( m|/|, $_->{path} ) );
        my $wanted_page =
          $self->result_source->schema()->resultset('WantedPage')
          ->find_or_create( { from_page => $page->id, to_path => $_->{path} } );
    }
}

=head2 encoded_body

Encode content body using numeric entities.

=cut

sub encoded_body { return encode_entities_numeric( shift->body ); }

=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;