summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 562f61d40f3cc4729f5da0f01e3d6b2c4a20660d (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 => $config{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)));
  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. $_->{baseurl}="$config{url}/" if length $config{url};
  159. $_;
  160. } @{$change->{pages}}
  161. ];
  162. push @{$change->{pages}}, { link => '...' } if $is_excess;
  163. if (length $config{cgiurl} &&
  164. exists $IkiWiki::hooks{rcs}{rcs_preprevert} &&
  165. exists $IkiWiki::hooks{rcs}{rcs_revert}) {
  166. $change->{reverturl} = IkiWiki::cgiurl(
  167. do => "revert",
  168. rev => $change->{rev}
  169. );
  170. }
  171. $change->{author}=$change->{user};
  172. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  173. if (defined $oiduser) {
  174. $change->{authorurl}=$change->{user};
  175. $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
  176. }
  177. elsif (length $config{cgiurl}) {
  178. $change->{authorurl} = IkiWiki::cgiurl(
  179. do => "goto",
  180. page => IkiWiki::userpage($change->{author}),
  181. );
  182. }
  183. if (ref $change->{message}) {
  184. foreach my $field (@{$change->{message}}) {
  185. if (exists $field->{line}) {
  186. # escape html
  187. $field->{line} = encode_entities($field->{line});
  188. # escape links and preprocessor stuff
  189. $field->{line} = encode_entities($field->{line}, '\[\]');
  190. }
  191. }
  192. }
  193. # Fill out a template with the change info.
  194. my $template=template("change.tmpl", blind_cache => 1);
  195. $template->param(
  196. %$change,
  197. commitdate => displaytime($change->{when}, "%X %x"),
  198. wikiname => $config{wikiname},
  199. );
  200. $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
  201. if exists $config{url};
  202. IkiWiki::run_hooks(pagetemplate => sub {
  203. shift->(page => $page, destpage => $page,
  204. template => $template, rev => $change->{rev});
  205. });
  206. my $file=$page."._change";
  207. writefile($file, $config{srcdir}, $template->output);
  208. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  209. return $page;
  210. }
  211. 1