summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: fa851e46678f208efa04def72e988366df6ea87d (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. },
  23. recentchangespage => {
  24. type => "string",
  25. example => "recentchanges",
  26. description => "name of the recentchanges page",
  27. safe => 1,
  28. rebuild => 1,
  29. },
  30. recentchangesnum => {
  31. type => "integer",
  32. example => 100,
  33. description => "number of changes to track",
  34. safe => 1,
  35. rebuild => 0,
  36. },
  37. }
  38. sub checkconfig () {
  39. $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
  40. $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
  41. }
  42. sub refresh ($) {
  43. my %seen;
  44. # add new changes
  45. foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
  46. $seen{store($change, $config{recentchangespage})}=1;
  47. }
  48. # delete old and excess changes
  49. foreach my $page (keys %pagesources) {
  50. if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
  51. unlink($config{srcdir}.'/'.$pagesources{$page});
  52. }
  53. }
  54. }
  55. # Enable the recentchanges link on wiki pages.
  56. sub pagetemplate (@) {
  57. my %params=@_;
  58. my $template=$params{template};
  59. my $page=$params{page};
  60. if (defined $config{recentchangespage} && $config{rcs} &&
  61. $page ne $config{recentchangespage} &&
  62. $template->query(name => "recentchangesurl")) {
  63. $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
  64. $template->param(have_actions => 1);
  65. }
  66. }
  67. # Pages with extension _change have plain html markup, pass through.
  68. sub htmlize (@) {
  69. my %params=@_;
  70. return $params{content};
  71. }
  72. sub store ($$$) {
  73. my $change=shift;
  74. my $page="$config{recentchangespage}/change_".titlepage($change->{rev});
  75. # Optimisation to avoid re-writing pages. Assumes commits never
  76. # change (or that any changes are not important).
  77. return $page if exists $pagesources{$page} && ! $config{rebuild};
  78. # Limit pages to first 10, and add links to the changed pages.
  79. my $is_excess = exists $change->{pages}[10];
  80. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  81. $change->{pages} = [
  82. map {
  83. if (length $config{cgiurl}) {
  84. $_->{link} = "<a href=\"".
  85. IkiWiki::cgiurl(
  86. do => "goto",
  87. page => $_->{page}
  88. ).
  89. "\" rel=\"nofollow\">".
  90. pagetitle($_->{page}).
  91. "</a>"
  92. }
  93. else {
  94. $_->{link} = pagetitle($_->{page});
  95. }
  96. $_->{baseurl}="$config{url}/" if length $config{url};
  97. $_;
  98. } @{$change->{pages}}
  99. ];
  100. push @{$change->{pages}}, { link => '...' } if $is_excess;
  101. # See if the committer is an openid.
  102. $change->{author}=$change->{user};
  103. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  104. if (defined $oiduser) {
  105. $change->{authorurl}=$change->{user};
  106. $change->{user}=$oiduser;
  107. }
  108. elsif (length $config{cgiurl}) {
  109. $change->{authorurl} = IkiWiki::cgiurl(
  110. do => "goto",
  111. page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
  112. );
  113. }
  114. if (ref $change->{message}) {
  115. foreach my $field (@{$change->{message}}) {
  116. if (exists $field->{line}) {
  117. # escape html
  118. $field->{line} = encode_entities($field->{line});
  119. # escape links and preprocessor stuff
  120. $field->{line} = encode_entities($field->{line}, '\[\]');
  121. }
  122. }
  123. }
  124. # Fill out a template with the change info.
  125. my $template=template("change.tmpl", blind_cache => 1);
  126. $template->param(
  127. %$change,
  128. commitdate => displaytime($change->{when}, "%X %x"),
  129. wikiname => $config{wikiname},
  130. );
  131. $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
  132. if exists $config{url};
  133. IkiWiki::run_hooks(pagetemplate => sub {
  134. shift->(page => $page, destpage => $page,
  135. template => $template, rev => $change->{rev});
  136. });
  137. my $file=$page."._change";
  138. writefile($file, $config{srcdir}, $template->output);
  139. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  140. return $page;
  141. }
  142. 1