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::rcs_preprevert(cgi => $q, session => $session, rev => $rev);
98 my ($form, $buttons) = confirmation_form($q, $session);
99 IkiWiki::decode_form_utf8($form);
101 if ($form->submitted eq 'Revert' && $form->validate) {
102 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
103 IkiWiki::disable_commit_hook();
104 my $r = IkiWiki::rcs_revert(
107 IkiWiki::enable_commit_hook();
110 die "Revert '$rev' failed.";
113 require IkiWiki::Render;
115 IkiWiki::saveindex();
119 $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
120 $form->tmpl_param(diff => encode_entities(scalar IkiWiki::rcs_diff($rev)));
121 $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
122 IkiWiki::showform($form, $buttons, $session, $q);
126 IkiWiki::redirect($q, urlto($config{recentchangespage}, ''));
130 # Enable the recentchanges link.
131 sub pagetemplate (@) {
133 my $template=$params{template};
134 my $page=$params{page};
136 if (defined $config{recentchangespage} && $config{rcs} &&
137 $template->query(name => "recentchangesurl") &&
138 $page ne $config{recentchangespage}) {
139 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
140 $template->param(have_actions => 1);
144 # Pages with extension _change have plain html markup, pass through.
147 return $params{content};
153 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
155 # Optimisation to avoid re-writing pages. Assumes commits never
156 # change (or that any changes are not important).
157 return $page if exists $pagesources{$page} && ! $config{rebuild};
159 # Limit pages to first 10, and add links to the changed pages.
160 my $is_excess = exists $change->{pages}[10];
161 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
164 if (length $config{cgiurl}) {
165 $_->{link} = "<a href=\"".
170 "\" rel=\"nofollow\">".
171 pagetitle($_->{page}).
175 $_->{link} = pagetitle($_->{page});
177 $_->{baseurl}="$config{url}/" if length $config{url};
180 } @{$change->{pages}}
182 push @{$change->{pages}}, { link => '...' } if $is_excess;
184 if (length $config{cgiurl}) {
185 $change->{reverturl} = IkiWiki::cgiurl(
187 rev => $change->{rev}
191 $change->{author}=$change->{user};
192 my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
193 if (defined $oiduser) {
194 $change->{authorurl}=$change->{user};
195 $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
197 elsif (length $config{cgiurl}) {
198 $change->{authorurl} = IkiWiki::cgiurl(
200 page => IkiWiki::userpage($change->{author}),
204 if (ref $change->{message}) {
205 foreach my $field (@{$change->{message}}) {
206 if (exists $field->{line}) {
208 $field->{line} = encode_entities($field->{line});
209 # escape links and preprocessor stuff
210 $field->{line} = encode_entities($field->{line}, '\[\]');
215 # Fill out a template with the change info.
216 my $template=template("change.tmpl", blind_cache => 1);
219 commitdate => displaytime($change->{when}, "%X %x"),
220 wikiname => $config{wikiname},
223 $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
224 if exists $config{url};
226 IkiWiki::run_hooks(pagetemplate => sub {
227 shift->(page => $page, destpage => $page,
228 template => $template, rev => $change->{rev});
231 my $file=$page."._change";
232 writefile($file, $config{srcdir}, $template->output);
233 utime $change->{when}, $change->{when}, "$config{srcdir}/$file";