summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 8ce9474be631dbd8ae428304e25d1d1d2adac3df (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::recentchanges;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. use Encode;
  7. use HTML::Entities;
  8. sub import {
  9. hook(type => "getsetup", id => "recentchanges", call => \&getsetup);
  10. hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
  11. hook(type => "refresh", id => "recentchanges", call => \&refresh);
  12. hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
  13. hook(type => "htmlize", id => "_change", call => \&htmlize);
  14. hook(type => "sessioncgi", id => "recentchanges", call => \&sessioncgi);
  15. # Load goto to fix up links from recentchanges
  16. IkiWiki::loadplugin("goto");
  17. # ... and transient as somewhere to put our internal pages
  18. IkiWiki::loadplugin("transient");
  19. }
  20. sub getsetup () {
  21. return
  22. plugin => {
  23. safe => 1,
  24. rebuild => 1,
  25. },
  26. recentchangespage => {
  27. type => "string",
  28. example => "recentchanges",
  29. description => "name of the recentchanges page",
  30. safe => 1,
  31. rebuild => 1,
  32. },
  33. recentchangesnum => {
  34. type => "integer",
  35. example => 100,
  36. description => "number of changes to track",
  37. safe => 1,
  38. rebuild => 0,
  39. },
  40. }
  41. sub checkconfig () {
  42. $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
  43. $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
  44. }
  45. sub refresh ($) {
  46. my %seen;
  47. # add new changes
  48. foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
  49. $seen{store($change, $config{recentchangespage})}=1;
  50. }
  51. # delete old and excess changes
  52. foreach my $page (keys %pagesources) {
  53. if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
  54. unlink($IkiWiki::Plugin::transient::transientdir.'/'.$pagesources{$page}) || unlink($config{srcdir}.'/'.$pagesources{$page});
  55. }
  56. }
  57. }
  58. sub sessioncgi ($$) {
  59. my ($q, $session) = @_;
  60. my $do = $q->param('do');
  61. my $rev = $q->param('rev');
  62. return unless $do eq 'revert' && $rev;
  63. my @changes=$IkiWiki::hooks{rcs}{rcs_preprevert}{call}->($rev);
  64. IkiWiki::check_canchange(
  65. cgi => $q,
  66. session => $session,
  67. changes => \@changes,
  68. );
  69. eval q{use CGI::FormBuilder};
  70. error($@) if $@;
  71. my $form = CGI::FormBuilder->new(
  72. name => "revert",
  73. header => 0,
  74. charset => "utf-8",
  75. method => 'POST',
  76. javascript => 0,
  77. params => $q,
  78. action => IkiWiki::cgiurl(),
  79. stylesheet => 1,
  80. template => { template('revert.tmpl') },
  81. fields => [qw{revertmessage do sid rev}],
  82. );
  83. my $buttons=["Revert", "Cancel"];
  84. $form->field(name => "revertmessage", type => "text", size => 80);
  85. $form->field(name => "sid", type => "hidden", value => $session->id,
  86. force => 1);
  87. $form->field(name => "do", type => "hidden", value => "revert",
  88. force => 1);
  89. IkiWiki::decode_form_utf8($form);
  90. if ($form->submitted eq 'Revert' && $form->validate) {
  91. IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
  92. my $message=sprintf(gettext("This reverts commit %s"), $rev);
  93. if (defined $form->field('revertmessage') &&
  94. length $form->field('revertmessage')) {
  95. $message=$form->field('revertmessage')."\n\n".$message;
  96. }
  97. my $r = $IkiWiki::hooks{rcs}{rcs_revert}{call}->($rev);
  98. error $r if defined $r;
  99. IkiWiki::disable_commit_hook();
  100. IkiWiki::rcs_commit_staged(
  101. message => $message,
  102. session => $session,
  103. );
  104. IkiWiki::enable_commit_hook();
  105. require IkiWiki::Render;
  106. IkiWiki::refresh();
  107. IkiWiki::saveindex();
  108. }
  109. elsif ($form->submitted ne 'Cancel') {
  110. $form->title(sprintf(gettext("confirm reversion of %s"), $rev));
  111. $form->tmpl_param(diff => encode_entities(scalar IkiWiki::rcs_diff($rev, 200)));
  112. $form->field(name => "rev", type => "hidden", value => $rev, force => 1);
  113. IkiWiki::showform($form, $buttons, $session, $q);
  114. exit 0;
  115. }
  116. IkiWiki::redirect($q, urlto($config{recentchangespage}));
  117. exit 0;
  118. }
  119. # Enable the recentchanges link.
  120. sub pagetemplate (@) {
  121. my %params=@_;
  122. my $template=$params{template};
  123. my $page=$params{page};
  124. if (defined $config{recentchangespage} && $config{rcs} &&
  125. $template->query(name => "recentchangesurl") &&
  126. $page ne $config{recentchangespage}) {
  127. $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
  128. $template->param(have_actions => 1);
  129. }
  130. }
  131. # Pages with extension _change have plain html markup, pass through.
  132. sub htmlize (@) {
  133. my %params=@_;
  134. return $params{content};
  135. }
  136. sub store ($$$) {
  137. my $change=shift;
  138. my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
  139. # Optimisation to avoid re-writing pages. Assumes commits never
  140. # change (or that any changes are not important).
  141. return $page if exists $pagesources{$page} && ! $config{rebuild};
  142. # Limit pages to first 10, and add links to the changed pages.
  143. my $is_excess = exists $change->{pages}[10];
  144. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  145. $change->{pages} = [
  146. map {
  147. if (length $config{cgiurl}) {
  148. $_->{link} = "<a href=\"".
  149. IkiWiki::cgiurl(
  150. do => "goto",
  151. page => $_->{page}
  152. ).
  153. "\" rel=\"nofollow\">".
  154. pagetitle($_->{page}).
  155. "</a>"
  156. }
  157. else {
  158. $_->{link} = pagetitle($_->{page});
  159. }
  160. $_;
  161. } @{$change->{pages}}
  162. ];
  163. push @{$change->{pages}}, { link => '...' } if $is_excess;
  164. if (length $config{cgiurl} &&
  165. exists $IkiWiki::hooks{rcs}{rcs_preprevert} &&
  166. exists $IkiWiki::hooks{rcs}{rcs_revert}) {
  167. $change->{reverturl} = IkiWiki::cgiurl(
  168. do => "revert",
  169. rev => $change->{rev}
  170. );
  171. }
  172. $change->{author}=$change->{user};
  173. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  174. if (defined $oiduser) {
  175. $change->{authorurl}=$change->{user};
  176. $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
  177. }
  178. elsif (length $config{cgiurl}) {
  179. $change->{authorurl} = IkiWiki::cgiurl(
  180. do => "goto",
  181. page => IkiWiki::userpage($change->{author}),
  182. );
  183. }
  184. if (ref $change->{message}) {
  185. foreach my $field (@{$change->{message}}) {
  186. if (exists $field->{line}) {
  187. # escape html
  188. $field->{line} = encode_entities($field->{line});
  189. # escape links and preprocessor stuff
  190. $field->{line} = encode_entities($field->{line}, '\[\]');
  191. }
  192. }
  193. }
  194. # Fill out a template with the change info.
  195. my $template=template("change.tmpl", blind_cache => 1);
  196. $template->param(
  197. %$change,
  198. commitdate => displaytime($change->{when}, "%X %x"),
  199. wikiname => $config{wikiname},
  200. );
  201. $template->param(permalink => urlto($config{recentchangespage})."#change-".titlepage($change->{rev}))
  202. if exists $config{url};
  203. IkiWiki::run_hooks(pagetemplate => sub {
  204. shift->(page => $page, destpage => $page,
  205. template => $template, rev => $change->{rev});
  206. });
  207. my $file=$page."._change";
  208. writefile($file, $IkiWiki::Plugin::transient::transientdir, $template->output);
  209. utime $change->{when}, $change->{when}, $IkiWiki::Plugin::transient::transientdir.'/'.$file;
  210. return $page;
  211. }
  212. 1