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