summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: dbb9065d494c9f14c06cb494d9f5296e1598f122 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::recentchanges;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. hook(type => "getsetup", id => "recentchanges", call => \&getsetup);
  8. hook(type => "checkconfig", id => "recentchanges", call => \&checkconfig);
  9. hook(type => "refresh", id => "recentchanges", call => \&refresh);
  10. hook(type => "pagetemplate", id => "recentchanges", call => \&pagetemplate);
  11. hook(type => "htmlize", id => "_change", call => \&htmlize);
  12. hook(type => "cgi", id => "recentchanges", call => \&cgi);
  13. } #}}}
  14. sub getsetup () { #{{{
  15. return
  16. plugin => {
  17. safe => 1,
  18. rebuild => 1,
  19. },
  20. recentchangespage => {
  21. type => "string",
  22. example => "recentchanges",
  23. description => "name of the recentchanges page",
  24. safe => 1,
  25. rebuild => 1,
  26. },
  27. recentchangesnum => {
  28. type => "integer",
  29. example => 100,
  30. description => "number of changes to track",
  31. safe => 1,
  32. rebuild => 0,
  33. },
  34. } #}}}
  35. sub checkconfig () { #{{{
  36. $config{recentchangespage}='recentchanges' unless defined $config{recentchangespage};
  37. $config{recentchangesnum}=100 unless defined $config{recentchangesnum};
  38. } #}}}
  39. sub refresh ($) { #{{{
  40. my %seen;
  41. # add new changes
  42. foreach my $change (IkiWiki::rcs_recentchanges($config{recentchangesnum})) {
  43. $seen{store($change, $config{recentchangespage})}=1;
  44. }
  45. # delete old and excess changes
  46. foreach my $page (keys %pagesources) {
  47. if ($pagesources{$page} =~ /\._change$/ && ! $seen{$page}) {
  48. unlink($config{srcdir}.'/'.$pagesources{$page});
  49. }
  50. }
  51. } #}}}
  52. # Enable the recentchanges link on wiki pages.
  53. sub pagetemplate (@) { #{{{
  54. my %params=@_;
  55. my $template=$params{template};
  56. my $page=$params{page};
  57. if (defined $config{recentchangespage} && $config{rcs} &&
  58. $page ne $config{recentchangespage} &&
  59. $template->query(name => "recentchangesurl")) {
  60. $template->param(recentchangesurl => urlto($config{recentchangespage}, $page));
  61. $template->param(have_actions => 1);
  62. }
  63. } #}}}
  64. # Pages with extension _change have plain html markup, pass through.
  65. sub htmlize (@) { #{{{
  66. my %params=@_;
  67. return $params{content};
  68. } #}}}
  69. sub cgi ($) { #{{{
  70. my $cgi=shift;
  71. if (defined $cgi->param('do') && $cgi->param('do') eq "recentchanges_link") {
  72. # This is a link from a change page to some
  73. # other page. Since the change pages are only generated
  74. # once, statically, links on them won't be updated if the
  75. # page they link to is deleted, or newly created, or
  76. # changes for whatever reason. So this CGI handles that
  77. # dynamic linking stuff.
  78. my $page=$cgi->param("page");
  79. if (!defined $page) {
  80. error("missing page parameter");
  81. }
  82. IkiWiki::loadindex();
  83. my $link=bestlink("", $page);
  84. if (! length $link) {
  85. print "Content-type: text/html\n\n";
  86. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  87. "<p>".
  88. sprintf(gettext("The page %s does not exist."),
  89. htmllink("", "", $page)).
  90. "</p>");
  91. }
  92. else {
  93. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  94. }
  95. exit;
  96. }
  97. }
  98. sub store ($$$) { #{{{
  99. my $change=shift;
  100. my $page="$config{recentchangespage}/change_".IkiWiki::titlepage($change->{rev});
  101. # Optimisation to avoid re-writing pages. Assumes commits never
  102. # change (or that any changes are not important).
  103. return $page if exists $pagesources{$page} && ! $config{rebuild};
  104. # Limit pages to first 10, and add links to the changed pages.
  105. my $is_excess = exists $change->{pages}[10];
  106. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  107. $change->{pages} = [
  108. map {
  109. if (length $config{cgiurl}) {
  110. $_->{link} = "<a href=\"".
  111. IkiWiki::cgiurl(
  112. do => "recentchanges_link",
  113. page => $_->{page}
  114. ).
  115. "\">".
  116. IkiWiki::pagetitle($_->{page}).
  117. "</a>"
  118. }
  119. else {
  120. $_->{link} = IkiWiki::pagetitle($_->{page});
  121. }
  122. $_->{baseurl}="$config{url}/" if length $config{url};
  123. $_;
  124. } @{$change->{pages}}
  125. ];
  126. push @{$change->{pages}}, { link => '...' } if $is_excess;
  127. # See if the committer is an openid.
  128. $change->{author}=$change->{user};
  129. my $oiduser=eval { IkiWiki::openiduser($change->{user}) };
  130. if (defined $oiduser) {
  131. $change->{authorurl}=$change->{user};
  132. $change->{user}=$oiduser;
  133. }
  134. elsif (length $config{cgiurl}) {
  135. $change->{authorurl} = IkiWiki::cgiurl(
  136. do => "recentchanges_link",
  137. page => (length $config{userdir} ? "$config{userdir}/" : "").$change->{author},
  138. );
  139. }
  140. # escape wikilinks and preprocessor stuff in commit messages
  141. if (ref $change->{message}) {
  142. foreach my $field (@{$change->{message}}) {
  143. if (exists $field->{line}) {
  144. $field->{line} =~ s/(?<!\\)\[\[/\\\[\[/g;
  145. }
  146. }
  147. }
  148. # Fill out a template with the change info.
  149. my $template=template("change.tmpl", blind_cache => 1);
  150. $template->param(
  151. %$change,
  152. commitdate => displaytime($change->{when}, "%X %x"),
  153. wikiname => $config{wikiname},
  154. );
  155. IkiWiki::run_hooks(pagetemplate => sub {
  156. shift->(page => $page, destpage => $page,
  157. template => $template, rev => $change->{rev});
  158. });
  159. my $file=$page."._change";
  160. writefile($file, $config{srcdir}, $template->output);
  161. utime $change->{when}, $change->{when}, "$config{srcdir}/$file";
  162. return $page;
  163. } #}}}
  164. 1