]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/recentchangesdiff.pm
recentchangesdiff: truncate extremely large diffs
[ikiwiki.git] / IkiWiki / Plugin / recentchangesdiff.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchangesdiff;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use HTML::Entities;
8
9 my $maxlines=200;
10
11 sub import {
12         hook(type => "getsetup", id => "recentchangesdiff",
13                 call => \&getsetup);
14         hook(type => "pagetemplate", id => "recentchangesdiff",
15                 call => \&pagetemplate);
16 }
17
18 sub getsetup () {
19         return 
20                 plugin => {
21                         safe => 1,
22                         rebuild => 1,
23                 },
24 }
25
26 sub pagetemplate (@) {
27         my %params=@_;
28         my $template=$params{template};
29         if ($config{rcs} && exists $params{rev} && length $params{rev} &&
30             $template->query(name => "diff")) {
31                 my @lines=IkiWiki::rcs_diff($params{rev}, $maxlines+1);
32                 if (@lines) {
33                         my $diff;
34                         my $trunc=0;
35                         if (@lines > $maxlines) {
36                                 $diff=join("", @lines[0..($maxlines-1)]);
37                                 $trunc=1;
38                         }
39                         else {
40                                 $diff=join("", @lines);
41                         }
42                         if (length $diff > 102400) {
43                                 $diff=substr($diff, 0, 10240);
44                                 $trunc=1;
45                         }
46                         if ($trunc) {
47                                 $diff.="\n".gettext("(Diff truncated)");
48                         }
49                         # escape html
50                         $diff = encode_entities($diff);
51                         # escape links and preprocessor stuff
52                         $diff = encode_entities($diff, '\[\]');
53                         $template->param(diff => $diff);
54                 }
55         }
56 }
57
58 1