summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 4d7023c1c1e5a0af91aad4841eb85c817dfefab3 (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. my $link=bestlink("", $page);
  86. if (! length $link) {
  87. print "Content-type: text/html\n\n";
  88. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  89. "<p>".
  90. sprintf(gettext("The page %s does not exist."),
  91. htmllink("", "", $page)).
  92. "</p>");
  93. }
  94. else {
  95. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  96. }
  97. exit;
  98. }
  99. }
  100. sub store ($$$) { #{{{
  101. my $change=shift;
  102. my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
  103. # Optimisation to avoid re-writing pages. Assumes commits never
  104. # change (or that any changes are not important).
  105. return $page if exists $pagesources{$page} && ! $config{rebuild};
  106. # Limit pages to first 10, and add links to the changed pages.
  107. my $is_excess = exists $change->{pages}[10];
  108. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  109. $change->{pages} = [
  110. map {
  111. if (length $config{cgiurl}) {
  112. $_->{link} = "<a href=\"".
  113. IkiWiki::cgiurl(
  114. do => "recentchanges_link",
  115. page => $_->{page}
  116. ).
  117. "\" rel=\"nofollow\">".
  118. pagetitle($_->{page}).
  119. "</a>"
  120. }
  121. else {
  122. $_->{link} = pagetitle($_->{page});
  123. }
  124. $_->{baseurl}="$config{url}/" if length $config{url};
  125. $_;
  126. } @{$change->{pages}}
  127. ];
  128. push @{$change->{pages}}, { link => '...' } if $is_excess;
  129. # See if the committer is an openid.
  130. $change->{author}=$change->{user};
  131. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  132. if (defined $oiduser) {
  133. $change->{authorurl}=$change->{user};
  134. $change->{user}=$oiduser;
  135. }
  136. elsif (length $config{cgiurl}) {
  137. $change->{authorurl} = IkiWiki::cgiurl(
  138. do => "recentchanges_link",
  139. page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
  140. );
  141. }
  142. if (ref $change->{message}) {
  143. foreach my $field (@{$change->{message}}) {
  144. if (exists $field->{line}) {
  145. # escape html
  146. $field->{line} = encode_entities($field->{line});
  147. # escape links and preprocessor stuff
  148. $field->{line} = encode_entities($field->{line}, '\[\]');
  149. }
  150. }
  151. }
  152. # Fill out a template with the change info.
  153. my $template=template("change.tmpl", blind_cache => 1);
  154. $template->param(
  155. %$change,
  156. commitdate => displaytime($change->{when}, "%X %x"),
  157. wikiname => $config{wikiname},
  158. );
  159. $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
  160. if exists $config{url};
  161. IkiWiki::run_hooks(pagetemplate => sub {
  162. shift->(page => $page, destpage => $page,
  163. template => $template, rev => $change->{rev});
  164. });
  165. my $file=$page."._change";
  166. writefile($file, $config{srcdir}, $template->output);
  167. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  168. return $page;
  169. } #}}}
  170. 1