]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/recentchanges.pm
propigate rcs_revert error message
[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 sessioncgi ($$) {
65         my ($q, $session) = @_;
66         my $do = $q->param('do');
67         my $rev = $q->param('rev');
68
69         return unless $do eq 'revert' && $rev;
70
71         my @changes=$IkiWiki::hooks{rcs}{rcs_preprevert}{call}->($rev);
72         IkiWiki::check_canchange(
73                 cgi => $q,
74                 session => $session,
75                 changes => \@changes,
76         );
77
78         eval q{use CGI::FormBuilder};
79         error($@) if $@;
80         my $form = CGI::FormBuilder->new(
81                 name => "revert",
82                 header => 0,
83                 charset => "utf-8",
84                 method => 'POST',
85                 javascript => 0,
86                 params => $q,
87                 action => $config{cgiurl},
88                 stylesheet => 1,
89                 template => { template('revert.tmpl') },
90         );
91         my $buttons=["Revert", "Cancel"];
92
93         $form->field(name => "sid", type => "hidden", value => $session->id,
94                 force => 1);
95         $form->field(name => "do", type => "hidden", value => "revert",
96                 force => 1);
97
98         IkiWiki::decode_form_utf8($form);
99
100         if ($form->submitted eq 'Revert' && $form->validate) {
101                 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
102                 my $r = $IkiWiki::hooks{rcs}{rcs_revert}{call}->($rev);
103                 error $r if defined $r;
104                 IkiWiki::disable_commit_hook();
105                 rcs_commit_staged(
106                         message => sprintf(gettext("This reverts commit %s"), $rev),
107                         session => $session,
108                 );
109                 IkiWiki::enable_commit_hook();
110         
111                 require IkiWiki::Render;
112                 IkiWiki::refresh();
113                 IkiWiki::saveindex();
114         }
115         elsif ($form->submitted ne 'Cancel') {
116                 $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
117                 $form->tmpl_param(diff => encode_entities(scalar IkiWiki::rcs_diff($rev)));
118                 $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
119                 IkiWiki::showform($form, $buttons, $session, $q);
120                 exit 0;
121         }
122
123         IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
124         exit 0;
125 }
126
127 # Enable the recentchanges link.
128 sub pagetemplate (@) {
129         my %params=@_;
130         my $template=$params{template};
131         my $page=$params{page};
132
133         if (defined $config{recentchangespage} && $config{rcs} &&
134             $template->query(name => "recentchangesurl") &&
135             $page ne $config{recentchangespage}) {
136                 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
137                 $template->param(have_actions => 1);
138         }
139 }
140
141 # Pages with extension _change have plain html markup, pass through.
142 sub htmlize (@) {
143         my %params=@_;
144         return $params{content};
145 }
146
147 sub store ($$$) {
148         my $change=shift;
149
150         my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
151
152         # Optimisation to avoid re-writing pages. Assumes commits never
153         # change (or that any changes are not important).
154         return $page if exists $pagesources{$page} && ! $config{rebuild};
155
156         # Limit pages to first 10, and add links to the changed pages.
157         my $is_excess = exists $change->{pages}[10];
158         delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
159         $change->{pages} = [
160                 map {
161                         if (length $config{cgiurl}) {
162                                 $_->{link} = "<a href=\"".
163                                         IkiWiki::cgiurl(
164                                                 do => "goto",
165                                                 page => $_->{page}
166                                         ).
167                                         "\" rel=\"nofollow\">".
168                                         pagetitle($_->{page}).
169                                         "</a>"
170                         }
171                         else {
172                                 $_->{link} = pagetitle($_->{page});
173                         }
174                         $_->{baseurl}="$config{url}/" if length $config{url};
175
176                         $_;
177                 } @{$change->{pages}}
178         ];
179         push @{$change->{pages}}, { link => '...' } if $is_excess;
180         
181         if (length $config{cgiurl} &&
182             exists $IkiWiki::hooks{rcs}{rcs_preprevert} &&
183             exists $IkiWiki::hooks{rcs}{rcs_revert}) {
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