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