56e80e7b815954e8a42c32bc284a03a62e94e327
[ikiwiki.git] / IkiWiki / Plugin / recentchanges.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchanges;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
8 use HTML::Entities;
9
10 sub import {
11         hook(type => "getsetup", id => "recentchanges", call => \&getsetup);
12         hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
13         hook(type => "refresh", id => "recentchanges", call => \&refresh);
14         hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
15         hook(type => "htmlize", id => "_change", call => \&htmlize);
16 }
17
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => 1,
23                 },
24                 recentchangespage => {
25                         type => "string",
26                         example => "recentchanges",
27                         description => "name of the recentchanges page",
28                         safe => 1,
29                         rebuild => 1,
30                 },
31                 recentchangesnum => {
32                         type => "integer",
33                         example => 100,
34                         description => "number of changes to track",
35                         safe => 1,
36                         rebuild => 0,
37                 },
38 }
39
40 sub checkconfig () {
41         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
42         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
43 }
44
45 sub refresh ($) {
46         my %seen;
47
48         # add new changes
49         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
50                 $seen{store($change, $config{recentchangespage})}=1;
51         }
52         
53         # delete old and excess changes
54         foreach my $page (keys %pagesources) {
55                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
56                         unlink($config{srcdir}.'/'.$pagesources{$page});
57                 }
58         }
59 }
60
61 # Enable the recentchanges link on wiki pages.
62 sub pagetemplate (@) {
63         my %params=@_;
64         my $template=$params{template};
65         my $page=$params{page};
66
67         if (defined $config{recentchangespage} && $config{rcs} &&
68             $page ne $config{recentchangespage} &&
69             $template->query(name => "recentchangesurl")) {
70                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
71                 $template->param(have_actions => 1);
72         }
73 }
74
75 # Pages with extension _change have plain html markup, pass through.
76 sub htmlize (@) {
77         my %params=@_;
78         return $params{content};
79 }
80
81 sub store ($$$) {
82         my $change=shift;
83
84         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
85
86         # Optimisation to avoid re-writing pages. Assumes commits never
87         # change (or that any changes are not important).
88         return $page if exists $pagesources{$page} && ! $config{rebuild};
89
90         # Limit pages to first 10, and add links to the changed pages.
91         my $is_excess = exists $change->{pages}[10];
92         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
93         $change->{pages} = [
94                 map {
95                         if (length $config{cgiurl}) {
96                                 $_->{link} = "<a href=\"".
97                                         IkiWiki::cgiurl(
98                                                 do => "goto",
99                                                 page => $_->{page}
100                                         ).
101                                         "\" rel=\"nofollow\">".
102                                         pagetitle($_->{page}).
103                                         "</a>"
104                         }
105                         else {
106                                 $_->{link} = pagetitle($_->{page});
107                         }
108                         $_->{baseurl}="$config{url}/" if length $config{url};
109
110                         $_;
111                 } @{$change->{pages}}
112         ];
113         push @{$change->{pages}}, { link => '...' } if $is_excess;
114
115         # See if the committer is an openid.
116         $change->{author}=$change->{user};
117         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
118         if (defined $oiduser) {
119                 $change->{authorurl}=$change->{user};
120                 $change->{user}=$oiduser;
121         }
122         elsif (length $config{cgiurl}) {
123                 $change->{authorurl} = IkiWiki::cgiurl(
124                         do => "recentchanges_link",
125                         page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
126                 );
127         }
128
129         if (ref $change->{message}) {
130                 foreach my $field (@{$change->{message}}) {
131                         if (exists $field->{line}) {
132                                 # escape html
133                                 $field->{line} = encode_entities($field->{line});
134                                 # escape links and preprocessor stuff
135                                 $field->{line} = encode_entities($field->{line}, '\[\]');
136                         }
137                 }
138         }
139
140         # Fill out a template with the change info.
141         my $template=template("change.tmpl", blind_cache => 1);
142         $template->param(
143                 %$change,
144                 commitdate => displaytime($change->{when}, "%X %x"),
145                 wikiname => $config{wikiname},
146         );
147         
148         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
149                 if exists $config{url};
150         
151         IkiWiki::run_hooks(pagetemplate => sub {
152                 shift->(page => $page, destpage => $page,
153                         template => $template, rev => $change->{rev});
154         });
155
156         my $file=$page."._change";
157         writefile($file, $config{srcdir}, $template->output);
158         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
159
160         return $page;
161 }
162
163 1