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