]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/recentchanges.pm
First cut at the revert plugin.
[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         # Load goto to fix up links from recentchanges
17         IkiWiki::loadplugin("goto");
18 }
19
20 sub getsetup () {
21         return
22                 plugin => {
23                         safe => 1,
24                         rebuild => 1,
25                 },
26                 recentchangespage => {
27                         type => "string",
28                         example => "recentchanges",
29                         description => "name of the recentchanges page",
30                         safe => 1,
31                         rebuild => 1,
32                 },
33                 recentchangesnum => {
34                         type => "integer",
35                         example => 100,
36                         description => "number of changes to track",
37                         safe => 1,
38                         rebuild => 0,
39                 },
40 }
41
42 sub checkconfig () {
43         $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
44         $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
45 }
46
47 sub refresh ($) {
48         my %seen;
49
50         # add new changes
51         foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
52                 $seen{store($change, $config{recentchangespage})}=1;
53         }
54         
55         # delete old and excess changes
56         foreach my $page (keys %pagesources) {
57                 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
58                         unlink($config{srcdir}.'/'.$pagesources{$page});
59                 }
60         }
61 }
62
63 sub confirmation_form {
64     my ($q, $session, $rev) = @_;
65
66     eval q{use CGI::FormBuilder};
67     error($@) if $@;
68     my $f = CGI::FormBuilder->new(
69                 name => "revert",
70                 header => 0,
71                 charset => "utf-8",
72                 method => 'POST',
73                 javascript => 0,
74                 params => $q,
75                 action => $config{cgiurl},
76                 stylesheet => 1,
77                 template => { template('revert.tmpl') },
78         );
79
80     $f->field(name => "sid", type => "hidden", value => $session->id,
81               force => 1);
82     $f->field(name => "do", type => "hidden", value => "revert", force => 1);
83     $f->field(name => "rev", type => "hidden", value => $rev, 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             IkiWiki::refresh();
114             IkiWiki::saveindex();
115             # FIXME indicate success.
116         }
117     } else {
118         $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
119         my $patch_contents = IkiWiki::rcs_showpatch($rev);
120         $form->tmpl_param(patch_contents => encode_entities($patch_contents));
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         $change->{author}=$change->{user};
184         my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
185         if (defined $oiduser) {
186                 $change->{authorurl}=$change->{user};
187                 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
188         }
189         elsif (length $config{cgiurl}) {
190                 $change->{authorurl} = IkiWiki::cgiurl(
191                         do => "goto",
192                         page => IkiWiki::userpage($change->{author}),
193                 );
194         }
195
196         if (ref $change->{message}) {
197                 foreach my $field (@{$change->{message}}) {
198                         if (exists $field->{line}) {
199                                 # escape html
200                                 $field->{line} = encode_entities($field->{line});
201                                 # escape links and preprocessor stuff
202                                 $field->{line} = encode_entities($field->{line}, '\[\]');
203                         }
204                 }
205         }
206
207         # Fill out a template with the change info.
208         my $template=template("change.tmpl", blind_cache => 1);
209         $template->param(
210                 %$change,
211                 commitdate => displaytime($change->{when}, "%X %x"),
212                 wikiname => $config{wikiname},
213         );
214         
215         $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
216                 if exists $config{url};
217         
218         IkiWiki::run_hooks(pagetemplate => sub {
219                 shift->(page => $page, destpage => $page,
220                         template => $template, rev => $change->{rev});
221         });
222
223         my $file=$page."._change";
224         writefile($file, $config{srcdir}, $template->output);
225         utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
226
227         return $page;
228 }
229
230 1