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 $IkiWiki::hooks{rcs}{rcs_preprevert}{call}->(
97 cgi => $q, session => $session, rev => $rev);
99 my ($form, $buttons) = confirmation_form($q, $session);
100 IkiWiki::decode_form_utf8($form);
102 if ($form->submitted eq 'Revert' && $form->validate) {
103 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
104 IkiWiki::disable_commit_hook();
105 my $r = $IkiWiki::hooks{rcs}{rcs_revert}{call}->($rev);
106 if (! defined $r) { # success
108 message => sprintf(gettext("This reverts commit %s"), $rev),
113 IkiWiki::enable_commit_hook();
116 die "Revert '$rev' failed.";
119 require IkiWiki::Render;
121 IkiWiki::saveindex();
124 elsif ($form->submitted ne 'Cancel') {
125 $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
126 $form->tmpl_param(diff => encode_entities(scalar IkiWiki::rcs_diff($rev)));
127 $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
128 IkiWiki::showform($form, $buttons, $session, $q);
132 IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
136 # Enable the recentchanges link.
137 sub pagetemplate (@) {
139 my $template=$params{template};
140 my $page=$params{page};
142 if (defined $config{recentchangespage} && $config{rcs} &&
143 $template->query(name => "recentchangesurl") &&
144 $page ne $config{recentchangespage}) {
145 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
146 $template->param(have_actions => 1);
150 # Pages with extension _change have plain html markup, pass through.
153 return $params{content};
159 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
161 # Optimisation to avoid re-writing pages. Assumes commits never
162 # change (or that any changes are not important).
163 return $page if exists $pagesources{$page} && ! $config{rebuild};
165 # Limit pages to first 10, and add links to the changed pages.
166 my $is_excess = exists $change->{pages}[10];
167 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
170 if (length $config{cgiurl}) {
171 $_->{link} = "<a href=\"".
176 "\" rel=\"nofollow\">".
177 pagetitle($_->{page}).
181 $_->{link} = pagetitle($_->{page});
183 $_->{baseurl}="$config{url}/" if length $config{url};
186 } @{$change->{pages}}
188 push @{$change->{pages}}, { link => '...' } if $is_excess;
190 if (length $config{cgiurl} &&
191 exists $IkiWiki::hooks{rcs}{rcs_preprevert} &&
192 exists $IkiWiki::hooks{rcs}{rcs_revert}) {
193 $change->{reverturl} = IkiWiki::cgiurl(
195 rev => $change->{rev}
199 $change->{author}=$change->{user};
200 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
201 if (defined $oiduser) {
202 $change->{authorurl}=$change->{user};
203 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
205 elsif (length $config{cgiurl}) {
206 $change->{authorurl} = IkiWiki::cgiurl(
208 page => IkiWiki::userpage($change->{author}),
212 if (ref $change->{message}) {
213 foreach my $field (@{$change->{message}}) {
214 if (exists $field->{line}) {
216 $field->{line} = encode_entities($field->{line});
217 # escape links and preprocessor stuff
218 $field->{line} = encode_entities($field->{line}, '\[\]');
223 # Fill out a template with the change info.
224 my $template=template("change.tmpl", blind_cache => 1);
227 commitdate => displaytime($change->{when}, "%X %x"),
228 wikiname => $config{wikiname},
231 $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
232 if exists $config{url};
234 IkiWiki::run_hooks(pagetemplate => sub {
235 shift->(page => $page, destpage => $page,
236 template => $template, rev => $change->{rev});
239 my $file=$page."._change";
240 writefile($file, $config{srcdir}, $template->output);
241 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";