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