summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 758b98348d126aa572a537f9f0b359e5baffbb5e (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.
  56. sub pagetemplate (@) {
  57. my %params=@_;
  58. my $template=$params{template};
  59. my $page=$params{page};
  60. if (defined $config{recentchangespage} && $config{rcs} &&
  61. $template->query(name => "recentchangesurl") &&
  62. $page ne $config{recentchangespage}) {
  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. $change->{author}=$change->{user};
  102. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  103. if (defined $oiduser) {
  104. $change->{authorurl}=$change->{user};
  105. $change->{user}=defined $change->{nickname} ? $change->{nickname} : $oiduser;
  106. }
  107. elsif (length $config{cgiurl}) {
  108. $change->{authorurl} = IkiWiki::cgiurl(
  109. do => "goto",
  110. page => IkiWiki::userpage($change->{author}),
  111. );
  112. }
  113. if (ref $change->{message}) {
  114. foreach my $field (@{$change->{message}}) {
  115. if (exists $field->{line}) {
  116. # escape html
  117. $field->{line} = encode_entities($field->{line});
  118. # escape links and preprocessor stuff
  119. $field->{line} = encode_entities($field->{line}, '\[\]');
  120. }
  121. }
  122. }
  123. # Fill out a template with the change info.
  124. my $template=template("change.tmpl", blind_cache => 1);
  125. $template->param(
  126. %$change,
  127. commitdate => displaytime($change->{when}, "%X %x"),
  128. wikiname => $config{wikiname},
  129. );
  130. $template->param(permalink => "$config{url}/$config{recentchangespage}/#change-".titlepage($change->{rev}))
  131. if exists $config{url};
  132. IkiWiki::run_hooks(pagetemplate => sub {
  133. shift->(page => $page, destpage => $page,
  134. template => $template, rev => $change->{rev});
  135. });
  136. my $file=$page."._change";
  137. writefile($file, $config{srcdir}, $template->output);
  138. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  139. return $page;
  140. }
  141. 1