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