79d175328fbd775f696c0c2812e77924a335ae63
[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", force => 1);
84
85     return $f, ["Revert", "Cancel"];
86 }
87
88 sub sessioncgi ($$) {
89     my ($q, $session) = @_;
90     my $do = $q->param('do');
91     my $rev = $q->param('rev');
92
93     return unless $do eq 'revert' && $rev;
94
95     # FIXME rcs_preprevert ??
96     IkiWiki::check_canedit('FIXME', $q, $session);
97
98     my ($form, $buttons) = confirmation_form($q, $session);
99     IkiWiki::decode_form_utf8($form);
100
101     if($form->submitted eq 'Revert' && $form->validate) {
102         IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
103
104         IkiWiki::disable_commit_hook();
105         my $r = IkiWiki::rcs_revert(
106                          session => $session,
107                          rev => $rev);
108         IkiWiki::enable_commit_hook();
109
110         if($r) {
111             die "FIXME revert '$rev' failed.";
112         } else {
113             require IkiWiki::Render;
114             IkiWiki::refresh();
115             IkiWiki::saveindex();
116             # FIXME indicate success.
117         }
118     } else {
119         $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
120         my $patch_contents = IkiWiki::rcs_showpatch($rev);
121         $form->tmpl_param(patch_contents => encode_entities($patch_contents));
122         $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
123         IkiWiki::showform($form, $buttons, $session, $q);
124         exit 0;
125     }
126
127     IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
128     exit 0;
129 }
130
131 # Enable the recentchanges link.
132 sub pagetemplate (@) {
133         my %params=@_;
134         my $template=$params{template};
135         my $page=$params{page};
136
137         if (defined $config{recentchangespage} && $config{rcs} &&
138             $template->query(name => "recentchangesurl") &&
139             $page ne $config{recentchangespage}) {
140                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
141                 $template->param(have_actions => 1);
142         }
143 }
144
145 # Pages with extension _change have plain html markup, pass through.
146 sub htmlize (@) {
147         my %params=@_;
148         return $params{content};
149 }
150
151 sub store ($$$) {
152         my $change=shift;
153
154         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
155
156         # Optimisation to avoid re-writing pages. Assumes commits never
157         # change (or that any changes are not important).
158         return $page if exists $pagesources{$page} && ! $config{rebuild};
159
160         # Limit pages to first 10, and add links to the changed pages.
161         my $is_excess = exists $change->{pages}[10];
162         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
163         $change->{pages} = [
164                 map {
165                         if (length $config{cgiurl}) {
166                                 $_->{link} = "<a href=\"".
167                                         IkiWiki::cgiurl(
168                                                 do => "goto",
169                                                 page => $_->{page}
170                                         ).
171                                         "\" rel=\"nofollow\">".
172                                         pagetitle($_->{page}).
173                                         "</a>"
174                         }
175                         else {
176                                 $_->{link} = pagetitle($_->{page});
177                         }
178                         $_->{baseurl}="$config{url}/" if length $config{url};
179
180                         $_;
181                 } @{$change->{pages}}
182         ];
183         push @{$change->{pages}}, { link => '...' } if $is_excess;
184
185         if (length $config{cgiurl}) {
186             $change->{reverturl} = IkiWiki::cgiurl(
187                   do => "revert",
188                   rev => $change->{rev}
189                 );
190         }
191
192         $change->{author}=$change->{user};
193         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
194         if (defined $oiduser) {
195                 $change->{authorurl}=$change->{user};
196                 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
197         }
198         elsif (length $config{cgiurl}) {
199                 $change->{authorurl} = IkiWiki::cgiurl(
200                         do => "goto",
201                         page => IkiWiki::userpage($change->{author}),
202                 );
203         }
204
205         if (ref $change->{message}) {
206                 foreach my $field (@{$change->{message}}) {
207                         if (exists $field->{line}) {
208                                 # escape html
209                                 $field->{line} = encode_entities($field->{line});
210                                 # escape links and preprocessor stuff
211                                 $field->{line} = encode_entities($field->{line}, '\[\]');
212                         }
213                 }
214         }
215
216         # Fill out a template with the change info.
217         my $template=template("change.tmpl", blind_cache => 1);
218         $template->param(
219                 %$change,
220                 commitdate => displaytime($change->{when}, "%X %x"),
221                 wikiname => $config{wikiname},
222         );
223         
224         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
225                 if exists $config{url};
226         
227         IkiWiki::run_hooks(pagetemplate => sub {
228                 shift->(page => $page, destpage => $page,
229                         template => $template, rev => $change->{rev});
230         });
231
232         my $file=$page."._change";
233         writefile($file, $config{srcdir}, $template->output);
234         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
235
236         return $page;
237 }
238
239 1