]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/recentchanges.pm
remove todo item
[git.ikiwiki.info.git] / IkiWiki / Plugin / recentchanges.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchanges;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
8 use HTML::Entities;
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 }
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 }
43 sub checkconfig () {
44         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
45         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
46 }
48 sub refresh ($) {
49         my %seen;
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 }
64 sub confirmation_form {
65         my ($q, $session, $rev) = @_;
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         );
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);
86         return $f, ["Revert", "Cancel"];
87 }
89 sub sessioncgi ($$) {
90         my ($q, $session) = @_;
91         my $do = $q->param('do');
92         my $rev = $q->param('rev');
94         return unless $do eq 'revert' && $rev;
96         my @changes=$IkiWiki::hooks{rcs}{rcs_preprevert}{call}->($rev);
97         IkiWiki::check_canchange(
98                 cgi => $q,
99                 session => $session,
100                 changes => \@changes,
101         );
103         my ($form, $buttons) = confirmation_form($q, $session);
104         IkiWiki::decode_form_utf8($form);
106         if ($form->submitted eq 'Revert' && $form->validate) {
107                 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
108                 IkiWiki::disable_commit_hook();
109                 my $r = $IkiWiki::hooks{rcs}{rcs_revert}{call}->($rev);
110                 if (! defined $r) { # success
111                         rcs_commit_staged(
112                                 message => sprintf(gettext("This reverts commit %s"), $rev),
113                                 session => $session,
114                                 rev => $rev,
115                         );
116                 }
117                 IkiWiki::enable_commit_hook();
118         
119                 if (defined $r) {
120                         die "Revert '$rev' failed.";
121                 }
122                 else {
123                         require IkiWiki::Render;
124                         IkiWiki::refresh();
125                         IkiWiki::saveindex();
126                 }
127         }
128         elsif ($form->submitted ne 'Cancel') {
129                 $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
130                 $form->tmpl_param(diff => encode_entities(scalar IkiWiki::rcs_diff($rev)));
131                 $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
132                 IkiWiki::showform($form, $buttons, $session, $q);
133                 exit 0;
134         }
136         IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
137         exit 0;
140 # Enable the recentchanges link.
141 sub pagetemplate (@) {
142         my %params=@_;
143         my $template=$params{template};
144         my $page=$params{page};
146         if (defined $config{recentchangespage} && $config{rcs} &&
147             $template->query(name => "recentchangesurl") &&
148             $page ne $config{recentchangespage}) {
149                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
150                 $template->param(have_actions => 1);
151         }
154 # Pages with extension _change have plain html markup, pass through.
155 sub htmlize (@) {
156         my %params=@_;
157         return $params{content};
160 sub store ($$$) {
161         my $change=shift;
163         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
165         # Optimisation to avoid re-writing pages. Assumes commits never
166         # change (or that any changes are not important).
167         return $page if exists $pagesources{$page} && ! $config{rebuild};
169         # Limit pages to first 10, and add links to the changed pages.
170         my $is_excess = exists $change->{pages}[10];
171         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
172         $change->{pages} = [
173                 map {
174                         if (length $config{cgiurl}) {
175                                 $_->{link} = "<a href=\"".
176                                         IkiWiki::cgiurl(
177                                                 do => "goto",
178                                                 page => $_->{page}
179                                         ).
180                                         "\" rel=\"nofollow\">".
181                                         pagetitle($_->{page}).
182                                         "</a>"
183                         }
184                         else {
185                                 $_->{link} = pagetitle($_->{page});
186                         }
187                         $_->{baseurl}="$config{url}/" if length $config{url};
189                         $_;
190                 } @{$change->{pages}}
191         ];
192         push @{$change->{pages}}, { link => '...' } if $is_excess;
193         
194         if (length $config{cgiurl} &&
195             exists $IkiWiki::hooks{rcs}{rcs_preprevert} &&
196             exists $IkiWiki::hooks{rcs}{rcs_revert}) {
197                 $change->{reverturl} = IkiWiki::cgiurl(
198                         do => "revert",
199                         rev => $change->{rev}
200                 );
201         }
203         $change->{author}=$change->{user};
204         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
205         if (defined $oiduser) {
206                 $change->{authorurl}=$change->{user};
207                 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
208         }
209         elsif (length $config{cgiurl}) {
210                 $change->{authorurl} = IkiWiki::cgiurl(
211                         do => "goto",
212                         page => IkiWiki::userpage($change->{author}),
213                 );
214         }
216         if (ref $change->{message}) {
217                 foreach my $field (@{$change->{message}}) {
218                         if (exists $field->{line}) {
219                                 # escape html
220                                 $field->{line} = encode_entities($field->{line});
221                                 # escape links and preprocessor stuff
222                                 $field->{line} = encode_entities($field->{line}, '\[\]');
223                         }
224                 }
225         }
227         # Fill out a template with the change info.
228         my $template=template("change.tmpl", blind_cache => 1);
229         $template->param(
230                 %$change,
231                 commitdate => displaytime($change->{when}, "%X %x"),
232                 wikiname => $config{wikiname},
233         );
234         
235         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
236                 if exists $config{url};
237         
238         IkiWiki::run_hooks(pagetemplate => sub {
239                 shift->(page => $page, destpage => $page,
240                         template => $template, rev => $change->{rev});
241         });
243         my $file=$page."._change";
244         writefile($file, $config{srcdir}, $template->output);
245         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
247         return $page;