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