56e17dcca4035b5d668cfd4b6d69e048c559a143
[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         hook(type => "sessioncgi", id => "recentchanges", call => \&sessioncgi);
17         # Load goto to fix up links from recentchanges
18         IkiWiki::loadplugin("goto");
19 }
20
21 sub getsetup () {
22         return
23                 plugin => {
24                         safe => 1,
25                         rebuild => 1,
26                 },
27                 recentchangespage => {
28                         type => "string",
29                         example => "recentchanges",
30                         description => "name of the recentchanges page",
31                         safe => 1,
32                         rebuild => 1,
33                 },
34                 recentchangesnum => {
35                         type => "integer",
36                         example => 100,
37                         description => "number of changes to track",
38                         safe => 1,
39                         rebuild => 0,
40                 },
41 }
42
43 sub checkconfig () {
44         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
45         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
46 }
47
48 sub refresh ($) {
49         my %seen;
50
51         # add new changes
52         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
53                 $seen{store($change, $config{recentchangespage})}=1;
54         }
55         
56         # delete old and excess changes
57         foreach my $page (keys %pagesources) {
58                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
59                         unlink($config{srcdir}.'/'.$pagesources{$page});
60                 }
61         }
62 }
63
64 sub confirmation_form {
65         my ($q, $session, $rev) = @_;
66
67         eval q{use CGI::FormBuilder};
68         error($@) if $@;
69         my $f = CGI::FormBuilder->new(
70                 name => "revert",
71                 header => 0,
72                 charset => "utf-8",
73                 method => 'POST',
74                 javascript => 0,
75                 params => $q,
76                 action => $config{cgiurl},
77                 stylesheet => 1,
78                 template => { template('revert.tmpl') },
79         );
80
81         $f->field(name => "sid", type => "hidden", value => $session->id,
82                 force => 1);
83         $f->field(name => "do", type => "hidden", value => "revert",
84                 force => 1);
85
86         return $f, ["Revert", "Cancel"];
87 }
88
89 sub sessioncgi ($$) {
90         my ($q, $session) = @_;
91         my $do = $q->param('do');
92         my $rev = $q->param('rev');
93
94         return unless $do eq 'revert' && $rev;
95
96         my @changes=$IkiWiki::hooks{rcs}{rcs_preprevert}{call}->($rev);
97         require IkiWiki::Receive;
98         IkiWiki::Receive::test_changes(
99                 cgi => $q,
100                 session => $session,
101                 changes => \@changes,
102         );
103
104         my ($form, $buttons) = confirmation_form($q, $session);
105         IkiWiki::decode_form_utf8($form);
106
107         if ($form->submitted eq 'Revert' && $form->validate) {
108                 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
109                 IkiWiki::disable_commit_hook();
110                 my $r = $IkiWiki::hooks{rcs}{rcs_revert}{call}->($rev);
111                 if (! defined $r) { # success
112                         rcs_commit_staged(
113                                 message => sprintf(gettext("This reverts commit %s"), $rev),
114                                 session => $session,
115                                 rev => $rev,
116                         );
117                 }
118                 IkiWiki::enable_commit_hook();
119         
120                 if (defined $r) {
121                         die "Revert '$rev' failed.";
122                 }
123                 else {
124                         require IkiWiki::Render;
125                         IkiWiki::refresh();
126                         IkiWiki::saveindex();
127                 }
128         }
129         elsif ($form->submitted ne 'Cancel') {
130                 $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
131                 $form->tmpl_param(diff => encode_entities(scalar IkiWiki::rcs_diff($rev)));
132                 $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
133                 IkiWiki::showform($form, $buttons, $session, $q);
134                 exit 0;
135         }
136
137         IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
138         exit 0;
139 }
140
141 # Enable the recentchanges link.
142 sub pagetemplate (@) {
143         my %params=@_;
144         my $template=$params{template};
145         my $page=$params{page};
146
147         if (defined $config{recentchangespage} && $config{rcs} &&
148             $template->query(name => "recentchangesurl") &&
149             $page ne $config{recentchangespage}) {
150                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
151                 $template->param(have_actions => 1);
152         }
153 }
154
155 # Pages with extension _change have plain html markup, pass through.
156 sub htmlize (@) {
157         my %params=@_;
158         return $params{content};
159 }
160
161 sub store ($$$) {
162         my $change=shift;
163
164         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
165
166         # Optimisation to avoid re-writing pages. Assumes commits never
167         # change (or that any changes are not important).
168         return $page if exists $pagesources{$page} && ! $config{rebuild};
169
170         # Limit pages to first 10, and add links to the changed pages.
171         my $is_excess = exists $change->{pages}[10];
172         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
173         $change->{pages} = [
174                 map {
175                         if (length $config{cgiurl}) {
176                                 $_->{link} = "<a href=\"".
177                                         IkiWiki::cgiurl(
178                                                 do => "goto",
179                                                 page => $_->{page}
180                                         ).
181                                         "\" rel=\"nofollow\">".
182                                         pagetitle($_->{page}).
183                                         "</a>"
184                         }
185                         else {
186                                 $_->{link} = pagetitle($_->{page});
187                         }
188                         $_->{baseurl}="$config{url}/" if length $config{url};
189
190                         $_;
191                 } @{$change->{pages}}
192         ];
193         push @{$change->{pages}}, { link => '...' } if $is_excess;
194         
195         if (length $config{cgiurl} &&
196             exists $IkiWiki::hooks{rcs}{rcs_preprevert} &&
197             exists $IkiWiki::hooks{rcs}{rcs_revert}) {
198                 $change->{reverturl} = IkiWiki::cgiurl(
199                         do => "revert",
200                         rev => $change->{rev}
201                 );
202         }
203
204         $change->{author}=$change->{user};
205         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
206         if (defined $oiduser) {
207                 $change->{authorurl}=$change->{user};
208                 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
209         }
210         elsif (length $config{cgiurl}) {
211                 $change->{authorurl} = IkiWiki::cgiurl(
212                         do => "goto",
213                         page => IkiWiki::userpage($change->{author}),
214                 );
215         }
216
217         if (ref $change->{message}) {
218                 foreach my $field (@{$change->{message}}) {
219                         if (exists $field->{line}) {
220                                 # escape html
221                                 $field->{line} = encode_entities($field->{line});
222                                 # escape links and preprocessor stuff
223                                 $field->{line} = encode_entities($field->{line}, '\[\]');
224                         }
225                 }
226         }
227
228         # Fill out a template with the change info.
229         my $template=template("change.tmpl", blind_cache => 1);
230         $template->param(
231                 %$change,
232                 commitdate => displaytime($change->{when}, "%X %x"),
233                 wikiname => $config{wikiname},
234         );
235         
236         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
237                 if exists $config{url};
238         
239         IkiWiki::run_hooks(pagetemplate => sub {
240                 shift->(page => $page, destpage => $page,
241                         template => $template, rev => $change->{rev});
242         });
243
244         my $file=$page."._change";
245         writefile($file, $config{srcdir}, $template->output);
246         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
247
248         return $page;
249 }
250
251 1