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