summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 65044080fe78d835d1d4b57e627ec0a9c8052661 (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 => "pageactions", id => "recentchanges", call => \&pageactions);
  13. hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
  14. hook(type => "htmlize", id => "_change", call => \&htmlize);
  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. # Enable the recentchanges link on wiki pages.
  57. sub pageactions (@) {
  58. my %params=@_;
  59. my $page=$params{page};
  60. if (defined $config{recentchangespage} && $config{rcs} &&
  61. $page ne $config{recentchangespage}) {
  62. return htmllink($page, $page, $config{recentchangespage},
  63. gettext("RecentChanges"));
  64. }
  65. }
  66. # Backwards compatability for templates still using
  67. # RECENTCHANGESURL.
  68. sub pagetemplate (@) {
  69. my %params=@_;
  70. my $template=$params{template};
  71. my $page=$params{page};
  72. if (defined $config{recentchangespage} && $config{rcs} &&
  73. $template->query(name => "recentchangesurl") &&
  74. $page ne $config{recentchangespage}) {
  75. $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
  76. $template->param(have_actions => 1);
  77. }
  78. }
  79. # Pages with extension _change have plain html markup, pass through.
  80. sub htmlize (@) {
  81. my %params=@_;
  82. return $params{content};
  83. }
  84. sub store ($$$) {
  85. my $change=shift;
  86. my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
  87. # Optimisation to avoid re-writing pages. Assumes commits never
  88. # change (or that any changes are not important).
  89. return $page if exists $pagesources{$page} && ! $config{rebuild};
  90. # Limit pages to first 10, and add links to the changed pages.
  91. my $is_excess = exists $change->{pages}[10];
  92. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  93. $change->{pages} = [
  94. map {
  95. if (length $config{cgiurl}) {
  96. $_->{link} = "<a href=\"".
  97. IkiWiki::cgiurl(
  98. do => "goto",
  99. page => $_->{page}
  100. ).
  101. "\" rel=\"nofollow\">".
  102. pagetitle($_->{page}).
  103. "</a>"
  104. }
  105. else {
  106. $_->{link} = pagetitle($_->{page});
  107. }
  108. $_->{baseurl}="$config{url}/" if length $config{url};
  109. $_;
  110. } @{$change->{pages}}
  111. ];
  112. push @{$change->{pages}}, { link => '...' } if $is_excess;
  113. # See if the committer is an openid.
  114. $change->{author}=$change->{user};
  115. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  116. if (defined $oiduser) {
  117. $change->{authorurl}=$change->{user};
  118. $change->{user}=$oiduser;
  119. }
  120. elsif (length $config{cgiurl}) {
  121. $change->{authorurl} = IkiWiki::cgiurl(
  122. do => "goto",
  123. page => IkiWiki::userpage($change->{author}),
  124. );
  125. }
  126. if (ref $change->{message}) {
  127. foreach my $field (@{$change->{message}}) {
  128. if (exists $field->{line}) {
  129. # escape html
  130. $field->{line} = encode_entities($field->{line});
  131. # escape links and preprocessor stuff
  132. $field->{line} = encode_entities($field->{line}, '\[\]');
  133. }
  134. }
  135. }
  136. # Fill out a template with the change info.
  137. my $template=template("change.tmpl", blind_cache => 1);
  138. $template->param(
  139. %$change,
  140. commitdate => displaytime($change->{when}, "%X %x"),
  141. wikiname => $config{wikiname},
  142. );
  143. $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
  144. if exists $config{url};
  145. IkiWiki::run_hooks(pagetemplate => sub {
  146. shift->(page => $page, destpage => $page,
  147. template => $template, rev => $change->{rev});
  148. });
  149. my $file=$page."._change";
  150. writefile($file, $config{srcdir}, $template->output);
  151. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  152. return $page;
  153. }
  154. 1