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");
19 # ... and transient as somewhere to put our internal pages
20 IkiWiki::loadplugin("transient");
29 recentchangespage => {
31 example => "recentchanges",
32 description => "name of the recentchanges page",
39 description => "number of changes to track",
46 $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
47 $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
54 foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
55 $seen{store($change, $config{recentchangespage})}=1;
58 # delete old and excess changes
59 foreach my $page (keys %pagesources) {
60 if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
61 unlink($config{srcdir}.'/'.$pagesources{$page});
62 unlink($IkiWiki::Plugin::transient::transientdir.'/'.$pagesources{$page});
68 my ($q, $session) = @_;
69 my $do = $q->param('do');
70 my $rev = $q->param('rev');
72 return unless $do eq 'revert' && $rev;
74 my @changes=$IkiWiki::hooks{rcs}{rcs_preprevert}{call}->($rev);
75 IkiWiki::check_canchange(
81 eval q{use CGI::FormBuilder};
83 my $form = CGI::FormBuilder->new(
90 action => IkiWiki::cgiurl(),
92 template => { template('revert.tmpl') },
93 fields => [qw{revertmessage do sid rev}],
95 my $buttons=["Revert", "Cancel"];
97 $form->field(name => "revertmessage", type => "text", size => 80);
98 $form->field(name => "sid", type => "hidden", value => $session->id,
100 $form->field(name => "do", type => "hidden", value => "revert",
103 IkiWiki::decode_form_utf8($form);
105 if ($form->submitted eq 'Revert' && $form->validate) {
106 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
107 my $message=sprintf(gettext("This reverts commit %s"), $rev);
108 if (defined $form->field('revertmessage') &&
109 length $form->field('revertmessage')) {
110 $message=$form->field('revertmessage')."\n\n".$message;
112 my $r = $IkiWiki::hooks{rcs}{rcs_revert}{call}->($rev);
113 error $r if defined $r;
114 IkiWiki::disable_commit_hook();
115 IkiWiki::rcs_commit_staged(
119 IkiWiki::enable_commit_hook();
121 require IkiWiki::Render;
123 IkiWiki::saveindex();
125 elsif ($form->submitted ne 'Cancel') {
126 $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
127 $form->tmpl_param(diff => encode_entities(scalar IkiWiki::rcs_diff($rev, 200)));
128 $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
129 IkiWiki::showform($form, $buttons, $session, $q);
133 IkiWiki::redirect($q, urlto($config{recentchangespage}));
137 # Enable the recentchanges link.
138 sub pagetemplate (@) {
140 my $template=$params{template};
141 my $page=$params{page};
143 if (defined $config{recentchangespage} && $config{rcs} &&
144 $template->query(name => "recentchangesurl") &&
145 $page ne $config{recentchangespage}) {
146 $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
147 $template->param(have_actions => 1);
151 # Pages with extension _change have plain html markup, pass through.
154 return $params{content};
160 my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
162 # Optimisation to avoid re-writing pages. Assumes commits never
163 # change (or that any changes are not important).
164 return $page if exists $pagesources{$page} && ! $config{rebuild};
166 # Limit pages to first 10, and add links to the changed pages.
167 my $is_excess = exists $change->{pages}[10];
168 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
171 if (length $config{cgiurl}) {
172 $_->{link} = "<a href=\"".
177 "\" rel=\"nofollow\">".
178 pagetitle($_->{page}).
182 $_->{link} = pagetitle($_->{page});
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 => urlto($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, $IkiWiki::Plugin::transient::transientdir, $template->output);
241 utime $change->{when}, $change->{when}, $IkiWiki::Plugin::transient::transientdir.'/'.$file;