2 package IkiWiki::Plugin::recentchanges;
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");
27 recentchangespage => {
29 example => "recentchanges",
30 description => "name of the recentchanges page",
37 description => "number of changes to track",
44 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
45 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
52 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
53 $seen{store($change, $config{recentchangespage})}=1;
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});
64 sub confirmation_form {
65 my ($q, $session, $rev) = @_;
67 eval q{use CGI::FormBuilder};
69 my $f = CGI::FormBuilder->new(
76 action => $config{cgiurl},
78 template => { template('revert.tmpl') },
81 $f->field(name => "sid", type => "hidden", value => $session->id,
83 $f->field(name => "do", type => "hidden", value => "revert",
86 return $f, ["Revert", "Cancel"];
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(
100 changes => \@changes,
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
112 message => sprintf(gettext("This reverts commit %s"), $rev),
117 IkiWiki::enable_commit_hook();
120 die "Revert '$rev' failed.";
123 require IkiWiki::Render;
125 IkiWiki::saveindex();
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);
136 IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
140 # Enable the recentchanges link.
141 sub pagetemplate (@) {
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);
154 # Pages with extension _change have plain html markup, pass through.
157 return $params{content};
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;
174 if (length $config{cgiurl}) {
175 $_->{link} = "<a href=\"".
180 "\" rel=\"nofollow\">".
181 pagetitle($_->{page}).
185 $_->{link} = pagetitle($_->{page});
187 $_->{baseurl}="$config{url}/" if length $config{url};
190 } @{$change->{pages}}
192 push @{$change->{pages}}, { link => '...' } if $is_excess;
194 if (length $config{cgiurl} &&
195 exists $IkiWiki::hooks{rcs}{rcs_preprevert} &&
196 exists $IkiWiki::hooks{rcs}{rcs_revert}) {
197 $change->{reverturl} = IkiWiki::cgiurl(
199 rev => $change->{rev}
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;
209 elsif (length $config{cgiurl}) {
210 $change->{authorurl} = IkiWiki::cgiurl(
212 page => IkiWiki::userpage($change->{author}),
216 if (ref $change->{message}) {
217 foreach my $field (@{$change->{message}}) {
218 if (exists $field->{line}) {
220 $field->{line} = encode_entities($field->{line});
221 # escape links and preprocessor stuff
222 $field->{line} = encode_entities($field->{line}, '\[\]');
227 # Fill out a template with the change info.
228 my $template=template("change.tmpl", blind_cache => 1);
231 commitdate => displaytime($change->{when}, "%X %x"),
232 wikiname => $config{wikiname},
235 $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
236 if exists $config{url};
238 IkiWiki::run_hooks(pagetemplate => sub {
239 shift->(page => $page, destpage => $page,
240 template => $template, rev => $change->{rev});
243 my $file=$page."._change";
244 writefile($file, $config{srcdir}, $template->output);
245 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";