summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 7071596e6603586d6d2682aef0c4d37f56b25788 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::recentchanges;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.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 => "cgi", id => "recentchanges", call => \&cgi);
  15. }
  16. sub getsetup () {
  17. return
  18. plugin => {
  19. safe => 1,
  20. rebuild => 1,
  21. },
  22. recentchangespage => {
  23. type => "string",
  24. example => "recentchanges",
  25. description => "name of the recentchanges page",
  26. safe => 1,
  27. rebuild => 1,
  28. },
  29. recentchangesnum => {
  30. type => "integer",
  31. example => 100,
  32. description => "number of changes to track",
  33. safe => 1,
  34. rebuild => 0,
  35. },
  36. }
  37. sub checkconfig () {
  38. $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
  39. $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
  40. }
  41. sub refresh ($) {
  42. my %seen;
  43. # add new changes
  44. foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
  45. $seen{store($change, $config{recentchangespage})}=1;
  46. }
  47. # delete old and excess changes
  48. foreach my $page (keys %pagesources) {
  49. if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
  50. unlink($config{srcdir}.'/'.$pagesources{$page});
  51. }
  52. }
  53. }
  54. # Enable the recentchanges link on wiki pages.
  55. sub pagetemplate (@) {
  56. my %params=@_;
  57. my $template=$params{template};
  58. my $page=$params{page};
  59. if (defined $config{recentchangespage} && $config{rcs} &&
  60. $page ne $config{recentchangespage} &&
  61. $template->query(name => "recentchangesurl")) {
  62. $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
  63. $template->param(have_actions => 1);
  64. }
  65. }
  66. # Pages with extension _change have plain html markup, pass through.
  67. sub htmlize (@) {
  68. my %params=@_;
  69. return $params{content};
  70. }
  71. sub cgi ($) {
  72. my $cgi=shift;
  73. if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
  74. # This is a link from a change page to some
  75. # other page. Since the change pages are only generated
  76. # once, statically, links on them won't be updated if the
  77. # page they link to is deleted, or newly created, or
  78. # changes for whatever reason. So this CGI handles that
  79. # dynamic linking stuff.
  80. my $page=decode_utf8($cgi->param("page"));
  81. if (!defined $page) {
  82. error("missing page parameter");
  83. }
  84. IkiWiki::loadindex();
  85. # If the page is internal (like a comment), see if it has a
  86. # permalink. Comments do.
  87. if (IkiWiki::isinternal($page) &&
  88. defined $pagestate{$page}{meta}{permalink}) {
  89. IkiWiki::redirect($cgi,
  90. $pagestate{$page}{meta}{permalink});
  91. exit;
  92. }
  93. my $link=bestlink("", $page);
  94. if (! length $link) {
  95. print "Content-type: text/html\n\n";
  96. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  97. "<p>".
  98. sprintf(gettext("The page %s does not exist."),
  99. htmllink("", "", $page)).
  100. "</p>");
  101. }
  102. else {
  103. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  104. }
  105. exit;
  106. }
  107. }
  108. sub store ($$$) {
  109. my $change=shift;
  110. my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
  111. # Optimisation to avoid re-writing pages. Assumes commits never
  112. # change (or that any changes are not important).
  113. return $page if exists $pagesources{$page} && ! $config{rebuild};
  114. # Limit pages to first 10, and add links to the changed pages.
  115. my $is_excess = exists $change->{pages}[10];
  116. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  117. $change->{pages} = [
  118. map {
  119. if (length $config{cgiurl}) {
  120. $_->{link} = "<a href=\"".
  121. IkiWiki::cgiurl(
  122. do => "recentchanges_link",
  123. page => $_->{page}
  124. ).
  125. "\" rel=\"nofollow\">".
  126. pagetitle($_->{page}).
  127. "</a>"
  128. }
  129. else {
  130. $_->{link} = pagetitle($_->{page});
  131. }
  132. $_->{baseurl}="$config{url}/" if length $config{url};
  133. $_;
  134. } @{$change->{pages}}
  135. ];
  136. push @{$change->{pages}}, { link => '...' } if $is_excess;
  137. # See if the committer is an openid.
  138. $change->{author}=$change->{user};
  139. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  140. if (defined $oiduser) {
  141. $change->{authorurl}=$change->{user};
  142. $change->{user}=$oiduser;
  143. }
  144. elsif (length $config{cgiurl}) {
  145. $change->{authorurl} = IkiWiki::cgiurl(
  146. do => "recentchanges_link",
  147. page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
  148. );
  149. }
  150. if (ref $change->{message}) {
  151. foreach my $field (@{$change->{message}}) {
  152. if (exists $field->{line}) {
  153. # escape html
  154. $field->{line} = encode_entities($field->{line});
  155. # escape links and preprocessor stuff
  156. $field->{line} = encode_entities($field->{line}, '\[\]');
  157. }
  158. }
  159. }
  160. # Fill out a template with the change info.
  161. my $template=template("change.tmpl", blind_cache => 1);
  162. $template->param(
  163. %$change,
  164. commitdate => displaytime($change->{when}, "%X %x"),
  165. wikiname => $config{wikiname},
  166. );
  167. $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
  168. if exists $config{url};
  169. IkiWiki::run_hooks(pagetemplate => sub {
  170. shift->(page => $page, destpage => $page,
  171. template => $template, rev => $change->{rev});
  172. });
  173. my $file=$page."._change";
  174. writefile($file, $config{srcdir}, $template->output);
  175. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  176. return $page;
  177. }
  178. 1